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

Dev #15

Merged
merged 2 commits into from
Jul 25, 2024
Merged

Dev #15

Show file tree
Hide file tree
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
67 changes: 67 additions & 0 deletions src/Lantern.Beacon.Console/CustomConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.Extensions.Logging;

namespace Lantern.Beacon.Console;

public class CustomConsoleLogger(
string name,
Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> filter,
CustomConsoleLogger.CustomConsoleLoggerConfiguration config)
: ILogger
{
private readonly string _name = name ?? throw new ArgumentNullException(nameof(name));
private readonly Func<CustomConsoleLoggerConfiguration, bool> _filter = filter ?? throw new ArgumentNullException(nameof(filter));
private readonly CustomConsoleLoggerConfiguration _config = config ?? throw new ArgumentNullException(nameof(config));

public IDisposable BeginScope<TState>(TState state) => null;

public bool IsEnabled(LogLevel logLevel) => _filter(_config);

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel)) return;

if (_config.EventId == 0 || _config.EventId == eventId.Id)
{
var timestamp = DateTime.UtcNow.ToString(_config.TimestampPrefix);
var logLevelString = GetLogLevelString(logLevel);
var logMessage = RemoveCategoryAndId(formatter(state, exception));
System.Console.WriteLine($"{timestamp} [{logLevelString}] {logMessage}");
}
}

private static string RemoveCategoryAndId(string message)
{
// Logic to remove the category and eventId from the message
// Assuming the category will be of the pattern 'Category[EventId]: message'
var index = message.IndexOf(']');

if (index != -1 && message.Length > index + 1)
{
message = message[(index + 2)..];
}

return message;
}

private string GetLogLevelString(LogLevel logLevel)
{
// Convert log level to custom string representation
return logLevel switch
{
LogLevel.Trace => "Trace",
LogLevel.Debug => "Debug",
LogLevel.Information => "Info",
LogLevel.Warning => "Warning",
LogLevel.Error => "Error",
LogLevel.Critical => "Critical",
LogLevel.None => "None",
_ => "Unknown"
};
}

public class CustomConsoleLoggerConfiguration
{
public int EventId { get; set; }
public string TimestampPrefix { get; set; }
}
}
24 changes: 24 additions & 0 deletions src/Lantern.Beacon.Console/CustomConsoleLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.Logging;

namespace Lantern.Beacon.Console;

