CheckTx and DeliverTx return ResponseCheckTx and ResponseDeliverTx respectively

Commit now returns ResponseCommit
This commit is contained in:
Anton Kaliaev
2017-11-20 20:26:37 -06:00
parent 92801dbd72
commit 3a3d508e5c
7 changed files with 74 additions and 44 deletions

View File

@@ -2,6 +2,7 @@ package counter
import (
"encoding/binary"
"fmt"
"github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
@@ -30,45 +31,53 @@ func (app *CounterApplication) SetOption(key string, value string) (log string)
return ""
}
func (app *CounterApplication) DeliverTx(tx []byte) types.Result {
func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
if app.serial {
if len(tx) > 8 {
return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
return types.ResponseDeliverTx{
Code: types.CodeType_EncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
}
tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue != uint64(app.txCount) {
return types.ErrBadNonce.SetLog(cmn.Fmt("Invalid nonce. Expected %v, got %v", app.txCount, txValue))
return types.ResponseDeliverTx{
Code: types.CodeType_BadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
}
}
app.txCount++
return types.OK
return types.ResponseDeliverTx{Code: types.CodeType_OK}
}
func (app *CounterApplication) CheckTx(tx []byte) types.Result {
func (app *CounterApplication) CheckTx(tx []byte) types.ResponseCheckTx {
if app.serial {
if len(tx) > 8 {
return types.ErrEncodingError.SetLog(cmn.Fmt("Max tx size is 8 bytes, got %d", len(tx)))
return types.ResponseCheckTx{
Code: types.CodeType_EncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
}
tx8 := make([]byte, 8)
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue < uint64(app.txCount) {
return types.ErrBadNonce.SetLog(cmn.Fmt("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue))
return types.ResponseCheckTx{
Code: types.CodeType_BadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue)}
}
}
return types.OK
return types.ResponseCheckTx{Code: types.CodeType_OK}
}
func (app *CounterApplication) Commit() types.Result {
func (app *CounterApplication) Commit() (resp types.ResponseCommit) {
app.hashCount++
if app.txCount == 0 {
return types.OK
return types.ResponseCommit{Code: types.CodeType_OK}
}
hash := make([]byte, 8)
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
return types.NewResultOK(hash, "")
return types.ResponseCommit{Code: types.CodeType_OK, Data: hash}
}
func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {