Skip to content

Commit

Permalink
consensus: set block_received_time during check commits (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaShaleva committed Sep 25, 2020
1 parent 4a0ed74 commit 94b098e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 94b098e

Please sign in to comment.