From 427dad73c4cc968833767e9132abd9fc362354e6 Mon Sep 17 00:00:00 2001 From: uink45 <79078981+uink45@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:04:40 +1000 Subject: [PATCH] Update directory path; --- src/Lantern.Beacon.Console/Program.cs | 13 ------- src/Lantern.Beacon.Sync/SyncProtocol.cs | 6 +-- src/Lantern.Beacon/BeaconClientManager.cs | 6 +-- .../BeaconClientServiceBuilder.cs | 4 +- .../Discovery/CustomDiscoveryProtocol.cs | 2 +- ...Settings.cs => LanternIdentitySettings.cs} | 2 +- .../CustomPubsub/CustomPubsubRouter.cs | 3 +- src/Lantern.Beacon/Storage/LiteDbService.cs | 18 +++++++-- .../LiteDbServiceTests.cs | 38 ++++--------------- 9 files changed, 33 insertions(+), 59 deletions(-) rename src/Lantern.Beacon/Networking/{LanternIdentifyProtocolSettings.cs => LanternIdentitySettings.cs} (74%) diff --git a/src/Lantern.Beacon.Console/Program.cs b/src/Lantern.Beacon.Console/Program.cs index 4b6737c..ea4512a 100644 --- a/src/Lantern.Beacon.Console/Program.cs +++ b/src/Lantern.Beacon.Console/Program.cs @@ -138,16 +138,3 @@ public static async Task Main() } } } - -// 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]" -// })); -// }); \ No newline at end of file diff --git a/src/Lantern.Beacon.Sync/SyncProtocol.cs b/src/Lantern.Beacon.Sync/SyncProtocol.cs index d03ba6a..ad6f43c 100644 --- a/src/Lantern.Beacon.Sync/SyncProtocol.cs +++ b/src/Lantern.Beacon.Sync/SyncProtocol.cs @@ -71,21 +71,21 @@ public void Init(AltairLightClientStore? altairStore, AltairLightClientStore = altairStore; IsInitialised = true; SetActiveFork(ForkType.Altair); - Logger?.LogInformation("Light client store initialised"); + Logger?.LogInformation("Initialised light client from local storage"); } else if(capellaStore != null) { CapellaLightClientStore = capellaStore; IsInitialised = true; SetActiveFork(ForkType.Capella); - Logger?.LogInformation("Light client store initialised"); + Logger?.LogInformation("Initialised light client from local storage"); } else if(denebStore != null) { DenebLightClientStore = denebStore; IsInitialised = true; SetActiveFork(ForkType.Deneb); - Logger?.LogInformation("Light client store initialised"); + Logger?.LogInformation("Initialised light client from local storage"); } if(finalityUpdate != null) diff --git a/src/Lantern.Beacon/BeaconClientManager.cs b/src/Lantern.Beacon/BeaconClientManager.cs index c561a7d..ac91067 100644 --- a/src/Lantern.Beacon/BeaconClientManager.cs +++ b/src/Lantern.Beacon/BeaconClientManager.cs @@ -82,12 +82,12 @@ public async Task InitAsync() if (peers > 0) { - _logger.LogInformation("Added {Count} stored peers for dialing", peers); + _logger.LogInformation("Added {Count} peers for dialing from local storage", peers); } customDiscoveryProtocol.OnAddPeer = HandleDiscoveredPeer; - _logger.LogInformation("Client started with address {Address}", LocalPeer.Address); + _logger.LogInformation("Client started with Libp2p address {Address}", LocalPeer.Address); } catch (Exception e) { @@ -99,7 +99,7 @@ public async Task StartAsync(CancellationToken token = default) { if (LocalPeer == null) { - throw new Exception("Local peer is not initialized. Cannot start peer manager"); + throw new Exception("Local peer is not initialised. Cannot start peer manager"); } CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token); diff --git a/src/Lantern.Beacon/BeaconClientServiceBuilder.cs b/src/Lantern.Beacon/BeaconClientServiceBuilder.cs index 3783ce3..b5ca7f9 100644 --- a/src/Lantern.Beacon/BeaconClientServiceBuilder.cs +++ b/src/Lantern.Beacon/BeaconClientServiceBuilder.cs @@ -33,8 +33,8 @@ public IBeaconClientServiceBuilder AddLibp2pProtocol(Func() .AddScoped(_ => new IdentifyProtocolSettings { - AgentVersion = LanternIdentifyProtocolSettings.AgentVersion, - ProtocolVersion = LanternIdentifyProtocolSettings.ProtocolVersion + AgentVersion = LanternIdentitySettings.AgentVersion, + ProtocolVersion = LanternIdentitySettings.ProtocolVersion }) .AddScoped(sp => sp.GetService()!.Build()); diff --git a/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs b/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs index ff452e1..1cc430b 100644 --- a/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs +++ b/src/Lantern.Beacon/Networking/Discovery/CustomDiscoveryProtocol.cs @@ -31,7 +31,7 @@ public async Task InitAsync() identityManager.Record.UpdateEntry(new EntryEth2(enrForkId)); - _logger.LogInformation("Self ENR updated => {Enr}", identityManager.Record); + _logger.LogInformation("Local ENR => {Enr}", identityManager.Record); return true; } diff --git a/src/Lantern.Beacon/Networking/LanternIdentifyProtocolSettings.cs b/src/Lantern.Beacon/Networking/LanternIdentitySettings.cs similarity index 74% rename from src/Lantern.Beacon/Networking/LanternIdentifyProtocolSettings.cs rename to src/Lantern.Beacon/Networking/LanternIdentitySettings.cs index fb202b0..18e8593 100644 --- a/src/Lantern.Beacon/Networking/LanternIdentifyProtocolSettings.cs +++ b/src/Lantern.Beacon/Networking/LanternIdentitySettings.cs @@ -1,6 +1,6 @@ namespace Lantern.Beacon.Networking; -public static class LanternIdentifyProtocolSettings +public static class LanternIdentitySettings { public const string AgentVersion = "lantern/1.0.0"; public const string ProtocolVersion = "eth2/1.0.0"; diff --git a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs index f80dd6c..f3d479f 100644 --- a/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs +++ b/src/Lantern.Beacon/Networking/Libp2pProtocols/CustomPubsub/CustomPubsubRouter.cs @@ -195,8 +195,7 @@ public async Task RunAsync(ILocalPeer localPeer, IDiscoveryProtocol discoveryPro private async Task StartDiscoveryAsync(IDiscoveryProtocol discoveryProtocol, CancellationToken token = default) { ArgumentNullException.ThrowIfNull(localPeer); - - ObservableCollection col = []; + discoveryProtocol.OnAddPeer = (addrs) => { _ = Task.Run(async () => diff --git a/src/Lantern.Beacon/Storage/LiteDbService.cs b/src/Lantern.Beacon/Storage/LiteDbService.cs index 5fe7987..efbc7c9 100644 --- a/src/Lantern.Beacon/Storage/LiteDbService.cs +++ b/src/Lantern.Beacon/Storage/LiteDbService.cs @@ -1,10 +1,11 @@ using LiteDB; using System.Linq.Expressions; +using Lantern.Beacon.Sync; using Microsoft.Extensions.Logging; namespace Lantern.Beacon.Storage; -public sealed class LiteDbService(BeaconClientOptions options, ILoggerFactory loggerFactory) : ILiteDbService, IDisposable +public sealed class LiteDbService(BeaconClientOptions beaconClientOptions, SyncProtocolOptions syncProtocolOptions, ILoggerFactory loggerFactory) : ILiteDbService, IDisposable { private LiteDatabase? _liteDatabase; private readonly object _lock = new(); @@ -19,15 +20,24 @@ public void Init() throw new InvalidOperationException("LiteDbService already initialized"); } - var directoryPath = Path.GetDirectoryName(options.DataDirectoryPath); + if(beaconClientOptions.DataDirectoryPath == null) + { + throw new ArgumentNullException(nameof(beaconClientOptions.DataDirectoryPath)); + } + + var directoryPath = Path.Combine( + Path.GetDirectoryName(beaconClientOptions.DataDirectoryPath)!, + syncProtocolOptions.Network.ToString(), + Path.GetFileName(beaconClientOptions.DataDirectoryPath) + ); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } - _liteDatabase = new LiteDatabase(options.DataDirectoryPath); - _logger.LogInformation("Data directory initialized with path: {Path}", options.DataDirectoryPath); + _liteDatabase = new LiteDatabase(beaconClientOptions.DataDirectoryPath); + _logger.LogInformation("Using data directory with path: {Path}", directoryPath); } } diff --git a/test/Lantern.Beacon.Tests/LiteDbServiceTests.cs b/test/Lantern.Beacon.Tests/LiteDbServiceTests.cs index 737ec14..cf73758 100644 --- a/test/Lantern.Beacon.Tests/LiteDbServiceTests.cs +++ b/test/Lantern.Beacon.Tests/LiteDbServiceTests.cs @@ -1,5 +1,7 @@ using Lantern.Beacon.Networking; using Lantern.Beacon.Storage; +using Lantern.Beacon.Sync; +using Lantern.Beacon.Sync.Types; using Lantern.Beacon.Sync.Types.Ssz.Altair; using Lantern.Beacon.Sync.Types.Ssz.Deneb; using Lantern.Beacon.Sync.Types.Ssz.Phase0; @@ -14,6 +16,7 @@ public class LiteDbServiceTests { private LiteDbService _liteDbService; private BeaconClientOptions _beaconClientOptions; + private SyncProtocolOptions _syncProtocolOptions; private LoggerFactory _loggerFactory; private string _testDirectoryPath; @@ -33,38 +36,13 @@ public void Setup() { DataDirectoryPath = Path.Combine(_testDirectoryPath, "test.db") }; + _syncProtocolOptions = new SyncProtocolOptions + { + Network = NetworkType.Mainnet + }; _loggerFactory = new LoggerFactory(); - _liteDbService = new LiteDbService(_beaconClientOptions, _loggerFactory); - } - - [Test] - public void Test() - { - // _liteDbService.Init(); - // - // var one = new MultiAddressStore(Multiaddress.Decode("/ip4/45.139.159.163/tcp/9010/p2p/16Uiu2HAmDUkkJabcfDgZiz4keDN9nJMETJSWhhanLLawQ3gyYre9").ToString()); - // var two = new MultiAddressStore(Multiaddress.Decode("/ip4/0.0.0.0/tcp/9005/p2p/16Uiu2HAmHLWaGqjTvSbunNkS9jry4jvFJ8AwiyXPK87JJu3V3Adb").ToString()); - // - // _liteDbService.Store(nameof(MultiAddressStore), one); - // _liteDbService.Store(nameof(MultiAddressStore), two); - // - // var storedMultiAddresses = _liteDbService.FetchAll(nameof(MultiAddressStore)); - // - // foreach (var address in storedMultiAddresses) - // { - // Console.WriteLine(address.MultiAddress); - // } - // - // _liteDbService.RemoveByPredicate(nameof(MultiAddressStore), x => x.MultiAddress.Equals(two.MultiAddress)); - // - // Console.WriteLine("\nAfter removal"); - // var storedMultiAddressesAfterRemoval = _liteDbService.FetchAll(nameof(MultiAddressStore)); - // - // foreach (var address in storedMultiAddressesAfterRemoval) - // { - // Console.WriteLine(address.MultiAddress); - // } + _liteDbService = new LiteDbService(_beaconClientOptions, _syncProtocolOptions, _loggerFactory); } [Test]