Skip to content

Commit

Permalink
Modify from StakingPower to VotingWeight
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Jul 28, 2022
1 parent 42f6c1f commit d57e0f5
Show file tree
Hide file tree
Showing 32 changed files with 625 additions and 621 deletions.
384 changes: 192 additions & 192 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

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.TotalStakingPower()
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.TotalStakingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalStakingPower())
"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.TotalStakingPower()
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.TotalStakingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalStakingPower())
"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.TotalStakingPower()
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.TotalStakingPower() == previousTotalVotingPower {
if css[0].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Fatalf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[0].GetRoundState().LastVoters.TotalStakingPower())
"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.TotalStakingPower()
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.TotalStakingPower() == previousTotalVotingPower {
if css[nVals].GetRoundState().LastVoters.TotalVotingWeight() == previousTotalVotingWeight {
t.Errorf(
"expected voting power to change (before: %d, after: %d)",
previousTotalVotingPower,
css[nVals].GetRoundState().LastVoters.TotalStakingPower())
"expected voting weight to change (before: %d, after: %d)",
previousTotalVotingWeight,
css[nVals].GetRoundState().LastVoters.TotalVotingWeight())
}

//---------------------------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1788,7 +1788,7 @@ func (cs *State) recordMetrics(height int64, block *types.Block) {
cs.metrics.Validators.Set(float64(cs.Validators.Size()))
cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalVotingPower()))
cs.metrics.Voters.Set(float64(cs.Voters.Size()))
cs.metrics.VotersPower.Set(float64(cs.Voters.TotalStakingPower()))
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.StakingPower
missingVotersPower += val.VotingWeight
}

