Skip to content

Commit

Permalink
consensus: set last_block_index during CheckCommits
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AnnaShaleva committed Sep 25, 2020
1 parent 98abed0 commit 899655a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit 899655a

Please sign in to comment.