From e44b3334c92860573e509dcdc27cb369a1c3b2a2 Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 12 Nov 2019 11:29:29 +0100 Subject: [PATCH 01/11] Decrease the time threshold --- neo/Consensus/ConsensusService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 574082043a..4bbfb45420 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -405,7 +405,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.AddMinutes(10).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.AddMinutes(2).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return; From 26d68b0adc7b468fc96c42dba606ce691256a55f Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 11:34:39 +0100 Subject: [PATCH 02/11] Change to a formula --- neo/Consensus/ConsensusContext.cs | 2 ++ neo/Consensus/ConsensusService.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index e7c2649782..895357fd65 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -26,6 +26,7 @@ internal class ConsensusContext : IDisposable, ISerializable public Block Block; public byte ViewNumber; public ECPoint[] Validators; + public TimeSpan PrepareRequestThreshold; public int MyIndex; public UInt256[] TransactionHashes; public Dictionary Transactions; @@ -349,6 +350,7 @@ public void Reset(byte viewNumber) }; var pv = Validators; Validators = NativeContract.NEO.GetNextBlockValidators(Snapshot); + PrepareRequestThreshold = TimeSpan.FromMilliseconds((Validators.Length + 1) * Blockchain.TimePerBlock.TotalMilliseconds); if (_witnessSize == 0 || (pv != null && pv.Length != Validators.Length)) { // Compute the expected size of the witness diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 4bbfb45420..b3f8d39ac3 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -405,7 +405,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.AddMinutes(2).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(context.PrepareRequestThreshold).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return; From fa13cc417c88806f7877fbce6cf7616db132474f Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 14 Nov 2019 11:39:33 +0100 Subject: [PATCH 03/11] UT --- neo.UnitTests/Consensus/UT_ConsensusContext.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/neo.UnitTests/Consensus/UT_ConsensusContext.cs index 9a0dd39ba7..ddace008fb 100644 --- a/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -3,6 +3,7 @@ using Moq; using Neo.Consensus; using Neo.IO; +using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.SmartContract; using Neo.SmartContract.Native; @@ -44,6 +45,7 @@ public void TestSetup() Validators = _validatorKeys.Select(u => u.PublicKey).ToArray() }; _context.Reset(0); + Assert.AreEqual((_validatorKeys.Length + 1) * Blockchain.TimePerBlock.TotalMilliseconds, _context.PrepareRequestThreshold.TotalMilliseconds); } [TestCleanup] From dca3da52e668f4ce429e3712deaf21657b7f1b5f Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 16 Nov 2019 10:04:17 +0100 Subject: [PATCH 04/11] Change formula --- neo/Consensus/ConsensusContext.cs | 2 -- neo/Consensus/ConsensusService.cs | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/neo/Consensus/ConsensusContext.cs b/neo/Consensus/ConsensusContext.cs index 895357fd65..e7c2649782 100644 --- a/neo/Consensus/ConsensusContext.cs +++ b/neo/Consensus/ConsensusContext.cs @@ -26,7 +26,6 @@ internal class ConsensusContext : IDisposable, ISerializable public Block Block; public byte ViewNumber; public ECPoint[] Validators; - public TimeSpan PrepareRequestThreshold; public int MyIndex; public UInt256[] TransactionHashes; public Dictionary Transactions; @@ -350,7 +349,6 @@ public void Reset(byte viewNumber) }; var pv = Validators; Validators = NativeContract.NEO.GetNextBlockValidators(Snapshot); - PrepareRequestThreshold = TimeSpan.FromMilliseconds((Validators.Length + 1) * Blockchain.TimePerBlock.TotalMilliseconds); if (_witnessSize == 0 || (pv != null && pv.Length != Validators.Length)) { // Compute the expected size of the witness diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index b3f8d39ac3..39ca38ffb9 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,6 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; + private static readonly TimeSpan prepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); /// /// This will record the information from last scheduled timer @@ -405,7 +406,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(context.PrepareRequestThreshold).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(prepareRequestThreshold).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return; From 1d18d5d3bc581ebd47edeaf5e657a30b26c5bac5 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 16 Nov 2019 10:06:47 +0100 Subject: [PATCH 05/11] UT --- neo.UnitTests/Consensus/UT_Consensus.cs | 6 ++++++ neo.UnitTests/Consensus/UT_ConsensusContext.cs | 1 - neo/Consensus/ConsensusService.cs | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index c8d8fbf6c4..6e3fd53aff 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -36,6 +36,12 @@ public void Cleanup() Shutdown(); } + [TestMethod] + public void TestPrepareRequestThreshold() + { + Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, ConsensusService.PrepareRequestThreshold.TotalMilliseconds); + } + [TestMethod] public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart() { diff --git a/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/neo.UnitTests/Consensus/UT_ConsensusContext.cs index ddace008fb..dbe4e60b49 100644 --- a/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -45,7 +45,6 @@ public void TestSetup() Validators = _validatorKeys.Select(u => u.PublicKey).ToArray() }; _context.Reset(0); - Assert.AreEqual((_validatorKeys.Length + 1) * Blockchain.TimePerBlock.TotalMilliseconds, _context.PrepareRequestThreshold.TotalMilliseconds); } [TestCleanup] diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 39ca38ffb9..4041b56f31 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,7 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; - private static readonly TimeSpan prepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); + public static readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); /// /// This will record the information from last scheduled timer From aaeb28d0fd3dd18d1cda73e751de19b8edb50bae Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 16 Nov 2019 10:07:11 +0100 Subject: [PATCH 06/11] Clean --- neo.UnitTests/Consensus/UT_ConsensusContext.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/neo.UnitTests/Consensus/UT_ConsensusContext.cs index dbe4e60b49..9a0dd39ba7 100644 --- a/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -3,7 +3,6 @@ using Moq; using Neo.Consensus; using Neo.IO; -using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.SmartContract; using Neo.SmartContract.Native; From 405e3da4eb609353bd80f9bd44a20c7057cdd5ff Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 16 Nov 2019 10:09:24 +0100 Subject: [PATCH 07/11] Remove static --- neo.UnitTests/Consensus/UT_Consensus.cs | 3 ++- neo/Consensus/ConsensusService.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index 6e3fd53aff..1cb5faa7f9 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -39,7 +39,8 @@ public void Cleanup() [TestMethod] public void TestPrepareRequestThreshold() { - Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, ConsensusService.PrepareRequestThreshold.TotalMilliseconds); + var service = new ConsensusService(null, null, null); + Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, service.PrepareRequestThreshold.TotalMilliseconds); } [TestMethod] diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 4041b56f31..a4a6d87a8a 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,7 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; - public static readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); + public readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); /// /// This will record the information from last scheduled timer @@ -406,7 +406,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(prepareRequestThreshold).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(PrepareRequestThreshold).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return; From 7baf00f53173ecfda79969aea9cdde39320538cd Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 16 Nov 2019 10:55:17 +0100 Subject: [PATCH 08/11] Fix isolated issues --- neo.UnitTests/Consensus/UT_Consensus.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index 1cb5faa7f9..db910638e5 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -20,7 +20,6 @@ namespace Neo.UnitTests.Consensus { - [TestClass] public class ConsensusTests : TestKit { @@ -36,7 +35,7 @@ public void Cleanup() Shutdown(); } - [TestMethod] + [TestMethod, NotReRunnable] public void TestPrepareRequestThreshold() { var service = new ConsensusService(null, null, null); From b8eccf349d15efbed8c75f2cc3c87c008b424bf3 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sun, 17 Nov 2019 18:29:45 +0100 Subject: [PATCH 09/11] Change to static --- neo.UnitTests/Consensus/UT_Consensus.cs | 3 +-- neo/Consensus/ConsensusService.cs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index db910638e5..574f4161af 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -38,8 +38,7 @@ public void Cleanup() [TestMethod, NotReRunnable] public void TestPrepareRequestThreshold() { - var service = new ConsensusService(null, null, null); - Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, service.PrepareRequestThreshold.TotalMilliseconds); + Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, ConsensusService.PrepareRequestThreshold.TotalMilliseconds); } [TestMethod] diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index a4a6d87a8a..f8ca35dce6 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,7 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; - public readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); + public static readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); /// /// This will record the information from last scheduled timer From 09bfd6f03c4f19c5210d5792b02e7ded9f4fff3d Mon Sep 17 00:00:00 2001 From: erikzhang Date: Mon, 18 Nov 2019 16:47:41 +0800 Subject: [PATCH 10/11] private --- neo.UnitTests/Consensus/UT_Consensus.cs | 6 ------ neo/Consensus/ConsensusService.cs | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/neo.UnitTests/Consensus/UT_Consensus.cs b/neo.UnitTests/Consensus/UT_Consensus.cs index 574f4161af..c10e6c94e3 100644 --- a/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/neo.UnitTests/Consensus/UT_Consensus.cs @@ -35,12 +35,6 @@ public void Cleanup() Shutdown(); } - [TestMethod, NotReRunnable] - public void TestPrepareRequestThreshold() - { - Assert.AreEqual(8 * Blockchain.TimePerBlock.TotalMilliseconds, ConsensusService.PrepareRequestThreshold.TotalMilliseconds); - } - [TestMethod] public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart() { diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index f8ca35dce6..56d6ae657c 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,7 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; - public static readonly TimeSpan PrepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.TimePerBlock.TotalMilliseconds); + private static readonly TimeSpan prepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.MillisecondsPerBlock); /// /// This will record the information from last scheduled timer @@ -406,7 +406,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(PrepareRequestThreshold).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(prepareRequestThreshold).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return; From 1bb6cbf08026290a6fc18e0ff299495b7c214b6d Mon Sep 17 00:00:00 2001 From: erikzhang Date: Mon, 18 Nov 2019 16:50:16 +0800 Subject: [PATCH 11/11] Simplify --- neo/Consensus/ConsensusService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index 56d6ae657c..9d0bd5a343 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -29,7 +29,6 @@ internal class Timer { public uint Height; public byte ViewNumber; } private ICancelable timer_token; private DateTime block_received_time; private bool started = false; - private static readonly TimeSpan prepareRequestThreshold = TimeSpan.FromMilliseconds(8 * Blockchain.MillisecondsPerBlock); /// /// This will record the information from last scheduled timer @@ -406,7 +405,7 @@ private void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest m if (context.RequestSentOrReceived || context.NotAcceptingPayloadsDueToViewChanging) return; if (payload.ValidatorIndex != context.Block.ConsensusData.PrimaryIndex || message.ViewNumber != context.ViewNumber) return; Log($"{nameof(OnPrepareRequestReceived)}: height={payload.BlockIndex} view={message.ViewNumber} index={payload.ValidatorIndex} tx={message.TransactionHashes.Length}"); - if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.Add(prepareRequestThreshold).ToTimestampMS()) + if (message.Timestamp <= context.PrevHeader.Timestamp || message.Timestamp > TimeProvider.Current.UtcNow.AddMilliseconds(8 * Blockchain.MillisecondsPerBlock).ToTimestampMS()) { Log($"Timestamp incorrect: {message.Timestamp}", LogLevel.Warning); return;