|
@ -3,6 +3,7 @@ package dummy |
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"encoding/hex" |
|
|
"encoding/hex" |
|
|
|
|
|
"fmt" |
|
|
"strconv" |
|
|
"strconv" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
@ -61,7 +62,7 @@ func (app *PersistentDummyApplication) SetOption(key string, value string) (log |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
|
|
|
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
|
|
|
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.Result { |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.ResponseDeliverTx { |
|
|
// if it starts with "val:", update the validator set
|
|
|
// if it starts with "val:", update the validator set
|
|
|
// format is "val:pubkey/power"
|
|
|
// format is "val:pubkey/power"
|
|
|
if isValidatorTx(tx) { |
|
|
if isValidatorTx(tx) { |
|
@ -74,12 +75,12 @@ func (app *PersistentDummyApplication) DeliverTx(tx []byte) types.Result { |
|
|
return app.app.DeliverTx(tx) |
|
|
return app.app.DeliverTx(tx) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.Result { |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) CheckTx(tx []byte) types.ResponseCheckTx { |
|
|
return app.app.CheckTx(tx) |
|
|
return app.app.CheckTx(tx) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Commit will panic if InitChain was not called
|
|
|
// Commit will panic if InitChain was not called
|
|
|
func (app *PersistentDummyApplication) Commit() types.Result { |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) Commit() types.ResponseCommit { |
|
|
|
|
|
|
|
|
// Save a new version for next height
|
|
|
// Save a new version for next height
|
|
|
height := app.app.state.LatestVersion() + 1 |
|
|
height := app.app.state.LatestVersion() + 1 |
|
@ -93,7 +94,7 @@ func (app *PersistentDummyApplication) Commit() types.Result { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
app.logger.Info("Commit block", "height", height, "root", appHash) |
|
|
app.logger.Info("Commit block", "height", height, "root", appHash) |
|
|
return types.NewResultOK(appHash, "") |
|
|
|
|
|
|
|
|
return types.ResponseCommit{Code: types.CodeType_OK, Data: appHash} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery { |
|
|
func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery { |
|
@ -148,30 +149,38 @@ func isValidatorTx(tx []byte) bool { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
|
|
|
// format is "val:pubkey1/power1,addr2/power2,addr3/power3"tx
|
|
|
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.Result { |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx { |
|
|
tx = tx[len(ValidatorSetChangePrefix):] |
|
|
tx = tx[len(ValidatorSetChangePrefix):] |
|
|
|
|
|
|
|
|
//get the pubkey and power
|
|
|
//get the pubkey and power
|
|
|
pubKeyAndPower := strings.Split(string(tx), "/") |
|
|
pubKeyAndPower := strings.Split(string(tx), "/") |
|
|
if len(pubKeyAndPower) != 2 { |
|
|
if len(pubKeyAndPower) != 2 { |
|
|
return types.ErrEncodingError.SetLog(cmn.Fmt("Expected 'pubkey/power'. Got %v", pubKeyAndPower)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_EncodingError, |
|
|
|
|
|
Log: fmt.Sprintf("Expected 'pubkey/power'. Got %v", pubKeyAndPower)} |
|
|
} |
|
|
} |
|
|
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1] |
|
|
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1] |
|
|
|
|
|
|
|
|
// decode the pubkey, ensuring its go-crypto encoded
|
|
|
// decode the pubkey, ensuring its go-crypto encoded
|
|
|
pubkey, err := hex.DecodeString(pubkeyS) |
|
|
pubkey, err := hex.DecodeString(pubkeyS) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return types.ErrEncodingError.SetLog(cmn.Fmt("Pubkey (%s) is invalid hex", pubkeyS)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_EncodingError, |
|
|
|
|
|
Log: fmt.Sprintf("Pubkey (%s) is invalid hex", pubkeyS)} |
|
|
} |
|
|
} |
|
|
_, err = crypto.PubKeyFromBytes(pubkey) |
|
|
_, err = crypto.PubKeyFromBytes(pubkey) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return types.ErrEncodingError.SetLog(cmn.Fmt("Pubkey (%X) is invalid go-crypto encoded", pubkey)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_EncodingError, |
|
|
|
|
|
Log: fmt.Sprintf("Pubkey (%X) is invalid go-crypto encoded", pubkey)} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// decode the power
|
|
|
// decode the power
|
|
|
power, err := strconv.Atoi(powerS) |
|
|
power, err := strconv.Atoi(powerS) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return types.ErrEncodingError.SetLog(cmn.Fmt("Power (%s) is not an int", powerS)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_EncodingError, |
|
|
|
|
|
Log: fmt.Sprintf("Power (%s) is not an int", powerS)} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// update
|
|
|
// update
|
|
@ -179,19 +188,23 @@ func (app *PersistentDummyApplication) execValidatorTx(tx []byte) types.Result { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// add, update, or remove a validator
|
|
|
// add, update, or remove a validator
|
|
|
func (app *PersistentDummyApplication) updateValidator(v *types.Validator) types.Result { |
|
|
|
|
|
|
|
|
func (app *PersistentDummyApplication) updateValidator(v *types.Validator) types.ResponseDeliverTx { |
|
|
key := []byte("val:" + string(v.PubKey)) |
|
|
key := []byte("val:" + string(v.PubKey)) |
|
|
if v.Power == 0 { |
|
|
if v.Power == 0 { |
|
|
// remove validator
|
|
|
// remove validator
|
|
|
if !app.app.state.Has(key) { |
|
|
if !app.app.state.Has(key) { |
|
|
return types.ErrUnauthorized.SetLog(cmn.Fmt("Cannot remove non-existent validator %X", key)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_Unauthorized, |
|
|
|
|
|
Log: fmt.Sprintf("Cannot remove non-existent validator %X", key)} |
|
|
} |
|
|
} |
|
|
app.app.state.Remove(key) |
|
|
app.app.state.Remove(key) |
|
|
} else { |
|
|
} else { |
|
|
// add or update validator
|
|
|
// add or update validator
|
|
|
value := bytes.NewBuffer(make([]byte, 0)) |
|
|
value := bytes.NewBuffer(make([]byte, 0)) |
|
|
if err := types.WriteMessage(v, value); err != nil { |
|
|
if err := types.WriteMessage(v, value); err != nil { |
|
|
return types.ErrInternalError.SetLog(cmn.Fmt("Error encoding validator: %v", err)) |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{ |
|
|
|
|
|
Code: types.CodeType_InternalError, |
|
|
|
|
|
Log: fmt.Sprintf("Error encoding validator: %v", err)} |
|
|
} |
|
|
} |
|
|
app.app.state.Set(key, value.Bytes()) |
|
|
app.app.state.Set(key, value.Bytes()) |
|
|
} |
|
|
} |
|
@ -199,5 +212,5 @@ func (app *PersistentDummyApplication) updateValidator(v *types.Validator) types |
|
|
// we only update the changes array if we successfully updated the tree
|
|
|
// we only update the changes array if we successfully updated the tree
|
|
|
app.changes = append(app.changes, v) |
|
|
app.changes = append(app.changes, v) |
|
|
|
|
|
|
|
|
return types.OK |
|
|
|
|
|
|
|
|
return types.ResponseDeliverTx{Code: types.CodeType_OK} |
|
|
} |
|
|
} |