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.

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