OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / example / counter / counter.go
1 package counter
2
3 import (
4         "encoding/binary"
5
6         "github.com/tendermint/abci/types"
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
10 type CounterApplication struct {
11         types.BaseApplication
12
13         hashCount int
14         txCount   int
15         serial    bool
16 }
17
18 func NewCounterApplication(serial bool) *CounterApplication {
19         return &CounterApplication{serial: serial}
20 }
21
22 func (app *CounterApplication) Info(req types.RequestInfo) types.ResponseInfo {
23         return types.ResponseInfo{Data: cmn.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
24 }
25
26 func (app *CounterApplication) SetOption(key string, value string) (log string) {
27         if key == "serial" && value == "on" {
28                 app.serial = true
29         }
30         return ""
31 }
32
33 func (app *CounterApplication) DeliverTx(tx []byte) types.Result {
34         if app.serial {
35                 if len(tx) > 8 {
36                         return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
37                 }
38                 tx8 := make([]byte, 8)
39                 copy(tx8[len(tx8)-len(tx):], tx)
40                 txValue := binary.BigEndian.Uint64(tx8)
41                 if txValue != uint64(app.txCount) {
42                         return types.ErrBadNonce.SetLog(cmn.Fmt("Invalid nonce. Expected %v, got %v", app.txCount, txValue))
43                 }
44         }
45         app.txCount++
46         return types.OK
47 }
48
49 func (app *CounterApplication) CheckTx(tx []byte) types.Result {
50         if app.serial {
51                 if len(tx) > 8 {
52                         return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
53                 }
54                 tx8 := make([]byte, 8)
55                 copy(tx8[len(tx8)-len(tx):], tx)
56                 txValue := binary.BigEndian.Uint64(tx8)
57                 if txValue < uint64(app.txCount) {
58                         return types.ErrBadNonce.SetLog(cmn.Fmt("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue))
59                 }
60         }
61         return types.OK
62 }
63
64 func (app *CounterApplication) Commit() types.Result {
65         app.hashCount++
66         if app.txCount == 0 {
67                 return types.OK
68         }
69         hash := make([]byte, 8)
70         binary.BigEndian.PutUint64(hash, uint64(app.txCount))
71         return types.NewResultOK(hash, "")
72 }
73
74 func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
75         switch reqQuery.Path {
76         case "hash":
77                 return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.hashCount))}
78         case "tx":
79                 return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.txCount))}
80         default:
81                 return types.ResponseQuery{Log: cmn.Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
82         }
83 }