From 7d344cfbff7b530702ab1f45de3cb7f5d1d2d337 Mon Sep 17 00:00:00 2001 From: Qiao Jin Date: Tue, 17 Aug 2021 10:50:12 +0800 Subject: [PATCH 1/2] fix IPersistencePlugin inconsistency --- src/neo/Ledger/Blockchain.cs | 23 +++++++++++++---------- src/neo/Plugins/IPersistencePlugin.cs | 5 +++++ tests/neo.UnitTests/Plugins/UT_Plugin.cs | 6 +++++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 2a02d6fb4b..4998a5cb51 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -417,26 +417,29 @@ private void Persist(Block block) } foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins) plugin.OnPersist(system, block, snapshot, all_application_executed); - snapshot.Commit(); List commitExceptions = null; foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins) { - try - { - plugin.OnCommit(system, block, snapshot); - } - catch (Exception ex) + if (plugin.CurrentHeight < block.Index) { - if (plugin.ShouldThrowExceptionFromCommit(ex)) + try { - if (commitExceptions == null) - commitExceptions = new List(); + plugin.OnCommit(system, block, snapshot); + } + catch (Exception ex) + { + if (plugin.ShouldThrowExceptionFromCommit(ex)) + { + if (commitExceptions == null) + commitExceptions = new List(); - commitExceptions.Add(ex); + commitExceptions.Add(ex); + } } } } if (commitExceptions != null) throw new AggregateException(commitExceptions); + snapshot.Commit(); system.MemPool.UpdatePoolForBlockPersisted(block, snapshot); } extensibleWitnessWhiteList = null; diff --git a/src/neo/Plugins/IPersistencePlugin.cs b/src/neo/Plugins/IPersistencePlugin.cs index 0b3efac727..ab7e914149 100644 --- a/src/neo/Plugins/IPersistencePlugin.cs +++ b/src/neo/Plugins/IPersistencePlugin.cs @@ -11,6 +11,11 @@ namespace Neo.Plugins /// public interface IPersistencePlugin { + /// + /// Indicate current persisted height. + /// + uint CurrentHeight { get; } + /// /// Called when a block is being persisted. /// diff --git a/tests/neo.UnitTests/Plugins/UT_Plugin.cs b/tests/neo.UnitTests/Plugins/UT_Plugin.cs index f194f0cff3..c17b628f8b 100644 --- a/tests/neo.UnitTests/Plugins/UT_Plugin.cs +++ b/tests/neo.UnitTests/Plugins/UT_Plugin.cs @@ -15,7 +15,10 @@ private class DummyP2PPlugin : IP2PPlugin { public void OnVerifiedInventory(IInventory inventory) { } } - private class DummyPersistencePlugin : IPersistencePlugin { } + private class DummyPersistencePlugin : IPersistencePlugin + { + public uint CurrentHeight => 0; + } [TestMethod] public void TestIP2PPlugin() @@ -35,6 +38,7 @@ public void TestIPersistencePlugin() pp.OnCommit(null, null, null); pp.OnPersist(null, null, null, null); + Assert.AreEqual(pp.CurrentHeight, 0u); } [TestMethod] From a2c713bcffeb59db73e9ec5d23ecfe016a5693b0 Mon Sep 17 00:00:00 2001 From: Qiao Jin Date: Tue, 17 Aug 2021 10:57:53 +0800 Subject: [PATCH 2/2] optimize --- src/neo/Ledger/Blockchain.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 4998a5cb51..5ecaed41a4 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -416,7 +416,12 @@ private void Persist(Block block) all_application_executed.Add(application_executed); } foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins) - plugin.OnPersist(system, block, snapshot, all_application_executed); + { + if (plugin.CurrentHeight < block.Index) + { + plugin.OnPersist(system, block, snapshot, all_application_executed); + } + } List commitExceptions = null; foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins) {