From dd3f8e615630322d6092cad64aafdb7874f30c0e Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 28 Nov 2019 12:29:28 +0800 Subject: [PATCH 1/2] Add IStoragePlugin --- src/neo/NeoSystem.cs | 6 ++++-- src/neo/Plugins/IStoragePlugin.cs | 9 +++++++++ src/neo/Plugins/Plugin.cs | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/neo/Plugins/IStoragePlugin.cs diff --git a/src/neo/NeoSystem.cs b/src/neo/NeoSystem.cs index b1ef9b6ec8..9f1312322b 100644 --- a/src/neo/NeoSystem.cs +++ b/src/neo/NeoSystem.cs @@ -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; @@ -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)); @@ -49,6 +50,7 @@ public void Dispose() // Dispose will call ActorSystem.Terminate() ActorSystem.Dispose(); ActorSystem.WhenTerminated.Wait(); + store.Dispose(); } public void EnsureStoped(IActorRef actor) diff --git a/src/neo/Plugins/IStoragePlugin.cs b/src/neo/Plugins/IStoragePlugin.cs new file mode 100644 index 0000000000..68f75dd2bb --- /dev/null +++ b/src/neo/Plugins/IStoragePlugin.cs @@ -0,0 +1,9 @@ +using Neo.Persistence; + +namespace Neo.Plugins +{ + public interface IStoragePlugin + { + IStore GetStore(); + } +} diff --git a/src/neo/Plugins/Plugin.cs b/src/neo/Plugins/Plugin.cs index 14345d9de1..caacc17d85 100644 --- a/src/neo/Plugins/Plugin.cs +++ b/src/neo/Plugins/Plugin.cs @@ -12,6 +12,7 @@ public abstract class Plugin : IDisposable { public static readonly List Plugins = new List(); private static readonly List Loggers = new List(); + internal static readonly Dictionary Storages = new Dictionary(); internal static readonly List RpcPlugins = new List(); internal static readonly List PersistencePlugins = new List(); internal static readonly List P2PPlugins = new List(); @@ -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); From 42886389d1c0f49099199a9850354d25fce7d15c Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 28 Nov 2019 12:39:54 +0800 Subject: [PATCH 2/2] Fix UT --- tests/neo.UnitTests/Consensus/UT_Consensus.cs | 2 +- tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs | 3 ++- tests/neo.UnitTests/Ledger/UT_Blockchain.cs | 4 +--- tests/neo.UnitTests/TestBlockchain.cs | 7 +------ 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/tests/neo.UnitTests/Consensus/UT_Consensus.cs b/tests/neo.UnitTests/Consensus/UT_Consensus.cs index bc4c2277a0..68a538dcf5 100644 --- a/tests/neo.UnitTests/Consensus/UT_Consensus.cs +++ b/tests/neo.UnitTests/Consensus/UT_Consensus.cs @@ -41,7 +41,7 @@ public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart() TestProbe subscriber = CreateTestProbe(); var mockWallet = new Mock(); mockWallet.Setup(p => p.GetAccount(It.IsAny())).Returns(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[] { diff --git a/tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs b/tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs index ee5eb5f9d1..469c28d3ba 100644 --- a/tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs +++ b/tests/neo.UnitTests/Consensus/UT_ConsensusContext.cs @@ -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; @@ -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() }; diff --git a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs index 27dd15b6aa..f46036edca 100644 --- a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs @@ -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] diff --git a/tests/neo.UnitTests/TestBlockchain.cs b/tests/neo.UnitTests/TestBlockchain.cs index 6b0cdccd94..786b2e7062 100644 --- a/tests/neo.UnitTests/TestBlockchain.cs +++ b/tests/neo.UnitTests/TestBlockchain.cs @@ -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