Skip to content

Commit

Permalink
Fix store (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Feb 21, 2024
1 parent febcc21 commit ffa9064
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 22 deletions.
3 changes: 1 addition & 2 deletions benchmarks/Neo.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// modifications are permitted.

using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
using System.Diagnostics;
Expand All @@ -20,7 +19,7 @@ namespace Neo;
static class Benchmarks
{
private static readonly ProtocolSettings protocol = ProtocolSettings.Load("config.json");
private static readonly NeoSystem system = new(protocol, new MemoryStore());
private static readonly NeoSystem system = new(protocol, (string)null);

public static void NeoIssue2725()
{
Expand Down
18 changes: 11 additions & 7 deletions src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class NeoSystem : IDisposable

private ImmutableList<object> services = ImmutableList<object>.Empty;
private readonly IStore store;
private readonly IStoreProvider storageProvider;
private ChannelsConfig start_message = null;
private int suspend = 0;

Expand All @@ -113,22 +114,25 @@ static NeoSystem()
/// Initializes a new instance of the <see cref="NeoSystem"/> class.
/// </summary>
/// <param name="settings">The protocol settings of the <see cref="NeoSystem"/>.</param>
/// <param name="storageEngine">The storage engine used to create the <see cref="IStore"/> objects. If this parameter is <see langword="null"/>, a default in-memory storage engine will be used.</param>
/// <param name="storagePath">The path of the storage. If <paramref name="storageEngine"/> is the default in-memory storage engine, this parameter is ignored.</param>
public NeoSystem(ProtocolSettings settings, string? storageEngine = null, string? storagePath = null) : this(settings, StoreFactory.GetStore(storageEngine ?? nameof(MemoryStore), storagePath))
/// <param name="storageProvider">The storage engine used to create the <see cref="IStoreProvider"/> objects. If this parameter is <see langword="null"/>, a default in-memory storage engine will be used.</param>
/// <param name="storagePath">The path of the storage. If <paramref name="storageProvider"/> is the default in-memory storage engine, this parameter is ignored.</param>
public NeoSystem(ProtocolSettings settings, string? storageProvider = null, string? storagePath = null) :

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 119 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
this(settings, StoreFactory.GetStoreProvider(storageProvider ?? nameof(MemoryStore)), storagePath)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NeoSystem"/> class.
/// </summary>
/// <param name="settings">The protocol settings of the <see cref="NeoSystem"/>.</param>
/// <param name="storage">The <see cref="IStore"/> to use.</param>
public NeoSystem(ProtocolSettings settings, IStore storage)
/// <param name="storageProvider">The <see cref="IStoreProvider"/> to use.</param>
/// <param name="storagePath">The path of the storage. If <paramref name="storageProvider"/> is the default in-memory storage engine, this parameter is ignored.</param>
public NeoSystem(ProtocolSettings settings, IStoreProvider storageProvider, string? storagePath = null)

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 130 in src/Neo/NeoSystem.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
this.Settings = settings;
this.GenesisBlock = CreateGenesisBlock(settings);
this.store = storage;
this.storageProvider = storageProvider;
this.store = storageProvider.GetStore(storagePath);
this.MemPool = new MemoryPool(this);
this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this));
this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this));
Expand Down Expand Up @@ -225,7 +229,7 @@ public void EnsureStopped(IActorRef actor)
/// <returns>The loaded <see cref="IStore"/>.</returns>
public IStore LoadStore(string path)
{
return StoreFactory.GetStore(store.GetType().Name, path);
return storageProvider.GetStore(path);
}

/// <summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Neo/Persistence/MemoryStoreProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// MemoryStoreProvider.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

namespace Neo.Persistence
{
internal class MemoryStoreProvider : IStoreProvider
{
public string Name => nameof(MemoryStore);
public IStore GetStore(string path) => new MemoryStore();
}
}
29 changes: 19 additions & 10 deletions src/Neo/Persistence/StoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ namespace Neo.Persistence;