public class CustomConsoleLoggerProvider : ILoggerProvider
{
private readonly CustomConsoleLogger.CustomConsoleLoggerConfiguration _config;
private readonly Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> _filter;

public CustomConsoleLoggerProvider(Func<CustomConsoleLogger.CustomConsoleLoggerConfiguration, bool> filter, CustomConsoleLogger.CustomConsoleLoggerConfiguration config)
{
_filter = filter;
_config = config;
}

public ILogger CreateLogger(string categoryName)
{
return new CustomConsoleLogger(categoryName, _filter, _config);
}

public void Dispose()
{
}
}
21 changes: 18 additions & 3 deletions src/Lantern.Beacon.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static async Task Main()
var libp2p2LoggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Debug)
.SetMinimumLevel(LogLevel.Information)
// .AddFilter((category, level) =>
// {
// if (category.StartsWith("Nethermind.Libp2p.Protocols.Pubsub.GossipsubProtocolV11") || category.StartsWith("Nethermind.Libp2p.Protocols.Pubsub.GossipsubProtocol"))
Expand All @@ -131,6 +131,18 @@ public static async Task Main()
l.UseUtcTimestamp = true;
});
});
// var libp2p2LoggerFactory = LoggerFactory.Create(builder =>
// {
// builder
// .SetMinimumLevel(LogLevel.Information)
// .AddProvider(new CustomConsoleLoggerProvider(
// config => config.EventId == 0,
// new CustomConsoleLogger.CustomConsoleLoggerConfiguration
// {
// EventId = 0,
// TimestampPrefix = "[HH:mm:ss]"
// }));
// });
services.AddBeaconClient(beaconClientBuilder =>
{
beaconClientBuilder.AddDiscoveryProtocol(discv5Builder =>
Expand All @@ -144,7 +156,10 @@ public static async Task Main()
beaconClientBuilder.WithBeaconClientOptions(options =>
{
options.TcpPort = 9005;
options.EnableDiscovery = false;
options.EnableDiscovery = true;
//options.Bootnodes = ["/ip4/50.195.130.74/tcp/9000/p2p/16Uiu2HAkvSLFzPogiUZn1wFEskrUoJt9DGot3PbfeSE5zHqS32FM"];
//options.Bootnodes = ["/ip4/73.186.232.187/tcp/9105/p2p/16Uiu2HAm37UA7fk8r2AnYtGLbddwkS2WEeSPTsjNDGh3gDW7VUBQ"]; // Teku
//options.Bootnodes = ["/ip4/69.175.102.62/tcp/31018/p2p/16Uiu2HAm2FWXMoKEsshxjXNsXmFwxPAm4eaWmcffFTGgNs3gi4Ww"]; // Erigon
//options.Bootnodes = ["/ip4/0.0.0.0/tcp/9000/p2p/16Uiu2HAm6R996q426GYUyExKSYdKxhbD5iYedbuqQovVPTJFVHPv"];
//options.Bootnodes = ["/ip4/135.148.103.80/tcp/9000/p2p/16Uiu2HAkwvVXtZj6u3R2F7hEXpnbDUom3rDepABdDCSzyzAM2k69"];
//options.Bootnodes = ["/ip4/54.38.80.34/tcp/9000/p2p/16Uiu2HAm8t1aQArVwrJ9fwHRGXL2sXumPGTvmsne14piPaFJ5FYi"]; // Lighthouse
Expand All @@ -158,7 +173,7 @@ public static async Task Main()
syncProtocol.Preset = SizePreset.MainnetPreset;
syncProtocol.GenesisValidatorsRoot = Convert.FromHexString("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95");
syncProtocol.GenesisTime = 1606824023;
syncProtocol.TrustedBlockRoot = Convert.FromHexString("0185a0716c173da858a048bfe9581c9329cd267fd086eb1f314166e503ec4a90");
syncProtocol.TrustedBlockRoot = Convert.FromHexString("d3045825aa880bee480fb638f164e49bd887a599b3b89f37ea6bfc4c4f7aadd3");
});
beaconClientBuilder.AddLibp2pProtocol(libp2PBuilder => libp2PBuilder);
beaconClientBuilder.WithLoggerFactory(libp2p2LoggerFactory);
Expand Down
93 changes: 8 additions & 85 deletions src/Lantern.Beacon/BeaconClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public async Task InitAsync(CancellationToken token = default)
try
{
liteDbService.Init();
InitialiseSyncProtocol();

var altairStore = liteDbService.Fetch<AltairLightClientStore>(nameof(AltairLightClientStore));
var capellaStore = liteDbService.Fetch<CapellaLightClientStore>(nameof(CapellaLightClientStore));
var denebStore = liteDbService.Fetch<DenebLightClientStore>(nameof(DenebLightClientStore));
var finalityUpdate = liteDbService.Fetch<DenebLightClientFinalityUpdate>(nameof(DenebLightClientFinalityUpdate));
var optimisticUpdate = liteDbService.Fetch<DenebLightClientOptimisticUpdate>(nameof(DenebLightClientOptimisticUpdate));

syncProtocol.Init(altairStore, capellaStore, denebStore, finalityUpdate, optimisticUpdate);
peerState.Init(peerFactoryBuilder.AppLayerProtocols);
gossipSubManager.Init();

Expand All @@ -33,9 +40,6 @@ public async Task InitAsync(CancellationToken token = default)
return;
}

gossipSubManager.LightClientFinalityUpdate.OnMessage += HandleLightClientFinalityUpdate;
gossipSubManager.LightClientOptimisticUpdate.OnMessage += HandleLightClientOptimisticUpdate;

await beaconClientManager.InitAsync(token);
}
catch (Exception e)
Expand Down Expand Up @@ -65,85 +69,4 @@ public async Task StopAsync()
await beaconClientManager.StopAsync();
liteDbService.Dispose();
}

private void HandleLightClientFinalityUpdate(byte[] update)
{
var denebFinalizedPeriod = AltairHelpers.ComputeSyncCommitteePeriod(Phase0Helpers.ComputeEpochAtSlot(syncProtocol.DenebLightClientStore.FinalizedHeader.Beacon.Slot));
var denebCurrentPeriod = AltairHelpers.ComputeSyncCommitteePeriod(Phase0Helpers.ComputeEpochAtSlot(Phase0Helpers.ComputeCurrentSlot(syncProtocol.Options.GenesisTime)));
var decompressedData = Snappy.Decode(update);
var currentSlot = Phase0Helpers.ComputeCurrentSlot(syncProtocol.Options.GenesisTime);
var lightClientFinalityUpdate = DenebLightClientFinalityUpdate.Deserialize(decompressedData, syncProtocol.Options.Preset);

_logger.LogInformation("Received light client finality update from gossip");

if (denebFinalizedPeriod + 1 < denebCurrentPeriod)
return;

var oldFinalizedHeader = syncProtocol.DenebLightClientStore.FinalizedHeader;
var result = DenebProcessors.ProcessLightClientFinalityUpdate(syncProtocol.DenebLightClientStore, lightClientFinalityUpdate, currentSlot, syncProtocol.Options, syncProtocol.Logger);
_logger.LogInformation("Processed light client finality update from gossip");

if (result)
{
if (!DenebHelpers.ShouldForwardFinalizedLightClientUpdate(lightClientFinalityUpdate, oldFinalizedHeader, syncProtocol))
return;

gossipSubManager.LightClientFinalityUpdate!.Publish(update);
_logger.LogInformation("Forwarded light client finality update to gossip");

syncProtocol.CurrentLightClientFinalityUpdate = lightClientFinalityUpdate;
liteDbService.Store(nameof(DenebLightClientFinalityUpdate), lightClientFinalityUpdate);
liteDbService.StoreOrUpdate(nameof(DenebLightClientStore), syncProtocol.DenebLightClientStore);
}
else
{
_logger.LogWarning("Failed to process light client finality update from gossip. Ignoring...");
}
}

private void HandleLightClientOptimisticUpdate(byte[] update)
{
var denebFinalizedPeriod = AltairHelpers.ComputeSyncCommitteePeriod(Phase0Helpers.ComputeEpochAtSlot(syncProtocol.DenebLightClientStore.FinalizedHeader.Beacon.Slot));
var denebCurrentPeriod = AltairHelpers.ComputeSyncCommitteePeriod(Phase0Helpers.ComputeEpochAtSlot(Phase0Helpers.ComputeCurrentSlot(syncProtocol.Options.GenesisTime)));
var decompressedData = Snappy.Decode(update);
var currentSlot = Phase0Helpers.ComputeCurrentSlot(syncProtocol.Options.GenesisTime);
var lightClientOptimisticUpdate = DenebLightClientOptimisticUpdate.Deserialize(decompressedData, syncProtocol.Options.Preset);

_logger.LogInformation("Received light client optimistic update from gossip");

if (denebFinalizedPeriod + 1 < denebCurrentPeriod)
return;

var oldOptimisticHeader = syncProtocol.DenebLightClientStore.OptimisticHeader;
var result = DenebProcessors.ProcessLightClientOptimisticUpdate(syncProtocol.DenebLightClientStore, lightClientOptimisticUpdate, currentSlot, syncProtocol.Options, syncProtocol.Logger);

if (result)
{
_logger.LogInformation("Processed light client optimistic update from gossip");
if (!DenebHelpers.ShouldForwardLightClientOptimisticUpdate(lightClientOptimisticUpdate, oldOptimisticHeader, syncProtocol))
return;

gossipSubManager.LightClientFinalityUpdate!.Publish(update);
_logger.LogInformation("Forwarded light client optimistic update to gossip");

syncProtocol.CurrentLightClientOptimisticUpdate = lightClientOptimisticUpdate;
liteDbService.Store(nameof(DenebLightClientOptimisticUpdate), lightClientOptimisticUpdate);
liteDbService.StoreOrUpdate(nameof(DenebLightClientStore), syncProtocol.DenebLightClientStore);
}
else
{
_logger.LogWarning("Failed to process light client optimistic update from gossip. Ignoring...");
}
}

private void InitialiseSyncProtocol()
{
var altairStore = liteDbService.Fetch<AltairLightClientStore>(nameof(AltairLightClientStore));
var capellaStore = liteDbService.Fetch<CapellaLightClientStore>(nameof(CapellaLightClientStore));
var denebStore = liteDbService.Fetch<DenebLightClientStore>(nameof(DenebLightClientStore));
var finalityUpdate = liteDbService.Fetch<DenebLightClientFinalityUpdate>(nameof(DenebLightClientFinalityUpdate));
var optimisticUpdate = liteDbService.Fetch<DenebLightClientOptimisticUpdate>(nameof(DenebLightClientOptimisticUpdate));

syncProtocol.Init(altairStore, capellaStore, denebStore, finalityUpdate, optimisticUpdate);
}
}
4 changes: 1 addition & 3 deletions src/Lantern.Beacon/BeaconClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public async Task InitAsync(CancellationToken token = default)
var identity = new Identity();

