From 9a6577a1ad6a16c46f5c3ccd9e2a9594d2e55053 Mon Sep 17 00:00:00 2001 From: KickSeason Date: Wed, 19 Aug 2020 16:46:38 +0800 Subject: [PATCH 1/5] force sync state root from all peers --- neo/Network/P2P/TaskManager.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 84d8fa6bb4..9508925f9f 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -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) { @@ -56,11 +57,14 @@ public TaskManager(NeoSystem system) private void OnInternalTaskCompleted(UInt256 hash) { - if (!sessions.TryGetValue(Sender, out TaskSession session)) - return; - session.Tasks.Remove(hash); + if (hash != StateRootTaskHash) + { + if (!sessions.TryGetValue(Sender, out TaskSession session)) + return; + session.Tasks.Remove(hash); + RequestTasks(session); + } DecrementGlobalTask(hash); - RequestTasks(session); } private void OnNewTasks(InvPayload payload) @@ -212,6 +216,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); } @@ -286,9 +294,9 @@ private void RequestTasks(TaskSession session) { var start_index = (uint)(state_height + 1); var count = Math.Min(height - start_index, StateRootsPayload.MaxStateRootsCount); - session.Tasks[StateRootTaskHash] = DateTime.UtcNow; + StateRootSyncTime = DateTime.UtcNow; IncrementGlobalTask(StateRootTaskHash); - session.RemoteNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count))); + system.LocalNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count))); } } } From 5a18bb8a712b5be3d09e86a6f46610dced7eec45 Mon Sep 17 00:00:00 2001 From: KickSeason Date: Wed, 19 Aug 2020 17:04:03 +0800 Subject: [PATCH 2/5] rm useless check --- neo/Network/P2P/TaskManager.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 9508925f9f..c484672832 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -289,15 +289,11 @@ private void RequestTasks(TaskSession session) var height = Blockchain.Singleton.Height; if (state_height + 1 < height) { - var state = Blockchain.Singleton.GetStateRoot((uint)(state_height + 1)); - if (state is null || state.Flag == StateRootVerifyFlag.Unverified) - { - var start_index = (uint)(state_height + 1); - var count = Math.Min(height - start_index, StateRootsPayload.MaxStateRootsCount); - StateRootSyncTime = DateTime.UtcNow; - IncrementGlobalTask(StateRootTaskHash); - system.LocalNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count))); - } + var start_index = (uint)(state_height + 1); + var count = Math.Min(height - start_index, StateRootsPayload.MaxStateRootsCount); + StateRootSyncTime = DateTime.UtcNow; + IncrementGlobalTask(StateRootTaskHash); + system.LocalNode.Tell(Message.Create("getroots", GetStateRootsPayload.Create(start_index, count))); } } } From 76bbac0dafce1f2a5eb6ae4c1d69ba2e4eea83ae Mon Sep 17 00:00:00 2001 From: KickSeason Date: Wed, 19 Aug 2020 17:16:44 +0800 Subject: [PATCH 3/5] modi task completed --- neo/Network/P2P/TaskManager.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index c484672832..ce11d55d24 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -57,14 +57,11 @@ public TaskManager(NeoSystem system) private void OnInternalTaskCompleted(UInt256 hash) { - if (hash != StateRootTaskHash) - { - if (!sessions.TryGetValue(Sender, out TaskSession session)) - return; - session.Tasks.Remove(hash); - RequestTasks(session); - } + if (!sessions.TryGetValue(Sender, out TaskSession session)) + return; + if (hash != StateRootTaskHash) session.Tasks.Remove(hash); DecrementGlobalTask(hash); + RequestTasks(session); } private void OnNewTasks(InvPayload payload) From c4887589ec9422378490f5d0f6966abbecccd0a6 Mon Sep 17 00:00:00 2001 From: zhangtao Date: Mon, 31 Aug 2020 21:19:14 +0800 Subject: [PATCH 4/5] format --- neo/Network/P2P/TaskManager.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index e1aa0ea8a6..323489d913 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -284,11 +284,11 @@ private void RequestTasks(TaskSession session) { if (Blockchain.Singleton.ExpectStateRootIndex < Blockchain.Singleton.Height) { - 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))); + 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))); } } } From 06e9887b0e7e099c212b91164bc7303116f797f1 Mon Sep 17 00:00:00 2001 From: zhangtao Date: Mon, 31 Aug 2020 21:53:06 +0800 Subject: [PATCH 5/5] rm unnecessary check --- neo/Network/P2P/TaskManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Network/P2P/TaskManager.cs b/neo/Network/P2P/TaskManager.cs index 323489d913..7af4c19677 100644 --- a/neo/Network/P2P/TaskManager.cs +++ b/neo/Network/P2P/TaskManager.cs @@ -59,7 +59,7 @@ private void OnInternalTaskCompleted(UInt256 hash) { if (!sessions.TryGetValue(Sender, out TaskSession session)) return; - if (hash != StateRootTaskHash) session.Tasks.Remove(hash); + session.Tasks.Remove(hash); DecrementGlobalTask(hash); RequestTasks(session); }