Example #1
0
    def test_parameter_to_json_list(self):
        expected_result = {
            'type':
            AbiType.Array.value,
            'value': [{
                'type': AbiType.Integer.value,
                'value': 1
            }, {
                'type': AbiType.Integer.value,
                'value': 2
            }, {
                'type': AbiType.Integer.value,
                'value': 3
            }]
        }
        result = contract_parameter_to_json([1, 2, 3])
        self.assertEqual(expected_result, result)

        expected_result = {
            'type':
            AbiType.Array.value,
            'value': [{
                'type': AbiType.Boolean.value,
                'value': True
            }, {
                'type': AbiType.Integer.value,
                'value': 2
            }, {
                'type': AbiType.String.value,
                'value': '3'
            }]
        }
        result = contract_parameter_to_json([True, 2, '3'])
        self.assertEqual(expected_result, result)
Example #2
0
    def test_parameter_to_json_tuple(self):
        expected_result = {
            'type':
            AbiType.Array.value,
            'value': [{
                'type': AbiType.ByteArray.value,
                'value': 'AQ=='
            }, {
                'type': AbiType.ByteArray.value,
                'value': 'Ag=='
            }, {
                'type': AbiType.ByteArray.value,
                'value': 'Aw=='
            }]
        }
        result = contract_parameter_to_json((b'\x01', b'\x02', b'\x03'))
        self.assertEqual(expected_result, result)

        expected_result = {
            'type':
            AbiType.Array.value,
            'value': [{
                'type': AbiType.Boolean.value,
                'value': True
            }, {
                'type': AbiType.Integer.value,
                'value': 2
            }, {
                'type': AbiType.String.value,
                'value': '3'
            }]
        }
        result = contract_parameter_to_json((True, 2, '3'))
        self.assertEqual(expected_result, result)
Example #3
0
    def test_parameter_to_json_int(self):
        expected_result = {'type': AbiType.Integer.value, 'value': 5}
        result = contract_parameter_to_json(5)
        self.assertEqual(expected_result, result)

        expected_result = {'type': AbiType.Integer.value, 'value': 5000000}
        result = contract_parameter_to_json(5000000)
        self.assertEqual(expected_result, result)
Example #4
0
    def test_parameter_to_json_bool(self):
        expected_result = {'type': AbiType.Boolean.value, 'value': True}
        result = contract_parameter_to_json(True)
        self.assertEqual(expected_result, result)

        expected_result = {'type': AbiType.Boolean.value, 'value': False}
        result = contract_parameter_to_json(False)
        self.assertEqual(expected_result, result)
Example #5
0
 def test_parameter_to_json_dict(self):
     expected_result = {
         'type':
         AbiType.Map.value,
         'value': [{
             'key': {
                 'type': AbiType.String.value,
                 'value': 'a'
             },
             'value': {
                 'type': AbiType.Integer.value,
                 'value': 1
             }
         }, {
             'key': {
                 'type': AbiType.String.value,
                 'value': 'b'
             },
             'value': {
                 'type': AbiType.Boolean.value,
                 'value': False
             }
         }]
     }
     result = contract_parameter_to_json({'a': 1, 'b': False})
     self.assertEqual(expected_result, result)
Example #6
0
    def test_parameter_to_json_str(self):
        expected_result = {'type': AbiType.String.value, 'value': 'unittest'}
        result = contract_parameter_to_json('unittest')
        self.assertEqual(expected_result, result)

        long_string = (
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam accumsan magna eu massa '
            'vulputate bibendum. Aliquam commodo euismod tristique. Sed purus erat, pretium ut interdum '
            'et, aliquet sed mauris. Curabitur vitae turpis euismod, hendrerit mi a, rhoncus justo. Mauris '
            'sollicitudin, nisl sit amet feugiat pharetra, odio ligula congue tellus, vel pellentesque '
            'libero leo id dui. Morbi vel risus vehicula, consectetur mauris eget, gravida ligula. '
            'Maecenas aliquam velit sit amet nisi ultricies, ac sollicitudin nisi mollis. Lorem ipsum '
            'dolor sit amet, consectetur adipiscing elit. Ut tincidunt, nisi in ullamcorper ornare, '
            'est enim dictum massa, id aliquet justo magna in purus.')
        expected_result = {'type': AbiType.String.value, 'value': long_string}
        result = contract_parameter_to_json(long_string)
        self.assertEqual(expected_result, result)
