tendermint/example/dummy/persistent_dummy.go

204 lines
5.6 KiB
Go
Raw Normal View History

2016-08-24 01:42:57 -04:00
package dummy
import (
"bytes"
2016-11-21 23:42:42 -05:00
"encoding/hex"
"strconv"
"strings"
2016-08-24 01:42:57 -04:00
2017-01-16 22:03:27 -08:00
"github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
2017-10-18 12:46:51 +02:00
"github.com/tendermint/iavl"
2017-04-21 18:25:13 -04:00
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
2017-04-28 00:37:18 +04:00
"github.com/tendermint/tmlibs/log"
2016-08-24 01:42:57 -04:00
)
2016-11-21 23:42:42 -05:00
const (
ValidatorSetChangePrefix string = "val:"
)
2016-08-24 01:42:57 -04:00
//-----------------------------------------
type PersistentDummyApplication struct {
app *DummyApplication
2016-09-09 23:01:53 -04:00
2016-11-21 23:42:42 -05:00
// validator set
changes []*types.Validator
2017-04-28 00:37:18 +04:00
logger log.Logger
2016-08-24 01:42:57 -04:00
}
func NewPersistentDummyApplication(dbDir string) *PersistentDummyApplication {
2017-10-18 12:46:51 +02:00
name := "dummy"
db, err := dbm.NewGoLevelDB(name, dbDir)
if err != nil {
panic(err)
}
2016-08-24 01:42:57 -04:00
2017-10-18 12:46:51 +02:00
stateTree := iavl.NewVersionedTree(500, db)
2017-10-23 14:08:36 +02:00
stateTree.Load()
2016-08-24 01:42:57 -04:00
return &PersistentDummyApplication{
2017-04-28 00:37:18 +04:00
app: &DummyApplication{state: stateTree},
logger: log.NewNopLogger(),
2016-08-24 01:42:57 -04:00
}
}
2017-04-28 00:37:18 +04:00
func (app *PersistentDummyApplication) SetLogger(l log.Logger) {
app.logger = l
}
2017-09-22 11:10:39 -04:00
func (app *PersistentDummyApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
resInfo = app.app.Info(req)
resInfo.LastBlockHeight = app.app.state.LatestVersion()
2017-10-18 12:46:51 +02:00
resInfo.LastBlockAppHash = app.app.state.Hash()
2016-12-26 17:44:36 -08:00
return resInfo
2016-08-24 01:42:57 -04:00
}
func (app *PersistentDummyApplication) SetOption(key string, value string) (log string) {
return app.app.SetOption(key, value)
}
2017-10-04 00:06:46 +04:00
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
2017-01-12 15:27:08 -05:00
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.Result {
2016-11-21 23:42:42 -05: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.changes
return app.execValidatorTx(tx)
}
// otherwise, update the key-value store
2017-01-12 15:27:08 -05:00
return app.app.DeliverTx(tx)
2016-08-24 01:42:57 -04:00
}
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.Result {
return app.app.CheckTx(tx)
}
// Commit will panic if InitChain was not called
2016-08-24 01:42:57 -04:00
func (app *PersistentDummyApplication) Commit() types.Result {
2017-10-18 12:46:51 +02:00
// Save a new version for next height
height := app.app.state.LatestVersion() + 1
2017-10-18 12:46:51 +02:00
var appHash []byte
var err error
2017-10-18 13:13:18 +02: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 12:46:51 +02:00
}
2016-09-09 23:01:53 -04:00
app.logger.Info("Commit block", "height", height, "root", appHash)
2016-09-09 23:01:53 -04:00
return types.NewResultOK(appHash, "")
2016-08-24 01:42:57 -04:00
}
func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
return app.app.Query(reqQuery)
2016-08-24 01:42:57 -04:00
}
2016-11-21 23:42:42 -05:00
// Save the validators in the merkle tree
func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) {
for _, v := range params.Validators {
2016-11-21 23:42:42 -05:00
r := app.updateValidator(v)
if r.IsErr() {
2017-04-28 00:37:18 +04:00
app.logger.Error("Error updating validators", "r", r)
2016-11-21 23:42:42 -05:00
}
}
2016-08-24 01:42:57 -04:00
}
2016-11-21 23:42:42 -05:00
// Track the block hash and header information
func (app *PersistentDummyApplication) BeginBlock(params types.RequestBeginBlock) {
2016-11-21 23:42:42 -05:00
// reset valset changes
app.changes = make([]*types.Validator, 0)
2016-08-24 01:42:57 -04:00
}
2016-11-21 23:42:42 -05:00
// Update the validator set
2016-12-26 22:12:32 -08:00
func (app *PersistentDummyApplication) EndBlock(height uint64) (resEndBlock types.ResponseEndBlock) {
return types.ResponseEndBlock{Diffs: app.changes}
2016-08-24 01:42:57 -04:00
}
2016-11-06 02:02:08 +00:00
2016-11-21 23:42:42 -05: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
}
func MakeValSetChangeTx(pubkey []byte, power uint64) []byte {
2017-03-03 18:39:10 -05:00
return []byte(cmn.Fmt("val:%X/%d", pubkey, power))
2016-11-21 23:42:42 -05:00
}
func isValidatorTx(tx []byte) bool {
2017-09-21 15:26:43 -04:00
return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
2016-11-21 23:42:42 -05:00
}
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.Result {
tx = tx[len(ValidatorSetChangePrefix):]
//get the pubkey and power
2016-11-21 23:42:42 -05:00
pubKeyAndPower := strings.Split(string(tx), "/")
if len(pubKeyAndPower) != 2 {
2017-03-03 18:39:10 -05:00
return types.ErrEncodingError.SetLog(cmn.Fmt("Expected 'pubkey/power'. Got %v", pubKeyAndPower))
2016-11-21 23:42:42 -05:00
}
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
// decode the pubkey, ensuring its go-crypto encoded
2016-11-21 23:42:42 -05:00
pubkey, err := hex.DecodeString(pubkeyS)
if err != nil {
2017-03-03 18:39:10 -05:00
return types.ErrEncodingError.SetLog(cmn.Fmt("Pubkey (%s) is invalid hex", pubkeyS))
2016-11-21 23:42:42 -05:00
}
_, err = crypto.PubKeyFromBytes(pubkey)
if err != nil {
return types.ErrEncodingError.SetLog(cmn.Fmt("Pubkey (%X) is invalid go-crypto encoded", pubkey))
}
// decode the power
2016-11-21 23:42:42 -05:00
power, err := strconv.Atoi(powerS)
if err != nil {
2017-03-03 18:39:10 -05:00
return types.ErrEncodingError.SetLog(cmn.Fmt("Power (%s) is not an int", powerS))
2016-11-21 23:42:42 -05:00
}
// update
return app.updateValidator(&types.Validator{pubkey, uint64(power)})
}
// add, update, or remove a validator
func (app *PersistentDummyApplication) updateValidator(v *types.Validator) types.Result {
key := []byte("val:" + string(v.PubKey))
if v.Power == 0 {
// remove validator
if !app.app.state.Has(key) {
2017-03-03 18:39:10 -05:00
return types.ErrUnauthorized.SetLog(cmn.Fmt("Cannot remove non-existent validator %X", key))
2016-11-21 23:42:42 -05: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 {
2017-03-03 18:39:10 -05:00
return types.ErrInternalError.SetLog(cmn.Fmt("Error encoding validator: %v", err))
2016-11-21 23:42:42 -05:00
}
app.app.state.Set(key, value.Bytes())
}
2017-09-21 15:26:43 -04:00
// we only update the changes array if we successfully updated the tree
2016-11-21 23:42:42 -05:00
app.changes = append(app.changes, v)
return types.OK
}