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.

111 lines
2.3 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
9 years ago
  1. package example
  2. import (
  3. "encoding/binary"
  4. "sync"
  5. . "github.com/tendermint/go-common"
  6. "github.com/tendermint/tmsp/types"
  7. )
  8. type CounterApplication struct {
  9. mtx sync.Mutex
  10. hashCount int
  11. txCount int
  12. commitCount int
  13. }
  14. func NewCounterApplication() *CounterApplication {
  15. return &CounterApplication{}
  16. }
  17. func (app *CounterApplication) Open() types.AppContext {
  18. return &CounterAppContext{
  19. app: app,
  20. hashCount: app.hashCount,
  21. txCount: app.txCount,
  22. commitCount: app.commitCount,
  23. }
  24. }
  25. //--------------------------------------------------------------------------------
  26. type CounterAppContext struct {
  27. app *CounterApplication
  28. hashCount int
  29. txCount int
  30. commitCount int
  31. serial bool
  32. }
  33. func (appC *CounterAppContext) Echo(message string) string {
  34. return message
  35. }
  36. func (appC *CounterAppContext) Info() []string {
  37. return []string{Fmt("hash, tx, commit counts:%d, %d, %d", appC.hashCount, appC.txCount, appC.commitCount)}
  38. }
  39. func (appC *CounterAppContext) SetOption(key string, value string) types.RetCode {
  40. if key == "serial" && value == "on" {
  41. appC.serial = true
  42. }
  43. return 0
  44. }
  45. func (appC *CounterAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
  46. if appC.serial {
  47. txValue, bz := binary.Varint(tx)
  48. if bz <= 0 {
  49. return nil, types.RetCodeInternalError
  50. }
  51. if txValue != int64(appC.txCount) {
  52. return nil, types.RetCodeInternalError
  53. }
  54. }
  55. appC.txCount += 1
  56. return nil, 0
  57. }
  58. func (appC *CounterAppContext) GetHash() ([]byte, types.RetCode) {
  59. appC.hashCount += 1
  60. if appC.txCount == 0 {
  61. return nil, 0
  62. } else {
  63. hash := make([]byte, 32)
  64. binary.PutVarint(hash, int64(appC.txCount))
  65. return hash, 0
  66. }
  67. }
  68. func (appC *CounterAppContext) Commit() types.RetCode {
  69. appC.commitCount += 1
  70. appC.app.mtx.Lock()
  71. appC.app.hashCount = appC.hashCount
  72. appC.app.txCount = appC.txCount
  73. appC.app.commitCount = appC.commitCount
  74. appC.app.mtx.Unlock()
  75. return 0
  76. }
  77. func (appC *CounterAppContext) Rollback() types.RetCode {
  78. appC.app.mtx.Lock()
  79. appC.hashCount = appC.app.hashCount
  80. appC.txCount = appC.app.txCount
  81. appC.commitCount = appC.app.commitCount
  82. appC.app.mtx.Unlock()
  83. return 0
  84. }
  85. func (appC *CounterAppContext) AddListener(key string) types.RetCode {
  86. return 0
  87. }
  88. func (appC *CounterAppContext) RemListener(key string) types.RetCode {
  89. return 0
  90. }
  91. func (appC *CounterAppContext) Close() error {
  92. return nil
  93. }