Skip to content

Commit

Permalink
Merge pull request #33 from Pier-Two/dev
Browse files Browse the repository at this point in the history
Improve connection behaviour;
  • Loading branch information
MPierTwo authored Oct 9, 2024
2 parents 1605423 + 10986d1 commit 62ec39e
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 77 deletions.
20 changes: 10 additions & 10 deletions src/Lantern.Beacon.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public static async Task Main()
.WithEntry(EnrEntryKey.Secp256K1, new EntrySecp256K1(sessionOptions.Signer.PublicKey));
var discv5LoggerFactory = LoggerFactory.Create(builder => builder.SetMinimumLevel(LogLevel.None));
// Beacon client options
var syncProtocolOptions = new SyncProtocolOptions
{
Preset = SizePreset.MainnetPreset,
GenesisValidatorsRoot = Convert.FromHexString("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"),
GenesisTime = 1606824023,
TrustedBlockRoot = Convert.FromHexString("b170fd52257200a0bc86f896ee9b688e9022f93e70810aa90e779a7bc1683a7f"),
Network = NetworkType.Mainnet
};
var beaconClientOptions = new BeaconClientOptions
{
TcpPort = 9005,
Expand All @@ -60,7 +52,16 @@ public static async Task Main()
//"/ip4/162.19.222.38/tcp/15401/p2p/16Uiu2HAmLA7eWnZUnjFQNR7sa8uZumNGA5hPvW6wiWoW1cT2Xkgg"
//"/ip4/116.202.215.20/tcp/9000/p2p/16Uiu2HAmB8gmsy3QGaLcL8gQHF5TUAn6fhQNzNT522xArY2tMhKr"
//"/ip4/88.99.208.221/tcp/9105/p2p/16Uiu2HAkvSit4sbSkr6AdiEzUcHFv7KQrssV1y4QVsDt4EVpVTYU"
]
],
SyncProtocolOptions = new SyncProtocolOptions
{
Preset = SizePreset.MainnetPreset,
GenesisValidatorsRoot = Convert.FromHexString("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"),
GenesisTime = 1606824023,
TrustedBlockRoot = Convert.FromHexString("b170fd52257200a0bc86f896ee9b688e9022f93e70810aa90e779a7bc1683a7f"),
Network = NetworkType.Mainnet
}

//Bootnodes = ["/ip4/162.19.222.38/tcp/15401/p2p/16Uiu2HAmLA7eWnZUnjFQNR7sa8uZumNGA5hPvW6wiWoW1cT2Xkgg"] // Good peer
//Bootnodes = ["/ip4/194.33.40.78/tcp/9001/p2p/16Uiu2HAky7NHnmvJcE2Kq459qNrgczXvfqjdFtsAJ6HyAoZpP4zw"]
//Bootnodes = ["/ip4/145.239.161.11/tcp/15401/p2p/16Uiu2HAkvcYHu3rHkJqs7VQyyHsdFq5fFyjKNR9VJnLDrFs93sb6"]
Expand Down Expand Up @@ -112,7 +113,6 @@ public static async Task Main()
});

beaconClientBuilder.WithBeaconClientOptions(beaconClientOptions);
beaconClientBuilder.WithSyncProtocolOptions(syncProtocolOptions);
beaconClientBuilder.AddLibp2pProtocol(libp2PBuilder => libp2PBuilder);
beaconClientBuilder.WithLoggerFactory(loggerFactory);
});
Expand Down
73 changes: 57 additions & 16 deletions src/Lantern.Beacon/BeaconClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public async Task InitAsync()
LocalPeer.Address.ReplaceOrAdd<TCP>(identityManager.Record.GetEntry<EntryTcp>(EnrEntryKey.Tcp).Value);
LocalPeer.Address.ReplaceOrAdd<P2P>(identityManager.Record.ToPeerId());

if (clientOptions.Bootnodes.Length > 0)
if (clientOptions.Bootnodes.Count > 0)
{
foreach (var bootnode in clientOptions.Bootnodes)
{
var peerAddress = Multiaddress.Decode(bootnode);
_peersToDial.Enqueue(peerAddress);
}

_logger.LogInformation("Added {Count} bootnodes for dialing", clientOptions.Bootnodes.Length);
_logger.LogInformation("Added {Count} bootnodes for dialing", clientOptions.Bootnodes.Count);
}

