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

Fine tunning view 0 timeouts based on last block timestamp (limited by a maximum offset) #626

Closed
wants to merge 15 commits into from
Closed
Changes from 10 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
30 changes: 26 additions & 4 deletions neo/Consensus/ConsensusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ internal class Timer { public uint Height; public byte ViewNumber; }
private readonly IActorRef taskManager;
private readonly Store store;
private ICancelable timer_token;
private DateTime block_received_time;
private DateTime reference_block_time;
/// <summary>
/// Theoritical delay between local UTC time and timestamp of last block
/// </summary>
private uint theoreticalDelay;
private bool started = false;
/// <summary>
/// This will be cleared every block (so it will not grow out of control, but is used to prevent repeatedly
Expand Down Expand Up @@ -155,7 +159,7 @@ private void InitializeConsensus(byte viewNumber)
}
else
{
TimeSpan span = TimeProvider.Current.UtcNow - block_received_time;
TimeSpan span = TimeProvider.Current.UtcNow - reference_block_time;
if (span >= Blockchain.TimePerBlock)
ChangeTimer(TimeSpan.Zero);
else
Expand Down Expand Up @@ -268,11 +272,29 @@ private void OnConsensusPayload(ConsensusPayload payload)
private void OnPersistCompleted(Block block)
{
Log($"persist block: {block.Hash}");
block_received_time = TimeProvider.Current.UtcNow;
reference_block_time = TimeProvider.Current.UtcNow;
theoreticalDelay = calcNetworkDelayOrClockDriftWithLimits(reference_block_time.ToTimestamp(), block.Timestamp);
reference_block_time.AddSeconds((double)-theoreticalDelay);
knownHashes.Clear();
InitializeConsensus(0);
}

private uint calcNetworkDelayOrClockDriftWithLimits(uint localClockTimestamp, uint lastBlockTimestamp)
{
if (localClockTimestamp > lastBlockTimestamp)
jsolman marked this conversation as resolved.
Show resolved Hide resolved
{
var currentTheoreticalDelay = localClockTimestamp - lastBlockTimestamp;
Log($"localClock:{localClockTimestamp}\nlastBlockTimestamp:{lastBlockTimestamp}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean these logs before merging.

Log($"diff:{localClockTimestamp - lastBlockTimestamp}");
// maximum expected delay 30% of block time - currently 4,5s
uint maxDelayToAdvance = Blockchain.SecondsPerBlock / 100 * 30;
currentTheoreticalDelay = currentTheoreticalDelay > maxDelayToAdvance ? maxDelayToAdvance : currentTheoreticalDelay;
Log($"-currentTheoreticalDelay:{-currentTheoreticalDelay}");
return currentTheoreticalDelay;
}
return 0;
}

private void OnRecoveryMessageReceived(ConsensusPayload payload, RecoveryMessage message)
{
if (message.ViewNumber < context.ViewNumber) return;
Expand Down Expand Up @@ -520,7 +542,7 @@ private void SendPrepareRequest()
foreach (InvPayload payload in InvPayload.CreateGroup(InventoryType.TX, context.TransactionHashes.Skip(1).ToArray()))
localNode.Tell(Message.Create("inv", payload));
}
ChangeTimer(TimeSpan.FromSeconds(Blockchain.SecondsPerBlock << (context.ViewNumber + 1)));
ChangeTimer(TimeSpan.FromSeconds((Blockchain.SecondsPerBlock << (context.ViewNumber + 1)) - (context.ViewNumber == 0 ? (Blockchain.SecondsPerBlock-theoreticalDelay) : 0)));
}

private bool VerifyRequest()
Expand Down