|
|
- package dummy
-
- import (
- "fmt"
- "strings"
-
- "github.com/tendermint/abci/types"
- wire "github.com/tendermint/go-wire"
- "github.com/tendermint/iavl"
- dbm "github.com/tendermint/tmlibs/db"
- )
-
- var _ types.Application = (*DummyApplication)(nil)
-
- type DummyApplication struct {
- types.BaseApplication
-
- state *iavl.VersionedTree
- }
-
- func NewDummyApplication() *DummyApplication {
- state := iavl.NewVersionedTree(0, dbm.NewMemDB())
- return &DummyApplication{state: state}
- }
-
- func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
- return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size())}
- }
-
- // tx is either "key=value" or just arbitrary bytes
- func (app *DummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
- parts := strings.Split(string(tx), "=")
- if len(parts) == 2 {
- app.state.Set([]byte(parts[0]), []byte(parts[1]))
- } else {
- app.state.Set(tx, tx)
- }
- return types.ResponseDeliverTx{Code: types.CodeType_OK}
- }
-
- func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
- return types.ResponseCheckTx{Code: types.CodeType_OK}
- }
-
- func (app *DummyApplication) Commit() types.ResponseCommit {
- // Save a new version
- var hash []byte
- var err error
-
- if app.state.Size() > 0 {
- // just add one more to height (kind of arbitrarily stupid)
- height := app.state.LatestVersion() + 1
- hash, err = app.state.SaveVersion(height)
- if err != nil {
- // if this wasn't a dummy app, we'd do something smarter
- panic(err)
- }
- }
-
- return types.ResponseCommit{Code: types.CodeType_OK, Data: hash}
- }
-
- func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
- if reqQuery.Prove {
- value, proof, err := app.state.GetWithProof(reqQuery.Data)
- // if this wasn't a dummy app, we'd do something smarter
- if err != nil {
- panic(err)
- }
- resQuery.Index = -1 // TODO make Proof return index
- resQuery.Key = reqQuery.Data
- resQuery.Value = value
- resQuery.Proof = wire.BinaryBytes(proof)
- if value != nil {
- resQuery.Log = "exists"
- } else {
- resQuery.Log = "does not exist"
- }
- return
- } else {
- index, value := app.state.Get(reqQuery.Data)
- resQuery.Index = int64(index)
- resQuery.Value = value
- if value != nil {
- resQuery.Log = "exists"
- } else {
- resQuery.Log = "does not exist"
- }
- return
- }
- }
|