2016-02-14 14:59:53 -08:00
|
|
|
package counter
|
2015-11-29 03:43:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2017-01-16 22:59:46 -08:00
|
|
|
"fmt"
|
2015-11-29 03:43:49 -05:00
|
|
|
|
2017-01-12 15:47:55 -05:00
|
|
|
"github.com/tendermint/abci/types"
|
2017-01-17 17:22:34 +01:00
|
|
|
. "github.com/tendermint/go-common"
|
2015-11-29 03:43:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type CounterApplication struct {
|
2016-01-08 16:52:02 -08:00
|
|
|
hashCount int
|
|
|
|
txCount int
|
|
|
|
serial bool
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2015-12-08 21:01:33 -08:00
|
|
|
func NewCounterApplication(serial bool) *CounterApplication {
|
|
|
|
return &CounterApplication{serial: serial}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:44:36 -08:00
|
|
|
func (app *CounterApplication) Info() types.ResponseInfo {
|
2017-01-16 22:59:46 -08:00
|
|
|
return types.ResponseInfo{Data: fmt.Sprintf("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
func (app *CounterApplication) SetOption(key string, value string) (log string) {
|
2015-11-30 15:22:42 -08:00
|
|
|
if key == "serial" && value == "on" {
|
2016-01-08 16:52:02 -08:00
|
|
|
app.serial = true
|
2015-11-30 15:22:42 -08:00
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
return ""
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:27:08 -05:00
|
|
|
func (app *CounterApplication) DeliverTx(tx []byte) types.Result {
|
2016-01-08 16:52:02 -08:00
|
|
|
if app.serial {
|
2016-11-23 18:22:22 -05:00
|
|
|
if len(tx) > 8 {
|
2017-01-16 22:59:46 -08:00
|
|
|
return types.ErrEncodingError.SetLog(fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx)))
|
2016-11-23 18:22:22 -05:00
|
|
|
}
|
2015-12-08 21:52:40 -08:00
|
|
|
tx8 := make([]byte, 8)
|
2016-01-25 08:01:18 -08:00
|
|
|
copy(tx8[len(tx8)-len(tx):], tx)
|
|
|
|
txValue := binary.BigEndian.Uint64(tx8)
|
2016-01-08 16:52:02 -08:00
|
|
|
if txValue != uint64(app.txCount) {
|
2017-01-16 22:59:46 -08:00
|
|
|
return types.ErrBadNonce.SetLog(fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue))
|
2015-11-30 15:22:42 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 00:26:32 -08:00
|
|
|
app.txCount++
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.OK
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-03-20 17:10:13 -07:00
|
|
|
func (app *CounterApplication) CheckTx(tx []byte) types.Result {
|
2016-01-08 16:52:02 -08:00
|
|
|
if app.serial {
|
2016-11-23 18:22:22 -05:00
|
|
|
if len(tx) > 8 {
|
2017-01-16 22:59:46 -08:00
|
|
|
return types.ErrEncodingError.SetLog(fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx)))
|
2016-11-23 18:22:22 -05:00
|
|
|
}
|
2016-01-08 16:52:02 -08:00
|
|
|
tx8 := make([]byte, 8)
|
2016-01-25 08:01:18 -08:00
|
|
|
copy(tx8[len(tx8)-len(tx):], tx)
|
|
|
|
txValue := binary.BigEndian.Uint64(tx8)
|
2016-01-08 16:52:02 -08:00
|
|
|
if txValue < uint64(app.txCount) {
|
2017-01-16 22:59:46 -08:00
|
|
|
return types.ErrBadNonce.SetLog(fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue))
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.OK
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:50:29 -07:00
|
|
|
func (app *CounterApplication) Commit() types.Result {
|
2017-01-17 00:26:32 -08:00
|
|
|
app.hashCount++
|
2016-01-08 16:52:02 -08:00
|
|
|
if app.txCount == 0 {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.OK
|
2015-12-04 00:59:26 -08:00
|
|
|
}
|
2017-01-17 00:26:32 -08:00
|
|
|
hash := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
|
|
|
|
return types.NewResultOK(hash, "")
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:42:09 -08:00
|
|
|
func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
|
|
|
|
switch reqQuery.Path {
|
2016-12-22 18:24:45 -05:00
|
|
|
case "hash":
|
2017-01-23 23:42:09 -08:00
|
|
|
return types.ResponseQuery{Value: []byte(Fmt("%v", app.hashCount))}
|
2016-12-22 18:24:45 -05:00
|
|
|
case "tx":
|
2017-01-23 23:42:09 -08:00
|
|
|
return types.ResponseQuery{Value: []byte(Fmt("%v", app.txCount))}
|
2017-01-27 22:27:32 -08:00
|
|
|
default:
|
|
|
|
return types.ResponseQuery{Log: Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
|
2016-12-22 18:24:45 -05:00
|
|
|
}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
2017-02-06 19:08:37 -05:00
|
|
|
|
|
|
|
func (app *CounterApplication) InitChain(validators []*types.Validator) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *CounterApplication) BeginBlock(hash []byte, header *types.Header) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *CounterApplication) EndBlock(height uint64) types.ResponseEndBlock {
|
|
|
|
return types.ResponseEndBlock{}
|
|
|
|
}
|