Skip to content

Commit

Permalink
add ImportBlocks (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Sep 3, 2018
1 parent 6f66abe commit d304434
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
91 changes: 91 additions & 0 deletions ImportBlocks/ImportBlocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Akka.Actor;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Neo.Plugins
{
public class ImportBlocks : Plugin
{
public ImportBlocks()
{
Task.Run(() =>
{
const string path_acc = "chain.acc";
if (File.Exists(path_acc))
using (FileStream fs = new FileStream(path_acc, FileMode.Open, FileAccess.Read, FileShare.Read))
System.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
{
Blocks = GetBlocks(fs)
}).Wait();
const string path_acc_zip = path_acc + ".zip";
if (File.Exists(path_acc_zip))
using (FileStream fs = new FileStream(path_acc_zip, FileMode.Open, FileAccess.Read, FileShare.Read))
using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read))
using (Stream zs = zip.GetEntry(path_acc).Open())
System.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
{
Blocks = GetBlocks(zs)
}).Wait();
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())
System.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
{
Blocks = GetBlocks(zs, true)
}).Wait();
else
using (FileStream fs = new FileStream(path.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
System.Blockchain.Ask<Blockchain.ImportCompleted>(new Blockchain.Import
{
Blocks = GetBlocks(fs, true)
}).Wait();
}
});
}

private static IEnumerable<Block> 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++)
{
byte[] array = r.ReadBytes(r.ReadInt32());

if (height > Blockchain.Singleton.Height && CheckMaxOnImportHeight(height))
{
Block block = array.AsSerializable<Block>();
yield return block;
}
}
}
}

private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight)
{
if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight)
return true;
return false;
}
}
}
13 changes: 13 additions & 0 deletions ImportBlocks/ImportBlocks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>3.0.0-preview2-11</Version>
<TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-preview2-11" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions ImportBlocks/ImportBlocks/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"PluginConfiguration": {
"MaxOnImportHeight": 0
}
}
29 changes: 29 additions & 0 deletions ImportBlocks/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Reflection;

namespace Neo.Plugins
{
internal class Settings
{
public uint MaxOnImportHeight { get; }

public static Settings Default { get; }

static Settings()
{
Default = new Settings(Assembly.GetExecutingAssembly().GetConfiguration());
}

public Settings(IConfigurationSection section)
{
this.MaxOnImportHeight = GetValueOrDefault(section.GetSection("MaxOnImportHeight"), 0u, p => uint.Parse(p));
}

public T GetValueOrDefault<T>(IConfigurationSection section, T defaultValue, Func<string, T> selector)
{
if (section.Value == null) return defaultValue;
return selector(section.Value);
}
}
}
8 changes: 7 additions & 1 deletion neo-plugins.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationLogs", "Applicat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RpcDisabled", "RpcDisabled\RpcDisabled.csproj", "{6800D782-8EC0-49E9-98C4-195C8F781A1F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatesDumper", "StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatesDumper", "StatesDumper\StatesDumper.csproj", "{86531DB1-A231-46C4-823F-BE60972F7523}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImportBlocks", "ImportBlocks\ImportBlocks.csproj", "{B7A42984-57BB-4F8D-967B-23B0E841B726}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +35,10 @@ 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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit d304434

Please sign in to comment.