Skip to content

Commit

Permalink
Merge pull request #15 from aboutcircles/fix/only-initial-gap-check
Browse files Browse the repository at this point in the history
Fix/only initial gap check
  • Loading branch information
jaensen authored Aug 4, 2024
2 parents 481493e + 4e587df commit 041c894
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Circles.Index/BlockIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private async Task Sink((BlockWithReceipts, IEnumerable<IIndexEvent>) data)
receiptsSourceBlock.LinkTo(parserBlock, new DataflowLinkOptions { PropagateCompletion = true });

ActionBlock<(BlockWithReceipts, IEnumerable<IIndexEvent>)> sinkBlock = new(Sink,
CreateOptions(cancellationToken, 50000, 1));
CreateOptions(cancellationToken, 64 * 1024, 1));
parserBlock.LinkTo(sinkBlock, new DataflowLinkOptions { PropagateCompletion = true });

return (sourceBlock, sinkBlock);
Expand Down
6 changes: 3 additions & 3 deletions Circles.Index/Circles.Index.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<Authors>Daniel Janz (Gnosis Service GmbH)</Authors>
<Copyright>Gnosis Service GmbH</Copyright>
<Product>Circles</Product>
<AssemblyVersion>1.4.0</AssemblyVersion>
<FileVersion>1.4.0</FileVersion>
<AssemblyVersion>1.4.1</AssemblyVersion>
<FileVersion>1.4.1</FileVersion>
</PropertyGroup>


Expand All @@ -22,7 +22,7 @@

<ItemGroup>
<PackageReference Include="Nethermind.Numerics.Int256" Version="1.2.0" />
<PackageReference Include="Nethermind.ReferenceAssemblies" Version="1.27.0" />
<PackageReference Include="Nethermind.ReferenceAssemblies" Version="1.27.1" />
</ItemGroup>


Expand Down
47 changes: 29 additions & 18 deletions Circles.Index/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ private record LeaveState : IEvent;

private State CurrentState { get; set; } = State.New;

private long LastIndexHeight => context.Database.FirstGap() ?? context.Database.LatestBlock() ?? 0;

