6 "github.com/tendermint/abci/types"
7 cmn "github.com/tendermint/tmlibs/common"
10 type CounterApplication struct {
18 func NewCounterApplication(serial bool) *CounterApplication {
19 return &CounterApplication{serial: serial}
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)}
26 func (app *CounterApplication) SetOption(key string, value string) (log string) {
27 if key == "serial" && value == "on" {
33 func (app *CounterApplication) DeliverTx(tx []byte) types.Result {
36 return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
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))
49 func (app *CounterApplication) CheckTx(tx []byte) types.Result {
52 return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
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))
64 func (app *CounterApplication) Commit() types.Result {
69 hash := make([]byte, 8)
70 binary.BigEndian.PutUint64(hash, uint64(app.txCount))
71 return types.NewResultOK(hash, "")
74 func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
75 switch reqQuery.Path {
77 return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.hashCount))}
79 return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.txCount))}
81 return types.ResponseQuery{Log: cmn.Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}