Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus: set last_block_index during CheckCommits #1960

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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