public async Task HandleEvent(IEvent e)
{
try
Expand All @@ -53,8 +51,11 @@ public async Task HandleEvent(IEvent e)
{
case EnterState:
{
// Initially delete all events for which no "System_Block" exists
await TransitionTo(State.Reorg, LastIndexHeight);
context.Logger.Info("Initializing: Finding the last persisted block...");
var lastPersistedBlock = context.Database.FirstGap() ?? context.Database.LatestBlock() ?? 0;

context.Logger.Info($"Initializing: Last persisted block is {lastPersistedBlock}. Deleting all events from this block onwards...");
await TransitionTo(State.Reorg, lastPersistedBlock);
return;
}
}
Expand All @@ -79,8 +80,8 @@ public async Task HandleEvent(IEvent e)
switch (e)
{
case NewHead newHead:
context.Logger.Info($"New head received: {newHead.Head}");
if (newHead.Head <= LastIndexHeight)
context.Logger.Debug($"New head received: {newHead.Head}");
if (newHead.Head <= context.Database.LatestBlock())
{
await TransitionTo(State.Reorg, newHead.Head);
return;
Expand All @@ -97,8 +98,8 @@ public async Task HandleEvent(IEvent e)
{
case EnterState<long> enterSyncing:
var importedBlockRange = await Sync(enterSyncing.Arg);
context.Logger.Info($"Imported blocks from {importedBlockRange.Min} " +
$"to {importedBlockRange.Max}");
context.Logger.Debug($"Imported blocks from {importedBlockRange.Min} " +
$"to {importedBlockRange.Max}");
Errors.Clear();

await TransitionTo(State.NotifySubscribers, importedBlockRange);
Expand Down Expand Up @@ -135,15 +136,25 @@ public async Task HandleEvent(IEvent e)
switch (e)
{
case EnterState:
if (Errors.Count >= 3)
// Exponential backoff based on the number of errors
var delay = Errors.Count * Errors.Count * 1000;

// If the delay is larger than 60 sec, clear the oldest errors
if (delay > 60000)
{
// If we have tried 3 times, give up
await TransitionTo(State.End);
return;
Errors.RemoveAt(0);
}

// Otherwise, wait for a bit before retrying
await Task.Delay(Errors.Count * Errors.Count * 1000, cancellationToken);
// Add some jitter to the delay
var jitter = new Random((int)DateTime.Now.TimeOfDay.TotalSeconds).Next(0, 1000);
delay += jitter;

// Wait 'delay' ms
context.Logger.Info($"Waiting {delay} ms before retrying after an error...");
await Task.Delay(delay, cancellationToken);

// Retry
context.Logger.Info("Transitioning to 'Initial' state after an error...");
await TransitionTo(State.Initial);
return;
case LeaveState:
Expand All @@ -156,7 +167,7 @@ public async Task HandleEvent(IEvent e)
return;
}

context.Logger.Debug($"Unhandled event {e} in state {CurrentState}");
context.Logger.Trace($"Unhandled event {e} in state {CurrentState}");
}
catch (Exception ex)
{
Expand All @@ -169,7 +180,7 @@ public async Task HandleEvent(IEvent e)

private async Task TransitionTo<TArgument>(State newState, TArgument? argument)
{
context.Logger.Info($"Transitioning from {CurrentState} to {newState}");
context.Logger.Debug($"Transitioning from {CurrentState} to {newState}");
if (newState is not State.Error)
{
await HandleEvent(new LeaveState());
Expand All @@ -187,15 +198,15 @@ public async Task TransitionTo(State newState)

private async IAsyncEnumerable<long> GetBlocksToSync(long toBlock)
{
long lastIndexHeight = LastIndexHeight;
long lastIndexHeight = context.Database.LatestBlock() ?? 0;
if (lastIndexHeight == toBlock)
{
context.Logger.Info("No blocks to sync.");
yield break;
}

var nextBlock = lastIndexHeight + 1;
context.Logger.Info($"Enumerating blocks to sync from {nextBlock} (LastIndexHeight + 1) to {toBlock}");
context.Logger.Debug($"Enumerating blocks to sync from {nextBlock} (LastIndexHeight + 1) to {toBlock}");

for (long i = nextBlock; i <= toBlock; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion arm64.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /circles-nethermind-plugin

FROM nethermind/nethermind:1.27.0 AS base
FROM nethermind/nethermind:1.27.1 AS base

# dotnet libs
COPY --from=build /circles-nethermind-plugin/Circles.Index.deps.json /nethermind/plugins
Expand Down
2 changes: 1 addition & 1 deletion x64.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .
RUN dotnet restore
RUN dotnet publish -c Release -o /circles-nethermind-plugin

FROM nethermind/nethermind:1.27.0 AS base
FROM nethermind/nethermind:1.27.1 AS base

# dotnet libs
COPY --from=build /circles-nethermind-plugin/Circles.Index.deps.json /nethermind/plugins
Expand Down
2 changes: 1 addition & 1 deletion x64.debug.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .
RUN dotnet restore
RUN dotnet publish -c Debug -o /circles-nethermind-plugin

FROM nethermind/nethermind:1.27.0 AS base
FROM nethermind/nethermind:1.27.1 AS base

# dotnet libs
COPY --from=build /circles-nethermind-plugin/Circles.Index.deps.json /nethermind/plugins
Expand Down
2 changes: 1 addition & 1 deletion x64.debug.spaceneth.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .
RUN dotnet restore
RUN dotnet publish -c Debug -o /circles-nethermind-plugin

FROM nethermind/nethermind:1.27.0 AS base
FROM nethermind/nethermind:1.27.1 AS base

# dotnet libs
COPY --from=build /circles-nethermind-plugin/Circles.Index.deps.json /nethermind/plugins
Expand Down

0 comments on commit 041c894

Please sign in to comment.