OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / example / dummy / dummy.go
1 package dummy
2
3 import (
4         "strings"
5
6         "github.com/tendermint/abci/types"
7         wire "github.com/tendermint/go-wire"
8         "github.com/tendermint/iavl"
9         cmn "github.com/tendermint/tmlibs/common"
10         dbm "github.com/tendermint/tmlibs/db"
11 )
12
13 type DummyApplication struct {
14         types.BaseApplication
15
16         state *iavl.VersionedTree
17 }
18
19 func NewDummyApplication() *DummyApplication {
20         state := iavl.NewVersionedTree(0, dbm.NewMemDB())
21         return &DummyApplication{state: state}
22 }
23
24 func (app *DummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
25         return types.ResponseInfo{Data: cmn.Fmt("{\"size\":%v}", app.state.Size())}
26 }
27
28 // tx is either "key=value" or just arbitrary bytes
29 func (app *DummyApplication) DeliverTx(tx []byte) types.Result {
30         parts := strings.Split(string(tx), "=")
31         if len(parts) == 2 {
32                 app.state.Set([]byte(parts[0]), []byte(parts[1]))
33         } else {
34                 app.state.Set(tx, tx)
35         }
36         return types.OK
37 }
38
39 func (app *DummyApplication) CheckTx(tx []byte) types.Result {
40         return types.OK
41 }
42
43 func (app *DummyApplication) Commit() types.Result {
44         // Save a new version
45         var hash []byte
46         var err error
47
48         if app.state.Size() > 0 {
49                 // just add one more to height (kind of arbitrarily stupid)
50                 height := app.state.LatestVersion() + 1
51                 hash, err = app.state.SaveVersion(height)
52                 if err != nil {
53                         // if this wasn't a dummy app, we'd do something smarter
54                         panic(err)
55                 }
56         }
57
58         return types.NewResultOK(hash, "")
59 }
60
61 func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
62         if reqQuery.Prove {
63                 value, proof, err := app.state.GetWithProof(reqQuery.Data)
64                 // if this wasn't a dummy app, we'd do something smarter
65                 if err != nil {
66                         panic(err)
67                 }
68                 resQuery.Index = -1 // TODO make Proof return index
69                 resQuery.Key = reqQuery.Data
70                 resQuery.Value = value
71                 resQuery.Proof = wire.BinaryBytes(proof)
72                 if value != nil {
73                         resQuery.Log = "exists"
74                 } else {
75                         resQuery.Log = "does not exist"
76                 }
77                 return
78         } else {
79                 index, value := app.state.Get(reqQuery.Data)
80                 resQuery.Index = int64(index)
81                 resQuery.Value = value
82                 if value != nil {
83                         resQuery.Log = "exists"
84                 } else {
85                         resQuery.Log = "does not exist"
86                 }
87                 return
88         }
89 }