Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap StakingPower and VotingPower, and modify from StakingPower to VotingWeight #449

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions abci/cmd/abci-cli/abci-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ var (
// kvstore
flagPersist string

// staking power for make validator_tx
flagStakingPower int64
// voting power for make validator_tx
flagVotingPower123456 int64
tnasu marked this conversation as resolved.
Show resolved Hide resolved
)

var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -154,8 +154,8 @@ func addKVStoreFlags() {
}

func addPersistKVStoreMakeValSetChangeTxFlags() {
kvstoreCmd.PersistentFlags().Int64VarP(&flagStakingPower, "staking_power", "p", int64(10),
"stakin power for ValSetChangeTx")
kvstoreCmd.PersistentFlags().Int64VarP(&flagVotingPower123456, "voting_power", "p", int64(10),
"voting power for ValSetChangeTx")
}

func addCommands() {
Expand Down Expand Up @@ -731,7 +731,7 @@ func cmdPersistKVStoreMakeValSetChangeTx(cmd *cobra.Command, args []string) erro
if err != nil {
panic(err)
}
pubStr, tx := kvstore.MakeValSetChangeTxAndMore(publicKey, flagStakingPower)
pubStr, tx := kvstore.MakeValSetChangeTxAndMore(publicKey, flagVotingPower123456)
{
fmt.Printf("DeliverTxSync: data=%s, tx=%s\n", pubStr, tx)
res, err := client.DeliverTxSync(types.RequestDeliverTx{Tx: []byte(tx)})
Expand Down
4 changes: 2 additions & 2 deletions abci/example/kvstore/tool/make_val_set_change_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
c.SetRoot(filepath.Join(userHome, config.DefaultOstraconDir))
keyFilePath := c.PrivValidatorKeyFile()
var flagKeyFilePath = flag.String("priv-key", keyFilePath, "priv val key file path")
var flagStakingPower = flag.Int64("staking", 10, "staking power for priv valedator")
var flagVotingPower = flag.Int64("voting", 10, "voting power for priv validator")
flag.Parse()
keyFile, err := kvstore.LoadPrivValidatorKeyFile(*flagKeyFilePath)
if err != nil {
Expand All @@ -31,7 +31,7 @@ func main() {
if err != nil {
panic(err)
}
pubStr, tx := kvstore.MakeValSetChangeTxAndMore(publicKey, *flagStakingPower)
pubStr, tx := kvstore.MakeValSetChangeTxAndMore(publicKey, *flagVotingPower)
{
fmt.Println("\n# Send tx of ValSetChangeTx for persist_kvstore")
fmt.Println("# See: persist_kvstore.go#DeliveredTx")
Expand Down
9 changes: 6 additions & 3 deletions abci/example/kvstore/tool/make_val_set_change_tx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
IFS_BACK=$IFS
IFS=$'\r\n'

cd `dirname $0`
# shellcheck disable=SC2046
# shellcheck disable=SC2006
cd `dirname "$0"` || exit

# updator validator with default parameter
commands=`go run make_val_set_change_tx.go --staking=10 --priv-key=${HOME}/.ostracon/config/priv_validator_key.json`
commands=`go run make_val_set_change_tx.go --voting=10 --priv-key=${HOME}/.ostracon/config/priv_validator_key.json`
# remove validator tx
commands=`go run make_val_set_change_tx.go --staking=0`
commands=`go run make_val_set_change_tx.go --voting=0`
# update validator tx
commands=`go run make_val_set_change_tx.go`
# shellcheck disable=SC2068
for command in ${commands[@]}; do
if [[ "$command" =~ \# ]]; then
echo $command
Expand Down
384 changes: 192 additions & 192 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion blockchain/v0/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
val, privVal := types.RandValidator(randPower, minPower)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/v1/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
val, privVal := types.RandValidator(randPower, minPower)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/v2/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func randGenesisDoc(chainID string, numValidators int, randPower bool, minPower
val, privVal := types.RandValidator(randPower, minPower)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
Expand Down
12 changes: 6 additions & 6 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,15 +984,15 @@ func genesisDoc(
val, privVal = createTestValidator(minPower, types.PrivKeyComposite)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
for i := nValsWithComposite; i < numValidators; i++ {
val, privVal = createTestValidator(minPower, types.PrivKeyEd25519)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
Expand All @@ -1018,7 +1018,7 @@ func randGenesisDoc(
val, privVal := types.RandValidator(randPower, minPower)
validators[i] = types.GenesisValidator{
PubKey: val.PubKey,
Power: val.StakingPower,
Power: val.VotingPower,
}
privValidators[i] = privVal
}
Expand Down Expand Up @@ -1123,13 +1123,13 @@ func signDataIsEqual(v1 *types.Vote, v2 *tmproto.Vote) bool {
// Validator
func createTestValidator(minPower int64, keytype types.PrivKeyType) (*types.Validator, types.PrivValidator) {
privVal := types.NewMockPV(keytype)
stakingPower := minPower
stakingPower += 100
votingPower := minPower
votingPower += 100

pubKey, err := privVal.GetPubKey()
if err != nil {
panic(fmt.Errorf("could not retrieve pubkey %w", err))
}
val := types.NewValidator(pubKey, stakingPower)
val := types.NewValidator(pubKey, votingPower)
return val, privVal
}
46 changes: 23 additions & 23 deletions consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) {
//-------------------------------------------------------------
// ensure we can make blocks despite cycling a validator set

func TestReactorStakingPowerChange(t *testing.T) {
func TestReactorVotingWeightChange(t *testing.T) {
nVals := 4
logger := log.TestingLogger()
css, cleanup := randConsensusNet(
nVals,
"consensus_voting_power_changes_test",
"consensus_voting_weight_changes_test",
newMockTickerFunc(true),
newPersistentKVStore)
defer cleanup()
Expand All @@ -345,56 +345,56 @@ func TestReactorStakingPowerChange(t *testing.T) {
}, css)

//---------------------------------------------------------------------------
logger.Debug("---------------------------- Testing changing the voting power of one validator a few times")
logger.Debug("---------------------------- Testing changing the voting weight of one validator a few times")

val1PubKey, err := css[0].privValidator.GetPubKey()
require.NoError(t, err)

val1PubKeyABCI, err := cryptoenc.PubKeyToProto(val1PubKey)
require.NoError(t, err)
updateValidatorTx := kvstore.MakeValSetChangeTx(val1PubKeyABCI, 25)
previousTotalVotingPower := css[0].GetRoundState().LastVoters.TotalVotingPower()
previousTotalVotingWeight := css[0].GetRoundState().LastVoters.TotalVotingWeight()

waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlockWithTx(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)

if css[0].GetRoundState().LastVoters.TotalVotingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected staking power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalVotingPower())
"expected voting weight to change (before: %d, after: %d)",
previousTotalVotingWeight,
css[0].GetRoundState().LastVoters.TotalVotingWeight())
}

updateValidatorTx = kvstore.MakeValSetChangeTx(val1PubKeyABCI, 2)
previousTotalVotingPower = css[0].GetRoundState().LastVoters.TotalVotingPower()
previousTotalVotingWeight = css[0].GetRoundState().LastVoters.TotalVotingWeight()

waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlockWithTx(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)

if css[0].GetRoundState().LastVoters.TotalVotingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalVotingPower())
"expected voting weight to change (before: %d, after: %d)",
previousTotalVotingWeight,
css[0].GetRoundState().LastVoters.TotalVotingWeight())
}

updateValidatorTx = kvstore.MakeValSetChangeTx(val1PubKeyABCI, 26)
previousTotalVotingPower = css[0].GetRoundState().LastVoters.TotalVotingPower()
previousTotalVotingWeight = css[0].GetRoundState().LastVoters.TotalVotingWeight()

waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlockWithTx(t, nVals, activeVals, blocksSubs, css, updateValidatorTx)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)
waitForAndValidateBlock(t, nVals, activeVals, blocksSubs, css)

if css[0].GetRoundState().LastVoters.TotalVotingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalVotingPower())
"expected voting weight to change (before: %d, after: %d)",
previousTotalVotingWeight,
css[0].GetRoundState().LastVoters.TotalVotingWeight())
}
}

Expand Down Expand Up @@ -464,18 +464,18 @@ func TestReactorValidatorSetChanges(t *testing.T) {
updatePubKey1ABCI, err := cryptoenc.PubKeyToProto(updateValidatorPubKey1)
require.NoError(t, err)
updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
previousTotalVotingPower := css[nVals].GetRoundState().LastVoters.TotalVotingPower()
previousTotalVotingWeight := css[nVals].GetRoundState().LastVoters.TotalVotingWeight()

waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, css, updateValidatorTx1)
waitForAndValidateBlockWithTx(t, nPeers, activeVals, blocksSubs, css, updateValidatorTx1)
waitForAndValidateBlock(t, nPeers, activeVals, blocksSubs, css)
waitForBlockWithUpdatedValsAndValidateIt(t, nPeers, activeVals, blocksSubs, css)

if css[nVals].GetRoundState().LastVoters.TotalVotingPower() == previousTotalVotingPower {
if css[nVals].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Errorf(
"expected staking power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[nVals].GetRoundState().LastVoters.TotalVotingPower())
"expected voting weight to change (before: %d, after: %d)",
previousTotalVotingWeight,
css[nVals].GetRoundState().LastVoters.TotalVotingWeight())
}

//---------------------------------------------------------------------------
Expand Down
16 changes: 8 additions & 8 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1786,9 +1786,9 @@ func (cs *State) pruneBlocks(retainHeight int64) (uint64, error) {

func (cs *State) recordMetrics(height int64, block *types.Block) {
cs.metrics.Validators.Set(float64(cs.Validators.Size()))
cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalStakingPower()))
cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalVotingPower()))
cs.metrics.Voters.Set(float64(cs.Voters.Size()))
cs.metrics.VotersPower.Set(float64(cs.Voters.TotalVotingPower()))
cs.metrics.VotersPower.Set(float64(cs.Voters.TotalVotingWeight()))

