From b2fe9fe3481cc1ffd2e2a996837b6bd1b7f14958 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Thu, 18 Mar 2021 18:29:30 -0700 Subject: [PATCH] [stream] node.IsRunningBeaconChain is refactored to a new method --- cmd/harmony/main.go | 4 ++-- node/double_signing.go | 3 +-- node/node.go | 15 ++++++++++----- node/node_cross_link.go | 2 +- node/node_handler.go | 8 ++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index e6656c9ce..388aa8fed 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -712,7 +712,7 @@ func setupPrometheusService(node *node.Node, hc harmonyConfig, sid uint32) { func setupSyncService(node *node.Node, host p2p.Host, hc harmonyConfig) { blockchains := []*core.BlockChain{node.Blockchain()} - if node.NodeConfig.ShardID != 0 { + if !node.IsRunningBeaconChain() { blockchains = append(blockchains, node.Beaconchain()) } @@ -729,7 +729,7 @@ func setupSyncService(node *node.Node, host p2p.Host, hc harmonyConfig) { } // If we are running side chain, we will need to do some extra works for beacon // sync - if node.NodeConfig.ShardID != 0 { + if !node.IsRunningBeaconChain() { dConfig.BHConfig = &downloader.BeaconHelperConfig{ BlockC: node.BeaconBlockChannel, InsertHook: node.BeaconSyncHook, diff --git a/node/double_signing.go b/node/double_signing.go index a8c40eda8..dd9787941 100644 --- a/node/double_signing.go +++ b/node/double_signing.go @@ -3,13 +3,12 @@ package node import ( "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/internal/utils" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/slash" ) // ProcessSlashCandidateMessage .. func (node *Node) processSlashCandidateMessage(msgPayload []byte) { - if node.NodeConfig.ShardID != shard.BeaconChainShardID { + if !node.IsRunningBeaconChain() { return } candidates := slash.Records{} diff --git a/node/node.go b/node/node.go index bb479ebf4..2de5dc463 100644 --- a/node/node.go +++ b/node/node.go @@ -217,7 +217,7 @@ func (node *Node) addPendingTransactions(newTxs types.Transactions) []error { // Add new staking transactions to the pending staking transaction list. func (node *Node) addPendingStakingTransactions(newStakingTxs staking.StakingTransactions) []error { - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { if node.Blockchain().Config().IsPreStaking(node.Blockchain().CurrentHeader().Epoch()) { poolTxs := types.PoolTransactions{} for _, tx := range newStakingTxs { @@ -245,7 +245,7 @@ func (node *Node) addPendingStakingTransactions(newStakingTxs staking.StakingTra func (node *Node) AddPendingStakingTransaction( newStakingTx *staking.StakingTransaction, ) error { - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { errs := node.addPendingStakingTransactions(staking.StakingTransactions{newStakingTx}) var err error for i := range errs { @@ -404,7 +404,7 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) ( case proto_node.SlashCandidate: nodeNodeMessageCounterVec.With(prometheus.Labels{"type": "slash"}).Inc() // only beacon chain node process slash candidate messages - if node.NodeConfig.ShardID != shard.BeaconChainShardID { + if !node.IsRunningBeaconChain() { return nil, 0, errIgnoreBeaconMsg } case proto_node.Receipt: @@ -412,7 +412,7 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) ( case proto_node.CrossLink: nodeNodeMessageCounterVec.With(prometheus.Labels{"type": "crosslink"}).Inc() // only beacon chain node process crosslink messages - if node.NodeConfig.ShardID != shard.BeaconChainShardID || + if !node.IsRunningBeaconChain() || node.NodeConfig.Role() == nodeconfig.ExplorerNode { return nil, 0, errIgnoreBeaconMsg } @@ -1016,7 +1016,7 @@ func New( go func() { webhooks.DoPost(url, &doubleSign) }() } } - if node.NodeConfig.ShardID != shard.BeaconChainShardID { + if !node.IsRunningBeaconChain() { go node.BroadcastSlash(&doubleSign) } else { records := slash.Records{doubleSign} @@ -1282,3 +1282,8 @@ func (node *Node) GetAddresses(epoch *big.Int) map[string]common.Address { // self addresses map can never be nil return node.KeysToAddrs } + +// IsRunningBeaconChain returns whether the node is running on beacon chain. +func (node *Node) IsRunningBeaconChain() bool { + return node.NodeConfig.ShardID == shard.BeaconChainShardID +} diff --git a/node/node_cross_link.go b/node/node_cross_link.go index f951807ce..0d52ccd46 100644 --- a/node/node_cross_link.go +++ b/node/node_cross_link.go @@ -61,7 +61,7 @@ func (node *Node) VerifyBlockCrossLinks(block *types.Block) error { // ProcessCrossLinkMessage verify and process Node/CrossLink message into crosslink when it's valid func (node *Node) ProcessCrossLinkMessage(msgPayload []byte) { - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { pendingCLs, err := node.Blockchain().ReadPendingCrossLinks() if err == nil && len(pendingCLs) >= maxPendingCrossLinkSize { utils.Logger().Debug(). diff --git a/node/node_handler.go b/node/node_handler.go index 8a86a8fa9..3e02a6b94 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -171,7 +171,7 @@ func (node *Node) BroadcastCrossLink() { return } - if node.NodeConfig.ShardID == shard.BeaconChainShardID || + if node.IsRunningBeaconChain() || !node.Blockchain().Config().IsCrossLink(curBlock.Epoch()) { // no need to broadcast crosslink if it's beacon chain or it's not crosslink epoch return @@ -308,7 +308,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error { // Verify cross links // TODO: move into ValidateNewBlock - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { err := node.VerifyBlockCrossLinks(newBlock) if err != nil { utils.Logger().Debug().Err(err).Msg("ops2 VerifyBlockCrossLinks Failed") @@ -336,7 +336,7 @@ func (node *Node) VerifyNewBlock(newBlock *types.Block) error { // 3. [leader] send cross shard tx receipts to destination shard func (node *Node) PostConsensusProcessing(newBlock *types.Block) error { if node.Consensus.IsLeader() { - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { node.BroadcastNewBlock(newBlock) } node.BroadcastCXReceipts(newBlock) @@ -360,7 +360,7 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) error { rnd := rand.Intn(100) if rnd < 1 { // Beacon validators also broadcast new blocks to make sure beacon sync is strong. - if node.NodeConfig.ShardID == shard.BeaconChainShardID { + if node.IsRunningBeaconChain() { node.BroadcastNewBlock(newBlock) } node.BroadcastCXReceipts(newBlock)