From ba7d2a557f08531a3182bad838b073534f4a5553 Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:53:52 -0400 Subject: [PATCH] Cleanup. --- consensus/consensus_service.go | 16 +---------- consensus/view_change.go | 13 +-------- internal/chain/engine.go | 52 ++++++++++++++++------------------ 3 files changed, 27 insertions(+), 54 deletions(-) diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index b77d942759..af3b38af51 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -465,21 +465,7 @@ func (consensus *Consensus) updateConsensusInformation() Mode { // a solution to take care of this case because the coinbase of the latest block doesn't really represent the // the real current leader in case of M1 view change. if !curHeader.IsLastBlockInEpoch() && curHeader.Number().Uint64() != 0 { - ss, err := consensus.Blockchain().ReadShardState(curHeader.Epoch()) - if err != nil { - utils.Logger().Err(err).Msg("[UpdateConsensusInformation] failed to read shard state") - return Syncing - } - committee, err := ss.FindCommitteeByID(curHeader.ShardID()) - if err != nil { - utils.Logger().Err(err).Msg("[UpdateConsensusInformation] failed to find committee by ID") - return Syncing - } - leaderPubKey, err := chain.GetLeaderPubKeyFromCoinbase( - committee.Slots, - curHeader.Coinbase(), - consensus.Blockchain().Config().IsStaking(curHeader.Epoch()), - ) + leaderPubKey, err := chain.GetLeaderPubKeyFromCoinbase(consensus.Blockchain(), curHeader) if err != nil || leaderPubKey == nil { consensus.getLogger().Error().Err(err). Msg("[UpdateConsensusInformation] Unable to get leaderPubKey from coinbase") diff --git a/consensus/view_change.go b/consensus/view_change.go index 0aec672407..3f9f0516a5 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -7,12 +7,10 @@ import ( "github.com/ethereum/go-ethereum/common" msg_pb "github.com/harmony-one/harmony/api/proto/message" - "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/consensus/quorum" "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/chain" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" - "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/shard" @@ -147,15 +145,6 @@ func (consensus *Consensus) getNextViewID() (uint64, time.Duration) { return nextViewID, viewChangeDuration } -type nextLeaderParams struct { - config *params.ChainConfig - curHeader *block.Header -} - -func (a nextLeaderParams) Config() *params.ChainConfig { - return a.config -} - // getNextLeaderKeySkipSameAddress uniquely determine who is the leader for given viewID // It receives the committee and returns // the next leader based on the gap of the viewID of the view change and the last @@ -200,7 +189,7 @@ func (consensus *Consensus) getNextLeaderKey(viewID uint64, committee *shard.Com stuckBlockViewID := curHeader.ViewID().Uint64() + 1 gap = int(viewID - stuckBlockViewID) // this is the truth of the leader based on blockchain blocks - lastLeaderPubKey, err = chain.GetLeaderPubKeyFromCoinbase(committee.Slots, curHeader.Coinbase(), blockchain.Config().IsStaking(curHeader.Epoch())) + lastLeaderPubKey, err = chain.GetLeaderPubKeyFromCoinbase(blockchain, curHeader) if err != nil || lastLeaderPubKey == nil { consensus.getLogger().Error().Err(err). Msg("[getNextLeaderKey] Unable to get leaderPubKey from coinbase. Set it to consensus.LeaderPubKey") diff --git a/internal/chain/engine.go b/internal/chain/engine.go index a9c3684f43..07a458ad7a 100644 --- a/internal/chain/engine.go +++ b/internal/chain/engine.go @@ -6,6 +6,7 @@ import ( "sort" "time" + bls2 "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/common/denominations" "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/numeric" @@ -156,24 +157,7 @@ func (e *engineImpl) VerifyVRF( return nil } - ss, err := bc.ReadShardState(header.Epoch()) - if err != nil { - return errors.WithMessagef( - err, "[VerifyVRF] failed to read shard state %v", header.Epoch(), - ) - } - committee, err := ss.FindCommitteeByID(header.ShardID()) - if err != nil { - return errors.WithMessagef( - err, "[VerifyVRF] failed to find committee %d", header.ShardID(), - ) - } - - leaderPubKey, err := GetLeaderPubKeyFromCoinbase( - committee.Slots, - header.Coinbase(), - bc.Config().IsStaking(header.Epoch()), - ) + leaderPubKey, err := GetLeaderPubKeyFromCoinbase(bc, header) if leaderPubKey == nil || err != nil { return err @@ -206,22 +190,35 @@ func (e *engineImpl) VerifyVRF( // GetLeaderPubKeyFromCoinbase retrieve corresponding blsPublicKey from Coinbase Address func GetLeaderPubKeyFromCoinbase( - slots shard.SlotList, coinbase common.Address, isStaking bool, + blockchain engine.ChainReader, h *block.Header, ) (*bls.PublicKeyWrapper, error) { - for _, member := range slots { + shardState, err := blockchain.ReadShardState(h.Epoch()) + if err != nil { + return nil, errors.Wrapf(err, "cannot read shard state %v %s", + h.Epoch(), + h.Coinbase().Hash().Hex(), + ) + } + + committee, err := shardState.FindCommitteeByID(h.ShardID()) + if err != nil { + return nil, err + } + + committerKey := new(bls2.PublicKey) + isStaking := blockchain.Config().IsStaking(h.Epoch()) + for _, member := range committee.Slots { if isStaking { // After staking the coinbase address will be the address of bls public key - if utils.GetAddressFromBLSPubKeyBytes(member.BLSPublicKey[:]) == coinbase { - committerKey, err := bls.BytesToBLSPublicKey(member.BLSPublicKey[:]) - if err != nil { + if utils.GetAddressFromBLSPubKeyBytes(member.BLSPublicKey[:]) == h.Coinbase() { + if committerKey, err = bls.BytesToBLSPublicKey(member.BLSPublicKey[:]); err != nil { return nil, err } return &bls.PublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil } } else { - if member.EcdsaAddress == coinbase { - committerKey, err := bls.BytesToBLSPublicKey(member.BLSPublicKey[:]) - if err != nil { + if member.EcdsaAddress == h.Coinbase() { + if committerKey, err = bls.BytesToBLSPublicKey(member.BLSPublicKey[:]); err != nil { return nil, err } return &bls.PublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil @@ -229,7 +226,8 @@ func GetLeaderPubKeyFromCoinbase( } } return nil, errors.Errorf( - "cannot find corresponding BLS Public Key coinbase %s", coinbase.Hex(), + "cannot find corresponding BLS Public Key coinbase %s", + h.Coinbase().Hex(), ) }