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.

70 lines
1.6 KiB

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. "github.com/tendermint/go-merkle"
  7. )
  8. type DummyApplication struct {
  9. types.BaseApplication
  10. state merkle.Tree
  11. }
  12. func NewDummyApplication() *DummyApplication {
  13. state := merkle.NewIAVLTree(0, nil)
  14. return &DummyApplication{state: state}
  15. }
  16. func (app *DummyApplication) Info() (resInfo types.ResponseInfo) {
  17. return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size())}
  18. }
  19. // tx is either "key=value" or just arbitrary bytes
  20. func (app *DummyApplication) DeliverTx(tx []byte) types.Result {
  21. parts := strings.Split(string(tx), "=")
  22. if len(parts) == 2 {
  23. app.state.Set([]byte(parts[0]), []byte(parts[1]))
  24. } else {
  25. app.state.Set(tx, tx)
  26. }
  27. return types.OK
  28. }
  29. func (app *DummyApplication) CheckTx(tx []byte) types.Result {
  30. return types.OK
  31. }
  32. func (app *DummyApplication) Commit() types.Result {
  33. hash := app.state.Hash()
  34. return types.NewResultOK(hash, "")
  35. }
  36. func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
  37. if reqQuery.Prove {
  38. value, proof, exists := app.state.Proof(reqQuery.Data)
  39. resQuery.Index = -1 // TODO make Proof return index
  40. resQuery.Key = reqQuery.Data
  41. resQuery.Value = value
  42. resQuery.Proof = proof
  43. if exists {
  44. resQuery.Log = "exists"
  45. } else {
  46. resQuery.Log = "does not exist"
  47. }
  48. return
  49. } else {
  50. index, value, exists := app.state.Get(reqQuery.Data)
  51. resQuery.Index = int64(index)
  52. resQuery.Value = value
  53. if exists {
  54. resQuery.Log = "exists"
  55. } else {
  56. resQuery.Log = "does not exist"
  57. }
  58. return
  59. }
  60. }