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.

89 lines
2.1 KiB

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