[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 // Assign closure functions to the consensus object
currentConsensus.BlockVerifier = currentNode.VerifyNewBlock currentConsensus.BlockVerifier = currentNode.VerifyNewBlock
currentConsensus.OnConsensusDone = currentNode.PostConsensusProcessing currentConsensus.OnConsensusDone = currentNode.PostConsensusProcessing
currentNode.State = node.NodeWaitToJoin
// update consensus information based on the blockchain // update consensus information based on the blockchain
currentConsensus.SetMode(currentConsensus.UpdateConsensusInformation()) currentConsensus.SetMode(currentConsensus.UpdateConsensusInformation())
// Setup block period and block due time. // Setup block period and block due time.

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/abool"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/api/client" "github.com/harmony-one/harmony/api/client"
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
@ -40,20 +41,6 @@ import (
"golang.org/x/sync/semaphore" "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 ( const (
// NumTryBroadCast is the number of times trying to broadcast // NumTryBroadCast is the number of times trying to broadcast
NumTryBroadCast = 3 NumTryBroadCast = 3
@ -71,26 +58,6 @@ const (
GlobalRxWorkers = 32 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 ( const (
maxBroadcastNodes = 10 // broadcast at most maxBroadcastNodes peers that need in sync maxBroadcastNodes = 10 // broadcast at most maxBroadcastNodes peers that need in sync
broadcastTimeout int64 = 60 * 1000000000 // 1 mins broadcastTimeout int64 = 60 * 1000000000 // 1 mins
@ -118,7 +85,6 @@ type Node struct {
SelfPeer p2p.Peer SelfPeer p2p.Peer
// TODO: Neighbors should store only neighbor nodes in the same shard // 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 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 stateMutex sync.Mutex // mutex for change node state
// BeaconNeighbors store only neighbor nodes in the beacon chain shard // 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 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 TransactionErrorSink *types.TransactionErrorSink
// BroadcastInvalidTx flag is considered when adding pending tx to tx-pool // BroadcastInvalidTx flag is considered when adding pending tx to tx-pool
BroadcastInvalidTx bool 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. // Blockchain returns the blockchain for the node's current shard.
@ -465,6 +433,7 @@ func New(
collection.DisableCache() collection.DisableCache()
} }
node.shardChains = collection node.shardChains = collection
node.IsInSync = abool.NewBool(false)
if host != nil && consensusObj != nil { if host != nil && consensusObj != nil {
// Consensus and associated channel to communicate blocks // 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 // TODO: treat fake maximum height
if node.stateSync.IsOutOfSync(bc) { if node.stateSync.IsOutOfSync(bc) {
node.stateMutex.Lock() node.IsInSync.UnSet()
node.State = NodeNotInSync
node.stateMutex.Unlock()
if willJoinConsensus { if willJoinConsensus {
node.Consensus.BlocksNotSynchronized() node.Consensus.BlocksNotSynchronized()
} }
node.stateSync.SyncLoop(bc, worker, false, node.Consensus) node.stateSync.SyncLoop(bc, worker, false, node.Consensus)
if willJoinConsensus { if willJoinConsensus {
node.stateMutex.Lock() node.IsInSync.Set()
node.State = NodeReadyForConsensus
node.stateMutex.Unlock()
node.Consensus.BlocksSynchronized() node.Consensus.BlocksSynchronized()
} }
} }
node.stateMutex.Lock() node.IsInSync.Set()
node.State = NodeReadyForConsensus
node.stateMutex.Unlock()
} }
// SupportBeaconSyncing sync with beacon chain for archival node in beacon chan or non-beacon node // 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 // this is the out of sync node acts as grpc server side
case downloader_pb.DownloaderRequest_NEWBLOCK: case downloader_pb.DownloaderRequest_NEWBLOCK:
if node.State != NodeNotInSync { if node.IsInSync.IsSet() {
utils.Logger().Debug().
Str("state", node.State.String()).
Msg("[SYNC] new block received, but state is")
response.Type = downloader_pb.DownloaderResponse_INSYNC response.Type = downloader_pb.DownloaderResponse_INSYNC
return response, nil return response, nil
} }
@ -461,7 +452,7 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in
} }
case downloader_pb.DownloaderRequest_REGISTERTIMEOUT: case downloader_pb.DownloaderRequest_REGISTERTIMEOUT:
if node.State == NodeNotInSync { if !node.IsInSync.IsSet() {
count := node.stateSync.RegisterNodeInfo() count := node.stateSync.RegisterNodeInfo()
utils.Logger().Debug(). utils.Logger().Debug().
Int("number", count). Int("number", count).

Loading…
Cancel
Save