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

Faster #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions MithrilShards.sln
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoiseProtocol", "src\Lightn
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoiseProtocol.Test", "src\Lightning\NoiseProtocol.Test\NoiseProtocol.Test.csproj", "{DF21CC3F-5433-4B5E-915D-805013DEE09D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "src\Lightning\Repository\Repository.csproj", "{AC7A7393-CEA5-4312-95E6-AE48648135AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository.Test", "src\Lightning\Repository.Test\Repository.Test.csproj", "{7C034C66-EE78-4A7C-872F-8F4F125261FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -191,6 +195,14 @@ Global
{DF98E5FE-19B1-4177-A2C6-9F75CF888421}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF98E5FE-19B1-4177-A2C6-9F75CF888421}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF98E5FE-19B1-4177-A2C6-9F75CF888421}.Release|Any CPU.Build.0 = Release|Any CPU
{AC7A7393-CEA5-4312-95E6-AE48648135AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC7A7393-CEA5-4312-95E6-AE48648135AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC7A7393-CEA5-4312-95E6-AE48648135AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC7A7393-CEA5-4312-95E6-AE48648135AA}.Release|Any CPU.Build.0 = Release|Any CPU
{7C034C66-EE78-4A7C-872F-8F4F125261FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C034C66-EE78-4A7C-872F-8F4F125261FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C034C66-EE78-4A7C-872F-8F4F125261FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C034C66-EE78-4A7C-872F-8F4F125261FA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -218,6 +230,8 @@ Global
{DF21CC3F-5433-4B5E-915D-805013DEE09D} = {F51BD2C5-C053-4F4D-9B39-8565C112C989}
{DD338359-EA98-4F10-BE5D-94F02F5884B5} = {F51BD2C5-C053-4F4D-9B39-8565C112C989}
{DF98E5FE-19B1-4177-A2C6-9F75CF888421} = {F51BD2C5-C053-4F4D-9B39-8565C112C989}
{AC7A7393-CEA5-4312-95E6-AE48648135AA} = {F51BD2C5-C053-4F4D-9B39-8565C112C989}
{7C034C66-EE78-4A7C-872F-8F4F125261FA} = {F51BD2C5-C053-4F4D-9B39-8565C112C989}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5624037D-68BC-4AD5-8802-500F2ADE8DED}
Expand Down
11 changes: 6 additions & 5 deletions src/Lightning/Network/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Network.Protocol.Transport;
using Network.Settings;
using NoiseProtocol;
using Repository.IoC;

namespace Network
{
Expand All @@ -33,23 +34,23 @@ public static IForgeBuilder UseLightningNetwork(this IForgeBuilder forgeBuilder)
.AddMessageProcessors()
.AddTlvComponents()
.ReplaceServices()
.AddNoiseComponents();
.AddNoiseComponents()
.AddRepositoryRegistrations();
});

return forgeBuilder;
}

private static IServiceCollection AddNoiseComponents(this IServiceCollection services)
{
//services.AddSingleton<IHandshakeStateFactory, HandshakeStateFactory>();
services.AddSingleton<IEllipticCurveActions,EllipticCurveActions>();
services.AddTransient<IHashWithState,HashWithState>();
services.AddSingleton<NoiseProtocol.IHkdf,Hkdf>();
services.AddSingleton<IHkdf,Hkdf>();
services.AddTransient<ICipherFunction,ChaCha20Poly1305CipherFunction>();
services.AddSingleton<INoiseHashFunction,NoiseProtocol.Sha256>();
services.AddSingleton<INoiseHashFunction,Sha256>();
services.AddTransient<INoiseMessageTransformer,NoiseMessageTransformer>();
services.AddSingleton<IKeyGenerator,KeyGenerator>();
services.AddSingleton<IHandshakeProcessor, NoiseProtocol.HandshakeProcessor>();
services.AddSingleton<IHandshakeProcessor, HandshakeProcessor>();
return services;
}

Expand Down
1 change: 1 addition & 0 deletions src/Lightning/Network/Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\MithrilShards.Dev.Controller\MithrilShards.Dev.Controller.csproj" />
<ProjectReference Include="..\..\MithrilShards.Network.Bedrock\MithrilShards.Network.Bedrock.csproj" />
<ProjectReference Include="..\NoiseProtocol\NoiseProtocol.csproj" />
<ProjectReference Include="..\Repository\Repository.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions src/Lightning/Repository.Test/PersistenceStoreFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Repository.IoC;
using Xunit;

namespace Repository.Test
{
public class PersistenceStoreFactoryTests
{
private PersistenceStoreFactory _factory;

[Fact]
public void FactoryCanCreateIPersistenceStoreWithDefaultStorageConfiguration()
{
var collection = new ServiceCollection()
.AddRepositoryRegistrations();

var builder = collection.BuildServiceProvider();

_factory = builder.GetService(typeof(IPersistenceStoreFactory)) as PersistenceStoreFactory;

var store = _factory?.CreatePersistenceStore();

store.Should().NotBeNull();

(store as IDisposable)?.Dispose();
}
}
}
28 changes: 28 additions & 0 deletions src/Lightning/Repository.Test/Repository.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Repository\Repository.csproj" />
</ItemGroup>

</Project>
98 changes: 98 additions & 0 deletions src/Lightning/Repository.Test/RepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using FluentAssertions;

