2015-11-29 03:43:49 -05:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
. "github.com/tendermint/go-common"
|
|
|
|
"github.com/tendermint/tmsp/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) Echo(message string) string {
|
2015-11-29 03:43:49 -05:00
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) Info() []string {
|
|
|
|
return []string{Fmt("hashes:%v, txs:%v", app.hashCount, app.txCount)}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) SetOption(key string, value string) types.RetCode {
|
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
|
|
|
}
|
2015-11-29 03:43:49 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
|
|
|
|
if app.serial {
|
2015-12-08 21:52:40 -08:00
|
|
|
tx8 := make([]byte, 8)
|
|
|
|
copy(tx8, tx)
|
|
|
|
txValue := binary.LittleEndian.Uint64(tx8)
|
2016-01-08 16:52:02 -08:00
|
|
|
if txValue != uint64(app.txCount) {
|
2015-11-30 15:22:42 -08:00
|
|
|
return nil, types.RetCodeInternalError
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 16:52:02 -08:00
|
|
|
app.txCount += 1
|
2015-11-29 03:43:49 -05:00
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -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.RetCodeInternalError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *CounterApplication) GetHash() ([]byte, types.RetCode) {
|
|
|
|
app.hashCount += 1
|
|
|
|
|
|
|
|
if app.txCount == 0 {
|
2015-12-04 00:59:26 -08:00
|
|
|
return nil, 0
|
|
|
|
} else {
|
|
|
|
hash := make([]byte, 32)
|
2016-01-08 16:52:02 -08:00
|
|
|
binary.LittleEndian.PutUint64(hash, uint64(app.txCount))
|
2015-12-04 00:59:26 -08:00
|
|
|
return hash, 0
|
|
|
|
}
|
2015-11-29 03:43:49 -05:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) AddListener(key string) types.RetCode {
|
2015-11-29 03:43:49 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
func (app *CounterApplication) RemListener(key string) types.RetCode {
|
2015-11-29 03:43:49 -05:00
|
|
|
return 0
|
|
|
|
}
|