tendermint/example/dummy/dummy.go

73 lines
1.7 KiB
Go

package dummy
import (
"strings"
"github.com/tendermint/abci/types"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-merkle"
)
type DummyApplication struct {
state merkle.Tree
}
func NewDummyApplication() *DummyApplication {
state := merkle.NewIAVLTree(0, nil)
return &DummyApplication{state: state}
}
func (app *DummyApplication) Info() (resInfo types.ResponseInfo) {
return types.ResponseInfo{Data: Fmt("{\"size\":%v}", app.state.Size())}
}
func (app *DummyApplication) SetOption(key string, value string) (log string) {
return ""
}
// tx is either "key=value" or just arbitrary bytes
func (app *DummyApplication) DeliverTx(tx []byte) types.Result {
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.OK
}
func (app *DummyApplication) CheckTx(tx []byte) types.Result {
return types.OK
}
func (app *DummyApplication) Commit() types.Result {
hash := app.state.Hash()
return types.NewResultOK(hash, "")
}
func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
if reqQuery.Prove {
value, proof, exists := app.state.Proof(reqQuery.Data)
resQuery.Index = -1 // TODO make Proof return index
resQuery.Key = reqQuery.Data
resQuery.Value = value
resQuery.Proof = proof
if exists {
resQuery.Log = "exists"
} else {
resQuery.Log = "does not exist"
}
return
} else {
index, value, exists := app.state.Get(reqQuery.Data)
resQuery.Index = int64(index)
resQuery.Value = value
if exists {
resQuery.Log = "exists"
} else {
resQuery.Log = "does not exist"
}
return
}
}