namespace Repository.Test
{
public class RepositoryTests
{
Repository<TestItem> _repository;

public class TestItem
{
public int Number;
public byte[] Bytes;
public string String;
}

public static TestItem RandomNewTestItem()
{
var random = new Random();

var testitem = new TestItem
{
Number = random.Next(int.MaxValue),
Bytes = new byte[random.Next(ushort.MaxValue)],
String = Guid.NewGuid().ToString()
};

random.NextBytes(testitem.Bytes);

return testitem;
}

[Fact]
public void RepositoryCanAddAndGetAnItem()
{
var testItem = RandomNewTestItem();

_repository = new Repository<TestItem>(new PersistenceStoreFactory(new NodeListStorageConfiguration()));

_repository.Add(testItem.Bytes, testItem);

var lookupItem = _repository.Get(testItem.Bytes);

lookupItem.Should()
.BeEquivalentTo(testItem);
}

[Fact]
public async Task RepositoryShouldSaveToFileOnSaveChanges()
{
var testItem = RandomNewTestItem();

var testConfiguration = new TestConfiguration();

ClearTestLogFiles();

_repository = new Repository<TestItem>(new PersistenceStoreFactory(testConfiguration));

_repository.Add(testItem.Bytes, testItem);

await _repository.SaveChangesAsync().ConfigureAwait(false);

_repository.Dispose();

string[] fileList = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(),
TestConfiguration.TEST_FILE_DIRECTORY_NAME));

fileList.Contains(testConfiguration.LogStoragePath + ".0")
.Should()
.BeTrue();

fileList.Contains(testConfiguration.ObjectLogStoragePath + ".0")
.Should()
.BeTrue();
}

static void ClearTestLogFiles()
{
string testFilesPath = Path.Combine(Directory.GetCurrentDirectory(), TestConfiguration.TEST_FILE_DIRECTORY_NAME);

if (!Directory.Exists(testFilesPath))
return;

Directory.Delete(testFilesPath,true);
}

private class TestConfiguration : IStorageConfiguration
{
public const string TEST_FILE_DIRECTORY_NAME = "TestRepo";
public string LogStoragePath => Path.Combine(Directory.GetCurrentDirectory(), $"{TEST_FILE_DIRECTORY_NAME}\\TestFile.log");
public string ObjectLogStoragePath => Path.Combine(Directory.GetCurrentDirectory(), $"{TEST_FILE_DIRECTORY_NAME}\\TestFileObj.log");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Repository.IoC;
using Xunit;

namespace Repository.Test
{
public class ServiceCollectionRegistrationTests
{
ServiceCollection _collection = new ServiceCollection();

[Fact]
public void RepositoryIsGeneratedSuccessfully()
{
_collection.AddRepositoryRegistrations();

var repository = _collection.BuildServiceProvider()
.GetService(typeof(IRepository<Test>)) as IRepository<Test>;

repository.Should().NotBeNull();
}

private class Test {}
}
}
3 changes: 3 additions & 0 deletions src/Lightning/Repository/Assembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Repository.Test")]
14 changes: 14 additions & 0 deletions src/Lightning/Repository/IPersistenceStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;

namespace Repository
{
internal interface IPersistenceStore
{
T GetById<T>(byte[] id) ;

void Add<T>(byte[] id, T item)
where T : class;

ValueTask SaveChangesAsync();
}
}
7 changes: 7 additions & 0 deletions src/Lightning/Repository/IPersistenceStoreFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Repository
{
internal interface IPersistenceStoreFactory
{
IPersistenceStore CreatePersistenceStore();
}
}
13 changes: 13 additions & 0 deletions src/Lightning/Repository/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;

namespace Repository
{
public interface IRepository<T> where T : class
{
void Add(byte[] key, T item);

T Get(byte[] key);

ValueTask SaveChangesAsync();
}
}
9 changes: 9 additions & 0 deletions src/Lightning/Repository/IStorageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Repository
{
public interface IStorageConfiguration
{
string LogStoragePath { get; }

string ObjectLogStoragePath { get; }
}
}
15 changes: 15 additions & 0 deletions src/Lightning/Repository/IoC/ServiceCollectionRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Extensions.DependencyInjection;

namespace Repository.IoC
{
public static class ServiceCollectionRegistration
{
public static IServiceCollection AddRepositoryRegistrations(this IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IPersistenceStoreFactory, PersistenceStoreFactory>();
serviceCollection.AddSingleton<IStorageConfiguration, NodeListStorageConfiguration>();
serviceCollection.AddScoped(typeof(IRepository<>), typeof(Repository<>));
return serviceCollection;
}
}
}
13 changes: 13 additions & 0 deletions src/Lightning/Repository/NodeListStorageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.IO;

namespace Repository
{
internal class NodeListStorageConfiguration : IStorageConfiguration
{
const string _logSuffix = ".log";

const string _objLogSuffix = "obj.log";
public string LogStoragePath => Path.Combine(Directory.GetCurrentDirectory(),nameof(NodeListStorageConfiguration) + _logSuffix);
public string ObjectLogStoragePath => Path.Combine(Directory.GetCurrentDirectory(),nameof(NodeListStorageConfiguration) + _objLogSuffix);
}
}
30 changes: 30 additions & 0 deletions src/Lightning/Repository/PersistenceStoreFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FASTER.core;

namespace Repository
{
internal class PersistenceStoreFactory : IPersistenceStoreFactory
{
readonly IStorageConfiguration _configuration;

public PersistenceStoreFactory(IStorageConfiguration configuration)
{
_configuration = configuration;
}

public IPersistenceStore CreatePersistenceStore()
{
var log = Devices.CreateLogDevice( _configuration.LogStoragePath, recoverDevice: true);

var objectLog = Devices.CreateLogDevice( _configuration.ObjectLogStoragePath, recoverDevice: true);

var store = new FasterKV<SpanByte, string>(1L << 5,
new LogSettings
{
LogDevice = log,
ObjectLogDevice = objectLog
});

return new SpanByteStringPersistenceStore(store); //Can be extended to
}
}
}
Loading