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

Feature: last signing power. #4584

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 13 additions & 27 deletions consensus/quorum/one-node-staked-vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ func (v *stakedVoteWeight) AddNewVote(
additionalVotePower = additionalVotePower.Add(votingPower)
}

tallyQuorum := func() *tallyAndQuorum {
switch p {
case Prepare:
return v.voteTally.Prepare
case Commit:
return v.voteTally.Commit
case ViewChange:
return v.voteTally.ViewChange
default:
// Should not happen
return nil
}
}()
var tallyQuorum *tallyAndQuorum
switch p {
case Prepare:
tallyQuorum = v.voteTally.Prepare
case Commit:
tallyQuorum = v.voteTally.Commit
case ViewChange:
tallyQuorum = v.voteTally.ViewChange
default:
// Should not happen
return nil, errors.New("stakedVoteWeight not cache this phase")
}

tallyQuorum.tally = tallyQuorum.tally.Add(additionalVotePower)

t := v.QuorumThreshold()
Expand Down Expand Up @@ -163,20 +163,6 @@ func (v *stakedVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool {
return (*currentTotalPower).GT(threshold)
}

func (v *stakedVoteWeight) currentTotalPower(p Phase) (*numeric.Dec, error) {
switch p {
case Prepare:
return &v.voteTally.Prepare.tally, nil
case Commit:
return &v.voteTally.Commit.tally, nil
case ViewChange:
return &v.voteTally.ViewChange.tally, nil
default:
// Should not happen
return nil, errors.New("wrong phase is provided")
}
}

// ComputeTotalPowerByMask computes the total power indicated by bitmap mask
func (v *stakedVoteWeight) computeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec {
currentTotal := numeric.ZeroDec()
Expand Down
1 change: 1 addition & 0 deletions hmy/hmy.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type NodeAPI interface {
GetConfig() commonRPC.Config
ShutDown()
GetLastSigningPower() (float64, error)
GetLastSigningPower2() (float64, error)
}

// New creates a new Harmony object (including the
Expand Down
28 changes: 28 additions & 0 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package node

import (
"github.com/harmony-one/harmony/consensus/quorum"
"github.com/harmony-one/harmony/consensus/votepower"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/hmy"
"github.com/harmony-one/harmony/internal/tikv"
Expand Down Expand Up @@ -183,3 +185,29 @@ func (node *Node) GetLastSigningPower() (float64, error) {
round := float64(power.MulInt64(10000).RoundInt64()) / 10000
return round, nil
}

func (node *Node) GetLastSigningPower2() (float64, error) {
bc := node.Consensus.Blockchain()
cur := bc.CurrentBlock()
ss, err := bc.ReadShardState(cur.Epoch())
if err != nil {
return 0, err
}
roster, err := votepower.Compute(&ss.Shards[bc.ShardID()], cur.Epoch())
if err != nil {
return 0, err
}
blsPubKeys, err := ss.Shards[bc.ShardID()].BLSPublicKeys()
if err != nil {
return 0, err
}

mask := bls.NewMask(blsPubKeys)
err = mask.SetMask(cur.Header().LastCommitBitmap())
if err != nil {
return 0, err
}
power := roster.VotePowerByMask(mask)
round := float64(power.MulInt64(10000).RoundInt64()) / 10000
return round, nil
}
7 changes: 7 additions & 0 deletions rpc/private_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ func (s *PrivateDebugService) GetLastSigningPower(
) (float64, error) {
return s.hmy.NodeAPI.GetLastSigningPower()
}

// GetLastSigningPower2 get last signed power
func (s *PrivateDebugService) GetLastSigningPower2(
ctx context.Context,
) (float64, error) {
return s.hmy.NodeAPI.GetLastSigningPower2()
}