Skip to content

Commit

Permalink
Allow persistence plugins to commit as a final step. (#568)
Browse files Browse the repository at this point in the history
* Allow persistence plugins to commit as a final step.

* Plugins commit before core commits, once all plugins have handled initial work OnPersist.

* Allow PersistencePlugin to determine whether to crash if commit fails.

* Add ShouldThrowExceptionFromCommit method to IPersistencePlugin.

* Throw all commit exceptions that should be thrown in an AggregateException.
  • Loading branch information
jsolman authored and shargon committed Jan 23, 2019
1 parent 7f6e552 commit bbbc0cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
19 changes: 19 additions & 0 deletions neo/Ledger/Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,25 @@ private void Persist(Block block)
foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins)
plugin.OnPersist(snapshot, all_application_executed);
snapshot.Commit();
List<Exception> commitExceptions = null;
foreach (IPersistencePlugin plugin in Plugin.PersistencePlugins)
{
try
{
plugin.OnCommit(snapshot);
}
catch (Exception ex)
{
if (plugin.ShouldThrowExceptionFromCommit(ex))
{
if (commitExceptions == null)
commitExceptions = new List<Exception>();

commitExceptions.Add(ex);
}
}
}
if (commitExceptions != null) throw new AggregateException(commitExceptions);
}
UpdateCurrentSnapshot();
OnPersistCompleted(block);
Expand Down
5 changes: 4 additions & 1 deletion neo/Plugins/IPersistencePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Neo.Persistence;
using System;
using Neo.Persistence;
using System.Collections.Generic;
using static Neo.Ledger.Blockchain;

Expand All @@ -7,5 +8,7 @@ namespace Neo.Plugins
public interface IPersistencePlugin
{
void OnPersist(Snapshot snapshot, IReadOnlyList<ApplicationExecuted> applicationExecutedList);
void OnCommit(Snapshot snapshot);
bool ShouldThrowExceptionFromCommit(Exception ex);
}
}

0 comments on commit bbbc0cf

Please sign in to comment.