tendermint/example/dummy/persistent_dummy.go

223 lines
6.3 KiB
Go
Raw Normal View History

2016-08-23 22:42:57 -07:00
package dummy
import (
"bytes"
2016-11-21 20:42:42 -08:00
"encoding/hex"
"fmt"
2016-11-21 20:42:42 -08:00
"strconv"
"strings"
2016-08-23 22:42:57 -07:00
2017-11-30 11:29:12 -08:00
"github.com/tendermint/abci/example/code"
2017-01-16 22:03:27 -08:00
"github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
2017-10-18 03:46:51 -07:00
"github.com/tendermint/iavl"
2017-04-21 15:25:13 -07:00
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
2017-04-27 13:37:18 -07:00
"github.com/tendermint/tmlibs/log"
2016-08-23 22:42:57 -07:00
)
2016-11-21 20:42:42 -08:00
const (
ValidatorSetChangePrefix string = "val:"
)
2016-08-23 22:42:57 -07:00
//-----------------------------------------
var _ types.Application = (*PersistentDummyApplication)(nil)
2016-08-23 22:42:57 -07:00
type PersistentDummyApplication struct {
app *DummyApplication
2016-09-09 20:01:53 -07:00
2016-11-21 20:42:42 -08:00
// validator set
valSetUpdates []*types.Validator
2017-04-27 13:37:18 -07:00
logger log.Logger
2016-08-23 22:42:57 -07:00
}
func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
2017-10-18 03:46:51 -07:00
name := "dummy"
db, err := dbm.NewGoLevelDB(name, dbDir)
if err != nil {
panic(err)
}
2016-08-23 22:42:57 -07:00
2017-10-18 03:46:51 -07:00
stateTree := iavl.NewVersionedTree(500, db)
2017-10-23 05:08:36 -07:00
stateTree.Load()
2016-08-23 22:42:57 -07:00
return &PersistentDummyApplication{
2017-04-27 13:37:18 -07:00
app: &DummyApplication{state: stateTree},
logger: log.NewNopLogger(),
2016-08-23 22:42:57 -07:00
}
}
2017-04-27 13:37:18 -07:00
func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
app.logger = l
}
func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
res := app.app.Info(req)
2017-11-30 21:41:07 -08:00
var latestVersion uint64 = app.app.state.LatestVersion() // TODO: change to int64
res.LastBlockHeight = int64(latestVersion)
res.LastBlockAppHash = app.app.state.Hash()
return res
2016-08-23 22:42:57 -07:00
}
func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
return app.app.SetOption(req)
2016-08-23 22:42:57 -07:00
}
2017-10-03 13:06:46 -07:00
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
2016-11-21 20:42:42 -08:00
// if it starts with "val:", update the validator set
// format is "val:pubkey/power"
if isValidatorTx(tx) {
// update validators in the merkle tree
// and in app.valSetUpdates
2016-11-21 20:42:42 -08:00
return app.execValidatorTx(tx)
}
// otherwise, update the key-value store
2017-01-12 12:27:08 -08:00
return app.app.DeliverTx(tx)
2016-08-23 22:42:57 -07:00
}
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
2016-08-23 22:42:57 -07:00
return app.app.CheckTx(tx)
}
// Commit will panic if InitChain was not called
func (app *PersistentDummyApplication) Commit() types.ResponseCommit {
2017-10-18 03:46:51 -07:00
// Save a new version for next height
height := app.app.state.LatestVersion() + 1
2017-10-18 03:46:51 -07:00
var appHash []byte
var err error
2017-10-18 04:13:18 -07:00
appHash, err = app.app.state.SaveVersion(height)
if err != nil {
// if this wasn't a dummy app, we'd do something smarter
panic(err)
2017-10-18 03:46:51 -07:00
}
2016-09-09 20:01:53 -07:00
app.logger.Info("Commit block", "height", height, "root", appHash)
2017-11-30 12:37:31 -08:00
return types.ResponseCommit{Code: code.CodeTypeOK, Data: appHash}
2016-08-23 22:42:57 -07:00
}
func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
return app.app.Query(reqQuery)
2016-08-23 22:42:57 -07:00
}
2016-11-21 20:42:42 -08:00
// Save the validators in the merkle tree
func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
for _, v := range req.Validators {
2016-11-21 20:42:42 -08:00
r := app.updateValidator(v)
if r.IsErr() {
2017-04-27 13:37:18 -07:00
app.logger.Error("Error updating validators", "r", r)
2016-11-21 20:42:42 -08:00
}
}
return types.ResponseInitChain{}
2016-08-23 22:42:57 -07:00
}
2016-11-21 20:42:42 -08:00
// Track the block hash and header information
func (app *PersistentDummyApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
2016-11-21 20:42:42 -08:00
// reset valset changes
app.valSetUpdates = make([]*types.Validator, 0)
return types.ResponseBeginBlock{}
2016-08-23 22:42:57 -07:00
}
2016-11-21 20:42:42 -08:00
// Update the validator set
func (app *PersistentDummyApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
return types.ResponseEndBlock{ValidatorSetUpdates: app.valSetUpdates}
2016-08-23 22:42:57 -07:00
}
2016-11-05 19:02:08 -07:00
2016-11-21 20:42:42 -08:00
//---------------------------------------------
// update validators
func (app *PersistentDummyApplication) Validators() (validators []*types.Validator) {
app.app.state.Iterate(func(key, value []byte) bool {
if isValidatorTx(key) {
validator := new(types.Validator)
err := types.ReadMessage(bytes.NewBuffer(value), validator)
if err != nil {
panic(err)
}
validators = append(validators, validator)
}
return false
})
return
}
2017-11-30 21:41:07 -08:00
func MakeValSetChangeTx(pubkey []byte, power int64) []byte {
2017-03-03 15:39:10 -08:00
return []byte(cmn.Fmt("val:%X/%d", pubkey, power))
2016-11-21 20:42:42 -08:00
}
func isValidatorTx(tx []byte) bool {
2017-09-21 12:26:43 -07:00
return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
2016-11-21 20:42:42 -08:00
}
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
2016-11-21 20:42:42 -08:00
tx = tx[len(ValidatorSetChangePrefix):]
//get the pubkey and power
2016-11-21 20:42:42 -08:00
pubKeyAndPower := strings.Split(string(tx), "/")
if len(pubKeyAndPower) != 2 {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Expected 'pubkey/power'. Got %v", pubKeyAndPower)}
2016-11-21 20:42:42 -08:00
}
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
// decode the pubkey, ensuring its go-crypto encoded
2016-11-21 20:42:42 -08:00
pubkey, err := hex.DecodeString(pubkeyS)
if err != nil {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Pubkey (%s) is invalid hex", pubkeyS)}
2016-11-21 20:42:42 -08:00
}
_, err = crypto.PubKeyFromBytes(pubkey)
if err != nil {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Pubkey (%X) is invalid go-crypto encoded", pubkey)}
}
// decode the power
2017-11-30 21:41:07 -08:00
power, err := strconv.ParseInt(powerS, 10, 64)
2016-11-21 20:42:42 -08:00
if err != nil {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
2016-11-21 20:42:42 -08:00
}
// update
2017-11-30 21:41:07 -08:00
return app.updateValidator(&types.Validator{pubkey, power})
2016-11-21 20:42:42 -08:00
}
// add, update, or remove a validator
func (app *PersistentDummyApplication) updateValidator(v *types.Validator) types.ResponseDeliverTx {
2016-11-21 20:42:42 -08:00
key := []byte("val:" + string(v.PubKey))
if v.Power == 0 {
// remove validator
if !app.app.state.Has(key) {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeUnauthorized,
Log: fmt.Sprintf("Cannot remove non-existent validator %X", key)}
2016-11-21 20:42:42 -08:00
}
app.app.state.Remove(key)
} else {
// add or update validator
value := bytes.NewBuffer(make([]byte, 0))
if err := types.WriteMessage(v, value); err != nil {
return types.ResponseDeliverTx{
2017-11-30 11:29:12 -08:00
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Error encoding validator: %v", err)}
2016-11-21 20:42:42 -08:00
}
app.app.state.Set(key, value.Bytes())
}
2017-09-21 12:26:43 -07:00
// we only update the changes array if we successfully updated the tree
app.valSetUpdates = append(app.valSetUpdates, v)
2016-11-21 20:42:42 -08:00
2017-11-30 12:37:31 -08:00
return types.ResponseDeliverTx{Code: code.CodeTypeOK}
2016-11-21 20:42:42 -08:00
}