-
Notifications
You must be signed in to change notification settings - Fork 21
/
test_storage.py
808 lines (585 loc) · 32.9 KB
/
test_storage.py
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
from boaconstructor import storage
from neo3.api import StackItemType
from boa3.internal.exception import CompilerError, CompilerWarning
from boa3.internal.neo.vm.opcode.Opcode import Opcode
from boa3.internal.neo.vm.type.Integer import Integer
from boa3.internal.neo.vm.type.String import String
from boa3.internal.neo3.contracts import FindOptions
from boa3_test.tests import boatestcase
class TestStorageInterop(boatestcase.BoaTestCase):
from boa3.internal.model.builtin.interop.interop import Interop
default_folder: str = 'test_sc/interop_test/storage'
storage_get_context_hash = Interop.StorageGetContext.interop_method_hash
storage_get_hash = Interop.StorageGet.interop_method_hash
storage_put_hash = Interop.StoragePut.interop_method_hash
storage_delete_hash = Interop.StorageDelete.interop_method_hash
def test_storage_get_bytes_key_compile(self):
expected_output = (
Opcode.INITSLOT
+ b'\x00'
+ b'\x01'
+ Opcode.LDARG0
+ Opcode.SYSCALL
+ self.storage_get_context_hash
+ Opcode.SYSCALL
+ self.storage_get_hash
+ Opcode.DUP
+ Opcode.ISNULL
+ Opcode.JMPIFNOT
+ Integer(5).to_byte_array(signed=True, min_length=1)
+ Opcode.DROP
+ Opcode.PUSHDATA1
+ Integer(0).to_byte_array(signed=False, min_length=1)
+ Opcode.RET
)
output, _ = self.assertCompile('StorageGetBytesKey.py')
self.assertEqual(expected_output, output)
output, _ = self.assertCompilerLogs(CompilerWarning.DeprecatedSymbol, 'StorageGetDeprecated.py')
self.assertEqual(expected_output, output)
async def test_storage_get_bytes_key_run(self):
await self.set_up_contract('StorageGetBytesKey.py')
result, _ = await self.call('Main', [b'123'], return_type=bytes)
self.assertEqual(b'', result)
def test_storage_get_str_key(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageGetStrKey.py')
def test_storage_get_mismatched_type(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageGetMismatchedType.py')
async def test_storage_get_check(self):
await self.set_up_contract('StorageGetCheck.py', compile_if_found=True)
key = b'example_key'
value = 42
expected = (0, False)
result, _ = await self.call('get_value', [key], return_type=tuple[int, bool])
self.assertEqual(expected, result)
contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertNotIn(key, contract_storage)
result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
expected = (value, True)
result, _ = await self.call('get_value', [key], return_type=tuple[int, bool])
self.assertEqual(expected, result)
contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertIn(key, contract_storage)
self.assertEqual(value, contract_storage[key])
async def test_storage_put_bytes_key_bytes_value(self):
await self.set_up_contract('StoragePutBytesKeyBytesValue.py')
storage_key_1 = b'test1'
storage_key_2 = b'test2'
stored_value = b'\x01\x02\x03'
result, _ = await self.call('Main', [storage_key_1], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
contract_storage = await self.get_storage()
self.assertIn(storage_key_1, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_1])
self.assertIn(storage_key_2, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_2])
def test_storage_put_bytes_key_int_value_compile(self):
value = Integer(123).to_byte_array()
expected_output = (
Opcode.INITSLOT
+ b'\x01'
+ b'\x01'
+ Opcode.PUSHINT8 + value
+ Opcode.STLOC0
+ Opcode.PUSHINT8 + value
+ Opcode.LDARG0
+ Opcode.SYSCALL
+ self.storage_get_context_hash
+ Opcode.SYSCALL
+ self.storage_put_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StoragePutBytesKeyIntValue.py')
self.assertEqual(expected_output, output)
output, _ = self.assertCompilerLogs(CompilerWarning.DeprecatedSymbol, 'StoragePutDeprecated.py')
self.assertEqual(expected_output, output)
async def test_storage_put_bytes_key_int_value(self):
await self.set_up_contract('StoragePutBytesKeyIntValue.py')
storage_key_1 = b'test1'
storage_key_2 = b'test2'
stored_value = 123
result, _ = await self.call('Main', [storage_key_1], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
contract_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertIn(storage_key_1, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_1])
self.assertIn(storage_key_2, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_2])
def test_storage_put_bytes_key_str_value_compile(self):
value = String('123').to_bytes()
expected_output = (
Opcode.INITSLOT
+ b'\x01'
+ b'\x01'
+ Opcode.PUSHDATA1
+ Integer(len(value)).to_byte_array(min_length=1, signed=True)
+ value
+ Opcode.STLOC0
+ Opcode.PUSHDATA1
+ Integer(len(value)).to_byte_array(min_length=1, signed=True)
+ value
+ Opcode.LDARG0
+ Opcode.SYSCALL
+ self.storage_get_context_hash
+ Opcode.SYSCALL
+ self.storage_put_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StoragePutBytesKeyStrValue.py')
self.assertEqual(expected_output, output)
async def test_storage_put_bytes_key_str_value(self):
await self.set_up_contract('StoragePutBytesKeyStrValue.py')
storage_key_1 = b'test1'
storage_key_2 = b'test2'
stored_value = '123'
result, _ = await self.call('Main', [storage_key_1], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
contract_storage = await self.get_storage(values_post_processor=storage.as_str)
self.assertIn(storage_key_1, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_1])
self.assertIn(storage_key_2, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_2])
def test_storage_put_str_key_bytes_value(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StoragePutStrKeyBytesValue.py')
def test_storage_put_str_key_int_value(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StoragePutStrKeyIntValue.py')
def test_storage_put_str_key_str_value(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StoragePutStrKeyStrValue.py')
def test_storage_put_mismatched_type_key(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StoragePutMismatchedTypeKey.py')
def test_storage_put_mismatched_type_value(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StoragePutMismatchedTypeValue.py')
def test_storage_delete_bytes_key_compile(self):
expected_output = (
Opcode.INITSLOT
+ b'\x00'
+ b'\x01'
+ Opcode.LDARG0
+ Opcode.SYSCALL
+ self.storage_get_context_hash
+ Opcode.SYSCALL
+ self.storage_delete_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StorageDeleteBytesKey.py')
self.assertStartsWith(output, expected_output)
output, _ = self.assertCompilerLogs(CompilerWarning.DeprecatedSymbol, 'StorageDeleteDeprecated.py')
self.assertStartsWith(output, expected_output)
async def test_storage_delete_bytes_key(self):
await self.set_up_contract('StorageDeleteBytesKey.py')
not_existing_key = b'unknown_key'
storage_key = b'example'
result, _ = await self.call('has_key', [not_existing_key], return_type=bool)
self.assertEqual(False, result)
result, _ = await self.call('has_key', [storage_key], return_type=bool)
self.assertEqual(True, result)
result, _ = await self.call('Main', [not_existing_key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('has_key', [storage_key], return_type=bool)
self.assertEqual(False, result)
contract_storage = await self.get_storage()
self.assertNotIn(not_existing_key, contract_storage)
self.assertNotIn(storage_key, contract_storage)
def test_storage_delete_str_key(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageDeleteStrKey.py')
def test_storage_delete_mismatched_type(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageDeleteMismatchedType.py')
async def test_storage_find_bytes_prefix(self):
await self.set_up_contract('StorageFindBytesPrefix.py')
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [b'example'], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=b'example')
self.assertEqual({}, contract_storage)
example_storage = {
'example_0': '0',
'example_1': '1',
'example_2': '3'
}
expected_result = [[key, value] for key, value in example_storage.items()]
for (key, value) in expected_result:
await self.call('put_on_storage', [key, value], return_type=None, signing_accounts=[self.genesis])
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [b'example'], return_type=list)
self.assertEqual(expected_result, result)
self.assertRegex(
str(context.exception),
fr"item is not of type 'StackItemType.\w+' but of type '{StackItemType.INTEROP_INTERFACE}'"
)
contract_storage = await self.get_storage(
prefix=b'example',
key_post_processor=storage.as_str,
values_post_processor=storage.as_str
)
self.assertEqual(example_storage, contract_storage)
def test_storage_find_str_prefix(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageFindStrPrefix.py')
def test_storage_find_mismatched_type(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'StorageFindMismatchedType.py')
def test_storage_get_context_compile(self):
expected_output = (
Opcode.SYSCALL
+ self.storage_get_context_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StorageGetContext.py')
self.assertEqual(expected_output, output)
async def test_storage_get_context(self):
await self.set_up_contract('StorageGetContext.py')
# StorageContext is an InteropInterface, so it is not possible validate outside of Neo
with self.assertRaises(ValueError) as context:
await self.call('main', [], return_type=None)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
def test_storage_get_read_only_context_compile(self):
from boa3.internal.model.builtin.interop.interop import Interop
expected_output = (
Opcode.SYSCALL
+ Interop.StorageGetReadOnlyContext.interop_method_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StorageGetReadOnlyContext.py')
self.assertEqual(expected_output, output)
async def test_storage_get_read_only_context(self):
await self.set_up_contract('StorageGetReadOnlyContext.py')
# StorageContext is an InteropInterface, so it is not possible validate outside of Neo
with self.assertRaises(ValueError) as context:
await self.call('main', [], return_type=None)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
async def test_storage_get_with_context(self):
await self.set_up_contract('StorageGetWithContext.py')
not_existing_key = b'unknown_key'
result, _ = await self.call('Main', [not_existing_key], return_type=bytes)
self.assertEqual(b'', result)
storage_key_1 = b'example'
storage_key_2 = b'test'
stored_value_1 = Integer(23).to_byte_array()
stored_value_2 = Integer(42).to_byte_array()
result, _ = await self.call('Main', [storage_key_1], return_type=bytes)
self.assertEqual(stored_value_1, result)
result, _ = await self.call('Main', [storage_key_2], return_type=bytes)
self.assertEqual(stored_value_2, result)
contract_storage = await self.get_storage()
self.assertNotIn(not_existing_key, contract_storage)
self.assertIn(storage_key_1, contract_storage)
self.assertEqual(stored_value_1, contract_storage[storage_key_1])
self.assertIn(storage_key_2, contract_storage)
self.assertEqual(stored_value_2, contract_storage[storage_key_2])
async def test_storage_put_with_context(self):
await self.set_up_contract('StoragePutWithContext.py')
storage_key_1 = b'test1'
storage_key_2 = b'test2'
stored_value = b'\x01\x02\x03'
result, _ = await self.call('Main', [storage_key_1], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key_2], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
contract_storage = await self.get_storage()
self.assertIn(storage_key_1, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_1])
self.assertIn(storage_key_2, contract_storage)
self.assertEqual(stored_value, contract_storage[storage_key_2])
def test_storage_delete_with_context_compile(self):
expected_output = (
Opcode.INITSLOT
+ b'\x01'
+ b'\x01'
+ Opcode.SYSCALL # context = get_context()
+ self.storage_get_context_hash
+ Opcode.STLOC0
+ Opcode.LDARG0 # delete(key, context)
+ Opcode.LDLOC0
+ Opcode.SYSCALL
+ self.storage_delete_hash
+ Opcode.RET
)
output, _ = self.assertCompile('StorageDeleteWithContext.py')
self.assertStartsWith(output, expected_output)
async def test_storage_delete_with_context(self):
await self.set_up_contract('StorageDeleteWithContext.py')
not_existing_key = 'unknown_key'
storage_key = 'example'
result, _ = await self.call('has_key', [not_existing_key], return_type=bool)
self.assertEqual(False, result)
result, _ = await self.call('has_key', [storage_key], return_type=bool)
self.assertEqual(True, result)
result, _ = await self.call('Main', [not_existing_key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('Main', [storage_key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('has_key', [storage_key], return_type=bool)
self.assertEqual(False, result)
contract_storage = await self.get_storage()
self.assertNotIn(not_existing_key, contract_storage)
self.assertNotIn(storage_key, contract_storage)
async def test_storage_find_with_context(self):
await self.set_up_contract('StorageFindWithContext.py')
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', ['example'], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=b'example')
self.assertEqual({}, contract_storage)
example_storage = {
'example_0': '0',
'example_1': '1',
'example_2': '3'
}
expected_result = [[key, value] for key, value in example_storage.items()]
for (key, value) in expected_result:
await self.call('put_on_storage', [key, value], return_type=None, signing_accounts=[self.genesis])
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [b'example'], return_type=list)
self.assertEqual(expected_result, result)
self.assertRegex(
str(context.exception),
fr"item is not of type 'StackItemType.\w+' but of type '{StackItemType.INTEROP_INTERFACE}'"
)
contract_storage = await self.get_storage(
prefix=b'example',
key_post_processor=storage.as_str,
values_post_processor=storage.as_str
)
self.assertEqual(example_storage, contract_storage)
async def test_storage_find_with_options(self):
await self.set_up_contract('StorageFindWithOptions.py')
prefix = 'example'
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=b'example')
self.assertEqual({}, contract_storage)
expected_result = [['_0', '0'],
['_1', '1'],
['_2', '2']
]
expected_storage = {item[0]: item[1] for item in expected_result}
for (key, value) in expected_result:
await self.call('put_on_storage', [(prefix + key), value], return_type=None, signing_accounts=[self.genesis])
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual(expected_result, result)
self.assertRegex(
str(context.exception),
fr"item is not of type 'StackItemType.\w+' but of type '{StackItemType.INTEROP_INTERFACE}'"
)
contract_storage = await self.get_storage(
prefix=String(prefix).to_bytes(),
remove_prefix=True,
key_post_processor=storage.as_str,
values_post_processor=storage.as_str
)
self.assertEqual(expected_storage, contract_storage)
async def test_boa2_storage_test(self):
await self.set_up_contract('StorageBoa2Test.py')
storage_key = b'something'
result, _ = await self.call('main', ['sget', storage_key, 'blah'], return_type=bytes)
self.assertEqual(b'', result)
result, _ = await self.call('main', ['sput', storage_key, 'blah'], return_type=bool, signing_accounts=[self.genesis])
self.assertEqual(True, result)
result, _ = await self.call('main', ['sget', storage_key, 'blah'], return_type=bytes)
self.assertEqual(b'blah', result)
result, _ = await self.call('main', ['sdel', storage_key, 'blah'], return_type=bool, signing_accounts=[self.genesis])
self.assertEqual(True, result)
result, _ = await self.call('main', ['sget', storage_key, 'blah'], return_type=bytes)
self.assertEqual(b'', result)
contract_storage = await self.get_storage()
self.assertNotIn(storage_key, contract_storage)
async def test_boa2_storage_test2(self):
await self.set_up_contract('StorageBoa2Test.py')
value = 10000000000
storage_key = Integer(100)
result, _ = await self.call('main', ['sget', storage_key, value], return_type=bytes)
self.assertEqual(b'', result)
result, _ = await self.call('main', ['sput', storage_key, value], return_type=bool, signing_accounts=[self.genesis])
self.assertEqual(True, result)
result, _ = await self.call('main', ['sget', storage_key, value], return_type=bytes)
self.assertEqual(Integer(value).to_byte_array(), result)
result, _ = await self.call('main', ['sdel', storage_key, value], return_type=bool, signing_accounts=[self.genesis])
self.assertEqual(True, result)
result, _ = await self.call('main', ['sget', storage_key, value], return_type=bytes)
self.assertEqual(b'', result)
contract_storage = await self.get_storage()
self.assertNotIn(storage_key.to_byte_array(), contract_storage)
async def test_storage_between_contracts(self):
await self.set_up_contract('StorageGetAndPut1.py')
contract2 = await self.compile_and_deploy('StorageGetAndPut2.py')
key = b'example_key'
value = 42
result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value', [key], return_type=int, target_contract=contract2)
self.assertEqual(0, result)
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(value, result)
contract1_storage = await self.get_storage(values_post_processor=storage.as_int)
self.assertIn(key, contract1_storage)
self.assertEqual(value, contract1_storage[key])
contract2_storage = await self.get_storage(target_contract=contract2)
self.assertNotIn(key, contract2_storage)
async def test_create_map(self):
await self.set_up_contract('StorageCreateMap.py')
map_key = b'example_'
storage_key = b'test1'
stored_value = b'123'
result, _ = await self.call('get_from_map', [storage_key], return_type=bytes)
self.assertEqual(b'', result)
result, _ = await self.call('insert_to_map', [storage_key, stored_value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_from_map', [b'test1'], return_type=bytes)
self.assertEqual(stored_value, result)
contract_storage = await self.get_storage()
self.assertNotIn(storage_key, contract_storage)
self.assertIn(map_key + storage_key, contract_storage)
self.assertEqual(stored_value, contract_storage[map_key + storage_key])
result, _ = await self.call('delete_from_map', [b'test1'], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_from_map', [b'test1'], return_type=bytes)
self.assertEqual(b'', result)
async def test_import_storage(self):
await self.set_up_contract('ImportStorage.py')
prefix = b'unit'
key = prefix + b'_test'
key_str = String.from_bytes(key)
value = 1234
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(0, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=prefix)
self.assertEqual({}, contract_storage)
result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(value, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([[key_str, Integer(value).to_byte_array()]], result)
self.assertRegex(
str(context.exception),
fr"item is not of type 'StackItemType.\w+' but of type '{StackItemType.INTEROP_INTERFACE}'"
)
contract_storage = await self.get_storage(prefix=prefix, key_post_processor=storage.as_str)
self.assertEqual({key_str: Integer(value).to_byte_array()}, contract_storage)
await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
await self.call('get_value', [key], return_type=int)
result, _ = await self.call('delete_value', [key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(0, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=prefix)
self.assertEqual({}, contract_storage)
async def test_import_interop_storage(self):
await self.set_up_contract('ImportInteropStorage.py')
prefix = b'unit'
key = prefix + b'_test'
key_str = String.from_bytes(key)
value = 1234
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(0, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=prefix)
self.assertEqual({}, contract_storage)
result, _ = await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(value, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([[key_str, Integer(value).to_byte_array()]], result)
self.assertRegex(
str(context.exception),
fr"item is not of type 'StackItemType.\w+' but of type '{StackItemType.INTEROP_INTERFACE}'"
)
contract_storage = await self.get_storage(prefix=prefix, key_post_processor=storage.as_str)
self.assertEqual({key_str: Integer(value).to_byte_array()}, contract_storage)
await self.call('put_value', [key, value], return_type=None, signing_accounts=[self.genesis])
await self.call('get_value', [key], return_type=int)
result, _ = await self.call('delete_value', [key], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value', [key], return_type=int)
self.assertEqual(0, result)
# TODO: #86drqwhx0 neo-go in the current version of boa-test-constructor is not configured to return Iterators
with self.assertRaises(ValueError) as context:
result, _ = await self.call('find_by_prefix', [prefix], return_type=list)
self.assertEqual([], result)
self.assertRegex(str(context.exception), 'Interop stack item only supports iterators')
contract_storage = await self.get_storage(prefix=prefix)
self.assertEqual({}, contract_storage)
async def test_as_read_only(self):
await self.set_up_contract('StorageAsReadOnly.py')
key = b'key'
value_old = 'old value'
value_new = 'new value'
# Putting old value in the storage
result, _ = await self.call('put_value_in_storage', [key, value_old], return_type=None, signing_accounts=[self.genesis])
self.assertIsNone(result)
result, _ = await self.call('get_value_in_storage_read_only', [key], return_type=str)
self.assertEqual(value_old, result)
result, _ = await self.call('get_value_in_storage', [key], return_type=str)
self.assertEqual(value_old, result)
result, _ = await self.call('get_value_in_storage_read_only', [key], return_type=str)
self.assertEqual(value_old, result)
result, _ = await self.call('get_value_in_storage', [key], return_type=str)
self.assertEqual(value_old, result)
# Trying to put a new value in the storage using read_only won't work
with self.assertRaises(boatestcase.FaultException) as context:
await self.call('put_value_in_storage_read_only', [key, value_new], return_type=None, signing_accounts=[self.genesis])
self.assertRegex(str(context.exception), 'storage.Context is read only')
async def test_find_options_values(self):
await self.set_up_contract('FindOptionsValues.py')
result, _ = await self.call('main', [FindOptions.KEYS_ONLY], return_type=int)
self.assertEqual(FindOptions.KEYS_ONLY, result)
result, _ = await self.call('option_keys_only', [], return_type=int)
self.assertEqual(FindOptions.KEYS_ONLY, result)
result, _ = await self.call('option_remove_prefix', [], return_type=int)
self.assertEqual(FindOptions.REMOVE_PREFIX, result)
result, _ = await self.call('option_values_only', [], return_type=int)
self.assertEqual(FindOptions.VALUES_ONLY, result)
result, _ = await self.call('option_deserialize_values', [], return_type=int)
self.assertEqual(FindOptions.DESERIALIZE_VALUES, result)
result, _ = await self.call('option_pick_field_0', [], return_type=int)
self.assertEqual(FindOptions.PICK_FIELD_0, result)
result, _ = await self.call('option_pick_field_1', [], return_type=int)
self.assertEqual(FindOptions.PICK_FIELD_1, result)
result, _ = await self.call('option_backwards', [], return_type=int)
self.assertEqual(FindOptions.BACKWARDS, result)
def test_find_options_mismatched_type(self):
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'FindOptionsMismatchedType.py')