-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refac statedump #789
Refac statedump #789
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,13 @@ namespace Neo.Plugins | |
{ | ||
public class StatesDumper : Plugin | ||
{ | ||
private readonly Dictionary<uint, JArray> bs_cache = new Dictionary<uint, JArray>(); | ||
private readonly Dictionary<uint, NeoSystem> systems = new Dictionary<uint, NeoSystem>(); | ||
|
||
private StreamWriter _writer; | ||
private JObject _currentBlock; | ||
private string _lastCreateDirectory; | ||
|
||
|
||
public override string Description => "Exports Neo-CLI status data"; | ||
|
||
public StatesDumper() | ||
|
@@ -82,7 +86,10 @@ private void OnDumpStorage(uint network, UInt160 contractHash = null) | |
private void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList) | ||
{ | ||
if (Settings.Default.PersistAction.HasFlag(PersistActions.StorageChanges)) | ||
{ | ||
InitFileWriter(system.Settings.Network, snapshot); | ||
OnPersistStorage(system.Settings.Network, snapshot); | ||
} | ||
} | ||
|
||
private void OnPersistStorage(uint network, DataCache snapshot) | ||
|
@@ -122,45 +129,63 @@ private void OnPersistStorage(uint network, DataCache snapshot) | |
bs_item["block"] = blockIndex; | ||
bs_item["size"] = array.Count; | ||
bs_item["storage"] = array; | ||
if (!bs_cache.TryGetValue(network, out JArray cache)) | ||
{ | ||
cache = new JArray(); | ||
} | ||
cache.Add(bs_item); | ||
bs_cache[network] = cache; | ||
_currentBlock = bs_item; | ||
} | ||
} | ||
|
||
|
||
private void OnCommitted(NeoSystem system, Block block) | ||
{ | ||
if (Settings.Default.PersistAction.HasFlag(PersistActions.StorageChanges)) | ||
{ | ||
OnCommitStorage(system.Settings.Network, system.StoreView); | ||
} | ||
} | ||
|
||
void OnCommitStorage(uint network, DataCache snapshot) | ||
{ | ||
if (!bs_cache.TryGetValue(network, out JArray cache)) return; | ||
if (cache.Count == 0) return; | ||
if (_currentBlock != null) | ||
{ | ||
_writer.WriteLine(_currentBlock.ToString()); | ||
_writer.Flush(); | ||
} | ||
} | ||
|
||
private void InitFileWriter(uint network, DataCache snapshot) | ||
{ | ||
uint blockIndex = NativeContract.Ledger.CurrentIndex(snapshot); | ||
if ((blockIndex % Settings.Default.BlockCacheSize == 0) || (Settings.Default.HeightToStartRealTimeSyncing != -1 && blockIndex >= Settings.Default.HeightToStartRealTimeSyncing)) | ||
if (_writer == null | ||
|| blockIndex % Settings.Default.BlockCacheSize == 0) | ||
{ | ||
string path = GetOrCreateDirectory(network, blockIndex); | ||
var filepart = (blockIndex / Settings.Default.BlockCacheSize) * Settings.Default.BlockCacheSize; | ||
path = $"{path}/dump-block-{filepart}.dump"; | ||
if (_writer != null) | ||
{ | ||
_writer.Dispose(); | ||
} | ||
_writer = new StreamWriter(new FileStream(path, FileMode.Append)); | ||
} | ||
} | ||
|
||
private string GetOrCreateDirectory(uint network, uint blockIndex) | ||
{ | ||
string dirPathWithBlock = GetDirectoryPath(network, blockIndex); | ||
if (_lastCreateDirectory != dirPathWithBlock) | ||
{ | ||
string path = HandlePaths(network, blockIndex); | ||
path = $"{path}/dump-block-{blockIndex}.json"; | ||
File.WriteAllText(path, cache.ToString()); | ||
cache.Clear(); | ||
Directory.CreateDirectory(dirPathWithBlock); | ||
_lastCreateDirectory = dirPathWithBlock; | ||
} | ||
return dirPathWithBlock; | ||
} | ||
|
||
private static string HandlePaths(uint network, uint blockIndex) | ||
private string GetDirectoryPath(uint network, uint blockIndex) | ||
{ | ||
//Default Parameter | ||
uint storagePerFolder = 100000; | ||
uint folder = (((blockIndex - 1) / storagePerFolder) + 1) * storagePerFolder; | ||
if (blockIndex == 0) | ||
folder = 0; | ||
string dirPathWithBlock = $"./Storage_{network:x8}/BlockStorage_{folder}"; | ||
Directory.CreateDirectory(dirPathWithBlock); | ||
return dirPathWithBlock; | ||
uint folder = (blockIndex / storagePerFolder) * storagePerFolder; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the naming scheme and will break scripts like https://github.com/nspcc-dev/neo-go/blob/master/scripts/compare-dumps/compare-dumps.go. Please keep the old scheme, I doubt changing it is important wrt to this PR goals. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I'll make a new simple plugin so you won't change it. |
||
return $"./Storage_{network}/BlockStorage_{folder}"; | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this configuration?
This can be useful when debugging or following the chain on real-time