[node] replace node.State with node.IsInSync atomic bool

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3204/head
Leo Chen 4 years ago
parent 7acb67f05e
commit 0d3101bcd3
  1. 1
      cmd/harmony/main.go
  2. 39
      node/node.go
  3. 19
      node/node_syncing.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.

@ -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

@ -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).

Loading…
Cancel
Save