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.

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