Skip to content

Commit

Permalink
Merge pull request #31 from Pier-Two/dev
Browse files Browse the repository at this point in the history
Update directory path;
  • Loading branch information
MPierTwo authored Oct 7, 2024
2 parents 5aeabd4 + 427dad7 commit 7b338ce
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 59 deletions.
13 changes: 0 additions & 13 deletions src/Lantern.Beacon.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
// }));
// });
6 changes: 3 additions & 3 deletions src/Lantern.Beacon.Sync/SyncProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Lantern.Beacon/BeaconClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Lantern.Beacon/BeaconClientServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public IBeaconClientServiceBuilder AddLibp2pProtocol(Func<ILibp2pPeerFactoryBuil
.AddScoped<MultiplexerSettings>()
.AddScoped<IdentifyProtocolSettings>(_ => new IdentifyProtocolSettings
{
AgentVersion = LanternIdentifyProtocolSettings.AgentVersion,
ProtocolVersion = LanternIdentifyProtocolSettings.ProtocolVersion
AgentVersion = LanternIdentitySettings.AgentVersion,
ProtocolVersion = LanternIdentitySettings.ProtocolVersion
})
.AddScoped(sp => sp.GetService<IPeerFactoryBuilder>()!.Build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task<bool> InitAsync()

identityManager.Record.UpdateEntry(new EntryEth2(enrForkId));

_logger.LogInformation("Self ENR updated => {Enr}", identityManager.Record);
_logger.LogInformation("Local ENR => {Enr}", identityManager.Record);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddress> col = [];

discoveryProtocol.OnAddPeer = (addrs) =>
{
_ = Task.Run(async () =>
Expand Down
18 changes: 14 additions & 4 deletions src/Lantern.Beacon/Storage/LiteDbService.cs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
}
}

Expand Down
38 changes: 8 additions & 30 deletions test/Lantern.Beacon.Tests/LiteDbServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +16,7 @@ public class LiteDbServiceTests
{
private LiteDbService _liteDbService;
private BeaconClientOptions _beaconClientOptions;
private SyncProtocolOptions _syncProtocolOptions;
private LoggerFactory _loggerFactory;
private string _testDirectoryPath;

Expand All @@ -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<MultiAddressStore>(nameof(MultiAddressStore));
//
// foreach (var address in storedMultiAddresses)
// {
// Console.WriteLine(address.MultiAddress);
// }
//
// _liteDbService.RemoveByPredicate<MultiAddressStore>(nameof(MultiAddressStore), x => x.MultiAddress.Equals(two.MultiAddress));
//
// Console.WriteLine("\nAfter removal");
// var storedMultiAddressesAfterRemoval = _liteDbService.FetchAll<MultiAddressStore>(nameof(MultiAddressStore));
//
// foreach (var address in storedMultiAddressesAfterRemoval)
// {
// Console.WriteLine(address.MultiAddress);
// }
_liteDbService = new LiteDbService(_beaconClientOptions, _syncProtocolOptions, _loggerFactory);
}

[Test]
Expand Down

0 comments on commit 7b338ce

Please sign in to comment.