var storedPeers = liteDbService.FetchAll<MultiAddressStore>(nameof(MultiAddressStore));
Expand Down Expand Up @@ -386,7 +386,7 @@ private async Task MonitorSyncStatus(CancellationToken token)
{
var peer = peerState.BootstrapPeers.Values.ElementAt(
new Random().Next(peerState.BootstrapPeers.Count));

if (!syncProtocol.IsInitialised)
{
_logger.LogInformation(
Expand All @@ -395,9 +395,12 @@ private async Task MonitorSyncStatus(CancellationToken token)
peer.Address.Get<TCP>().Value.ToString(),
peer.Address.Get<P2P>().Value.ToString());

await peer.DialAsync<LightClientBootstrapProtocol>(token);
var dialTask = peer.DialAsync<LightClientBootstrapProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}

if (!syncProtocol.IsInitialised)
{
_logger.LogWarning(
Expand All @@ -407,7 +410,11 @@ private async Task MonitorSyncStatus(CancellationToken token)
peer.Address.Get<P2P>().Value.ToString());

peerState.BootstrapPeers.TryRemove(peer.Address.GetPeerId()!, out _);
await peer.DialAsync<GoodbyeProtocol>(token);

var goodbyeTask = peer.DialAsync<GoodbyeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(goodbyeTask, timeoutTask);
}
else
{
Expand Down Expand Up @@ -447,7 +454,7 @@ private async Task MonitorSyncStatus(CancellationToken token)
}
}
}

private async Task SyncDenebForkAsync(IRemotePeer peer, CancellationToken token = default)
{
if (!clientOptions.GossipSubEnabled)
Expand Down Expand Up @@ -475,7 +482,10 @@ private async Task SyncDenebForkAsync(IRemotePeer peer, CancellationToken token
denebOptimisticPeriod
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}

if (denebFinalizedPeriod + 1 < denebCurrentPeriod)
Expand All @@ -491,10 +501,13 @@ private async Task SyncDenebForkAsync(IRemotePeer peer, CancellationToken token
count
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}
}

private async Task SyncCapellaForkAsync(IRemotePeer peer, CancellationToken token = default)
{
if (!clientOptions.GossipSubEnabled)
Expand Down Expand Up @@ -529,7 +542,10 @@ private async Task SyncCapellaForkAsync(IRemotePeer peer, CancellationToken toke
capellaOptimisticPeriod
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}

if (capellaFinalizedPeriod + 1 < capellaCurrentPeriod)
Expand All @@ -546,7 +562,10 @@ private async Task SyncCapellaForkAsync(IRemotePeer peer, CancellationToken toke
count
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}
}

Expand Down Expand Up @@ -584,7 +603,10 @@ private async Task SyncAltairForkAsync(IRemotePeer peer, CancellationToken token
altairOptimisticPeriod
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}

if (altairFinalizedPeriod + 1 < altairCurrentPeriod)
Expand All @@ -601,7 +623,10 @@ private async Task SyncAltairForkAsync(IRemotePeer peer, CancellationToken token
count
);

await peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var dialTask = peer.DialAsync<LightClientUpdatesByRangeProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);

await Task.WhenAny(dialTask, timeoutTask);
}
}

Expand Down Expand Up @@ -643,7 +668,15 @@ private async Task RunOptimisticUpdateLoopAsync(IRemotePeer peer, CancellationTo

_logger.LogInformation("Requesting optimistic update...");

await peer.DialAsync<LightClientOptimisticUpdateProtocol>(token);
var dialTask = peer.DialAsync<LightClientOptimisticUpdateProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);
var completedTask = await Task.WhenAny(dialTask, timeoutTask);

if (completedTask == timeoutTask)
{
break;
}

await Task.Delay(Config.SecondsPerSlot * 1000, token);
}
catch (TaskCanceledException)
Expand Down Expand Up @@ -695,7 +728,15 @@ private async Task RunFinalityUpdateLoopAsync(IRemotePeer peer, CancellationToke

_logger.LogInformation("Requesting finality update...");

await peer.DialAsync<LightClientFinalityUpdateProtocol>(token);
var dialTask = peer.DialAsync<LightClientFinalityUpdateProtocol>(token);
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(clientOptions.DialTimeoutSeconds), token);
var completedTask = await Task.WhenAny(dialTask, timeoutTask);

if (completedTask == timeoutTask)
{
break;
}

await Task.Delay(Config.SecondsPerSlot * 32 * 1000, token);
}
catch (TaskCanceledException)
Expand Down
Loading

0 comments on commit 62ec39e

Please sign in to comment.