tendermint/abci/example/counter/counter.go

105 lines
3.0 KiB
Go
Raw Normal View History

2016-02-14 14:59:53 -08:00
package counter
2015-11-29 00:43:49 -08:00
import (
"encoding/binary"
"fmt"
2015-11-29 00:43:49 -08:00
2018-06-21 21:59:02 -07:00
"github.com/tendermint/tendermint/abci/example/code"
"github.com/tendermint/tendermint/abci/types"
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
2015-11-29 00:43:49 -08:00
)
type CounterApplication struct {
2017-02-13 15:48:59 -08:00
types.BaseApplication
hashCount int
txCount int
serial bool
2015-11-29 00:43:49 -08:00
}
func NewCounterApplication(serial bool) *CounterApplication {
return &CounterApplication{serial: serial}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) Info(req types.RequestInfo) types.ResponseInfo {
return types.ResponseInfo{Data: cmn.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
key, value := req.Key, req.Value
if key == "serial" && value == "on" {
app.serial = true
} else {
/*
TODO Panic and have the ABCI server pass an exception.
The client can call SetOptionSync() and get an `error`.
return types.ResponseSetOption{
Error: cmn.Fmt("Unknown key (%s) or value (%s)", key, value),
}
*/
return types.ResponseSetOption{}
}
return types.ResponseSetOption{}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
if app.serial {
2016-11-23 15:22:22 -08:00
if len(tx) > 8 {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
2016-11-23 15:22:22 -08:00
}
tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue != uint64(app.txCount) {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeBadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
}
}
2017-01-17 00:26:32 -08:00
app.txCount++
return types.ResponseDeliverTx{Code: code.CodeTypeOK}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) CheckTx(tx []byte) types.ResponseCheckTx {
if app.serial {
2016-11-23 15:22:22 -08:00
if len(tx) > 8 {
return types.ResponseCheckTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
2016-11-23 15:22:22 -08:00
}
tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue < uint64(app.txCount) {
return types.ResponseCheckTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeBadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue)}
}
}
return types.ResponseCheckTx{Code: code.CodeTypeOK}
}
func (app *CounterApplication) Commit() (resp types.ResponseCommit) {
2017-01-17 00:26:32 -08:00
app.hashCount++
if app.txCount == 0 {
return types.ResponseCommit{}
}
2017-01-17 00:26:32 -08:00
hash := make([]byte, 8)
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
return types.ResponseCommit{Data: hash}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
switch reqQuery.Path {
2016-12-22 15:24:45 -08:00
case "hash":
return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.hashCount))}
2016-12-22 15:24:45 -08:00
case "tx":
return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.txCount))}
2017-01-27 22:27:32 -08:00
default:
return types.ResponseQuery{Log: cmn.Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
2016-12-22 15:24:45 -08:00
}
2015-11-29 00:43:49 -08:00
}