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

Feat/export import tx final application state #90

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
64 changes: 64 additions & 0 deletions ImportBlocks/ImportBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.IO.Json;
using System;
using System.IO;

Expand Down Expand Up @@ -30,7 +31,11 @@ private bool OnExport(string[] args)
uint 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";
if (Settings.Default.PersistTXState)
path += ".v2";

using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
if (fs.Length > 0)
Expand All @@ -55,6 +60,14 @@ private bool OnExport(string[] args)
byte[] array = block.ToArray();
fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int));
fs.Write(array, 0, array.Length);

if (Settings.Default.PersistTXState)
if(!ExportAppLog(block, fs))
{
Console.Write("Error while exporting files with AppLog\n");
return false;
}

Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"[{i}/{end}]");
}
Expand All @@ -66,6 +79,9 @@ private bool OnExport(string[] args)
uint end = Blockchain.Singleton.Height;
uint count = end - start + 1;
string path = args.Length >= 3 ? args[2] : "chain.acc";
if (Settings.Default.PersistTXState)
path += ".v2";

using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
if (fs.Length > 0)
Expand All @@ -85,6 +101,14 @@ private bool OnExport(string[] args)
byte[] array = block.ToArray();
fs.Write(BitConverter.GetBytes(array.Length), 0, sizeof(int));
fs.Write(array, 0, array.Length);

if (Settings.Default.PersistTXState)
if (!ExportAppLog(block, fs))
{
Console.Write("Error while exporting files with AppLog.");
return false;
}

Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"[{i}/{end}]");
}
Expand All @@ -94,6 +118,46 @@ private bool OnExport(string[] args)
return true;
}

private bool ExportAppLog(Block block, FileStream fs)
{
foreach (Transaction tx in block.Transactions)
{
if (tx.Type != TransactionType.InvocationTransaction)
continue;

JArray _params = new JArray();
JString txHash = new JString(tx.Hash.ToString());
Console.Write($"Export tx {txHash}\n");

_params.Add(txHash);
JObject getAppLogRequest = new JObject();
getAppLogRequest["id"] = -1;
getAppLogRequest["method"] = "getapplicationlog";
getAppLogRequest["params"] = _params;
JObject txState = System.RpcServer.ProcessRequest(null, getAppLogRequest);

Console.Write($"Result is {txState["result"]}\n");
if (!txState["result"])
if ((int)txState["code"].AsNumber() == -100 || (int)txState["code"].AsNumber() == -32602 || (int)txState["code"].AsNumber() == -32600)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikzhang, @shargon, it would be nice to change this line for any kind of error.

{
Console.Write($"Code is {txState["code"]}\n");
return false;
}

JArray appExecuted = (JArray)txState["result"]["executions"];
uint nExec = (uint)appExecuted.Count;
fs.Write(BitConverter.GetBytes(nExec), 0, sizeof(uint));
byte[] vmStates = new byte[nExec];
for (int k = 0; k < nExec; k++)
vmStates[k] = appExecuted[k]["vmstate"].ToString().Contains("FAULT") ? (byte)VM.VMState.FAULT : (byte)VM.VMState.HALT;

Console.Write($"Exporting states {nExec} = {vmStates}\n");

fs.Write(vmStates, 0, vmStates.Length);
}
return true;
}

private bool OnHelp(string[] args)
{
if (args.Length < 2) return false;
Expand Down
3 changes: 2 additions & 1 deletion ImportBlocks/ImportBlocks/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"PluginConfiguration": {
"MaxOnImportHeight": 0
"MaxOnImportHeight": 0,
"PersistTXState": false
}
}
8 changes: 8 additions & 0 deletions ImportBlocks/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ internal class Settings
{
public uint MaxOnImportHeight { get; }

/// <summary>
/// Flag for persisting (exporting and importing) transaction state:
/// fault or halt; number of notifications and stack type.
/// </summary>
public bool PersistTXState { get; }

public static Settings Default { get; private set; }

private Settings(IConfigurationSection section)
{
this.MaxOnImportHeight = GetValueOrDefault(section.GetSection("MaxOnImportHeight"), 0u, p => uint.Parse(p));
// TODO - set to false after finishing
this.PersistTXState = GetValueOrDefault(section.GetSection("PersistTXState"), true, p => bool.Parse(p));
}

public T GetValueOrDefault<T>(IConfigurationSection section, T defaultValue, Func<string, T> selector)
Expand Down