2016-08-24 01:42:57 -04:00
|
|
|
package dummy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-21 23:42:42 -05:00
|
|
|
"encoding/hex"
|
2017-11-20 20:26:37 -06:00
|
|
|
"fmt"
|
2016-11-21 23:42:42 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-08-24 01:42:57 -04:00
|
|
|
|
2017-11-30 14:29:12 -05:00
|
|
|
"github.com/tendermint/abci/example/code"
|
2017-01-16 22:03:27 -08:00
|
|
|
"github.com/tendermint/abci/types"
|
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
|
|
|
//-----------------------------------------
|
|
|
|
|
2017-11-27 19:04:21 +00:00
|
|
|
var _ types.Application = (*PersistentDummyApplication)(nil)
|
|
|
|
|
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
|
2017-12-22 19:41:19 -08:00
|
|
|
ValUpdates []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
|
|
|
|
2018-02-03 02:02:40 -05:00
|
|
|
state := loadState(db)
|
2016-08-24 01:42:57 -04:00
|
|
|
|
|
|
|
return &PersistentDummyApplication{
|
2018-02-03 02:02:40 -05:00
|
|
|
app: &DummyApplication{state: state},
|
2017-04-28 00:37:18 +04:00
|
|
|
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-11-27 19:04:21 +00:00
|
|
|
func (app *PersistentDummyApplication) Info(req types.RequestInfo) types.ResponseInfo {
|
|
|
|
res := app.app.Info(req)
|
2018-02-03 02:02:40 -05:00
|
|
|
res.LastBlockHeight = app.app.state.Height
|
|
|
|
res.LastBlockAppHash = app.app.state.AppHash
|
2017-11-27 19:04:21 +00:00
|
|
|
return res
|
2016-08-24 01:42:57 -04:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:04:21 +00:00
|
|
|
func (app *PersistentDummyApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
|
|
|
|
return app.app.SetOption(req)
|
2016-08-24 01:42:57 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 00:06:46 +04:00
|
|
|
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
|
2017-11-20 20:26:37 -06:00
|
|
|
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
|
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
|
2017-12-20 00:02:41 -08:00
|
|
|
// and in app.ValUpdates
|
2016-11-21 23:42:42 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-20 20:26:37 -06:00
|
|
|
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.ResponseCheckTx {
|
2016-08-24 01:42:57 -04:00
|
|
|
return app.app.CheckTx(tx)
|
|
|
|
}
|
|
|
|
|
2017-10-23 00:06:50 -04:00
|
|
|
// Commit will panic if InitChain was not called
|
2017-11-20 20:26:37 -06:00
|
|
|
func (app *PersistentDummyApplication) Commit() types.ResponseCommit {
|
2018-02-03 02:02:40 -05:00
|
|
|
return app.app.Commit()
|
2016-08-24 01:42:57 -04:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:42:09 -08: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
|
2017-11-27 19:04:21 +00:00
|
|
|
func (app *PersistentDummyApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
|
|
|
|
for _, v := range req.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
|
|
|
}
|
|
|
|
}
|
2017-11-27 19:04:21 +00:00
|
|
|
return types.ResponseInitChain{}
|
2016-08-24 01:42:57 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 23:42:42 -05:00
|
|
|
// Track the block hash and header information
|
2017-11-27 19:04:21 +00:00
|
|
|
func (app *PersistentDummyApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
|
2016-11-21 23:42:42 -05:00
|
|
|
// reset valset changes
|
2017-12-22 19:41:19 -08:00
|
|
|
app.ValUpdates = make([]types.Validator, 0)
|
2017-11-27 19:04:21 +00:00
|
|
|
return types.ResponseBeginBlock{}
|
2016-08-24 01:42:57 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 23:42:42 -05:00
|
|
|
// Update the validator set
|
2017-11-27 19:04:21 +00:00
|
|
|
func (app *PersistentDummyApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
|
2017-12-20 00:02:41 -08:00
|
|
|
return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
|
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
|
|
|
|
|
2017-12-22 19:41:19 -08:00
|
|
|
func (app *PersistentDummyApplication) Validators() (validators []types.Validator) {
|
2018-02-03 02:02:40 -05:00
|
|
|
itr := app.app.state.db.Iterator(nil, nil)
|
|
|
|
for ; itr.Valid(); itr.Next() {
|
|
|
|
if isValidatorTx(itr.Key()) {
|
2016-11-21 23:42:42 -05:00
|
|
|
validator := new(types.Validator)
|
2018-02-03 02:02:40 -05:00
|
|
|
err := types.ReadMessage(bytes.NewBuffer(itr.Value()), validator)
|
2016-11-21 23:42:42 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-12-22 19:41:19 -08:00
|
|
|
validators = append(validators, *validator)
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
2018-02-03 02:02:40 -05:00
|
|
|
}
|
2016-11-21 23:42:42 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:41:07 -05:00
|
|
|
func MakeValSetChangeTx(pubkey []byte, power int64) []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
|
2017-11-20 20:26:37 -06:00
|
|
|
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
|
2016-11-21 23:42:42 -05:00
|
|
|
tx = tx[len(ValidatorSetChangePrefix):]
|
2017-10-25 23:34:37 -04:00
|
|
|
|
|
|
|
//get the pubkey and power
|
2016-11-21 23:42:42 -05:00
|
|
|
pubKeyAndPower := strings.Split(string(tx), "/")
|
|
|
|
if len(pubKeyAndPower) != 2 {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeEncodingError,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Expected 'pubkey/power'. Got %v", pubKeyAndPower)}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
|
|
|
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
|
2017-10-25 23:34:37 -04:00
|
|
|
|
|
|
|
// 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-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeEncodingError,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Pubkey (%s) is invalid hex", pubkeyS)}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
2018-02-03 02:39:34 -05:00
|
|
|
/*_, err = crypto.PubKeyFromBytes(pubkey)
|
2017-10-25 23:34:37 -04:00
|
|
|
if err != nil {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeEncodingError,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Pubkey (%X) is invalid go-crypto encoded", pubkey)}
|
2018-02-03 02:39:34 -05:00
|
|
|
}*/
|
2017-10-25 23:34:37 -04:00
|
|
|
|
|
|
|
// decode the power
|
2017-12-01 00:41:07 -05:00
|
|
|
power, err := strconv.ParseInt(powerS, 10, 64)
|
2016-11-21 23:42:42 -05:00
|
|
|
if err != nil {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeEncodingError,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2017-12-22 19:41:19 -08:00
|
|
|
return app.updateValidator(types.Validator{pubkey, power})
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// add, update, or remove a validator
|
2017-12-22 19:41:19 -08:00
|
|
|
func (app *PersistentDummyApplication) updateValidator(v types.Validator) types.ResponseDeliverTx {
|
2016-11-21 23:42:42 -05:00
|
|
|
key := []byte("val:" + string(v.PubKey))
|
|
|
|
if v.Power == 0 {
|
|
|
|
// remove validator
|
2018-02-03 02:02:40 -05:00
|
|
|
if !app.app.state.db.Has(key) {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeUnauthorized,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Cannot remove non-existent validator %X", key)}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
2018-02-03 02:02:40 -05:00
|
|
|
app.app.state.db.Delete(key)
|
2016-11-21 23:42:42 -05:00
|
|
|
} else {
|
|
|
|
// add or update validator
|
|
|
|
value := bytes.NewBuffer(make([]byte, 0))
|
2017-12-22 19:41:19 -08:00
|
|
|
if err := types.WriteMessage(&v, value); err != nil {
|
2017-11-20 20:26:37 -06:00
|
|
|
return types.ResponseDeliverTx{
|
2017-11-30 14:29:12 -05:00
|
|
|
Code: code.CodeTypeEncodingError,
|
2017-11-20 20:26:37 -06:00
|
|
|
Log: fmt.Sprintf("Error encoding validator: %v", err)}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
2018-02-03 02:02:40 -05:00
|
|
|
app.app.state.db.Set(key, value.Bytes())
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|
|
|
|
|
2017-09-21 15:26:43 -04:00
|
|
|
// we only update the changes array if we successfully updated the tree
|
2017-12-20 00:02:41 -08:00
|
|
|
app.ValUpdates = append(app.ValUpdates, v)
|
2016-11-21 23:42:42 -05:00
|
|
|
|
2017-11-30 15:37:31 -05:00
|
|
|
return types.ResponseDeliverTx{Code: code.CodeTypeOK}
|
2016-11-21 23:42:42 -05:00
|
|
|
}
|