You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.5 KiB

9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package dummy
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/tendermint/abci/example/code"
  6. "github.com/tendermint/abci/types"
  7. wire "github.com/tendermint/go-wire"
  8. "github.com/tendermint/iavl"
  9. dbm "github.com/tendermint/tmlibs/db"
  10. )
  11. var _ types.Application = (*DummyApplication)(nil)
  12. type DummyApplication struct {
  13. types.BaseApplication
  14. state *iavl.VersionedTree
  15. }
  16. func NewDummyApplication() *DummyApplication {
  17. state := iavl.NewVersionedTree(0, dbm.NewMemDB())
  18. return &DummyApplication{state: state}
  19. }
  20. func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
  21. return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size())}
  22. }
  23. // tx is either "key=value" or just arbitrary bytes
  24. func (app *DummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
  25. var key, value []byte
  26. parts := bytes.Split(tx, []byte("="))
  27. if len(parts) == 2 {
  28. key, value = parts[0], parts[1]
  29. } else {
  30. key, value = tx, tx
  31. }
  32. app.state.Set(key, value)
  33. tags := []*types.KVPair{
  34. {Key: "app.creator", ValueType: types.KVPair_STRING, ValueString: "jae"},
  35. {Key: "app.key", ValueType: types.KVPair_STRING, ValueString: string(key)},
  36. }
  37. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
  38. }
  39. func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
  40. return types.ResponseCheckTx{Code: code.CodeTypeOK}
  41. }
  42. func (app *DummyApplication) Commit() types.ResponseCommit {
  43. // Save a new version
  44. var hash []byte
  45. var err error
  46. if app.state.Size() > 0 {
  47. // just add one more to height (kind of arbitrarily stupid)
  48. height := app.state.LatestVersion() + 1
  49. hash, err = app.state.SaveVersion(height)
  50. if err != nil {
  51. // if this wasn't a dummy app, we'd do something smarter
  52. panic(err)
  53. }
  54. }
  55. return types.ResponseCommit{Code: code.CodeTypeOK, Data: hash}
  56. }
  57. func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
  58. if reqQuery.Prove {
  59. value, proof, err := app.state.GetWithProof(reqQuery.Data)
  60. // if this wasn't a dummy app, we'd do something smarter
  61. if err != nil {
  62. panic(err)
  63. }
  64. resQuery.Index = -1 // TODO make Proof return index
  65. resQuery.Key = reqQuery.Data
  66. resQuery.Value = value
  67. resQuery.Proof = wire.BinaryBytes(proof)
  68. if value != nil {
  69. resQuery.Log = "exists"
  70. } else {
  71. resQuery.Log = "does not exist"
  72. }
  73. return
  74. } else {
  75. index, value := app.state.Get(reqQuery.Data)
  76. resQuery.Index = int64(index)
  77. resQuery.Value = value
  78. if value != nil {
  79. resQuery.Log = "exists"
  80. } else {
  81. resQuery.Log = "does not exist"
  82. }
  83. return
  84. }
  85. }