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"
|
2017-11-20 20:26:37 -06:00
|
|
|
"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"
|
2017-12-26 04:52:02 -08:00
|
|
|
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
|
|
|
)
|
|
|
|
|
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-12-26 00:45:31 -08:00
|
|
|
state := iavl.NewVersionedTree(dbm.NewMemDB(), 0)
|
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 {
|
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)
|
|
|
|
|
2017-12-26 04:52:02 -08:00
|
|
|
tags := []cmn.KVPair{
|
2017-12-24 18:39:16 -08:00
|
|
|
{[]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
|
|
|
}
|
|
|
|
|
2017-11-20 20:26:37 -06: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
|
|
|
}
|
|
|
|
|
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 {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 15:46:06 -08:00
|
|
|
return types.ResponseCommit{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-12-26 00:45:31 -08:00
|
|
|
resQuery.Proof = proof.Bytes()
|
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
|
|
|
}
|