Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frozen committed Nov 25, 2024
1 parent 038e2f3 commit ba7d2a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 54 deletions.
16 changes: 1 addition & 15 deletions consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 1 addition & 12 deletions consensus/view_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
52 changes: 25 additions & 27 deletions internal/chain/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -206,30 +190,44 @@ 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
}
}
}
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(),
)
}

Expand Down

0 comments on commit ba7d2a5

Please sign in to comment.