LocalPeer = peerFactory.Create(identity);

LocalPeer.Address.ReplaceOrAdd<TCP>(identityManager.Record.GetEntry<EntryTcp>(EnrEntryKey.Tcp).Value);
LocalPeer.Address.ReplaceOrAdd<P2P>(identityManager.Record.ToPeerId());

Expand Down Expand Up @@ -271,7 +270,6 @@ private async Task DialPeer(Multiaddress? peer, CancellationToken token = defaul
return;
}

// Make it so that if not the light client is not initialised then if the peer supports bootstrap protocol at least then still connect to it
var supportsLightClientProtocols = LightClientProtocols.All.All(protocol => peerProtocols!.Contains(protocol));

if (supportsLightClientProtocols)
Expand Down Expand Up @@ -321,7 +319,7 @@ private async Task RunSyncProtocolAsync(IRemotePeer peer, CancellationToken toke
}
else
{
_logger.LogInformation("Successfully initialised light client. Starting sync");
_logger.LogInformation("Successfully initialised light client. Started syncing");
var activeFork = syncProtocol.ActiveFork;

switch (activeFork)
Expand Down
2 changes: 1 addition & 1 deletion src/Lantern.Beacon/BeaconClientPeerFactoryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override ProtocolStack BuildStack()
.Over<MultistreamProtocol>()
.Over<NoiseProtocol>()
.Over<MultistreamProtocol>()
.Over<MplexProtocol>();//.Or<MplexProtocol>();
.Over<MplexProtocol>();

return
Over<MultiaddressBasedSelectorProtocol>()
Expand Down
Loading
Loading