Example #7
0
    def test_parameter_to_json_bytes(self):
        expected_result = {
            'type': AbiType.ByteArray.value,
            'value': 'AQIDBAU='
        }
        result = contract_parameter_to_json(b'\x01\x02\x03\x04\x05')
        self.assertEqual(expected_result, result)

        long_bytes = (String(
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam accumsan magna eu massa '
            'vulputate bibendum. Aliquam commodo euismod tristique. Sed purus erat, pretium ut '
            'interdum et, aliquet sed mauris. Curabitur vitae turpis euismod, hendrerit mi a, '
            'rhoncus justo. Mauris sollicitudin, nisl sit amet feugiat pharetra, odio ligula congue '
            'tellus, vel pellentesque libero leo id dui. Morbi vel risus vehicula, consectetur '
            'mauris eget, gravida ligula. Maecenas aliquam velit sit amet nisi ultricies, '
            'ac sollicitudin nisi mollis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
            'Ut tincidunt, nisi in ullamcorper ornare, est enim dictum massa, id aliquet justo magna '
            'in purus.').to_bytes())
        long_encoded = (
            'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTnVsbGFtIGFjY3V'
            +
            'tc2FuIG1hZ25hIGV1IG1hc3NhIHZ1bHB1dGF0ZSBiaWJlbmR1bS4gQWxpcXVhbSBjb21tb2RvIGV1aXNtb2QgdHJpc3'
            +
            'RpcXVlLiBTZWQgcHVydXMgZXJhdCwgcHJldGl1bSB1dCBpbnRlcmR1bSBldCwgYWxpcXVldCBzZWQgbWF1cmlzLiBDd'
            +
            'XJhYml0dXIgdml0YWUgdHVycGlzIGV1aXNtb2QsIGhlbmRyZXJpdCBtaSBhLCByaG9uY3VzIGp1c3RvLiBNYXVyaXMg'
            +
            'c29sbGljaXR1ZGluLCBuaXNsIHNpdCBhbWV0IGZldWdpYXQgcGhhcmV0cmEsIG9kaW8gbGlndWxhIGNvbmd1ZSB0ZWx'
            +
            'sdXMsIHZlbCBwZWxsZW50ZXNxdWUgbGliZXJvIGxlbyBpZCBkdWkuIE1vcmJpIHZlbCByaXN1cyB2ZWhpY3VsYSwgY2'
            +
            '9uc2VjdGV0dXIgbWF1cmlzIGVnZXQsIGdyYXZpZGEgbGlndWxhLiBNYWVjZW5hcyBhbGlxdWFtIHZlbGl0IHNpdCBhb'
            +
            'WV0IG5pc2kgdWx0cmljaWVzLCBhYyBzb2xsaWNpdHVkaW4gbmlzaSBtb2xsaXMuIExvcmVtIGlwc3VtIGRvbG9yIHNp'
            +
            'dCBhbWV0LCBjb25zZWN0ZXR1ciBhZGlwaXNjaW5nIGVsaXQuIFV0IHRpbmNpZHVudCwgbmlzaSBpbiB1bGxhbWNvcnB'
            +
            'lciBvcm5hcmUsIGVzdCBlbmltIGRpY3R1bSBtYXNzYSwgaWQgYWxpcXVldCBqdXN0byBtYWduYSBpbiBwdXJ1cy4='
        )
        expected_result = {
            'type': AbiType.ByteArray.value,
            'value': long_encoded
        }
        result = contract_parameter_to_json(long_bytes)
        self.assertEqual(expected_result, result)
Example #8
0
 def to_json(self, contract_id: Union[str, UInt160], method: str, *args: Any) -> Dict[str, Any]:
     json = {
         'path': contract_id if isinstance(contract_id, str) else '',
         'scripthash': str(contract_id) if not isinstance(contract_id, str) else None,
         'method': method,
         'arguments': [contract_parameter_to_json(x) for x in args],
         'storage': self._storage.to_json(),
         'contracts': [{'nef': contract_path} for contract_path in self.contracts],
         'signeraccounts': [address.to_json() for address in self._accounts],
         'height': self.height,
         'blocks': [block.to_json() for block in self.blocks]
     }
     if isinstance(self._current_tx, Transaction):
         json['currenttx'] = self._current_tx.to_json()
     return json
Example #9
0
 def to_json(self, path: Union[str, UInt160], method: str,
             *args: Any) -> Dict[str, Any]:
     return {
         'path':
         path if isinstance(path, str) else '',
         'scripthash':
         str(path) if isinstance(path, UInt160) else None,
         'method':
         method,
         'arguments': [contract_parameter_to_json(x) for x in args],
         'storage':
         self._storage.to_json(),
         'contracts': [{
             'nef': contract_path
         } for contract_path in self._contract_paths],
         'signerAccounts':
         [to_hex_str(address) for address in self._accounts],
         'height':
         self.height,
         'blocks': [block.to_json() for block in self.blocks]
     }
Example #10
0
 def to_json(self) -> Dict[str, Any]:
     return {
         'isconstant': self._is_constant,
         'value': contract_parameter_to_json(self._value)
     }
Example #11
0
 def to_json(self) -> Dict[str, Any]:
     return {'id': self._ID, 'key': contract_parameter_to_json(self._key)}
Example #12
0
 def test_parameter_to_json_none(self):
     expected_result = {
         'type': AbiType.Any.value,
     }
     result = contract_parameter_to_json(None)
     self.assertEqual(expected_result, result)
Example #13
0
    def run(self,
            nef_path: str,
            method: str,
            *arguments: Any,
            reset_engine: bool = False) -> Any:
        import json
        import subprocess

        contract_parameters = [
            contract_parameter_to_json(x) for x in arguments
        ]
        param_json = json.dumps(contract_parameters).replace(' ', '')

        process = subprocess.Popen(
            ['dotnet', self._test_engine_path, nef_path, method, param_json],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True)
        stdout, stderr = process.communicate()

        if reset_engine:
            self.reset_engine()
        else:
            self.reset_state()

        stdout = stdout.splitlines()[-1]

        try:
            result = json.loads(stdout)

            self._error_message = result['error'] if 'error' in result else None
            if 'vm_state' in result:
                self._vm_state = VMState.get_vm_state(result['vm_state'])

            if 'gasconsumed' in result:
                self._gas_consumed = result['gasconsumed']

            if 'result_stack' in result:
                if isinstance(result['result_stack'], list):
                    self._result_stack = [
                        stack_item_from_json(value)
                        for value in result['result_stack']
                    ]
                else:
                    self._result_stack = [
                        stack_item_from_json(result['result_stack'])
                    ]

            if 'notifications' in result:
                json_notifications = result['notifications']
                if not isinstance(json_notifications, list):
                    json_notifications = [json_notifications]

                notifications = []
                for n in json_notifications:
                    new = Notification.from_json(n)
                    if new is not None:
                        notifications.append(new)
                self._notifications = notifications

        except BaseException as e:
            self._error_message = str(e)

        # TODO: convert the result to the return type of the function in the manifest
        return self._result_stack[-1] if len(self._result_stack) > 0 else None