2016-02-14 14:59:53 -08:00
|
|
|
package counter
|
2015-11-29 03:43:49 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
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 {
|
|
|
|
return types.ResponseInfo{Data: Fmt("{\"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 {
|
|
|
|
return types.ErrEncodingError.SetLog(Fmt("Max tx size is 8 bytes, got %d", len(tx)))
|
|
|
|
}
|
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) {
|
2016-11-21 23:42:42 -05:00
|
|
|
return types.ErrBadNonce.SetLog(Fmt("Invalid nonce. Expected %v, got %v", app.txCount, txValue))
|
2015-11-30 15:22:42 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 16:52:02 -08:00
|
|
|
app.txCount += 1
|
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 {
|
|
|
|
return types.ErrEncodingError.SetLog(Fmt("Max tx size is 8 bytes, got %d", len(tx)))
|
|
|
|
}
|
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) {
|
2016-11-21 23:42:42 -05:00
|
|
|
return types.ErrBadNonce.SetLog(Fmt("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 {
|
2016-01-08 16:52:02 -08:00
|
|
|
app.hashCount += 1
|
|
|
|
|
|
|
|
if app.txCount == 0 {
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.OK
|
2015-12-04 00:59:26 -08:00
|
|
|
} else {
|
2016-01-25 08:01:18 -08:00
|
|
|
hash := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
|
2016-03-23 02:50:29 -07:00
|
|
|
return types.NewResultOK(hash, "")
|
2015-12-04 00:59:26 -08:00
|
|
|
}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-03-20 17:10:13 -07:00
|
|
|
func (app *CounterApplication) Query(query []byte) types.Result {
|
2016-12-22 18:24:45 -05:00
|
|
|
queryStr := string(query)
|
|
|
|
|
|
|
|
switch queryStr {
|
|
|
|
case "hash":
|
|
|
|
return types.NewResultOK(nil, Fmt("%v", app.hashCount))
|
|
|
|
case "tx":
|
|
|
|
return types.NewResultOK(nil, Fmt("%v", app.txCount))
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.ErrUnknownRequest.SetLog(Fmt("Invalid nonce. Expected hash or tx, got %v", queryStr))
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
2017-01-10 15:49:26 +01:00
|
|
|
|
2017-01-17 17:22:34 +01:00
|
|
|
func (app *CounterApplication) Proof(key []byte, blockHeight uint64) types.Result {
|
2017-01-10 15:49:26 +01:00
|
|
|
return types.NewResultOK(nil, Fmt("Proof is not supported"))
|
|
|
|
}
|