Skip to content

Commit

Permalink
[master-2.x] Force sync state root from all peers (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangTao1596 authored Sep 1, 2020
1 parent 6504fc3 commit a423b18
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private class Timer { }
private readonly UInt256 StateRootTaskHash = UInt256.Parse("0x0000000000000000000000000000000000000000000000000000000000000001");
private bool HasHeaderTask => globalTasks.ContainsKey(HeaderTaskHash);
private bool HasStateRootTask => globalTasks.ContainsKey(StateRootTaskHash);
private DateTime StateRootSyncTime;

public TaskManager(NeoSystem system)
{
Expand Down Expand Up @@ -212,6 +213,10 @@ private void OnTimer()
if (session.Tasks.Remove(task.Key))
DecrementGlobalTask(task.Key);
}

if (HasStateRootTask && DateTime.UtcNow - StateRootSyncTime > TaskTimeout)
DecrementGlobalTask(StateRootTaskHash);

foreach (TaskSession session in sessions.Values)
RequestTasks(session);
}
Expand Down Expand Up @@ -279,15 +284,11 @@ private void RequestTasks(TaskSession session)
{
if (Blockchain.Singleton.ExpectStateRootIndex < Blockchain.Singleton.Height)
{
var state = Blockchain.Singleton.GetStateRoot(Blockchain.Singleton.ExpectStateRootIndex);
if (state is null || state.Flag == StateRootVerifyFlag.Unverified)
{
var start_index = Blockchain.Singleton.ExpectStateRootIndex;
var count = Math.Min(Blockchain.Singleton.Height - start_index, StateRootsPayload.MaxStateRootsCount);
session.Tasks[StateRootTaskHash] = DateTime.UtcNow;
IncrementGlobalTask(StateRootTaskHash);
session.RemoteNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count)));
}
var start_index = Blockchain.Singleton.ExpectStateRootIndex;
var count = Math.Min(Blockchain.Singleton.Height - start_index, StateRootsPayload.MaxStateRootsCount);
StateRootSyncTime = DateTime.UtcNow;
IncrementGlobalTask(StateRootTaskHash);
system.LocalNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count)));
}
}
}
Expand Down

0 comments on commit a423b18

Please sign in to comment.