Skip to content

Commit

Permalink
consensus: set last_block_index during CheckCommits (neo-project#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaShaleva authored and Shawn committed Jan 8, 2021
1 parent 941b9b1 commit 9029664
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 9029664

Please sign in to comment.