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

Help for Plugins #28

Merged
merged 3 commits into from
Nov 16, 2018
Merged
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
45 changes: 34 additions & 11 deletions ImportBlocks/ImportBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public ImportBlocks()
});
}

private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight)
{
if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight)
return true;
return false;
}

private static IEnumerable<Block> GetBlocks(Stream stream, bool read_start = false)
{
using (BinaryReader r = new BinaryReader(stream))
Expand All @@ -83,19 +90,12 @@ private static IEnumerable<Block> GetBlocks(Stream stream, bool read_start = fal
}
}

private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight)
{
if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight)
return true;
return false;
}

protected override bool OnMessage(object message)
private bool OnExport(string[] args)
{
if (!(message is string[] args)) return false;
if (args.Length < 2) return false;
if (args[0] != "export") return false;
if (args[1] != "block" && args[1] != "blocks") 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 (start > Blockchain.Singleton.Height)
Expand Down Expand Up @@ -166,5 +166,28 @@ protected override bool OnMessage(object message)
Console.WriteLine();
return true;
}

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] <index>\n");
return true;
}

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;
}
}
}
51 changes: 36 additions & 15 deletions StatesDumper/StatesDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,40 @@ namespace Neo.Plugins
{
public class StatesDumper : Plugin
{
private static void Dump<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> states)
where TKey : ISerializable
where TValue : ISerializable
{
const string path = "dump.json";
JArray array = new JArray(states.Select(p =>
{
JObject state = new JObject();
state["key"] = p.Key.ToArray().ToHexString();
state["value"] = p.Value.ToArray().ToHexString();
return state;
}));
File.WriteAllText(path, array.ToString());
Console.WriteLine($"States ({array.Count}) have been dumped into file {path}");
}

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 "dump":
return OnDump(args);
}
return false;
}

private bool OnDump(string[] args)
{
if (args.Length < 2) return false;
if (args[0] != "dump") return false;
switch (args[1])
switch (args[1].ToLower())
{
case "storage":
Dump(args.Length >= 3
Expand All @@ -27,20 +55,13 @@ protected override bool OnMessage(object message)
}
}

private static void Dump<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> states)
where TKey : ISerializable
where TValue : ISerializable
private bool OnHelp(string[] args)
{
const string path = "dump.json";
JArray array = new JArray(states.Select(p =>
{
JObject state = new JObject();
state["key"] = p.Key.ToArray().ToHexString();
state["value"] = p.Value.ToArray().ToHexString();
return state;
}));
File.WriteAllText(path, array.ToString());
Console.WriteLine($"States ({array.Count}) have been dumped into file {path}");
if (args.Length < 2) return false;
if (!string.Equals(args[1], Name, StringComparison.OrdinalIgnoreCase))
return false;
Console.Write($"{Name} Commands:\n" + "\tdump storage <key>\n");
return true;
}
}
}