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.

97 lines
1.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package example
  2. import (
  3. "sync"
  4. . "github.com/tendermint/go-common"
  5. "github.com/tendermint/go-merkle"
  6. "github.com/tendermint/go-wire"
  7. "github.com/tendermint/tmsp/types"
  8. )
  9. type DummyApplication struct {
  10. mtx sync.Mutex
  11. state merkle.Tree
  12. }
  13. func NewDummyApplication() *DummyApplication {
  14. state := merkle.NewIAVLTree(
  15. wire.BasicCodec,
  16. wire.BasicCodec,
  17. 0,
  18. nil,
  19. )
  20. return &DummyApplication{state: state}
  21. }
  22. func (dapp *DummyApplication) Open() types.AppContext {
  23. dapp.mtx.Lock()
  24. defer dapp.mtx.Unlock()
  25. return &DummyAppContext{
  26. app: dapp,
  27. state: dapp.state.Copy(),
  28. }
  29. }
  30. func (dapp *DummyApplication) commitState(state merkle.Tree) {
  31. dapp.mtx.Lock()
  32. defer dapp.mtx.Unlock()
  33. dapp.state = state.Copy()
  34. }
  35. func (dapp *DummyApplication) getState() merkle.Tree {
  36. dapp.mtx.Lock()
  37. defer dapp.mtx.Unlock()
  38. return dapp.state.Copy()
  39. }
  40. //--------------------------------------------------------------------------------
  41. type DummyAppContext struct {
  42. app *DummyApplication
  43. state merkle.Tree
  44. }
  45. func (dac *DummyAppContext) Echo(message string) string {
  46. return message
  47. }
  48. func (dac *DummyAppContext) Info() []string {
  49. return []string{Fmt("size:%v", dac.state.Size())}
  50. }
  51. func (dac *DummyAppContext) SetOption(key string, value string) types.RetCode {
  52. return 0
  53. }
  54. func (dac *DummyAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
  55. dac.state.Set(tx, tx)
  56. return nil, 0
  57. }
  58. func (dac *DummyAppContext) GetHash() ([]byte, types.RetCode) {
  59. hash := dac.state.Hash()
  60. return hash, 0
  61. }
  62. func (dac *DummyAppContext) Commit() types.RetCode {
  63. dac.app.commitState(dac.state)
  64. return 0
  65. }
  66. func (dac *DummyAppContext) Rollback() types.RetCode {
  67. dac.state = dac.app.getState()
  68. return 0
  69. }
  70. func (dac *DummyAppContext) AddListener(key string) types.RetCode {
  71. return 0
  72. }
  73. func (dac *DummyAppContext) RemListener(key string) types.RetCode {
  74. return 0
  75. }
  76. func (dac *DummyAppContext) Close() error {
  77. return nil
  78. }