public static class StoreFactory
{
private class MemoryStoreProvider : IStoreProvider
{
public string Name => nameof(MemoryStore);
public IStore GetStore(string path) => new MemoryStore();
}

private static readonly Dictionary<string, IStoreProvider> providers = new();

static StoreFactory()
Expand All @@ -37,14 +31,29 @@ public static void RegisterProvider(IStoreProvider provider)
providers.Add(provider.Name, provider);
}

/// <summary>
/// Get store provider by name
/// </summary>
/// <param name="name">Name</param>
/// <returns>Store provider</returns>
public static IStoreProvider? GetStoreProvider(string name)

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 39 in src/Neo/Persistence/StoreFactory.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (providers.TryGetValue(name, out var provider))
{
return provider;
}

return null;
}

/// <summary>
/// Get store from name
/// </summary>
/// <param name="storageEngine">The storage engine used to create the <see cref="IStore"/> objects. If this parameter is <see langword="null"/>, a default in-memory storage engine will be used.</param>
/// <param name="path">The path of the storage. If <paramref name="storageEngine"/> is the default in-memory storage engine, this parameter is ignored.</param>
/// <param name="storageProvider">The storage engine used to create the <see cref="IStore"/> objects. If this parameter is <see langword="null"/>, a default in-memory storage engine will be used.</param>
/// <param name="path">The path of the storage. If <paramref name="storageProvider"/> is the default in-memory storage engine, this parameter is ignored.</param>
/// <returns>The storage engine.</returns>
public static IStore GetStore(string storageEngine, string path)
public static IStore GetStore(string storageProvider, string path)
{
return providers[storageEngine].GetStore(path);
return providers[storageProvider].GetStore(path);
}
}
4 changes: 2 additions & 2 deletions tests/Neo.UnitTests/Ledger/UT_MemoryPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void TestSetup()
TimeProvider.ResetToDefault();

// Create a MemoryPool with capacity of 100
_unit = new MemoryPool(new NeoSystem(TestProtocolSettings.Default with { MemoryPoolMaxTransactions = 100 }, new MemoryStore()));
_unit = new MemoryPool(new NeoSystem(TestProtocolSettings.Default with { MemoryPoolMaxTransactions = 100 }, storageProvider: (string)null));

// Verify capacity equals the amount specified
_unit.Capacity.Should().Be(100);
Expand Down Expand Up @@ -648,7 +648,7 @@ public void TestGetVerifiedTransactions()
[TestMethod]
public void TestReVerifyTopUnverifiedTransactionsIfNeeded()
{
_unit = new MemoryPool(new NeoSystem(TestProtocolSettings.Default with { MemoryPoolMaxTransactions = 600 }, new MemoryStore()));
_unit = new MemoryPool(new NeoSystem(TestProtocolSettings.Default with { MemoryPoolMaxTransactions = 600 }, storageProvider: (string)null));

AddTransaction(CreateTransaction(100000001));
AddTransaction(CreateTransaction(100000001));
Expand Down
6 changes: 6 additions & 0 deletions tests/Neo.UnitTests/Persistence/UT_MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace Neo.UnitTests.Persistence
[TestClass]
public class UT_MemoryStore
{
[TestMethod]
public void LoadStoreTest()
{
Assert.IsInstanceOfType<MemoryStore>(TestBlockchain.TheNeoSystem.LoadStore("abc"));
}

[TestMethod]
public void StoreTest()
{
Expand Down
9 changes: 8 additions & 1 deletion tests/Neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ public static class TestBlockchain
public static readonly UInt160[] DefaultExtensibleWitnessWhiteList;
private static readonly MemoryStore Store = new();

private class StoreProvider : IStoreProvider
{
public string Name => "TestProvider";

public IStore GetStore(string path) => Store;
}

static TestBlockchain()
{
Console.WriteLine("initialize NeoSystem");
TheNeoSystem = new NeoSystem(TestProtocolSettings.Default, Store);
TheNeoSystem = new NeoSystem(TestProtocolSettings.Default, new StoreProvider());
}

internal static void ResetStore()
Expand Down

0 comments on commit ffa9064

Please sign in to comment.