var (
missingVoters int
Expand Down Expand Up @@ -1834,14 +1834,14 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
commitSig := block.LastCommit.Signatures[i]
if commitSig.Absent() {
missingVoters++
missingVotersPower += val.VotingPower
missingVotersPower += val.VotingWeight
}

if bytes.Equal(val.Address, address) {
label := []string{
"validator_address", val.Address.String(),
}
cs.metrics.VoterPower.With(label...).Set(float64(val.VotingPower))
cs.metrics.VoterPower.With(label...).Set(float64(val.VotingWeight))
selectedAsVoter = true
if commitSig.ForBlock() {
cs.metrics.VoterLastSignedHeight.With(label...).Set(float64(height))
Expand Down Expand Up @@ -1877,7 +1877,7 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
if dve, ok := ev.(*types.DuplicateVoteEvidence); ok {
if _, val := cs.Voters.GetByAddress(dve.VoteA.ValidatorAddress); val != nil {
byzantineVotersCount++
byzantineVotersPower += val.VotingPower
byzantineVotersPower += val.VotingWeight
}
}
}
Expand Down Expand Up @@ -2424,11 +2424,11 @@ func (cs *State) calculatePrevoteMessageDelayMetrics() {
return pl[i].Timestamp.Before(pl[j].Timestamp)
})

var votingPowerSeen int64
var totalVotingWeight int64
for _, v := range pl {
_, voter := cs.Voters.GetByAddress(v.ValidatorAddress)
votingPowerSeen += voter.VotingPower
if votingPowerSeen >= cs.Voters.TotalVotingPower()*2/3+1 {
totalVotingWeight += voter.VotingWeight
if totalVotingWeight >= cs.Voters.TotalVotingWeight()*2/3+1 {
cs.metrics.QuorumPrevoteMessageDelay.Set(v.Timestamp.Sub(cs.Proposal.Timestamp).Seconds())
break
}
Expand Down
24 changes: 12 additions & 12 deletions consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2110,13 +2110,13 @@ func makeVssMap(vss []*validatorStub) map[string]*validatorStub {
}

func votersPrivVals(voterSet *types.VoterSet, vssMap map[string]*validatorStub) []*validatorStub {
totalVotingPower := voterSet.TotalVotingPower()
votingPower := int64(0)
totalVotingWeight := voterSet.TotalVotingWeight()
votingWeight := int64(0)
voters := 0
for i, v := range voterSet.Voters {
vssMap[v.PubKey.Address().String()].Index = int32(i) // NOTE: re-indexing for new voters
if votingPower < totalVotingPower*2/3+1 {
votingPower += v.VotingPower
if votingWeight < totalVotingWeight*2/3+1 {
votingWeight += v.VotingWeight
voters++
}
}
Expand Down Expand Up @@ -2233,7 +2233,7 @@ func ensureVotingPowerOfVoteSet(t *testing.T, voteSet *types.VoteSet, votingPowe
func TestStateBadVoterWithSelectedVoter(t *testing.T) {
// if validators are 9, then selected voters are 4+
// if one of 4+ voters does not vote, the consensus state does not progress to next step
// making him having 1/3 + 1 voting power of total
// making him having 1/3 + 1 voting weight of total
cs, vss := randStateWithVoterParams(9, &types.VoterParams{
VoterElectionThreshold: 5,
MaxTolerableByzantinePercentage: 20})
Expand All @@ -2255,18 +2255,18 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
}
}

// make the invalid voter having voting power of 1/3+1 of total
cs.Voters.Voters[nonMyIndex].VotingPower =
(cs.Voters.TotalVotingPower()-cs.Voters.Voters[nonMyIndex].VotingPower)/2 + 1
// make the invalid voter having voting weight of 1/3+1 of total
cs.Voters.Voters[nonMyIndex].VotingWeight =
(cs.Voters.TotalVotingWeight()-cs.Voters.Voters[nonMyIndex].VotingWeight)/2 + 1

voters := cs.Voters.Copy()

// make a voter having invalid pub key
voters.Voters[nonMyIndex] = &types.Validator{
PubKey: notVoter.PubKey,
Address: notVoter.Address,
StakingPower: cs.Voters.Voters[nonMyIndex].StakingPower,
VotingPower: cs.Voters.Voters[nonMyIndex].VotingPower,
VotingWeight: cs.Voters.Voters[nonMyIndex].VotingWeight,
}

vss[0].Height = 1 // this is needed because of `incrementHeight(vss[1:]...)` of randStateWithVoterParams()
Expand Down Expand Up @@ -2298,17 +2298,17 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
signAddVotes(cs, tmproto.PrevoteType, propBlock.Hash(), propBlock.MakePartSet(types.BlockPartSizeBytes).Header(),
voterPrivVals...)

sumVotingPower := int64(0)
sumVotingWeight := int64(0)
for i := range voterPrivVals { // one failed
if i == nonMyIndex {
continue
}
ensurePrevote(voteCh, height, round) // wait for prevote
sumVotingPower += voters.Voters[i].VotingPower
sumVotingWeight += voters.Voters[i].VotingWeight
}

// ensure we didn't get a vote for Voters[nonMyIndex]
ensureVotingPowerOfVoteSet(t, cs.Votes.Prevotes(round), sumVotingPower)
ensureVotingPowerOfVoteSet(t, cs.Votes.Prevotes(round), sumVotingWeight)
assert.False(t, cs.Votes.Prevotes(round).HasTwoThirdsMajority())

// add remain vote
Expand Down
2 changes: 1 addition & 1 deletion evidence/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func initializeStateFromValidatorSet(valSet *types.ValidatorSet, height int64) s
func initializeValidatorState(privVal types.PrivValidator, height int64) sm.Store {

pubKey, _ := privVal.GetPubKey()
validator := &types.Validator{Address: pubKey.Address(), StakingPower: 10, PubKey: pubKey}
validator := &types.Validator{Address: pubKey.Address(), VotingPower: 10, PubKey: pubKey}

// create validator set and state
valSet := &types.ValidatorSet{
Expand Down
4 changes: 2 additions & 2 deletions evidence/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ func exampleVote(t byte) *types.Vote {
func TestEvidenceVectors(t *testing.T) {

val := &types.Validator{
Address: crypto.AddressHash([]byte("validator_address")),
StakingPower: 10,
Address: crypto.AddressHash([]byte("validator_address")),
VotingPower: 10,
}

voterSet := types.WrapValidatorsToVoterSet([]*types.Validator{val})
Expand Down
Loading