Skip to content

Commit

Permalink
New plugin interface: IStoragePlugin (neo-project#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored and Tommo-L committed Jun 22, 2020
1 parent 10e141c commit 60dfeab
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Neo.Network.P2P;
using Neo.Network.RPC;
using Neo.Persistence;
using Neo.Persistence.Memory;
using Neo.Plugins;
using Neo.Wallets;
using System;
Expand All @@ -30,10 +31,10 @@ public class NeoSystem : IDisposable
private ChannelsConfig start_message = null;
private bool suspend = false;

public NeoSystem(IStore store)
public NeoSystem(string storageEngine = null)
{
this.store = store;
Plugin.LoadPlugins(this);
this.store = storageEngine is null ? new Store() : Plugin.Storages[storageEngine].GetStore();
this.Blockchain = ActorSystem.ActorOf(Ledger.Blockchain.Props(this, store));
this.LocalNode = ActorSystem.ActorOf(Network.P2P.LocalNode.Props(this));
this.TaskManager = ActorSystem.ActorOf(Network.P2P.TaskManager.Props(this));
Expand All @@ -49,6 +50,7 @@ public void Dispose()
// Dispose will call ActorSystem.Terminate()
ActorSystem.Dispose();
ActorSystem.WhenTerminated.Wait();
store.Dispose();
}

public void EnsureStoped(IActorRef actor)
Expand Down
9 changes: 9 additions & 0 deletions src/neo/Plugins/IStoragePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Neo.Persistence;

namespace Neo.Plugins
{
public interface IStoragePlugin
{
IStore GetStore();
}
}
2 changes: 2 additions & 0 deletions src/neo/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public abstract class Plugin : IDisposable
{
public static readonly List<Plugin> Plugins = new List<Plugin>();
private static readonly List<ILogPlugin> Loggers = new List<ILogPlugin>();
internal static readonly Dictionary<string, IStoragePlugin> Storages = new Dictionary<string, IStoragePlugin>();
internal static readonly List<IRpcPlugin> RpcPlugins = new List<IRpcPlugin>();
internal static readonly List<IPersistencePlugin> PersistencePlugins = new List<IPersistencePlugin>();
internal static readonly List<IP2PPlugin> P2PPlugins = new List<IP2PPlugin>();
Expand Down Expand Up @@ -48,6 +49,7 @@ protected Plugin()
Plugins.Add(this);

if (this is ILogPlugin logger) Loggers.Add(logger);
if (this is IStoragePlugin storage) Storages.Add(Name, storage);
if (this is IP2PPlugin p2p) P2PPlugins.Add(p2p);
if (this is IRpcPlugin rpc) RpcPlugins.Add(rpc);
if (this is IPersistencePlugin persistence) PersistencePlugins.Add(persistence);
Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/Consensus/UT_Consensus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart()
TestProbe subscriber = CreateTestProbe();
var mockWallet = new Mock<Wallet>();
mockWallet.Setup(p => p.GetAccount(It.IsAny<UInt160>())).Returns<UInt160>(p => new TestWalletAccount(p));
ConsensusContext context = new ConsensusContext(mockWallet.Object, TestBlockchain.Store);
ConsensusContext context = new ConsensusContext(mockWallet.Object, Blockchain.Singleton.Store);

int timeIndex = 0;
var timeValues = new[] {
Expand Down
3 changes: 2 additions & 1 deletion tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Moq;
using Neo.Consensus;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract.Native;
using Neo.Wallets;
Expand Down Expand Up @@ -38,7 +39,7 @@ public void TestSetup()
_validatorKeys[x] = new KeyPair(pk);
}

_context = new ConsensusContext(mockWallet.Object, TestBlockchain.Store)
_context = new ConsensusContext(mockWallet.Object, Blockchain.Singleton.Store)
{
Validators = _validatorKeys.Select(u => u.PublicKey).ToArray()
};
Expand Down
4 changes: 1 addition & 3 deletions tests/neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ public static TestHeader Cast(Header input)
public class UT_Blockchain
{
private NeoSystem system;
private IStore store;
Transaction txSample = Blockchain.GenesisBlock.Transactions[0];

[TestInitialize]
public void Initialize()
{
system = TestBlockchain.TheNeoSystem;
store = TestBlockchain.Store;
Blockchain.Singleton.MemPool.TryAdd(txSample.Hash, txSample);
}

[TestMethod]
public void TestConstructor()
{
system.ActorSystem.ActorOf(Blockchain.Props(system, store)).Should().NotBeSameAs(system.Blockchain);
system.ActorSystem.ActorOf(Blockchain.Props(system, Blockchain.Singleton.Store)).Should().NotBeSameAs(system.Blockchain);
}

[TestMethod]
Expand Down
7 changes: 1 addition & 6 deletions tests/neo.UnitTests/TestBlockchain.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
using Neo.Ledger;
using Neo.Persistence;
using System;
using MemoryStore = Neo.Persistence.Memory.Store;

namespace Neo.UnitTests
{
public static class TestBlockchain
{
public static readonly IStore Store;
public static readonly NeoSystem TheNeoSystem;

static TestBlockchain()
{
Store = new MemoryStore();

Console.WriteLine("initialize NeoSystem");
TheNeoSystem = new NeoSystem(Store);
TheNeoSystem = new NeoSystem();

// Ensure that blockchain is loaded

Expand Down

0 comments on commit 60dfeab

Please sign in to comment.