90 lines
2.2 KiB
Go
Raw Normal View History

2016-02-14 14:59:53 -08:00
package dummy
2015-11-02 07:39:53 -08:00
import (
"fmt"
2016-02-08 13:47:47 -08:00
"strings"
2017-01-15 14:43:16 -08:00
"github.com/tendermint/abci/types"
2017-10-18 12:46:51 +02:00
wire "github.com/tendermint/go-wire"
"github.com/tendermint/iavl"
dbm "github.com/tendermint/tmlibs/db"
2015-11-02 07:39:53 -08:00
)
type DummyApplication struct {
2017-02-13 18:48:59 -05:00
types.BaseApplication
2017-10-18 12:46:51 +02:00
state *iavl.VersionedTree
2015-11-02 07:39:53 -08:00
}
func NewDummyApplication() *DummyApplication {
2017-10-18 12:46:51 +02:00
state := iavl.NewVersionedTree(0, dbm.NewMemDB())
return &DummyApplication{state: state}
}
2017-09-22 11:10:39 -04:00
func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
return types.ResponseInfo{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 *DummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
2016-02-08 13:47:47 -08:00
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}
2015-11-02 07:39:53 -08:00
}
func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
return types.ResponseCheckTx{Code: types.CodeType_OK}
2015-11-02 07:39:53 -08:00
}
func (app *DummyApplication) Commit() types.ResponseCommit {
2017-10-18 13:13:18 +02:00
// 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}
2016-01-18 14:37:42 -08:00
}
func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
if reqQuery.Prove {
2017-10-18 12:46:51 +02:00
value, proof, err := app.state.GetWithProof(reqQuery.Data)
2017-10-19 14:43:34 +02:00
// if this wasn't a dummy app, we'd do something smarter
2017-10-18 12:46:51 +02:00
if err != nil {
panic(err)
}
resQuery.Index = -1 // TODO make Proof return index
resQuery.Key = reqQuery.Data
resQuery.Value = value
2017-10-18 12:46:51 +02:00
resQuery.Proof = wire.BinaryBytes(proof)
2017-10-19 14:43:34 +02:00
if value != nil {
resQuery.Log = "exists"
} else {
resQuery.Log = "does not exist"
}
return
} else {
2017-10-18 12:46:51 +02:00
index, value := app.state.Get(reqQuery.Data)
resQuery.Index = int64(index)
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
}