Exemple #1
0
    def test_deserialization_basics(self):
        # test empty object
        item = contracts.JSONSerializer.deserialize(
            contracts.NEOJson.loads("{}"), vm.ReferenceCounter())
        self.assertIsInstance(item, vm.MapStackItem)
        self.assertEqual(0, len(item))

        # test empty array
        item = contracts.JSONSerializer.deserialize(
            contracts.NEOJson.loads("[]"), vm.ReferenceCounter())
        self.assertIsInstance(item, vm.ArrayStackItem)
        self.assertEqual(0, len(item))
Exemple #2
0
    def _post_transfer(self, engine: contracts.ApplicationEngine,
                       account_from: types.UInt160, account_to: types.UInt160,
                       amount: vm.BigInteger, data: vm.StackItem,
                       call_on_payment: bool) -> None:
        state = vm.ArrayStackItem(vm.ReferenceCounter())
        if account_from == types.UInt160.zero():
            state.append(vm.NullStackItem())
        else:
            state.append(vm.ByteStringStackItem(account_from.to_array()))
        if account_to == types.UInt160.zero():
            state.append(vm.NullStackItem())
        else:
            state.append(vm.ByteStringStackItem(account_to.to_array()))
        state.append(vm.IntegerStackItem(amount))

        msgrouter.interop_notify(self.hash, "Transfer", state)

        # wallet or smart contract
        if not call_on_payment \
                or account_to == types.UInt160.zero() \
                or contracts.ManagementContract().get_contract(engine.snapshot, account_to) is None:
            return

        if account_from == types.UInt160.zero():
            from_: vm.StackItem = vm.NullStackItem()
        else:
            from_ = vm.ByteStringStackItem(account_from.to_array())
        engine.call_from_native(
            self.hash, account_to, "onNEP17Payment",
            [from_, vm.IntegerStackItem(amount), data])
Exemple #3
0
    def test_deserialization_array_of_items(self):
        ref_ctr = vm.ReferenceCounter()
        array = contracts.JSONSerializer.deserialize(
            contracts.NEOJson.loads(
                r'[[true,"test1", 123, null],[false,"test2",321]]'), ref_ctr)
        self.assertIsInstance(array, vm.ArrayStackItem)
        self.assertEqual(2, len(array))

        sub_array1 = array[0]
        self.assertIsInstance(sub_array1, vm.ArrayStackItem)
        self.assertEqual(4, len(sub_array1))

        self.assertIsInstance(sub_array1[0], vm.BooleanStackItem)
        self.assertTrue(sub_array1[0])

        self.assertIsInstance(sub_array1[1], vm.ByteStringStackItem)
        self.assertEqual(vm.ByteStringStackItem("test1"), sub_array1[1])

        self.assertIsInstance(sub_array1[2], vm.IntegerStackItem)
        self.assertEqual(vm.BigInteger(123), sub_array1[2].to_biginteger())

        self.assertIsInstance(sub_array1[3], vm.NullStackItem)

        sub_array2 = array[1]
        self.assertIsInstance(sub_array2, vm.ArrayStackItem)
        self.assertEqual(3, len(sub_array2))

        self.assertIsInstance(sub_array2[0], vm.BooleanStackItem)
        self.assertFalse(sub_array2[0])

        self.assertIsInstance(sub_array2[1], vm.ByteStringStackItem)
        self.assertEqual(vm.ByteStringStackItem("test2"), sub_array2[1])

        self.assertIsInstance(sub_array2[2], vm.IntegerStackItem)
        self.assertEqual(vm.BigInteger(321), sub_array2[2].to_biginteger())
Exemple #4
0
 def tokens_of(self, snapshot: storage.Snapshot, owner: types.UInt160) -> interop.IIterator:
     storage_item_account = snapshot.storages.try_get(self.key_account + owner.to_array(), read_only=True)
     reference_counter = vm.ReferenceCounter()
     if storage_item_account is None:
         return interop.ArrayWrapper(vm.ArrayStackItem(reference_counter))
     account = storage_item_account.get(NFTAccountState)
     tokens: List[vm.StackItem] = list(map(lambda t: vm.ByteStringStackItem(t), account.tokens))
     return interop.ArrayWrapper(vm.ArrayStackItem(reference_counter, tokens))
