diff --git a/neo-plugins.sln b/neo-plugins.sln index 107bd4127..ae23c748c 100644 --- a/neo-plugins.sln +++ b/neo-plugins.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.28729.10 @@ -13,8 +12,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcSecurity", "src\RpcSecur EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "src\StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportBlocks", "src\ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcWallet", "src\RpcWallet\RpcWallet.csproj", "{EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcNep5Tracker", "src\RpcNep5Tracker\RpcNep5Tracker.csproj", "{BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E}" @@ -47,10 +44,6 @@ Global {86531DB1-A231-46C4-823F-BE60972F7523}.Debug|Any CPU.Build.0 = Debug|Any CPU {86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.ActiveCfg = Release|Any CPU {86531DB1-A231-46C4-823F-BE60972F7523}.Release|Any CPU.Build.0 = Release|Any CPU - {B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B7A42984-57BB-4F8D-967B-23B0E841B726}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B7A42984-57BB-4F8D-967B-23B0E841B726}.Release|Any CPU.Build.0 = Release|Any CPU {EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Debug|Any CPU.Build.0 = Debug|Any CPU {EF32A7E5-EDF9-438C-8041-8DA6E675A7FD}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -87,7 +80,6 @@ Global {84DA8EA6-EF60-4FCD-B1C6-65C1A8323B3F} = {97E81C78-1637-481F-9485-DA1225E94C23} {6800D782-8EC0-49E9-98C4-195C8F781A1F} = {97E81C78-1637-481F-9485-DA1225E94C23} {86531DB1-A231-46C4-823F-BE60972F7523} = {97E81C78-1637-481F-9485-DA1225E94C23} - {B7A42984-57BB-4F8D-967B-23B0E841B726} = {97E81C78-1637-481F-9485-DA1225E94C23} {EF32A7E5-EDF9-438C-8041-8DA6E675A7FD} = {97E81C78-1637-481F-9485-DA1225E94C23} {BBE8AC15-12DF-4AF0-ABC1-F1557EB5DC8E} = {97E81C78-1637-481F-9485-DA1225E94C23} {AEFFF003-3500-416B-AD9B-8C838C33C1F4} = {97E81C78-1637-481F-9485-DA1225E94C23} diff --git a/src/ImportBlocks/BlockImporter.cs b/src/ImportBlocks/BlockImporter.cs deleted file mode 100644 index 83d350f28..000000000 --- a/src/ImportBlocks/BlockImporter.cs +++ /dev/null @@ -1,134 +0,0 @@ -using Akka.Actor; -using Neo.IO; -using Neo.Ledger; -using Neo.Network.P2P; -using Neo.Network.P2P.Payloads; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Text.RegularExpressions; - -namespace Neo.Plugins -{ - public class BlockImporter : UntypedActor - { - public class StartImport { public IActorRef BlockchainActorRef; public Action OnComplete; } - - private const int BlocksPerBatch = 10; - private IActorRef _blockchainActorRef; - private bool isImporting; - private IEnumerator blocksBeingImported; - private Action _doneAction; - - private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight) - { - if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight) - return true; - return false; - } - - private static IEnumerable GetBlocks(Stream stream, bool read_start = false) - { - using (BinaryReader r = new BinaryReader(stream)) - { - uint start = read_start ? r.ReadUInt32() : 0; - uint count = r.ReadUInt32(); - uint end = start + count - 1; - if (end <= Blockchain.Singleton.Height) yield break; - for (uint height = start; height <= end; height++) - { - var size = r.ReadInt32(); - if (size > Message.PayloadMaxSize) - throw new ArgumentException($"Block {height} exceeds the maximum allowed size"); - - byte[] array = r.ReadBytes(size); - if (!CheckMaxOnImportHeight(height)) yield break; - if (height > Blockchain.Singleton.Height) - { - Block block = array.AsSerializable(); - yield return block; - } - } - } - } - - private IEnumerable GetBlocksFromFile() - { - const string pathAcc = "chain.acc"; - if (File.Exists(pathAcc)) - using (FileStream fs = new FileStream(pathAcc, FileMode.Open, FileAccess.Read, FileShare.Read)) - foreach (var block in GetBlocks(fs)) - yield return block; - - const string pathAccZip = pathAcc + ".zip"; - if (File.Exists(pathAccZip)) - using (FileStream fs = new FileStream(pathAccZip, FileMode.Open, FileAccess.Read, FileShare.Read)) - using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read)) - using (Stream zs = zip.GetEntry(pathAcc).Open()) - foreach (var block in GetBlocks(zs)) - yield return block; - - var paths = Directory.EnumerateFiles(".", "chain.*.acc", SearchOption.TopDirectoryOnly).Concat(Directory.EnumerateFiles(".", "chain.*.acc.zip", SearchOption.TopDirectoryOnly)).Select(p => new - { - FileName = Path.GetFileName(p), - Start = uint.Parse(Regex.Match(p, @"\d+").Value), - IsCompressed = p.EndsWith(".zip") - }).OrderBy(p => p.Start); - - foreach (var path in paths) - { - if (path.Start > Blockchain.Singleton.Height + 1) break; - if (path.IsCompressed) - using (FileStream fs = new FileStream(path.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read)) - using (Stream zs = zip.GetEntry(Path.GetFileNameWithoutExtension(path.FileName)).Open()) - foreach (var block in GetBlocks(zs, true)) - yield return block; - else - using (FileStream fs = new FileStream(path.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - foreach (var block in GetBlocks(fs, true)) - yield return block; - } - } - - protected override void OnReceive(object message) - { - switch (message) - { - case StartImport startImport: - if (isImporting) return; - isImporting = true; - _blockchainActorRef = startImport.BlockchainActorRef; - _doneAction = startImport.OnComplete; - blocksBeingImported = GetBlocksFromFile().GetEnumerator(); - // Start the first import - Self.Tell(new Blockchain.ImportCompleted()); - break; - case Blockchain.ImportCompleted _: - // Import the next batch - List blocksToImport = new List(); - for (int i = 0; i < BlocksPerBatch; i++) - { - if (!blocksBeingImported.MoveNext()) - break; - blocksToImport.Add(blocksBeingImported.Current); - } - if (blocksToImport.Count > 0) - _blockchainActorRef.Tell(new Blockchain.Import { Blocks = blocksToImport }); - else - { - blocksBeingImported.Dispose(); - _doneAction(); - } - break; - } - } - - public static Props Props() - { - return Akka.Actor.Props.Create(() => new BlockImporter()); - } - } -} diff --git a/src/ImportBlocks/ImportBlocks.cs b/src/ImportBlocks/ImportBlocks.cs deleted file mode 100644 index e93413421..000000000 --- a/src/ImportBlocks/ImportBlocks.cs +++ /dev/null @@ -1,130 +0,0 @@ -using Akka.Actor; -using Neo.IO; -using Neo.Ledger; -using Neo.Network.P2P.Payloads; -using System; -using System.IO; - -namespace Neo.Plugins -{ - public class ImportBlocks : Plugin - { - private IActorRef _blockImporter; - - protected override void Configure() - { - Settings.Load(GetConfiguration()); - } - - private bool OnExport(string[] args) - { - bool writeStart; - uint start, count; - string path; - if (args.Length < 2) return false; - if (!string.Equals(args[1], "block", StringComparison.OrdinalIgnoreCase) - && !string.Equals(args[1], "blocks", StringComparison.OrdinalIgnoreCase)) - return false; - if (args.Length >= 3 && uint.TryParse(args[2], out start)) - { - if (Blockchain.Singleton.Height < start) return true; - count = args.Length >= 4 ? uint.Parse(args[3]) : uint.MaxValue; - count = Math.Min(count, Blockchain.Singleton.Height - start + 1); - path = $"chain.{start}.acc"; - writeStart = true; - } - else - { - start = 0; - count = Blockchain.Singleton.Height - start + 1; - path = args.Length >= 3 ? args[2] : "chain.acc"; - writeStart = false; - } - - WriteBlocks(start, count, path, writeStart); - - Console.WriteLine(); - return true; - } - - private void WriteBlocks(uint start, uint count, string path, bool writeStart) - { - uint end = start + count - 1; - using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.WriteThrough)) - { - if (fs.Length > 0) - { - byte[] buffer = new byte[sizeof(uint)]; - if (writeStart) - { - fs.Seek(sizeof(uint), SeekOrigin.Begin); - fs.Read(buffer, 0, buffer.Length); - start += BitConverter.ToUInt32(buffer, 0); - fs.Seek(sizeof(uint), SeekOrigin.Begin); - } - else - { - fs.Read(buffer, 0, buffer.Length); - start = BitConverter.ToUInt32(buffer, 0); - fs.Seek(0, SeekOrigin.Begin); - } - } - else - { - if (writeStart) - { - fs.Write(BitConverter.GetBytes(start), 0, sizeof(uint)); - } - } - if (start <= end) - fs.Write(BitConverter.GetBytes(count), 0, sizeof(uint)); - fs.Seek(0, SeekOrigin.End); - for (uint i = start; i <= end; i++) - { - Block block = Blockchain.Singleton.GetBlock(i); - byte[] array = block.ToArray(); - fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int)); - fs.Write(array, 0, array.Length); - Console.SetCursorPosition(0, Console.CursorTop); - Console.Write($"[{i}/{end}]"); - } - } - } - - private bool OnHelp(string[] args) - { - if (args.Length < 2) return false; - if (!string.Equals(args[1], Name, StringComparison.OrdinalIgnoreCase)) - return false; - Console.Write($"{Name} Commands:\n" + "\texport block[s] \n"); - return true; - } - - private void OnImportComplete() - { - ResumeNodeStartup(); - System.ActorSystem.Stop(_blockImporter); - } - - protected override void OnPluginsLoaded() - { - SuspendNodeStartup(); - _blockImporter = System.ActorSystem.ActorOf(BlockImporter.Props()); - _blockImporter.Tell(new BlockImporter.StartImport { BlockchainActorRef = System.Blockchain, OnComplete = OnImportComplete }); - } - - protected override bool OnMessage(object message) - { - if (!(message is string[] args)) return false; - if (args.Length == 0) return false; - switch (args[0].ToLower()) - { - case "help": - return OnHelp(args); - case "export": - return OnExport(args); - } - return false; - } - } -} diff --git a/src/ImportBlocks/ImportBlocks.csproj b/src/ImportBlocks/ImportBlocks.csproj deleted file mode 100644 index c76c33e35..000000000 --- a/src/ImportBlocks/ImportBlocks.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - 3.0.0-preview1 - netstandard2.1 - Neo.Plugins - - - - - PreserveNewest - PreserveNewest - - - - - - - - diff --git a/src/ImportBlocks/ImportBlocks/config.json b/src/ImportBlocks/ImportBlocks/config.json deleted file mode 100644 index ed125974c..000000000 --- a/src/ImportBlocks/ImportBlocks/config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "PluginConfiguration": { - "MaxOnImportHeight": 0 - } -} diff --git a/src/ImportBlocks/Settings.cs b/src/ImportBlocks/Settings.cs deleted file mode 100644 index 279e80127..000000000 --- a/src/ImportBlocks/Settings.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Microsoft.Extensions.Configuration; -using System; - -namespace Neo.Plugins -{ - internal class Settings - { - public uint MaxOnImportHeight { get; } - - public static Settings Default { get; private set; } - - private Settings(IConfigurationSection section) - { - this.MaxOnImportHeight = GetValueOrDefault(section.GetSection("MaxOnImportHeight"), 0u, p => uint.Parse(p)); - } - - public T GetValueOrDefault(IConfigurationSection section, T defaultValue, Func selector) - { - if (section.Value == null) return defaultValue; - return selector(section.Value); - } - - public static void Load(IConfigurationSection section) - { - Default = new Settings(section); - } - } -}