Skip to content

Commit

Permalink
Swap StakingPower and VotingPower
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Jul 27, 2022
1 parent 81b4fb7 commit 42f6c1f
Show file tree
Hide file tree
Showing 48 changed files with 872 additions and 868 deletions.
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
)

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
}
28 changes: 14 additions & 14 deletions consensus/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,48 +353,48 @@ func TestReactorStakingPowerChange(t *testing.T) {
val1PubKeyABCI, err := cryptoenc.PubKeyToProto(val1PubKey)
require.NoError(t, err)
updateValidatorTx := kvstore.MakeValSetChangeTx(val1PubKeyABCI, 25)
previousTotalVotingPower := css[0].GetRoundState().LastVoters.TotalVotingPower()
previousTotalVotingPower := css[0].GetRoundState().LastVoters.TotalStakingPower()

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

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

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

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

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

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()
previousTotalVotingPower := css[nVals].GetRoundState().LastVoters.TotalStakingPower()

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

//---------------------------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 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.TotalStakingPower()))

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.StakingPower
}

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.StakingPower))
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.StakingPower
}
}
}
Expand Down Expand Up @@ -2427,8 +2427,8 @@ func (cs *State) calculatePrevoteMessageDelayMetrics() {
var votingPowerSeen int64
for _, v := range pl {
_, voter := cs.Voters.GetByAddress(v.ValidatorAddress)
votingPowerSeen += voter.VotingPower
if votingPowerSeen >= cs.Voters.TotalVotingPower()*2/3+1 {
votingPowerSeen += voter.StakingPower
if votingPowerSeen >= cs.Voters.TotalStakingPower()*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)
totalStakingPower := voterSet.TotalStakingPower()
stakingPower := 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 stakingPower < totalStakingPower*2/3+1 {
stakingPower += v.StakingPower
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 staking power 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 staking power of 1/3+1 of total
cs.Voters.Voters[nonMyIndex].StakingPower =
(cs.Voters.TotalStakingPower()-cs.Voters.Voters[nonMyIndex].StakingPower)/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,
StakingPower: cs.Voters.Voters[nonMyIndex].StakingPower,
}

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)
sumStakingPower := int64(0)
for i := range voterPrivVals { // one failed
if i == nonMyIndex {
continue
}
ensurePrevote(voteCh, height, round) // wait for prevote
sumVotingPower += voters.Voters[i].VotingPower
sumStakingPower += voters.Voters[i].StakingPower
}

// ensure we didn't get a vote for Voters[nonMyIndex]
ensureVotingPowerOfVoteSet(t, cs.Votes.Prevotes(round), sumVotingPower)
ensureVotingPowerOfVoteSet(t, cs.Votes.Prevotes(round), sumStakingPower)
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

0 comments on commit 42f6c1f

Please sign in to comment.