-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFairy.Tester.Snapshot.cs
92 lines (84 loc) · 3.21 KB
/
Fairy.Tester.Snapshot.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
using Neo.Json;
using Neo.Persistence;
using System.Collections.Concurrent;
namespace Neo.Plugins
{
public partial class Fairy
{
public readonly ConcurrentDictionary<string, FairySession> sessionStringToFairySession = new();
public FairySession GetOrCreateFairySession(string session)
{
FairySession? fairySession;
if (!sessionStringToFairySession.TryGetValue(session, out fairySession))
{ // we allow initializing a new session when executing
fairySession = NewFairySession(system, this);
sessionStringToFairySession[session] = fairySession;
}
return fairySession;
}
private FairyEngine BuildSnapshotWithDummyScript(FairyEngine? engine = null)
{
return FairyEngine.Run(new byte[] { 0x40 }, engine != null ? engine.Snapshot.CreateSnapshot() : system.StoreView, this, settings: system.Settings, gas: settings.MaxGasInvoke, oldEngine: engine, copyRuntimeArgs: true);
}
[RpcMethod]
protected virtual JToken NewSnapshotsFromCurrentSystem(JArray _params)
{
JObject json = new();
foreach (var param in _params)
{
string session = param!.AsString();
if (sessionStringToFairySession.TryGetValue(session, out _))
json[session] = true;
else
json[session] = false;
sessionStringToFairySession[session] = NewFairySession(system, this);
}
return json;
}
[RpcMethod]
protected virtual JToken DeleteSnapshots(JArray _params)
{
JObject json = new();
foreach (var s in _params)
{
string str = s!.AsString();
json[str] = sessionStringToFairySession.Remove(str, out var _);
}
return json;
}
[RpcMethod]
protected virtual JToken ListSnapshots(JArray _params)
{
JArray session = new JArray();
foreach (string s in sessionStringToFairySession.Keys)
{
session.Add(s);
}
return session;
}
[RpcMethod]
protected virtual JToken RenameSnapshot(JArray _params)
{
string from = _params[0]!.AsString();
string to = _params[1]!.AsString();
sessionStringToFairySession[to] = sessionStringToFairySession[from];
sessionStringToFairySession.Remove(from, out var _);
JObject json = new();
json[to] = from;
return json;
}
[RpcMethod]
protected virtual JToken CopySnapshot(JArray _params)
{
string from = _params[0]!.AsString();
string to = _params[1]!.AsString();
FairySession testSessionTo = NewFairySession(system, this);
testSessionTo.engine = BuildSnapshotWithDummyScript(sessionStringToFairySession[from].engine);
testSessionTo.debugEngine = null;
sessionStringToFairySession[to] = testSessionTo;
JObject json = new();
json[to] = from;
return json;
}
}
}