Exemple #5
0
 def tokens(self, snapshot: storage.Snapshot) -> interop.IIterator:
     result = snapshot.storages.find(self.key_token.to_array())
     options = contracts.FindOptions
     # this deviates from C#, but we can't use a 'null' as reference counter.
     reference_counter = vm.ReferenceCounter()
     return interop.StorageIterator(result,
                                    options.VALUES_ONLY | options.DESERIALIZE_VALUES | options.PICK_FIELD1,
                                    reference_counter)
Exemple #6
0
 def test_serialization_map_with_integer_key(self):
     i = vm.IntegerStackItem(123)
     v = vm.IntegerStackItem(321)
     ref_ctr = vm.ReferenceCounter()
     m = vm.MapStackItem(ref_ctr)
     m[i] = v
     expected = r'{"123":321}'
     s = contracts.JSONSerializer.serialize(m, 999)
     self.assertEqual(expected, s)
Exemple #7
0
 def test_serialization_array(self):
     b = vm.BooleanStackItem(True)
     bs = vm.ByteStringStackItem("test")
     i = vm.IntegerStackItem(123)
     n = vm.NullStackItem()
     ref_ctr = vm.ReferenceCounter()
     a = vm.ArrayStackItem(ref_ctr)
     a.append([b, bs, i, n])
     expected = r'[true,"test",123,null]'
     self.assertEqual(expected, contracts.JSONSerializer.serialize(a, 999))
Exemple #8
0
    def test_deserialization_map(self):
        ref_ctr = vm.ReferenceCounter()
        item = contracts.JSONSerializer.deserialize(
            contracts.NEOJson.loads(r'{"test1":123, "test2": 321}'), ref_ctr)
        self.assertIsInstance(item, vm.MapStackItem)
        self.assertEqual(2, len(item))

        key1 = vm.ByteStringStackItem("test1")
        self.assertEqual(vm.BigInteger(123), item[key1].to_biginteger())
        key2 = vm.ByteStringStackItem("test2")
        self.assertEqual(vm.BigInteger(321), item[key2].to_biginteger())
Exemple #9
0
    def test_serialization_basics(self):
        ref = vm.ReferenceCounter()
        m = vm.MapStackItem(ref)
        s = contracts.JSONSerializer.serialize(m, 999)
        self.assertEqual("{}", s)

        a = vm.ArrayStackItem(ref)
        s = contracts.JSONSerializer.serialize(a, 999)
        self.assertEqual(r'[]', s)

        i1 = vm.IntegerStackItem(1)
        i2 = vm.IntegerStackItem(9007199254740992)
        a.append([i1, i2])
        s = contracts.JSONSerializer.serialize(a, 999)
        self.assertEqual(r'[1,"9007199254740992"]', s)
Exemple #10
0
 def test_serialization_map(self):
     ref = vm.ReferenceCounter()
     key1 = vm.ByteStringStackItem("test1")
     key2 = vm.ByteStringStackItem("test2")
     key3 = vm.ByteStringStackItem("test3")
     v1 = vm.IntegerStackItem(1)
     v2 = vm.IntegerStackItem(2)
     v3 = vm.IntegerStackItem(3)
     m = vm.MapStackItem(ref)
     m[key1] = v1
     m[key3] = v3
     m[key2] = v2
     s = contracts.JSONSerializer.serialize(m, 999)
     # this is a known deviation. NEO preserved key order, we don't
     # but shouldn't matter as it gets deserialized to a map stackitem
     expected = r'{"test1":1,"test2":2,"test3":3}'
     self.assertEqual(expected, s)
Exemple #11
0
    def test_serialization_array_nested(self):
        bool_t = vm.BooleanStackItem(True)
        bool_f = vm.BooleanStackItem(False)
        bs1 = vm.ByteStringStackItem("test1")
        bs2 = vm.ByteStringStackItem("test2")

        i1 = vm.IntegerStackItem(123)
        i2 = vm.IntegerStackItem(321)
        ref_ctr = vm.ReferenceCounter()

        a1 = vm.ArrayStackItem(ref_ctr)
        a1.append([bool_t, bs1, i1])

        a2 = vm.ArrayStackItem(ref_ctr)
        a2.append([bool_f, bs2, i2])

        parent = vm.ArrayStackItem(ref_ctr)
        parent.append([a1, a2])
        expected = r'[[true,"test1",123],[false,"test2",321]]'
        self.assertEqual(expected,
                         contracts.JSONSerializer.serialize(parent, 999))
Exemple #12
0
 def setUpClass(cls) -> None:
     cls.reference_counter = vm.ReferenceCounter()