|
| 1 | +// Copyright (C) 2015-2023 The Neo Project. |
| 2 | +// |
| 3 | +// The Neo.Plugins.StatesDumper is free software distributed under the MIT software license, |
| 4 | +// see the accompanying file LICENSE in the main directory of the |
| 5 | +// project or http://www.opensource.org/licenses/mit-license.php |
| 6 | +// for more details. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms with or without |
| 9 | +// modifications are permitted. |
| 10 | + |
| 11 | +using Neo.ConsoleService; |
| 12 | +using Neo.IO; |
| 13 | +using Neo.Json; |
| 14 | +using Neo.Ledger; |
| 15 | +using Neo.Network.P2P.Payloads; |
| 16 | +using Neo.Persistence; |
| 17 | +using Neo.SmartContract.Native; |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.IO; |
| 21 | +using System.Linq; |
| 22 | + |
| 23 | +namespace Neo.Plugins |
| 24 | +{ |
| 25 | + public class StorageDumper : Plugin |
| 26 | + { |
| 27 | + private readonly Dictionary<uint, NeoSystem> systems = new Dictionary<uint, NeoSystem>(); |
| 28 | + |
| 29 | + private StreamWriter _writer; |
| 30 | + private JObject _currentBlock; |
| 31 | + private string _lastCreateDirectory; |
| 32 | + |
| 33 | + |
| 34 | + public override string Description => "Exports Neo-CLI status data"; |
| 35 | + |
| 36 | + public StorageDumper() |
| 37 | + { |
| 38 | + Blockchain.Committing += OnCommitting; |
| 39 | + Blockchain.Committed += OnCommitted; |
| 40 | + } |
| 41 | + |
| 42 | + public override void Dispose() |
| 43 | + { |
| 44 | + Blockchain.Committing -= OnCommitting; |
| 45 | + Blockchain.Committed -= OnCommitted; |
| 46 | + } |
| 47 | + |
| 48 | + protected override void Configure() |
| 49 | + { |
| 50 | + Settings.Load(GetConfiguration()); |
| 51 | + } |
| 52 | + |
| 53 | + protected override void OnSystemLoaded(NeoSystem system) |
| 54 | + { |
| 55 | + systems.Add(system.Settings.Network, system); |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Process "dump contract-storage" command |
| 60 | + /// </summary> |
| 61 | + [ConsoleCommand("dump contract-storage", Category = "Storage", Description = "You can specify the contract script hash or use null to get the corresponding information from the storage")] |
| 62 | + private void OnDumpStorage(uint network, UInt160 contractHash = null) |
| 63 | + { |
| 64 | + if (!systems.ContainsKey(network)) throw new InvalidOperationException("invalid network"); |
| 65 | + string path = $"dump_{network}.json"; |
| 66 | + byte[] prefix = null; |
| 67 | + if (contractHash is not null) |
| 68 | + { |
| 69 | + var contract = NativeContract.ContractManagement.GetContract(systems[network].StoreView, contractHash); |
| 70 | + if (contract is null) throw new InvalidOperationException("contract not found"); |
| 71 | + prefix = BitConverter.GetBytes(contract.Id); |
| 72 | + } |
| 73 | + var states = systems[network].StoreView.Find(prefix); |
| 74 | + JArray array = new JArray(states.Where(p => !Settings.Default.Exclude.Contains(p.Key.Id)).Select(p => new JObject |
| 75 | + { |
| 76 | + ["key"] = Convert.ToBase64String(p.Key.ToArray()), |
| 77 | + ["value"] = Convert.ToBase64String(p.Value.ToArray()) |
| 78 | + })); |
| 79 | + File.WriteAllText(path, array.ToString()); |
| 80 | + ConsoleHelper.Info("States", |
| 81 | + $"({array.Count})", |
| 82 | + " have been dumped into file ", |
| 83 | + $"{path}"); |
| 84 | + } |
| 85 | + |
| 86 | + private void OnCommitting(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList) |
| 87 | + { |
| 88 | + InitFileWriter(system.Settings.Network, snapshot); |
| 89 | + OnPersistStorage(system.Settings.Network, snapshot); |
| 90 | + } |
| 91 | + |
| 92 | + private void OnPersistStorage(uint network, DataCache snapshot) |
| 93 | + { |
| 94 | + uint blockIndex = NativeContract.Ledger.CurrentIndex(snapshot); |
| 95 | + if (blockIndex >= Settings.Default.HeightToBegin) |
| 96 | + { |
| 97 | + JArray array = new JArray(); |
| 98 | + |
| 99 | + foreach (var trackable in snapshot.GetChangeSet()) |
| 100 | + { |
| 101 | + if (Settings.Default.Exclude.Contains(trackable.Key.Id)) |
| 102 | + continue; |
| 103 | + JObject state = new JObject(); |
| 104 | + switch (trackable.State) |
| 105 | + { |
| 106 | + case TrackState.Added: |
| 107 | + state["state"] = "Added"; |
| 108 | + state["key"] = Convert.ToBase64String(trackable.Key.ToArray()); |
| 109 | + state["value"] = Convert.ToBase64String(trackable.Item.ToArray()); |
| 110 | + // Here we have a new trackable.Key and trackable.Item |
| 111 | + break; |
| 112 | + case TrackState.Changed: |
| 113 | + state["state"] = "Changed"; |
| 114 | + state["key"] = Convert.ToBase64String(trackable.Key.ToArray()); |
| 115 | + state["value"] = Convert.ToBase64String(trackable.Item.ToArray()); |
| 116 | + break; |
| 117 | + case TrackState.Deleted: |
| 118 | + state["state"] = "Deleted"; |
| 119 | + state["key"] = Convert.ToBase64String(trackable.Key.ToArray()); |
| 120 | + break; |
| 121 | + } |
| 122 | + array.Add(state); |
| 123 | + } |
| 124 | + |
| 125 | + JObject bs_item = new JObject(); |
| 126 | + bs_item["block"] = blockIndex; |
| 127 | + bs_item["size"] = array.Count; |
| 128 | + bs_item["storage"] = array; |
| 129 | + _currentBlock = bs_item; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + private void OnCommitted(NeoSystem system, Block block) |
| 135 | + { |
| 136 | + OnCommitStorage(system.Settings.Network, system.StoreView); |
| 137 | + } |
| 138 | + |
| 139 | + void OnCommitStorage(uint network, DataCache snapshot) |
| 140 | + { |
| 141 | + if (_currentBlock != null) |
| 142 | + { |
| 143 | + _writer.WriteLine(_currentBlock.ToString()); |
| 144 | + _writer.Flush(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void InitFileWriter(uint network, DataCache snapshot) |
| 149 | + { |
| 150 | + uint blockIndex = NativeContract.Ledger.CurrentIndex(snapshot); |
| 151 | + if (_writer == null |
| 152 | + || blockIndex % Settings.Default.BlockCacheSize == 0) |
| 153 | + { |
| 154 | + string path = GetOrCreateDirectory(network, blockIndex); |
| 155 | + var filepart = (blockIndex / Settings.Default.BlockCacheSize) * Settings.Default.BlockCacheSize; |
| 156 | + path = $"{path}/dump-block-{filepart}.dump"; |
| 157 | + if (_writer != null) |
| 158 | + { |
| 159 | + _writer.Dispose(); |
| 160 | + } |
| 161 | + _writer = new StreamWriter(new FileStream(path, FileMode.Append)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private string GetOrCreateDirectory(uint network, uint blockIndex) |
| 166 | + { |
| 167 | + string dirPathWithBlock = GetDirectoryPath(network, blockIndex); |
| 168 | + if (_lastCreateDirectory != dirPathWithBlock) |
| 169 | + { |
| 170 | + Directory.CreateDirectory(dirPathWithBlock); |
| 171 | + _lastCreateDirectory = dirPathWithBlock; |
| 172 | + } |
| 173 | + return dirPathWithBlock; |
| 174 | + } |
| 175 | + |
| 176 | + private string GetDirectoryPath(uint network, uint blockIndex) |
| 177 | + { |
| 178 | + //Default Parameter |
| 179 | + uint storagePerFolder = 100000; |
| 180 | + uint folder = (blockIndex / storagePerFolder) * storagePerFolder; |
| 181 | + return $"./StorageDumper_{network}/BlockStorage_{folder}"; |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | +} |
0 commit comments