diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 4836e7da89..c92816f604 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -433,28 +433,36 @@ private void Persist(Block block) Context.System.EventStream.Publish(application_executed); all_application_executed.Add(application_executed); } - 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 + if (plugin.CurrentHeight < block.Index) { - plugin.OnCommit(system, block, snapshot); + plugin.OnPersist(system, block, snapshot, all_application_executed); } - catch (Exception ex) + } + List commitExceptions = null; + foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins) + { + 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 1d01e8b0a9..1288ac8e8f 100644 --- a/src/neo/Plugins/IPersistencePlugin.cs +++ b/src/neo/Plugins/IPersistencePlugin.cs @@ -21,6 +21,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]