-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathExampleStorage.cs
More file actions
229 lines (194 loc) · 7.63 KB
/
ExampleStorage.cs
File metadata and controls
229 lines (194 loc) · 7.63 KB
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
// Copyright (C) 2015-2026 The Neo Project.
//
// Storage.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 Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Attributes;
using Neo.SmartContract.Framework.Services;
using System.ComponentModel;
namespace ExampleStorage
{
[DisplayName("SampleStorage")]
[ContractAuthor("code-dev", "dev@neo.org")]
[ContractDescription("A sample contract to demonstrate how to use storage")]
[ContractVersion("0.0.1")]
[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/examples/")]
[ContractPermission(Permission.Any, Method.Any)]
public class SampleStorage : SmartContract
{
#region Byte
public static bool TestPutByte(byte[] key, byte[] value)
{
var storage = new StorageMap(0x11);
storage.Put((ByteString)key, (ByteString)value);
return true;
}
public static void TestDeleteByte(byte[] key)
{
var storage = new StorageMap(0x11);
storage.Delete((ByteString)key);
}
public static byte[] TestGetByte(byte[] key)
{
var storage = new StorageMap(0x11);
var value = storage.Get((ByteString)key);
return (byte[])value;
}
public static byte[] TestOver16Bytes()
{
var value = new byte[] { 0x3b, 0x00, 0x32, 0x03, 0x23, 0x23, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02, 0x23, 0x23, 0x02 };
StorageMap storageMap = new StorageMap("test_map");
storageMap.Put((ByteString)new byte[] { 0x01 }, (ByteString)value);
return (byte[])storageMap.Get((ByteString)new byte[] { 0x01 });
}
#endregion
#region String
public static bool TestPutString(byte[] key, byte[] value)
{
var prefix = "aa";
var storage = new StorageMap(prefix);
storage.Put((ByteString)key, (ByteString)value);
return true;
}
public static void TestDeleteString(byte[] key)
{
var prefix = "aa";
var storage = new StorageMap(prefix);
storage.Delete((ByteString)key);
}
public static byte[] TestGetString(byte[] key)
{
var prefix = "aa";
var storage = new StorageMap(prefix);
var value = storage.Get((ByteString)key);
return (byte[])value;
}
#endregion
#region ByteArray
public static bool TestPutByteArray(byte[] key, byte[] value)
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
storage.Put((ByteString)key, (ByteString)value);
return true;
}
public static void TestDeleteByteArray(byte[] key)
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
storage.Delete((ByteString)key);
}
public static byte[] TestGetByteArray(byte[] key)
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
var value = storage.Get((ByteString)key);
return (byte[])value;
}
public static bool TestNewGetMethods()
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
var boolValue = true;
var intValue = 123;
var stringValue = "hello world";
var uint160Value = (UInt160)new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
};
var uint256Value = (UInt256)new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01
};
var ecPointValue = (ECPoint)new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x00, 0x01, 0x02
};
storage.Put("bool", boolValue);
storage.Put("int", intValue);
storage.Put("string", stringValue);
storage.Put("uint160", uint160Value);
storage.Put("uint256", uint256Value);
storage.Put("ecpoint", ecPointValue);
var boolValue2 = storage.GetBoolean("bool");
var intValue2 = storage.GetInteger("int");
var stringValue2 = storage.GetString("string");
var uint160Value2 = storage.GetUInt160("uint160");
var uint256Value2 = storage.GetUInt256("uint256");
var ecPointValue2 = storage.GetECPoint("ecpoint");
return boolValue == boolValue2
&& intValue == intValue2
&& stringValue == stringValue2
&& uint160Value == uint160Value2
&& uint256Value == uint256Value2
&& ecPointValue == ecPointValue2;
}
public static byte[] TestNewGetByteArray()
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
var byteArray = new byte[] { 0x00, 0x01 };
storage.Put("byteArray", byteArray);
var byteArray2 = storage.GetByteArray("byteArray");
return byteArray2;
}
#endregion
public static bool TestPutReadOnly(byte[] key, byte[] value)
{
var prefix = new byte[] { 0x00, 0xFF };
var storage = new StorageMap(prefix);
storage.Put((ByteString)key, (ByteString)value);
return true;
}
#region Serialize
class Value
{
public int Val;
}
public static int SerializeTest(byte[] key, int value)
{
var prefix = new byte[] { 0x01, 0xAA };
var storage = new StorageMap(prefix);
var val = new Value { Val = value };
storage.PutObject(key, val);
val = (Value)storage.GetObject(key);
return val.Val;
}
#endregion
#region Find
public static byte[] TestFind()
{
Storage.Put((ByteString)"key1", (ByteString)new byte[] { 0x01 });
Storage.Put((ByteString)"key2", (ByteString)new byte[] { 0x02 });
var iterator = (Iterator<byte[]>)Storage.Find("key", FindOptions.ValuesOnly);
iterator.Next();
return iterator.Value;
}
#endregion
#region IndexProperty
public static bool TestIndexPut(byte[] key, byte[] value)
{
var prefix = "ii";
var storage = new StorageMap(prefix);
storage[(ByteString)key] = (ByteString)value;
return true;
}
public static byte[] TestIndexGet(byte[] key)
{
var prefix = "ii";
var storage = new StorageMap(prefix);
return (byte[])storage[(ByteString)key];
}
#endregion
}
}