Skip to content

Commit

Permalink
check node sync status before sign self snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Mar 5, 2019
1 parent 43a0e74 commit f5c3935
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion kernel/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 16 additions & 1 deletion kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions kernel/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit f5c3935

Please sign in to comment.