-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathLogStorageStore.cs
415 lines (370 loc) · 15.4 KB
/
LogStorageStore.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (C) 2015-2024 The Neo Project.
//
// LogStorageStore.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using ApplicationLogs.Store.States;
using Neo;
using Neo.IO;
using Neo.Persistence;
using Neo.Plugins;
using Neo.Plugins.Store.States;
using Neo.SmartContract;
using Neo.VM;
using Neo.VM.Types;
namespace ApplicationLogs.Store
{
public sealed class LogStorageStore : IDisposable
{
#region Prefixes
private static readonly int Prefix_Size = sizeof(int) + sizeof(byte);
private static readonly int Prefix_Block_Trigger_Size = Prefix_Size + UInt256.Length;
private static readonly int Prefix_Execution_Block_Trigger_Size = Prefix_Size + UInt256.Length;
private static readonly int Prefix_Id = 0x414c4f47; // Magic Code: (ALOG);
private static readonly byte Prefix_Engine = 0x18; // Engine_GUID -> ScriptHash, Message
private static readonly byte Prefix_Engine_Transaction = 0x19; // TxHash -> Engine_GUID_List
private static readonly byte Prefix_Block = 0x20; // BlockHash, Trigger -> NotifyLog_GUID_List
private static readonly byte Prefix_Notify = 0x21; // NotifyLog_GUID -> ScriptHash, EventName, StackItem_GUID_List
private static readonly byte Prefix_Contract = 0x22; // ScriptHash, TimeStamp, EventIterIndex -> txHash, Trigger, NotifyLog_GUID
private static readonly byte Prefix_Execution = 0x23; // Execution_GUID -> Data, StackItem_GUID_List
private static readonly byte Prefix_Execution_Block = 0x24; // BlockHash, Trigger -> Execution_GUID
private static readonly byte Prefix_Execution_Transaction = 0x25; // TxHash -> Execution_GUID
private static readonly byte Prefix_Transaction = 0x26; // TxHash -> NotifyLog_GUID_List
private static readonly byte Prefix_StackItem = 0xed; // StackItem_GUID -> Data
#endregion
#region Global Variables
private readonly ISnapshot _snapshot;
#endregion
#region Ctor
public LogStorageStore(ISnapshot snapshot)
{
ArgumentNullException.ThrowIfNull(snapshot, nameof(snapshot));
_snapshot = snapshot;
}
#endregion
#region IDisposable
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
#region Put
public Guid PutEngineState(EngineLogState state)
{
var id = Guid.NewGuid();
var key = new KeyBuilder(Prefix_Id, Prefix_Engine)
.Add(id.ToByteArray())
.ToArray();
_snapshot.Put(key, state.ToArray());
return id;
}
public void PutTransactionEngineState(UInt256 hash, TransactionEngineLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Engine_Transaction)
.Add(hash)
.ToArray();
_snapshot.Put(key, state.ToArray());
}
public void PutBlockState(UInt256 hash, TriggerType trigger, BlockLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Block)
.Add(hash)
.Add((byte)trigger)
.ToArray();
_snapshot.Put(key, state.ToArray());
}
public Guid PutNotifyState(NotifyLogState state)
{
var id = Guid.NewGuid();
var key = new KeyBuilder(Prefix_Id, Prefix_Notify)
.Add(id.ToByteArray())
.ToArray();
_snapshot.Put(key, state.ToArray());
return id;
}
public void PutContractState(UInt160 scriptHash, ulong timestamp, uint iterIndex, ContractLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.AddBigEndian(timestamp)
.AddBigEndian(iterIndex)
.ToArray();
_snapshot.Put(key, state.ToArray());
}
public Guid PutExecutionState(ExecutionLogState state)
{
var id = Guid.NewGuid();
var key = new KeyBuilder(Prefix_Id, Prefix_Execution)
.Add(id.ToByteArray())
.ToArray();
_snapshot.Put(key, state.ToArray());
return id;
}
public void PutExecutionBlockState(UInt256 blockHash, TriggerType trigger, Guid executionStateId)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Execution_Block)
.Add(blockHash)
.Add((byte)trigger)
.ToArray();
_snapshot.Put(key, executionStateId.ToByteArray());
}
public void PutExecutionTransactionState(UInt256 txHash, Guid executionStateId)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Execution_Transaction)
.Add(txHash)
.ToArray();
_snapshot.Put(key, executionStateId.ToByteArray());
}
public void PutTransactionState(UInt256 hash, TransactionLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Transaction)
.Add(hash)
.ToArray();
_snapshot.Put(key, state.ToArray());
}
public Guid PutStackItemState(StackItem stackItem)
{
var id = Guid.NewGuid();
var key = new KeyBuilder(Prefix_Id, Prefix_StackItem)
.Add(id.ToByteArray())
.ToArray();
try
{
_snapshot.Put(key, BinarySerializer.Serialize(stackItem, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Default.MaxStackSize }));
}
catch
{
_snapshot.Put(key, BinarySerializer.Serialize(StackItem.Null, ExecutionEngineLimits.Default with { MaxItemSize = (uint)Settings.Default.MaxStackSize }));
}
return id;
}
#endregion
#region Find
public IEnumerable<(BlockLogState State, TriggerType Trigger)> FindBlockState(UInt256 hash)
{
var prefixKey = new KeyBuilder(Prefix_Id, Prefix_Block)
.Add(hash)
.ToArray();
foreach (var (key, value) in _snapshot.Seek(prefixKey, SeekDirection.Forward))
{
if (key.AsSpan().StartsWith(prefixKey))
yield return (value.AsSerializable<BlockLogState>(), (TriggerType)key.AsSpan(Prefix_Block_Trigger_Size)[0]);
else
yield break;
}
}
public IEnumerable<ContractLogState> FindContractState(UInt160 scriptHash, uint page, uint pageSize)
{
var prefix = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.ToArray();
var prefixKey = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.AddBigEndian(ulong.MaxValue) // Get newest to oldest (timestamp)
.ToArray();
uint index = 1;
foreach (var (key, value) in _snapshot.Seek(prefixKey, SeekDirection.Backward)) // Get newest to oldest
{
if (key.AsSpan().StartsWith(prefix))
{
if (index >= page && index < (pageSize + page))
yield return value.AsSerializable<ContractLogState>();
index++;
}
else
yield break;
}
}
public IEnumerable<ContractLogState> FindContractState(UInt160 scriptHash, TriggerType trigger, uint page, uint pageSize)
{
var prefix = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.ToArray();
var prefixKey = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.AddBigEndian(ulong.MaxValue) // Get newest to oldest (timestamp)
.ToArray();
uint index = 1;
foreach (var (key, value) in _snapshot.Seek(prefixKey, SeekDirection.Backward)) // Get newest to oldest
{
if (key.AsSpan().StartsWith(prefix))
{
var state = value.AsSerializable<ContractLogState>();
if (state.Trigger == trigger)
{
if (index >= page && index < (pageSize + page))
yield return state;
index++;
}
}
else
yield break;
}
}
public IEnumerable<ContractLogState> FindContractState(UInt160 scriptHash, TriggerType trigger, string eventName, uint page, uint pageSize)
{
var prefix = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.ToArray();
var prefixKey = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.AddBigEndian(ulong.MaxValue) // Get newest to oldest (timestamp)
.ToArray();
uint index = 1;
foreach (var (key, value) in _snapshot.Seek(prefixKey, SeekDirection.Backward)) // Get newest to oldest
{
if (key.AsSpan().StartsWith(prefix))
{
var state = value.AsSerializable<ContractLogState>();
if (state.Trigger == trigger && state.EventName.Equals(eventName, StringComparison.OrdinalIgnoreCase))
{
if (index >= page && index < (pageSize + page))
yield return state;
index++;
}
}
else
yield break;
}
}
public IEnumerable<(Guid ExecutionStateId, TriggerType Trigger)> FindExecutionBlockState(UInt256 hash)
{
var prefixKey = new KeyBuilder(Prefix_Id, Prefix_Execution_Block)
.Add(hash)
.ToArray();
foreach (var (key, value) in _snapshot.Seek(prefixKey, SeekDirection.Forward))
{
if (key.AsSpan().StartsWith(prefixKey))
yield return (new Guid(value), (TriggerType)key.AsSpan(Prefix_Execution_Block_Trigger_Size)[0]);
else
yield break;
}
}
#endregion
#region TryGet
public bool TryGetEngineState(Guid engineStateId, out EngineLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Engine)
.Add(engineStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<EngineLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetTransactionEngineState(UInt256 hash, out TransactionEngineLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Engine_Transaction)
.Add(hash)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<TransactionEngineLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetBlockState(UInt256 hash, TriggerType trigger, out BlockLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Block)
.Add(hash)
.Add((byte)trigger)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<BlockLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetNotifyState(Guid notifyStateId, out NotifyLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Notify)
.Add(notifyStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<NotifyLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetContractState(UInt160 scriptHash, ulong timestamp, uint iterIndex, out ContractLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Contract)
.Add(scriptHash)
.AddBigEndian(timestamp)
.AddBigEndian(iterIndex)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<ContractLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetExecutionState(Guid executionStateId, out ExecutionLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Execution)
.Add(executionStateId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<ExecutionLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetExecutionBlockState(UInt256 blockHash, TriggerType trigger, out Guid executionStateId)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Execution_Block)
.Add(blockHash)
.Add((byte)trigger)
.ToArray();
var data = _snapshot.TryGet(key);
if (data == null)
{
executionStateId = Guid.Empty;
return false;
}
else
{
executionStateId = new Guid(data);
return true;
}
}
public bool TryGetExecutionTransactionState(UInt256 txHash, out Guid executionStateId)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Execution_Transaction)
.Add(txHash)
.ToArray();
var data = _snapshot.TryGet(key);
if (data == null)
{
executionStateId = Guid.Empty;
return false;
}
else
{
executionStateId = new Guid(data);
return true;
}
}
public bool TryGetTransactionState(UInt256 hash, out TransactionLogState state)
{
var key = new KeyBuilder(Prefix_Id, Prefix_Transaction)
.Add(hash)
.ToArray();
var data = _snapshot.TryGet(key);
state = data?.AsSerializable<TransactionLogState>()!;
return data != null && data.Length > 0;
}
public bool TryGetStackItemState(Guid stackItemId, out StackItem stackItem)
{
var key = new KeyBuilder(Prefix_Id, Prefix_StackItem)
.Add(stackItemId.ToByteArray())
.ToArray();
var data = _snapshot.TryGet(key);
if (data == null)
{
stackItem = StackItem.Null;
return false;
}
else
{
stackItem = BinarySerializer.Deserialize(data, ExecutionEngineLimits.Default);
return true;
}
}
#endregion
}
}