-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFairyPlugin.cs
160 lines (147 loc) · 6.91 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Microsoft.Extensions.Configuration;
using Neo.ConsoleService;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
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();
}
}
public uint SyncUntilBlock { get; internal set; } = uint.MaxValue;
public readonly TimeSpan SleepTimeMs = TimeSpan.FromMilliseconds(3600_000);
public CancellationTokenSource? CancelSyncSleep;
Settings settings;
NeoSystem system;
readonly List<Fairy> fairyServers = new();
public FairyPlugin() : base()
{
Blockchain.Committing += SyncControl;
}
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, this);
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, this);
fairy.StartRpcServer();
fairy.StartWebsocketServer();
fairyServers.Add(fairy);
}
string pauseFileName = "pause.txt";
string pauseFileFullPath = System.IO.Path.Combine(RootPath, pauseFileName);
if (File.Exists(pauseFileFullPath))
{
StreamReader sr = new StreamReader(pauseFileFullPath);
string? line = sr.ReadLine();
if (uint.TryParse(line, out uint blockIndex))
SyncUntilBlock = blockIndex;
else
SyncUntilBlock = 0;
if (SyncUntilBlock < uint.MaxValue)
ConsoleHelper.Warning($"Block sync at index > {SyncUntilBlock} paused by Fairy plugin, because {pauseFileName} exists in {RootPath}. Execute `sync` in Neo.CLI to continue block synchronization.");
}
}
/// <summary>
/// Sleep at block committing, if user does not want to synchronoze new blocks
/// </summary>
protected void SyncControl(NeoSystem system, Block block, DataCache snapshot, IReadOnlyList<Blockchain.ApplicationExecuted> applicationExecutedList)
{
while (block.Index > SyncUntilBlock)
{
string error = $"""{DateTime.Now.ToString("yyyy-MM-dd h:mm:ss tt")} Block synchronization at index {block.Index} > {SyncUntilBlock} paused by fairy. Current block height {NativeContract.Ledger.CurrentIndex(system.StoreView)}. Execute `sync` in Neo.CLI to continue sync, or `sync 0` to pause sync. Reminding you again in {SleepTimeMs.Days}d {SleepTimeMs.Hours}h:{SleepTimeMs.Minutes}m:{SleepTimeMs.Seconds}.{SleepTimeMs.Milliseconds}s""";
ConsoleHelper.Warning(error);
CancelSyncSleep = new();
CancelSyncSleep.Token.WaitHandle.WaitOne((int)SleepTimeMs.TotalMilliseconds);
}
}
[ConsoleCommand("sync", Category = "Fairy Commands", Description = "Pause or continue block synchronization")]
protected void OnFairySyncCommand(uint blockIndex = uint.MaxValue)
{
SyncUntilBlock = blockIndex;
CancelSyncSleep?.Cancel();
ConsoleHelper.Info($"Sync until block {blockIndex}");
}
[ConsoleCommand("fairy", Category = "Fairy Commands", Description = "List Fairy snapshots")]
protected 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!");
}
}
if (SyncUntilBlock < uint.MaxValue)
ConsoleHelper.Warning($"Fairy sync until block index {SyncUntilBlock}");
}
}
}