From 899655a377362923745eda65ea92b58aa53f5e73 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 23 Sep 2020 18:24:16 +0300 Subject: [PATCH] consensus: set last_block_index during CheckCommits This commit makes block time closer to the desired MillisecondsPerBlock value provided via node configuration. It also allows the node to work more instead of waiting for a timer. --- src/neo/Consensus/ConsensusService.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/neo/Consensus/ConsensusService.cs b/src/neo/Consensus/ConsensusService.cs index 8054147eb5..3f9f3d29b6 100644 --- a/src/neo/Consensus/ConsensusService.cs +++ b/src/neo/Consensus/ConsensusService.cs @@ -29,6 +29,7 @@ internal class Timer { public uint Height; public byte ViewNumber; } private readonly IActorRef blockchain; private ICancelable timer_token; private DateTime block_received_time; + private uint block_received_index; private bool started = false; /// @@ -135,6 +136,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.Block.Index; + block_received_time = TimeProvider.Current.UtcNow; Block block = context.CreateBlock(); Log($"relay block: height={block.Index} hash={block.Hash} tx={block.Transactions.Length}"); blockchain.Tell(block); @@ -188,11 +191,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.Block.Index) + { + var diff = TimeProvider.Current.UtcNow - block_received_time; + if (diff >= span) + span = TimeSpan.Zero; + else + span -= diff; + } + ChangeTimer(span); } } else @@ -321,7 +329,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); }