mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-07 12:30:45 +00:00
Spell out the package explicitly. This commit is totally textual, and does not change any logic. The swiss-army knife package may serve a kick-start in early stage development. But as the codebase growing, we might want to retire it gradually: For simple wrapping functions, just inline it on the call site. For larger pice of code, make it an independent package.
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package counter
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"github.com/tendermint/abci/types"
|
|
common "github.com/tendermint/go-common"
|
|
)
|
|
|
|
type CounterApplication struct {
|
|
hashCount int
|
|
txCount int
|
|
serial bool
|
|
}
|
|
|
|
func NewCounterApplication(serial bool) *CounterApplication {
|
|
return &CounterApplication{serial: serial}
|
|
}
|
|
|
|
func (app *CounterApplication) Info() types.ResponseInfo {
|
|
return types.ResponseInfo{Data: common.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
|
|
}
|
|
|
|
func (app *CounterApplication) SetOption(key string, value string) (log string) {
|
|
if key == "serial" && value == "on" {
|
|
app.serial = true
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (app *CounterApplication) DeliverTx(tx []byte) types.Result {
|
|
if app.serial {
|
|
if len(tx) > 8 {
|
|
return types.ErrEncodingError.SetLog(common.Fmt("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(common.Fmt("Invalid nonce. Expected %v, got %v", app.txCount, txValue))
|
|
}
|
|
}
|
|
app.txCount += 1
|
|
return types.OK
|
|
}
|
|
|
|
func (app *CounterApplication) CheckTx(tx []byte) types.Result {
|
|
if app.serial {
|
|
if len(tx) > 8 {
|
|
return types.ErrEncodingError.SetLog(common.Fmt("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(common.Fmt("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue))
|
|
}
|
|
}
|
|
return types.OK
|
|
}
|
|
|
|
func (app *CounterApplication) Commit() types.Result {
|
|
app.hashCount += 1
|
|
|
|
if app.txCount == 0 {
|
|
return types.OK
|
|
} else {
|
|
hash := make([]byte, 8)
|
|
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
|
|
return types.NewResultOK(hash, "")
|
|
}
|
|
}
|
|
|
|
func (app *CounterApplication) Query(query []byte) types.Result {
|
|
queryStr := string(query)
|
|
|
|
switch queryStr {
|
|
case "hash":
|
|
return types.NewResultOK(nil, common.Fmt("%v", app.hashCount))
|
|
case "tx":
|
|
return types.NewResultOK(nil, common.Fmt("%v", app.txCount))
|
|
}
|
|
|
|
return types.ErrUnknownRequest.SetLog(common.Fmt("Invalid nonce. Expected hash or tx, got %v", queryStr))
|
|
}
|