From f5c393506d49a123900f88815c6bb2b61765a8eb Mon Sep 17 00:00:00 2001 From: Cedric Fung Date: Tue, 5 Mar 2019 12:39:31 +0800 Subject: [PATCH] check node sync status before sign self snapshot --- kernel/graph.go | 2 +- kernel/node.go | 17 ++++++++++++++++- kernel/self.go | 11 ++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/kernel/graph.go b/kernel/graph.go index 4d3eaa141..a730769fe 100644 --- a/kernel/graph.go +++ b/kernel/graph.go @@ -21,7 +21,7 @@ func (node *Node) handleSnapshotInput(s *common.Snapshot) error { return node.handleSyncFinalSnapshot(s) } - if !node.CheckSync() { + if !node.CheckCatchUp() { return node.queueSnapshotOrPanic(s, false) } diff --git a/kernel/node.go b/kernel/node.go index a0606c370..e308e5947 100644 --- a/kernel/node.go +++ b/kernel/node.go @@ -267,7 +267,7 @@ func (node *Node) QueueAppendSnapshot(peerId crypto.Hash, s *common.Snapshot) er if !signersMap[peerId] { return nil } - if !node.CheckSync() { + if !node.CheckCatchUp() { return nil } return node.store.QueueAppendSnapshot(peerId, s, false) @@ -311,6 +311,21 @@ func (node *Node) UpdateSyncPoint(peerId crypto.Hash, points []*network.SyncPoin } func (node *Node) CheckSync() bool { + count := 1 + final := node.Graph.MyFinalNumber + for id, _ := range node.ConsensusNodes { + remote := node.SyncPoints.Get(id) + if remote == nil { + continue + } + if remote.Number+1 >= final { + count += 1 + } + } + return count >= len(node.ConsensusNodes)*2/3+1 +} + +func (node *Node) CheckCatchUp() bool { if node.SyncPoints.Len() != len(node.ConsensusNodes)-1 { return false } diff --git a/kernel/self.go b/kernel/self.go index f52a9491a..477f5823b 100644 --- a/kernel/self.go +++ b/kernel/self.go @@ -138,13 +138,18 @@ func (node *Node) signSelfSnapshot(s *common.Snapshot, tx *common.SignedTransact if s.NodeId != node.IdForNetwork || len(s.Signatures) != 0 || s.Timestamp != 0 { panic("should never be here") } + + cache := node.Graph.CacheRound[s.NodeId].Copy() + final := node.Graph.FinalRound[s.NodeId].Copy() + if !node.checkCacheCapability() { time.Sleep(10 * time.Millisecond) return node.queueSnapshotOrPanic(s, false) } - - cache := node.Graph.CacheRound[s.NodeId].Copy() - final := node.Graph.FinalRound[s.NodeId].Copy() + if !node.CheckSync() && len(cache.Snapshots) == 0 { + time.Sleep(time.Duration(config.SnapshotRoundGap / 2)) + return node.queueSnapshotOrPanic(s, false) + } for { s.Timestamp = uint64(time.Now().UnixNano())