Skip to content
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

Fix/memory (#135) (master-2x) #141

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 49 additions & 51 deletions ImportBlocks/ImportBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,77 @@ public override void Configure()

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 uint start))
if (args.Length >= 3 && uint.TryParse(args[2], out start))
{
if (start > Blockchain.Singleton.Height)
return true;
uint count = args.Length >= 4 ? uint.Parse(args[3]) : uint.MaxValue;
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);
uint end = start + count - 1;
string path = $"chain.{start}.acc";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
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)
{
if (fs.Length > 0)
byte[] buffer = new byte[sizeof(uint)];
if (writeStart)
{
fs.Seek(sizeof(uint), SeekOrigin.Begin);
byte[] buffer = new byte[sizeof(uint)];
fs.Read(buffer, 0, buffer.Length);
start += BitConverter.ToUInt32(buffer, 0);
fs.Seek(sizeof(uint), SeekOrigin.Begin);
}
else
{
fs.Write(BitConverter.GetBytes(start), 0, sizeof(uint));
fs.Read(buffer, 0, buffer.Length);
start = BitConverter.ToUInt32(buffer, 0);
fs.Seek(0, SeekOrigin.Begin);
}
if (start <= end)
fs.Write(BitConverter.GetBytes(count), 0, sizeof(uint));
fs.Seek(0, SeekOrigin.End);
using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
for (uint i = start; i <= end; i++)
{
Block block = snapshot.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}]");
}
}
}
else
{
start = 0;
uint end = Blockchain.Singleton.Height;
uint count = end - start + 1;
string path = args.Length >= 3 ? args[2] : "chain.acc";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
else
{
if (fs.Length > 0)
if (writeStart)
{
byte[] buffer = new byte[sizeof(uint)];
fs.Read(buffer, 0, buffer.Length);
start = BitConverter.ToUInt32(buffer, 0);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(BitConverter.GetBytes(start), 0, sizeof(uint));
}
if (start <= end)
fs.Write(BitConverter.GetBytes(count), 0, sizeof(uint));
fs.Seek(0, SeekOrigin.End);
using (Snapshot snapshot = Blockchain.Singleton.GetSnapshot())
for (uint i = start; i <= end; i++)
{
Block block = snapshot.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}]");
}
}
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.Store.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}]");
}
}
Console.WriteLine();
return true;
}

private bool OnHelp(string[] args)
Expand Down