if bytes.Equal(val.Address, address) {
label := []string{
"validator_address", val.Address.String(),
}
cs.metrics.VoterPower.With(label...).Set(float64(val.StakingPower))
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.StakingPower
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 totalVotinWeight int64
for _, v := range pl {
_, voter := cs.Voters.GetByAddress(v.ValidatorAddress)
votingPowerSeen += voter.StakingPower
if votingPowerSeen >= cs.Voters.TotalStakingPower()*2/3+1 {
totalVotinWeight += voter.VotingWeight
if totalVotinWeight >= 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 {
totalStakingPower := voterSet.TotalStakingPower()
stakingPower := 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 stakingPower < totalStakingPower*2/3+1 {
stakingPower += v.StakingPower
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 staking 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,9 +2255,9 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
}
}

// make the invalid voter having staking power of 1/3+1 of total
cs.Voters.Voters[nonMyIndex].StakingPower =
(cs.Voters.TotalStakingPower()-cs.Voters.Voters[nonMyIndex].StakingPower)/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()

Expand All @@ -2266,7 +2266,7 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
PubKey: notVoter.PubKey,
Address: notVoter.Address,
VotingPower: cs.Voters.Voters[nonMyIndex].VotingPower,
StakingPower: cs.Voters.Voters[nonMyIndex].StakingPower,
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...)

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

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

// add remain vote
Expand Down
14 changes: 7 additions & 7 deletions evidence/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func VerifyLightClientAttack(
}

// Assert the correct amount of voting power of the validator set
if evTotal, votersTotal := e.TotalVotingPower, commonVoters.TotalStakingPower(); evTotal != votersTotal {
if evTotal, votersTotal := e.TotalVotingPower, commonVoters.TotalVotingWeight(); evTotal != votersTotal {
return fmt.Errorf("total voting power from the evidence and our voter set does not match (%d != %d)",
evTotal, votersTotal)
}
Expand Down Expand Up @@ -211,9 +211,9 @@ func VerifyDuplicateVote(e *types.DuplicateVoteEvidence, chainID string, voterSe
return fmt.Errorf("validator power from evidence and our voter set does not match (%d != %d)",
e.ValidatorPower, val.VotingPower)
}
if voterSet.TotalStakingPower() != e.TotalVotingPower {
if voterSet.TotalVotingWeight() != e.TotalVotingPower {
return fmt.Errorf("total voting power from the evidence and our voter set does not match (%d != %d)",
e.TotalVotingPower, voterSet.TotalStakingPower())
e.TotalVotingPower, voterSet.TotalVotingWeight())
}

va := e.VoteA.ToProto()
Expand Down Expand Up @@ -265,15 +265,15 @@ func validateABCIEvidence(

if ev.ByzantineValidators[idx].VotingPower != val.VotingPower {
return fmt.Errorf(
"evidence contained unexpected byzantine validator staking power; expected %d, got %d",
"evidence contained unexpected byzantine validator voting power; expected %d, got %d",
val.VotingPower, ev.ByzantineValidators[idx].VotingPower,
)
}

if ev.ByzantineValidators[idx].StakingPower != val.StakingPower {
if ev.ByzantineValidators[idx].VotingWeight != val.VotingWeight {
return fmt.Errorf(
"evidence contained unexpected byzantine validator voting power; expected %d, got %d",
val.StakingPower, ev.ByzantineValidators[idx].StakingPower,
"evidence contained unexpected byzantine validator voting weight; expected %d, got %d",
val.VotingWeight, ev.ByzantineValidators[idx].VotingWeight,
)
}
}
Expand Down
14 changes: 7 additions & 7 deletions evidence/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestVerifyLightClientAttack_validateABCIEvidence(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "evidence contained an unexpected byzantine validator address;")

// illegal byzantine validator staking power
// illegal byzantine validator voting power
phantomVoterSet = types.ToVoterAll(ev.ConflictingBlock.VoterSet.Voters)
phantomVoterSet.Voters[0].VotingPower = votingPower + 1
ev.ByzantineValidators = phantomVoterSet.Voters
Expand All @@ -139,18 +139,18 @@ func TestVerifyLightClientAttack_validateABCIEvidence(t *testing.T) {
ev.ConflictingBlock.VoterSet, // Should use correct VoterSet for bls.VerifyAggregatedSignature
defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour, types.DefaultVoterParams())
require.Error(t, err)
require.Contains(t, err.Error(), "evidence contained unexpected byzantine validator staking power;")
require.Contains(t, err.Error(), "evidence contained unexpected byzantine validator voting power;")

// illegal byzantine validator voting power
// illegal byzantine validator voting weight
phantomVoterSet = types.ToVoterAll(ev.ConflictingBlock.VoterSet.Voters)
phantomVoterSet.Voters[0].StakingPower = votingPower + 1
phantomVoterSet.Voters[0].VotingWeight = votingPower + 1
ev.ByzantineValidators = phantomVoterSet.Voters
err = evidence.VerifyLightClientAttack(ev, common.SignedHeader, trusted.SignedHeader,
common.ValidatorSet,
ev.ConflictingBlock.VoterSet, // Should use correct VoterSet for bls.VerifyAggregatedSignature
defaultEvidenceTime.Add(2*time.Hour), 3*time.Hour, types.DefaultVoterParams())
require.Error(t, err)
require.Contains(t, err.Error(), "evidence contained unexpected byzantine validator voting power;")
require.Contains(t, err.Error(), "evidence contained unexpected byzantine validator voting weight;")
}

func TestVerify_LunaticAttackAgainstState(t *testing.T) {
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestVerify_LunaticAttackAgainstState(t *testing.T) {
pool, err = evidence.NewPool(dbm.NewMemDB(), stateStore, blockStore)
require.NoError(t, err)
assert.Error(t, pool.AddEvidence(ev))
ev.TotalVotingPower = common.VoterSet.TotalStakingPower()
ev.TotalVotingPower = common.VoterSet.TotalVotingWeight()
}

func TestVerify_ForwardLunaticAttack(t *testing.T) {
Expand Down Expand Up @@ -643,7 +643,7 @@ func makeLunaticEvidence(
VoterSet: conflictingVoters,
},
CommonHeight: commonHeight,
TotalVotingPower: commonVoterSet.TotalStakingPower(),
TotalVotingPower: commonVoterSet.TotalVotingWeight(),
ByzantineValidators: byzantineValidators,
Timestamp: commonTime,
}
Expand Down
4 changes: 2 additions & 2 deletions light/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,11 @@ func newLightClientAttackEvidence(conflicted, trusted, common *types.LightBlock)
if ev.ConflictingHeaderIsInvalid(trusted.Header) {
ev.CommonHeight = common.Height
ev.Timestamp = common.Time
ev.TotalVotingPower = common.VoterSet.TotalStakingPower()
ev.TotalVotingPower = common.VoterSet.TotalVotingWeight()
} else {
ev.CommonHeight = trusted.Height
ev.Timestamp = trusted.Time
ev.TotalVotingPower = trusted.VoterSet.TotalStakingPower()
ev.TotalVotingPower = trusted.VoterSet.TotalVotingWeight()
}
ev.ByzantineValidators = ev.GetByzantineValidators(common.VoterSet, trusted.SignedHeader)
return ev
Expand Down
2 changes: 1 addition & 1 deletion light/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (e ErrOldHeaderExpired) Error() string {
// ErrNewValSetCantBeTrusted means the new validator set cannot be trusted
// because < 1/3rd (+trustLevel+) of the old validator set has signed.
type ErrNewValSetCantBeTrusted struct {
Reason types.ErrNotEnoughStakingPowerSigned
Reason types.ErrNotEnoughVotingWeightSigned
}

func (e ErrNewValSetCantBeTrusted) Error() string {
Expand Down
4 changes: 2 additions & 2 deletions light/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (pkz privKeys) signHeaderByRate(header *types.Header, voterSet *types.Voter
}

// Fill in the votes we want.
until := int64(float64(voterSet.TotalStakingPower()) * rate)
until := int64(float64(voterSet.TotalVotingWeight()) * rate)
sum := int64(0)
for i := 0; i < len(pkz); i++ {
_, voter := voterSet.GetByAddress(pkz[i].PubKey().Address())
Expand All @@ -131,7 +131,7 @@ func (pkz privKeys) signHeaderByRate(header *types.Header, voterSet *types.Voter
vote := makeVote(header, voterSet, pkz[i], blockID)
commitSigs[vote.ValidatorIndex] = vote.CommitSig()

sum += voter.StakingPower
sum += voter.VotingWeight
if sum > until {
break
}
Expand Down
2 changes: 1 addition & 1 deletion light/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func VerifyNonAdjacent(
err := trustedVoters.VerifyCommitLightTrusting(trustedHeader.ChainID, untrustedHeader.Commit, trustLevel)
if err != nil {
switch e := err.(type) {
case types.ErrNotEnoughStakingPowerSigned:
case types.ErrNotEnoughVotingWeightSigned:
return ErrNewValSetCantBeTrusted{e}
default:
return e
Expand Down
Loading

0 comments on commit d57e0f5

Please sign in to comment.