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.

126 lines
2.9 KiB

9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package kvstore
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/tendermint/abci/example/code"
  8. "github.com/tendermint/abci/types"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. dbm "github.com/tendermint/tmlibs/db"
  11. )
  12. var (
  13. stateKey = []byte("stateKey")
  14. kvPairPrefixKey = []byte("kvPairKey:")
  15. )
  16. type State struct {
  17. db dbm.DB
  18. Size int64 `json:"size"`
  19. Height int64 `json:"height"`
  20. AppHash []byte `json:"app_hash"`
  21. }
  22. func loadState(db dbm.DB) State {
  23. stateBytes := db.Get(stateKey)
  24. var state State
  25. if len(stateBytes) != 0 {
  26. err := json.Unmarshal(stateBytes, &state)
  27. if err != nil {
  28. panic(err)
  29. }
  30. }
  31. state.db = db
  32. return state
  33. }
  34. func saveState(state State) {
  35. stateBytes, err := json.Marshal(state)
  36. if err != nil {
  37. panic(err)
  38. }
  39. state.db.Set(stateKey, stateBytes)
  40. }
  41. func prefixKey(key []byte) []byte {
  42. return append(kvPairPrefixKey, key...)
  43. }
  44. //---------------------------------------------------
  45. var _ types.Application = (*KVStoreApplication)(nil)
  46. type KVStoreApplication struct {
  47. types.BaseApplication
  48. state State
  49. }
  50. func NewKVStoreApplication() *KVStoreApplication {
  51. state := loadState(dbm.NewMemDB())
  52. return &KVStoreApplication{state: state}
  53. }
  54. func (app *KVStoreApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
  55. return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
  56. }
  57. // tx is either "key=value" or just arbitrary bytes
  58. func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
  59. var key, value []byte
  60. parts := bytes.Split(tx, []byte("="))
  61. if len(parts) == 2 {
  62. key, value = parts[0], parts[1]
  63. } else {
  64. key, value = tx, tx
  65. }
  66. app.state.db.Set(prefixKey(key), value)
  67. app.state.Size += 1
  68. tags := []cmn.KVPair{
  69. {[]byte("app.creator"), []byte("jae")},
  70. {[]byte("app.key"), key},
  71. }
  72. return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
  73. }
  74. func (app *KVStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx {
  75. return types.ResponseCheckTx{Code: code.CodeTypeOK}
  76. }
  77. func (app *KVStoreApplication) Commit() types.ResponseCommit {
  78. // Using a memdb - just return the big endian size of the db
  79. appHash := make([]byte, 8)
  80. binary.PutVarint(appHash, app.state.Size)
  81. app.state.AppHash = appHash
  82. app.state.Height += 1
  83. saveState(app.state)
  84. return types.ResponseCommit{Data: appHash}
  85. }
  86. func (app *KVStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
  87. if reqQuery.Prove {
  88. value := app.state.db.Get(prefixKey(reqQuery.Data))
  89. resQuery.Index = -1 // TODO make Proof return index
  90. resQuery.Key = reqQuery.Data
  91. resQuery.Value = value
  92. if value != nil {
  93. resQuery.Log = "exists"
  94. } else {
  95. resQuery.Log = "does not exist"
  96. }
  97. return
  98. } else {
  99. value := app.state.db.Get(prefixKey(reqQuery.Data))
  100. resQuery.Value = value
  101. if value != nil {
  102. resQuery.Log = "exists"
  103. } else {
  104. resQuery.Log = "does not exist"
  105. }
  106. return
  107. }
  108. }