127 lines
2.9 KiB
Go
Raw Normal View History

2018-02-19 20:34:51 +00:00
package kvstore
2015-11-02 07:39:53 -08:00
import (
2017-11-30 02:45:40 +00:00
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
2016-02-08 13:47:47 -08:00
2017-11-30 15:37:31 -05:00
"github.com/tendermint/abci/example/code"
2017-01-15 14:43:16 -08:00
"github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
2017-10-18 12:46:51 +02:00
dbm "github.com/tendermint/tmlibs/db"
2015-11-02 07:39:53 -08:00
)
var (
stateKey = []byte("stateKey")
kvPairPrefixKey = []byte("kvPairKey:")
)
type State struct {
db dbm.DB
Size int64 `json:"size"`
Height int64 `json:"height"`
AppHash []byte `json:"app_hash"`
}
func loadState(db dbm.DB) State {
stateBytes := db.Get(stateKey)
var state State
if len(stateBytes) != 0 {
err := json.Unmarshal(stateBytes, &state)
if err != nil {
panic(err)
}
}
state.db = db
return state
}
func saveState(state State) {
stateBytes, err := json.Marshal(state)
if err != nil {
panic(err)
}
state.db.Set(stateKey, stateBytes)
}
func prefixKey(key []byte) []byte {
return append(kvPairPrefixKey, key...)
}
//---------------------------------------------------
2018-02-19 20:34:51 +00:00
var _ types.Application = (*KVStoreApplication)(nil)
2018-02-19 20:34:51 +00:00
type KVStoreApplication struct {
2017-02-13 18:48:59 -05:00
types.BaseApplication
state State
2015-11-02 07:39:53 -08:00
}
2018-02-19 20:34:51 +00:00
func NewKVStoreApplication() *KVStoreApplication {
state := loadState(dbm.NewMemDB())
2018-02-19 20:34:51 +00:00
return &KVStoreApplication{state: state}
}
func (app *KVStoreApplication) Info(req types.ParamsInfo) (resInfo types.ResultInfo) {
return types.ResultInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
}
2016-07-01 20:22:58 -04:00
// tx is either "key=value" or just arbitrary bytes
func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResultDeliverTx {
2017-11-30 02:45:40 +00:00
var key, value []byte
parts := bytes.Split(tx, []byte("="))
2016-02-08 13:47:47 -08:00
if len(parts) == 2 {
2017-11-30 02:45:40 +00:00
key, value = parts[0], parts[1]
2016-02-08 13:47:47 -08:00
} else {
2017-11-30 02:45:40 +00:00
key, value = tx, tx
}
app.state.db.Set(prefixKey(key), value)
app.state.Size += 1
2017-11-30 02:45:40 +00:00
tags := []cmn.KVPair{
{[]byte("app.creator"), []byte("jae")},
{[]byte("app.key"), key},
2016-02-08 13:47:47 -08:00
}
return types.ResultDeliverTx{Code: code.CodeTypeOK, Tags: tags}
2015-11-02 07:39:53 -08:00
}
func (app *KVStoreApplication) CheckTx(tx []byte) types.ResultCheckTx {
return types.ResultCheckTx{Code: code.CodeTypeOK}
2015-11-02 07:39:53 -08:00
}
func (app *KVStoreApplication) Commit() types.ResultCommit {
// Using a memdb - just return the big endian size of the db
appHash := make([]byte, 8)
binary.PutVarint(appHash, app.state.Size)
app.state.AppHash = appHash
app.state.Height += 1
saveState(app.state)
return types.ResultCommit{Data: appHash}
2016-01-18 14:37:42 -08:00
}
func (app *KVStoreApplication) Query(reqQuery types.ParamsQuery) (resQuery types.ResultQuery) {
if reqQuery.Prove {
value := app.state.db.Get(prefixKey(reqQuery.Data))
resQuery.Index = -1 // TODO make Proof return index
resQuery.Key = reqQuery.Data
resQuery.Value = value
2017-10-19 14:43:34 +02:00
if value != nil {
resQuery.Log = "exists"
} else {
resQuery.Log = "does not exist"
}
return
} else {
value := app.state.db.Get(prefixKey(reqQuery.Data))
resQuery.Value = value
2017-10-18 12:46:51 +02:00
if value != nil {
resQuery.Log = "exists"
} else {
resQuery.Log = "does not exist"
}
return
}
2015-11-02 07:39:53 -08:00
}