2016-02-14 14:59:53 -08:00
|
|
|
package dummy
|
2015-11-02 07:39:53 -08:00
|
|
|
|
|
|
|
import (
|
2017-11-20 20:26:37 -06:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-11-27 19:04:21 +00:00
|
|
|
var _ types.Application = (*DummyApplication)(nil)
|
|
|
|
|
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())
|
2015-11-29 14:22:30 -08:00
|
|
|
return &DummyApplication{state: state}
|
|
|
|
}
|
|
|
|
|
2017-09-22 11:10:39 -04:00
|
|
|
func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size())}
|
2015-11-09 16:29:45 -08:00
|
|
|
}
|
|
|
|
|
2016-07-01 20:22:58 -04:00
|
|
|
// tx is either "key=value" or just arbitrary bytes
|
2017-11-20 20:26:37 -06:00
|
|
|
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)
|
|
|
|
}
|
2017-11-29 20:13:59 -06:00
|
|
|
tags := []*types.KVPair{{Key: "app.creator", ValueType: types.KVPair_STRING, ValueString: "jae"}}
|
|
|
|
return types.ResponseDeliverTx{Code: types.CodeType_OK, Tags: tags}
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:26:37 -06:00
|
|
|
func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
|
|
|
return types.ResponseCheckTx{Code: types.CodeType_OK}
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:26:37 -06: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseCommit{Code: types.CodeType_OK, Data: hash}
|
2016-01-18 14:37:42 -08:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:42:09 -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)
|
|
|
|
}
|
2017-01-23 23:42:09 -08:00
|
|
|
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 {
|
2017-01-23 23:42:09 -08:00
|
|
|
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)
|
2017-01-23 23:42:09 -08:00
|
|
|
resQuery.Index = int64(index)
|
|
|
|
resQuery.Value = value
|
2017-10-18 12:46:51 +02:00
|
|
|
if value != nil {
|
2017-01-23 23:42:09 -08:00
|
|
|
resQuery.Log = "exists"
|
|
|
|
} else {
|
|
|
|
resQuery.Log = "does not exist"
|
|
|
|
}
|
|
|
|
return
|
2017-01-10 17:06:51 +01:00
|
|
|
}
|
2015-11-02 07:39:53 -08:00
|
|
|
}
|