98 lines
2.3 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 (
2017-11-30 02:45:40 +00:00
"bytes"
"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"
2017-10-18 12:46:51 +02:00
"github.com/tendermint/iavl"
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 _ 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-12-26 00:45:31 -08:00
state := iavl.NewVersionedTree(dbm.NewMemDB(), 0)
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 {
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.Set(key, value)
tags := []cmn.KVPair{
{[]byte("app.creator"), []byte("jae")},
{[]byte("app.key"), key},
2016-02-08 13:47:47 -08:00
}
2017-11-30 15:37:31 -05:00
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
2015-11-02 07:39:53 -08:00
}
func (app *DummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
2017-11-30 15:37:31 -05:00
return types.ResponseCheckTx{Code: code.CodeTypeOK}
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 {
2017-12-26 00:45:31 -08:00
hash, _, err = app.state.SaveVersion()
2017-10-18 13:13:18 +02:00
if err != nil {
// if this wasn't a dummy app, we'd do something smarter
panic(err)
}
}
return types.ResponseCommit{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-12-26 00:45:31 -08:00
resQuery.Proof = proof.Bytes()
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
}