diff --git a/consensus/quorum/one-node-staked-vote.go b/consensus/quorum/one-node-staked-vote.go index e3a45540a..2532f9691 100644 --- a/consensus/quorum/one-node-staked-vote.go +++ b/consensus/quorum/one-node-staked-vote.go @@ -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() @@ -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() diff --git a/hmy/hmy.go b/hmy/hmy.go index 24f0caa12..097e597d0 100644 --- a/hmy/hmy.go +++ b/hmy/hmy.go @@ -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 diff --git a/node/api.go b/node/api.go index ceda96808..ef76079f1 100644 --- a/node/api.go +++ b/node/api.go @@ -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" @@ -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 +} diff --git a/rpc/private_debug.go b/rpc/private_debug.go index 921d6645d..97ade82dd 100644 --- a/rpc/private_debug.go +++ b/rpc/private_debug.go @@ -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() +}