From f2248e16da7a44e5b45f6d1b3807bae3bba9227a Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 25 Sep 2020 13:33:05 +0300 Subject: [PATCH] consensus: set block_received_time during check commits Part of #1959. Port 899655a377362923745eda65ea92b58aa53f5e73 changes to master-2.x branch. --- neo/Consensus/ConsensusService.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/neo/Consensus/ConsensusService.cs b/neo/Consensus/ConsensusService.cs index ba4dfbb037..f138a5e739 100644 --- a/neo/Consensus/ConsensusService.cs +++ b/neo/Consensus/ConsensusService.cs @@ -27,6 +27,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private readonly IActorRef taskManager; private ICancelable timer_token; private DateTime block_received_time; + private uint block_received_index; private bool started = false; /// @@ -114,6 +115,8 @@ private void CheckCommits() { if (context.CommitPayloads.Count(p => p?.ConsensusMessage.ViewNumber == context.ViewNumber) >= context.M() && context.TransactionHashes.All(p => context.Transactions.ContainsKey(p))) { + block_received_index = context.BlockIndex; + block_received_time = TimeProvider.Current.UtcNow; Block block = context.CreateBlock(); Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); localNode.Tell(new LocalNode.Relay { Inventory = block }); @@ -172,11 +175,16 @@ private void InitializeConsensus(byte viewNumber) } else { - TimeSpan span = TimeProvider.Current.UtcNow - block_received_time; - if (span >= Blockchain.TimePerBlock) - ChangeTimer(TimeSpan.Zero); - else - ChangeTimer(Blockchain.TimePerBlock - span); + TimeSpan span = Blockchain.TimePerBlock; + if (block_received_index + 1 == context.BlockIndex) + { + var diff = TimeProvider.Current.UtcNow - block_received_time; + if (diff >= span) + span = TimeSpan.Zero; + else + span -= diff; + } + ChangeTimer(span); } } else @@ -313,7 +321,6 @@ private void OnConsensusPayload(ConsensusPayload payload) private void OnPersistCompleted(Block block) { Log($"persist block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); - block_received_time = TimeProvider.Current.UtcNow; knownHashes.Clear(); InitializeConsensus(0); }