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

[master-2.x] Set block_received_time during CheckCommits #1972

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 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