diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index a0202e7b2..111f8183c 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -530,7 +530,6 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node { // Assign closure functions to the consensus object currentConsensus.BlockVerifier = currentNode.VerifyNewBlock currentConsensus.OnConsensusDone = currentNode.PostConsensusProcessing - currentNode.State = node.NodeWaitToJoin // update consensus information based on the blockchain currentConsensus.SetMode(currentConsensus.UpdateConsensusInformation()) // Setup block period and block due time. diff --git a/node/node.go b/node/node.go index 85cd9d677..bed430d3e 100644 --- a/node/node.go +++ b/node/node.go @@ -11,6 +11,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/harmony-one/abool" "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/api/client" msg_pb "github.com/harmony-one/harmony/api/proto/message" @@ -40,20 +41,6 @@ import ( "golang.org/x/sync/semaphore" ) -// State is a state of a node. -type State byte - -// All constants except the NodeLeader below are for validators only. -const ( - NodeInit State = iota // Node just started, before contacting BeaconChain - NodeWaitToJoin // Node contacted BeaconChain, wait to join Shard - NodeNotInSync // Node out of sync, might be just joined Shard or offline for a period of time - NodeOffline // Node is offline - NodeReadyForConsensus // Node is ready for doing consensus - NodeDoingConsensus // Node is already doing consensus - NodeLeader // Node is the leader of some shard. -) - const ( // NumTryBroadCast is the number of times trying to broadcast NumTryBroadCast = 3 @@ -71,26 +58,6 @@ const ( GlobalRxWorkers = 32 ) -func (state State) String() string { - switch state { - case NodeInit: - return "NodeInit" - case NodeWaitToJoin: - return "NodeWaitToJoin" - case NodeNotInSync: - return "NodeNotInSync" - case NodeOffline: - return "NodeOffline" - case NodeReadyForConsensus: - return "NodeReadyForConsensus" - case NodeDoingConsensus: - return "NodeDoingConsensus" - case NodeLeader: - return "NodeLeader" - } - return "Unknown" -} - const ( maxBroadcastNodes = 10 // broadcast at most maxBroadcastNodes peers that need in sync broadcastTimeout int64 = 60 * 1000000000 // 1 mins @@ -118,7 +85,6 @@ type Node struct { SelfPeer p2p.Peer // TODO: Neighbors should store only neighbor nodes in the same shard Neighbors sync.Map // All the neighbor nodes, key is the sha256 of Peer IP/Port, value is the p2p.Peer - State State // State of the Node stateMutex sync.Mutex // mutex for change node state // BeaconNeighbors store only neighbor nodes in the beacon chain shard BeaconNeighbors sync.Map // All the neighbor nodes, key is the sha256 of Peer IP/Port, value is the p2p.Peer @@ -156,6 +122,8 @@ type Node struct { TransactionErrorSink *types.TransactionErrorSink // BroadcastInvalidTx flag is considered when adding pending tx to tx-pool BroadcastInvalidTx bool + // InSync flag indicates the node is in-sync or not + IsInSync *abool.AtomicBool } // Blockchain returns the blockchain for the node's current shard. @@ -465,6 +433,7 @@ func New( collection.DisableCache() } node.shardChains = collection + node.IsInSync = abool.NewBool(false) if host != nil && consensusObj != nil { // Consensus and associated channel to communicate blocks diff --git a/node/node_syncing.go b/node/node_syncing.go index 6618d3f1a..50d065a51 100644 --- a/node/node_syncing.go +++ b/node/node_syncing.go @@ -243,23 +243,17 @@ func (node *Node) doSync(bc *core.BlockChain, worker *worker.Worker, willJoinCon } // TODO: treat fake maximum height if node.stateSync.IsOutOfSync(bc) { - node.stateMutex.Lock() - node.State = NodeNotInSync - node.stateMutex.Unlock() + node.IsInSync.UnSet() if willJoinConsensus { node.Consensus.BlocksNotSynchronized() } node.stateSync.SyncLoop(bc, worker, false, node.Consensus) if willJoinConsensus { - node.stateMutex.Lock() - node.State = NodeReadyForConsensus - node.stateMutex.Unlock() + node.IsInSync.Set() node.Consensus.BlocksSynchronized() } } - node.stateMutex.Lock() - node.State = NodeReadyForConsensus - node.stateMutex.Unlock() + node.IsInSync.Set() } // SupportBeaconSyncing sync with beacon chain for archival node in beacon chan or non-beacon node @@ -405,10 +399,7 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in // this is the out of sync node acts as grpc server side case downloader_pb.DownloaderRequest_NEWBLOCK: - if node.State != NodeNotInSync { - utils.Logger().Debug(). - Str("state", node.State.String()). - Msg("[SYNC] new block received, but state is") + if node.IsInSync.IsSet() { response.Type = downloader_pb.DownloaderResponse_INSYNC return response, nil } @@ -461,7 +452,7 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in } case downloader_pb.DownloaderRequest_REGISTERTIMEOUT: - if node.State == NodeNotInSync { + if !node.IsInSync.IsSet() { count := node.stateSync.RegisterNodeInfo() utils.Logger().Debug(). Int("number", count).