-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathEngineStorage.cs
168 lines (146 loc) · 5.05 KB
/
EngineStorage.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
161
162
163
164
165
166
167
168
using Neo.Json;
using Neo.Persistence;
using System;
using System.Buffers.Binary;
using System.Linq;
namespace Neo.SmartContract.Testing.Storage
{
/// <summary>
/// TestStorage centralizes the storage management of our TestEngine
/// </summary>
public class EngineStorage
{
// Key to check if native contracts are initialized, by default: Neo.votersCountPrefix
private static readonly StorageKey _initKey = new() { Id = Neo.SmartContract.Native.NativeContract.NEO.Id, Key = new byte[] { 1 } };
/// <summary>
/// Store
/// </summary>
public IStore Store { get; }
/// <summary>
/// Snapshot
/// </summary>
public DataCache Snapshot { get; private set; }
/// <summary>
/// Return true if native contract are initialized
/// </summary>
public bool IsInitialized => Snapshot.Contains(_initKey);
/// <summary>
/// Constructor
/// </summary>
/// <param name="store">Store</param>
public EngineStorage(IStore store) : this(store, new SnapshotCache(store.GetSnapshot())) { }
/// <summary>
/// Constructor
/// </summary>
/// <param name="store">Store</param>
/// <param name="snapshotCache">Snapshot cache</param>
internal EngineStorage(IStore store, DataCache snapshotCache)
{
Store = store;
Snapshot = snapshotCache;
}
/// <summary>
/// Commit
/// </summary>
public void Commit()
{
Snapshot.Commit();
}
/// <summary>
/// Rollback
/// </summary>
public void Rollback()
{
if (Snapshot is IDisposable sp)
{
sp.Dispose();
}
Snapshot = new SnapshotCache(Store.GetSnapshot());
}
/// <summary>
/// Get storage checkpoint
/// </summary>
/// <returns>EngineCheckpoint</returns>
public EngineCheckpoint Checkpoint() => new(Snapshot);
/// <summary>
/// Restore
/// </summary>
/// <param name="checkpoint">Checkpoint</param>
public void Restore(EngineCheckpoint checkpoint)
{
checkpoint.Restore(Snapshot);
}
/// <summary>
/// Import data from json, expected data (in base64):
/// - "key" : "value"
/// - "prefix" : { "key":"value" }
/// </summary>
/// <param name="json">Json Object</param>
public void Import(string json)
{
if (JToken.Parse(json) is not JObject jo)
{
throw new FormatException("The json is not a valid JObject");
}
Import(jo);
}
/// <summary>
/// Import data from json, expected data (in base64):
/// - "key" : "value"
/// - "prefix" : { "key":"value" }
/// </summary>
/// <param name="json">Json Object</param>
public void Import(JObject json)
{
foreach (var entry in json.Properties)
{
if (entry.Value is JString str)
{
// "key":"value" in base64
Snapshot.Add(new StorageKey(Convert.FromBase64String(entry.Key)), new StorageItem(Convert.FromBase64String(str.Value)));
}
else if (entry.Value is JObject obj)
{
// "prefix": { "key":"value" } in base64
byte[] prefix = Convert.FromBase64String(entry.Key);
foreach (var subEntry in obj.Properties)
{
if (subEntry.Value is JString subStr)
{
Snapshot.Add(
new StorageKey(prefix.Concat(Convert.FromBase64String(subEntry.Key)).ToArray()),
new StorageItem(Convert.FromBase64String(subStr.Value))
);
}
}
}
}
}
/// <summary>
/// Export data to json
/// </summary>
public JObject Export()
{
var buffer = new byte[(sizeof(int))];
JObject ret = new();
foreach (var entry in Snapshot.Seek(Array.Empty<byte>(), SeekDirection.Forward))
{
// "key":"value" in base64
JObject prefix;
BinaryPrimitives.WriteInt32LittleEndian(buffer, entry.Key.Id);
var keyId = Convert.ToBase64String(buffer);
if (ret.ContainsProperty(keyId))
{
prefix = (JObject)ret[keyId]!;
}
else
{
prefix = new();
ret[keyId] = prefix;
}
prefix[Convert.ToBase64String(entry.Key.Key.ToArray())] = Convert.ToBase64String(entry.Value.Value.ToArray());
}
return ret;
}
}
}