forked from neo-project/neo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationEngineBase.SystemStorage.cs
145 lines (111 loc) · 5.75 KB
/
ApplicationEngineBase.SystemStorage.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
// Copyright (C) 2015-2025 The Neo Project.
//
// ApplicationEngineBase.SystemStorage.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 Microsoft.Extensions.Logging;
using Neo.Build.Core.Logging;
using Neo.Extensions;
using Neo.SmartContract;
using Neo.SmartContract.Iterators;
using System;
using System.Linq;
namespace Neo.Build.Core.SmartContract
{
public partial class ApplicationEngineBase
{
protected virtual StorageContext SystemStorageGetContext()
{
_traceLogger.LogInformation(VMEventLog.Call,
"{SysCall}",
nameof(System_Storage_GetContext));
var result = GetStorageContext();
string[] resultString = [result.Id.ToString(), result.IsReadOnly.ToString()];
_traceLogger.LogInformation(VMEventLog.Result,
"{SysCall} result={Result}",
nameof(System_Storage_GetContext), string.Join(';', resultString));
return result;
}
protected virtual StorageContext SystemStorageGetReadOnlyContext()
{
_traceLogger.LogInformation(VMEventLog.Call,
"{SysCall}",
nameof(System_Storage_GetReadOnlyContext));
var result = GetReadOnlyContext();
string[] resultString = [result.Id.ToString(), result.IsReadOnly.ToString()];
_traceLogger.LogInformation(VMEventLog.Result,
"{SysCall} result={Result}",
nameof(System_Storage_GetReadOnlyContext), string.Join(';', resultString));
return result;
}
protected virtual StorageContext SystemStorageAsReadOnly(StorageContext storageContext)
{
_traceLogger.LogInformation(VMEventLog.Call,
"{SysCall} id={Id}, readonly={ReadOnly}",
nameof(System_Storage_AsReadOnly), storageContext.Id, storageContext.IsReadOnly);
var result = AsReadOnly(storageContext);
string[] resultStrings = [result.Id.ToString(), result.IsReadOnly.ToString()];
_traceLogger.LogInformation(VMEventLog.Result,
"{SysCall} result={Result}",
nameof(System_Storage_AsReadOnly), string.Join(';', resultStrings));
return result;
}
protected virtual ReadOnlyMemory<byte>? SystemStorageGet(StorageContext storageContext, byte[] key)
{
var keyString = GetStorageKeyValueString(key, _storageSettings.KeyFormat);
_traceLogger.LogInformation(VMEventLog.StorageGet,
"{SysCall} id={Id}, readonly={ReadOnly}, key={Key}",
nameof(System_Storage_Get), storageContext.Id, storageContext.IsReadOnly, keyString);
var result = Get(storageContext, key)?.Span.ToArray() ?? [];
var resultString = GetStorageKeyValueString(result, _storageSettings.ValueFormat);
_traceLogger.LogInformation(VMEventLog.StorageGet,
"{SysCall} result={Result}",
nameof(System_Storage_Get), resultString);
return result;
}
protected virtual IIterator SystemStorageFind(StorageContext storageContext, byte[] prefix, FindOptions options)
{
var prefixString = GetStorageKeyValueString(prefix, _storageSettings.KeyFormat);
_traceLogger.LogInformation(VMEventLog.StorageFind,
"{SysCall} id={Id}, readonly={ReadOnly}, prefix={Prefix}, options={Options}",
nameof(System_Storage_Find), storageContext.Id, storageContext.IsReadOnly, prefixString, options.ToString());
var result = Find(storageContext, prefix, options);
_traceLogger.LogInformation(VMEventLog.StorageFind,
"{SysCall} result={Result}",
nameof(System_Storage_Find), result.GetType().Name);
return result;
}
protected virtual void SystemStoragePut(StorageContext storageContext, byte[] key, byte[] value)
{
var keyString = GetStorageKeyValueString(key, _storageSettings.KeyFormat);
var valueString = GetStorageKeyValueString(value, _storageSettings.ValueFormat);
_traceLogger.LogInformation(VMEventLog.StoragePut,
"{SysCall} id={Id}, readonly={ReadOnly}, key={Key}, value={Value}",
nameof(System_Runtime_Platform), storageContext.Id, storageContext.IsReadOnly, keyString, valueString);
Put(storageContext, key, value);
}
protected virtual void SystemStorageDelete(StorageContext storageContext, byte[] key)
{
var keyString = GetStorageKeyValueString(key, _storageSettings.KeyFormat);
_traceLogger.LogInformation(VMEventLog.StorageDelete,
"{SysCall} id={Id}, readonly={ReadOnly}, key={Key}",
nameof(System_Runtime_Platform), storageContext.Id, storageContext.IsReadOnly, keyString);
Delete(storageContext, key);
}
private string GetStorageKeyValueString(byte[] data, TextFormatterType formatter) =>
formatter switch
{
TextFormatterType.HexString => data.ToHexString(),
TextFormatterType.String => _encoding.GetString(data),
TextFormatterType.ArrayString => $"[{string.Join(',', data.Select(static s => s.ToString("x02")))}]",
TextFormatterType.Default or
TextFormatterType.Base64String or
_ => System.Convert.ToBase64String(data),
};
}
}