From fd685763ee98293c7baf657685927ef3874415a8 Mon Sep 17 00:00:00 2001 From: NashMiao <18191964+NashMiao@users.noreply.github.com> Date: Mon, 4 Nov 2019 00:56:59 -0800 Subject: [PATCH] update UT & fix bug (#394) * update UT * update UT * update UT * update README * update UT & fix bug --- ontology/contract/wasm/params_builder.py | 4 +- setup.py | 2 +- tests/test_wasm_params_builder.py | 54 +++++++++++++++++++----- tests/test_wasm_vm.py | 2 + 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/ontology/contract/wasm/params_builder.py b/ontology/contract/wasm/params_builder.py index 1366b25..b5a62ce 100644 --- a/ontology/contract/wasm/params_builder.py +++ b/ontology/contract/wasm/params_builder.py @@ -73,10 +73,10 @@ def write_var_uint(self, value: int): raise SDKException(ErrorCode.other_error('invalid data')) elif value < 0xFD: self.write_bytes(value.to_bytes(length=1, byteorder='little', signed=False)) - elif value < 0xFFFF: + elif value <= 0xFFFF: self.write_bytes(b'\xFD') self.write_bytes(value.to_bytes(length=2, byteorder='little', signed=False)) - elif value < 0xFFFFFFFF: + elif value <= 0xFFFFFFFF: self.write_bytes(b'\xFE') self.write_bytes(value.to_bytes(length=4, byteorder='little', signed=False)) else: diff --git a/setup.py b/setup.py index 770eeeb..5dfbd51 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ setup( name='ontology-python-sdk', - version='2.1.0.RC2', + version='2.1.0.RC3', description='Comprehensive Python library for the Ontology BlockChain.', long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_wasm_params_builder.py b/tests/test_wasm_params_builder.py index 4af401b..9dd814c 100644 --- a/tests/test_wasm_params_builder.py +++ b/tests/test_wasm_params_builder.py @@ -44,10 +44,22 @@ def test_push_int(self): self.assertRaises(SDKException, self.builder.push_int, WASM_INT128_MIN - 1) def test_push_address(self): - b58_address_list = ['AS3SCXw8GKTEeXpdwVw7EcC4rqSebFYpfb', 'AJkkLbouowk6teTaxz1F2DYKfJh24PVk3r', - 'Ad4pjz2bqep4RhQrUAzMuZJkBC3qJ1tZuT', 'AK98G45DhmPXg4TFPG1KjftvkEaHbU8SHM'] - wasm_address_list = ['70a2ababdae0a9d1f9fc7296df3c6d343b772cf7', '20b1dc499cdf56ba70a574a1e17ac986d1f06ec2', - 'e98f4998d837fcdd44a50561f7f32140c7c6c260', '24ed4f965d3a5a76f5d0e87633c0b76941fc8827'] + b58_address_list = [ + 'AS7MjVEicEsJ4zjEfm2LoKoYoFsmapD7rT', + 'AFmseVrdL9f9oyCzZefL9tG6UbviEH9ugK', + 'AS3SCXw8GKTEeXpdwVw7EcC4rqSebFYpfb', + 'AJkkLbouowk6teTaxz1F2DYKfJh24PVk3r', + 'Ad4pjz2bqep4RhQrUAzMuZJkBC3qJ1tZuT', + 'AK98G45DhmPXg4TFPG1KjftvkEaHbU8SHM' + ] + wasm_address_list = [ + '71609b2c2f7b9447b089ad1da31586f42ca9eb10', + '0000000000000000000000000000000000000007', + '70a2ababdae0a9d1f9fc7296df3c6d343b772cf7', + '20b1dc499cdf56ba70a574a1e17ac986d1f06ec2', + 'e98f4998d837fcdd44a50561f7f32140c7c6c260', + '24ed4f965d3a5a76f5d0e87633c0b76941fc8827' + ] for index, b58_address in enumerate(b58_address_list): self.builder.push_address(Address.b58decode(b58_address)) self.assertEqual(wasm_address_list[index], self.builder.to_bytes().hex()) @@ -89,16 +101,36 @@ def test_push_list(self): self.builder.push_list(py_list) self.assertEqual(wasm_list, self.builder.to_bytes().hex()) - def test_read_var_int(self): - wasm_hex_uint = ['00', '01', 'fdfe00', 'fe00000100', 'fe01000100', - 'feffffffff', 'ff0000000001000000', 'ffffffffffffffff0f', 'ffffffffffffffffff', - 'ffffffffffffffffffff', 'ffffffffffffffffffffffff'] - int_data = [0, 1, 254, 65536, 65537, - 4294967295, 4294967296, 1152921504606846975, 18446744073709551615, - 18446744073709551615, 18446744073709551615] + def test_read_var_uint(self): + wasm_hex_uint = [ + '00', + '01', + 'fdfe00', + 'fe00000100', + 'fe01000100', + 'feffffffff', + 'ff0000000001000000', + 'ffffffffffffffff0f', + 'ffffffffffffffffff', + ] + int_data = [ + 0, + 1, + 254, + 65536, + 65537, + 4294967295, + 4294967296, + 1152921504606846975, + 18446744073709551615 + ] for index, hex_uint in enumerate(wasm_hex_uint): + self.builder.write_var_uint(int_data[index]) + self.assertEqual(wasm_hex_uint[index], self.builder.to_bytes().hex()) + self.builder.clear_up() self.builder.set_buffer(hex_uint) self.assertEqual(int_data[index], self.builder.read_var_uint()) + self.builder.clear_up() def test_pop_str_struct(self): hex_wasm_str = '01606438646337313735616137333633323165343639643539623663303661396531666462356432373735633' \ diff --git a/tests/test_wasm_vm.py b/tests/test_wasm_vm.py index af68046..2330e80 100644 --- a/tests/test_wasm_vm.py +++ b/tests/test_wasm_vm.py @@ -86,6 +86,7 @@ def test_invoke_add_transaction(self): func.set_params_value(-2, 3) tx = sdk.wasm_vm.make_invoke_transaction(self.basic_test_case_contract_address, func, acct3.get_address(), self.gas_price, self.gas_limit) + print(tx.payload.hex()) tx.sign_transaction(acct3) result = sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result', '') self.assertEqual('01000000000000000000000000000000', result) @@ -98,6 +99,7 @@ def test_invoke_add_transaction(self): '000000000000000000000000002000000000000000000000000000000' self.assertEqual(target_payload, tx.payload.hex()) tx.sign_transaction(acct3) + print(tx.serialize().hex()) result = sdk.rpc.send_raw_transaction_pre_exec(tx).get('Result', '') self.assertEqual('03000000000000000000000000000000', result) self.assertEqual(3, WasmData.to_int(result))