Skip to content

Commit

Permalink
Commit as the final step of persistence in StatesDumper (neo-project#40)
Browse files Browse the repository at this point in the history
* Support commit as the final step of persistence.

* Update for new method .

* OnCommitStorage
  • Loading branch information
jsolman authored and 陈志同 committed Oct 13, 2020
1 parent 196fb92 commit 8140287
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions StatesDumper/StatesDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ private void OnPersistStorage(Snapshot snapshot)
uint blockIndex = snapshot.Height;
if (blockIndex >= Settings.Default.HeightToBegin)
{
string dirPath = "./Storage";
Directory.CreateDirectory(dirPath);
string path = $"{HandlePaths(dirPath, blockIndex)}/dump-block-{blockIndex.ToString()}.json";

JArray array = new JArray();

foreach (DataCache<StorageKey, StorageItem>.Trackable trackable in snapshot.Storages.GetChangeSet())
Expand Down Expand Up @@ -121,14 +117,37 @@ private void OnPersistStorage(Snapshot snapshot)
bs_item["size"] = array.Count;
bs_item["storage"] = array;
bs_cache.Add(bs_item);
}
}

public void OnCommit(Snapshot snapshot)
{
if (Settings.Default.PersistAction.HasFlag(PersistActions.StorageChanges))
OnCommitStorage(snapshot);
}

public void OnCommitStorage(Snapshot snapshot)
{
uint blockIndex = snapshot.Height;
if (bs_cache.Count > 0)
{
if ((blockIndex % Settings.Default.BlockCacheSize == 0) || (blockIndex > Settings.Default.HeightToStartRealTimeSyncing))
{
string dirPath = "./Storage";
Directory.CreateDirectory(dirPath);
string path = $"{HandlePaths(dirPath, blockIndex)}/dump-block-{blockIndex.ToString()}.json";

File.WriteAllText(path, bs_cache.ToString());
bs_cache.Clear();
}
}
}

public bool ShouldThrowExceptionFromCommit(Exception ex)
{
Console.WriteLine($"Error writing States with StatesDumper.{Environment.NewLine}{ex}");
return true;
}

private static string HandlePaths(string dirPath, uint blockIndex)
{
Expand Down

0 comments on commit 8140287

Please sign in to comment.