-
Notifications
You must be signed in to change notification settings - Fork 3
/
FairyPlugin.cs
110 lines (102 loc) · 4.27 KB
/
FairyPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Microsoft.Extensions.Configuration;
using Neo.ConsoleService;
using Neo.SmartContract.Native;
namespace Neo.Plugins
{
public class FairyPlugin : RpcServerPlugin
{
public override string Name => "Fairy";
public override string Description => "Test and debug fairy transactions through RPC";
class Settings
{
public IReadOnlyList<RpcServerSettings> Servers { get; }
public Settings(IConfigurationSection section)
{
Servers = section.GetSection(nameof(Servers)).GetChildren().Select(p => RpcServerSettings.Load(p)).ToArray();
}
}
Settings settings;
NeoSystem system;
readonly List<Fairy> fairyServers = new();
protected override void Configure()
{
base.Configure();
settings = new Settings(GetConfiguration());
}
protected override void OnSystemLoaded(NeoSystem system)
{
this.system = system;
bool hasServer = false;
foreach (RpcServerSettings s in settings.Servers)
{
if (s.Network == system.Settings.Network)
{
hasServer = true;
Fairy fairy = new(system, s);
fairy.StartRpcServer();
fairy.StartWebsocketServer();
fairyServers.Add(fairy);
}
}
if (hasServer == false)
{
ConsoleHelper.Warning("No valid server from config. Using default!");
RpcServerSettings s = RpcServerSettings.Default;
Fairy fairy = new(system, s);
fairy.StartRpcServer();
fairy.StartWebsocketServer();
fairyServers.Add(fairy);
}
}
[ConsoleCommand("fairy", Category = "Fairy Commands", Description = "List Fairy snapshots")]
private void OnFairyCommand()
{
foreach (Fairy fairy in fairyServers)
{
Console.WriteLine($">>> Fairy@{fairy.settings.BindAddress}:{fairy.settings.Port}");
ConsoleHelper.Info("★ Test snapshots:");
if (fairy.sessionStringToFairySession.Keys.Count > 0)
{
Console.WriteLine("session name:\t\t\t");
foreach (string k in fairy.sessionStringToFairySession.Keys)
{
ConsoleHelper.Info($"{k}\t\t\t{fairy.sessionStringToFairySession[k].ToString()}");
}
}
else
{
ConsoleHelper.Warning($"No test snapshot created!");
}
Console.WriteLine("------");
ConsoleHelper.Info("☆ DebugInfo registration:");
if (fairy.contractScriptHashToSourceLineFilenames.Keys.Count > 0)
{
Console.Error.WriteLine($"test snapshot\t\t\tcontract name\t\t\tscript hash");
foreach (UInt160 k in fairy.contractScriptHashToSourceLineFilenames.Keys)
{
string? contractName = null;
string? testSession = null;
foreach (string s in fairy.sessionStringToFairySession.Keys)
{
contractName = NativeContract.ContractManagement.GetContract(fairy.sessionStringToFairySession[s].engine.Snapshot, k)?.Manifest.Name;
if (contractName != null)
{
testSession = s;
break;
}
}
if (contractName == null)
contractName = "Unknown Contract";
ConsoleHelper.Info($"{testSession}\t\t\t\t{contractName}\t\t\t{k}");
ConsoleHelper.Info(String.Join(", ", fairy.contractScriptHashToSourceLineFilenames[k]));
Console.WriteLine("---");
}
}
else
{
ConsoleHelper.Warning($"No DebugInfo registration!");
}
}
}
}
}