tendermint/example/golang/counter.go

83 lines
1.8 KiB
Go
Raw Normal View History

2015-11-29 00:43:49 -08:00
package example
import (
"encoding/binary"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/types"
)
type CounterApplication struct {
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) Echo(message string) string {
2015-11-29 00:43:49 -08:00
return message
}
func (app *CounterApplication) Info() []string {
return []string{Fmt("hashes:%v, txs:%v", app.hashCount, app.txCount)}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) SetOption(key string, value string) types.RetCode {
if key == "serial" && value == "on" {
app.serial = true
}
2016-01-18 14:37:42 -08:00
return types.RetCodeOK
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
if app.serial {
tx8 := make([]byte, 8)
copy(tx8, tx)
txValue := binary.LittleEndian.Uint64(tx8)
if txValue != uint64(app.txCount) {
return nil, types.RetCodeBadNonce
}
}
app.txCount += 1
2016-01-18 14:37:42 -08:00
return nil, types.RetCodeOK
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) CheckTx(tx []byte) types.RetCode {
if app.serial {
tx8 := make([]byte, 8)
copy(tx8, tx)
txValue := binary.LittleEndian.Uint64(tx8)
if txValue < uint64(app.txCount) {
return types.RetCodeBadNonce
}
}
2016-01-18 14:37:42 -08:00
return types.RetCodeOK
}
func (app *CounterApplication) GetHash() ([]byte, types.RetCode) {
app.hashCount += 1
if app.txCount == 0 {
2016-01-18 14:37:42 -08:00
return nil, types.RetCodeOK
} else {
hash := make([]byte, 32)
binary.LittleEndian.PutUint64(hash, uint64(app.txCount))
2016-01-18 14:37:42 -08:00
return hash, types.RetCodeOK
}
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) AddListener(key string) types.RetCode {
2016-01-18 14:37:42 -08:00
return types.RetCodeOK
2015-11-29 00:43:49 -08:00
}
func (app *CounterApplication) RemListener(key string) types.RetCode {
2016-01-18 14:37:42 -08:00
return types.RetCodeOK
}
2016-01-22 16:18:35 -08:00
func (app *CounterApplication) Query(query []byte) ([]byte, types.RetCode) {
return nil, types.RetCodeOK
2015-11-29 00:43:49 -08:00
}