Ver código fonte

Substrate: Test for instantiation nonce to increase (#1235)

Cyrill Leutwiler 2 anos atrás
pai
commit
a5561a6827

+ 12 - 2
integration/substrate/create_contract.spec.ts

@@ -1,5 +1,5 @@
 import expect from 'expect';
-import { weight, createConnection, deploy, transaction, aliceKeypair, query, } from './index';
+import { weight, createConnection, deploy, transaction, aliceKeypair, query, debug_buffer, dry_run, } from './index';
 import { ContractPromise } from '@polkadot/api-contract';
 import { ApiPromise } from '@polkadot/api';
 
@@ -27,7 +27,12 @@ describe('Deploy create_contract contract and test', () => {
 
         let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
 
-        let gasLimit = await weight(conn, contract, "createChild");
+        let dry = await dry_run(conn, contract, "createChild");
+        // Expect the instantiation nonce to be present
+        const current_nonce = dry.debugMessage.split('\n')[0].split('=');
+        expect(current_nonce[0]).toEqual("call: instantiation_nonce");
+
+        const gasLimit = dry.gasRequired;
         let tx = contract.tx.createChild({ gasLimit });
 
         await transaction(tx, alice);
@@ -44,5 +49,10 @@ describe('Deploy create_contract contract and test', () => {
         let { data: { free: childBalance } } = await conn.query.system.account(child);
 
         expect(BigInt(1e15) - childBalance.toBigInt()).toBeLessThan(1e11);
+
+        // Expect the instantiation nonce to be present and to increase
+        const next_nonce = (await debug_buffer(conn, contract, "createChild")).split('\n')[0].split('=');
+        expect(next_nonce[0]).toEqual("call: instantiation_nonce");
+        expect(parseInt(current_nonce[1])).toBeLessThan(parseInt(next_nonce[1]));
     });
 });

+ 1 - 1
integration/substrate/package.json

@@ -5,7 +5,7 @@
   "main": "index.js",
   "scripts": {
     "test": "tsc; ts-mocha -t 20000 *.spec.ts",
-    "build": "parallel solang compile -v -g --target substrate --log-runtime-errors --math-overflow ::: *.sol test/*.sol;  solang compile debug_buffer_format.sol --target substrate -v --log-runtime-errors --log-api-return-codes",
+    "build": "parallel solang compile -v -g --target substrate --log-runtime-errors --math-overflow --log-api-return-codes ::: *.sol test/*.sol",
     "build-ink": "docker run --rm -v $(pwd)/ink/caller:/opt/contract paritytech/contracts-ci-linux@sha256:2eaa0869c562adabcfc011a2f3ad6b2289cdd0b3a8923e24d29efd4452b397de cargo contract build --manifest-path /opt/contract/Cargo.toml"
   },
   "contributors": [

+ 16 - 51
integration/substrate/runtime_errors.spec.ts

@@ -4,103 +4,68 @@ import { ContractPromise } from "@polkadot/api-contract";
 
 describe('Deploy runtime_errors.sol and test the debug buffer', () => {
     it('logs errors to the debug buffer', async function () {
-
         let conn = await createConnection();
         const alice = aliceKeypair();
-
-
         let deployed_contract = await deploy(
             conn,
             alice,
             "RuntimeErrors.contract",
             BigInt(0)
         );
-
         let contract = new ContractPromise(
             conn,
             deployed_contract.abi,
             deployed_contract.address
         );
-
         let child_contract = await deploy(conn, alice, 'child_runtime_errors.contract', BigInt(0));
 
         let res = await debug_buffer(conn, contract, `get_storage_bytes`, [])
-        expect(res).toEqual(`runtime_error: storage array index out of bounds in runtime_errors.sol:46:19-23,
-`)
+        expect(res).toContain(`runtime_error: storage array index out of bounds in runtime_errors.sol:46:19-23`)
 
         let res1 = await debug_buffer(conn, contract, `transfer_abort`, [])
-        expect(res1).toEqual(`runtime_error: value transfer failure in runtime_errors.sol:53:29-31,
-`)
-
+        expect(res1).toContain(`runtime_error: value transfer failure in runtime_errors.sol:53:29-31`)
 
         let res2 = await debug_buffer(conn, contract, `pop_empty_storage`, [])
-        expect(res2).toEqual(`runtime_error: pop from empty storage array in runtime_errors.sol:58:13-16,
-`)
-
+        expect(res2).toContain(`runtime_error: pop from empty storage array in runtime_errors.sol:58:13-16`)
 
         let res3 = await debug_buffer(conn, contract, `call_ext`, [child_contract.address])
-        expect(res3).toEqual(`runtime_error: external call failed in runtime_errors.sol:63:9-24,
-`)
-
+        expect(res3).toContain(`runtime_error: external call failed in runtime_errors.sol:63:9-24`)
 
         let res4 = await debug_buffer(conn, contract, `create_child`);
-        expect(res4).toEqual(`runtime_error: contract creation failed in runtime_errors.sol:68:13-39,
-`)
-
+        expect(res4).toContain(`runtime_error: contract creation failed in runtime_errors.sol:68:13-39`)
 
         let res5 = await debug_buffer(conn, contract, `set_storage_bytes`, [])
-        expect(res5).toEqual(`runtime_error: storage index out of bounds in runtime_errors.sol:39:11-12,
-`)
-
+        expect(res5).toContain(`runtime_error: storage index out of bounds in runtime_errors.sol:39:11-12`)
 
         let res6 = await debug_buffer(conn, contract, `dont_pay_me`, [], 1);
-        expect(res6).toEqual(`runtime_error: non payable function dont_pay_me received value,
-`)
-
+        expect(res6).toContain(`runtime_error: non payable function dont_pay_me received value`)
 
         let res7 = await debug_buffer(conn, contract, `assert_test`, [9], 0);
-        expect(res7).toEqual(`runtime_error: assert failure in runtime_errors.sol:32:16-24,
-`)
-
+        expect(res7).toContain(`runtime_error: assert failure in runtime_errors.sol:32:16-24`)
 
         let res8 = await debug_buffer(conn, contract, `i_will_revert`, [], 0);
-        expect(res8).toEqual(`runtime_error: revert encountered in runtime_errors.sol:77:9-15,
-`)
-
+        expect(res8).toContain(`runtime_error: revert encountered in runtime_errors.sol:77:9-15`)
 
         let res9 = await debug_buffer(conn, contract, `write_integer_failure`, [1], 0);
-        expect(res9).toEqual(`runtime_error: integer too large to write in buffer in runtime_errors.sol:82:18-31,
-`)
-
+        expect(res9).toContain(`runtime_error: integer too large to write in buffer in runtime_errors.sol:82:18-31`)
 
         let res10 = await debug_buffer(conn, contract, `write_bytes_failure`, [9], 0);
-        expect(res10).toEqual(`runtime_error: data does not fit into buffer in runtime_errors.sol:88:18-28,
-`)
-
+        expect(res10).toContain(`runtime_error: data does not fit into buffer in runtime_errors.sol:88:18-28`)
 
         let res11 = await debug_buffer(conn, contract, `read_integer_failure`, [2], 0);
-        expect(res11).toEqual(`runtime_error: read integer out of bounds in runtime_errors.sol:93:18-30,
-`)
-
+        expect(res11).toContain(`runtime_error: read integer out of bounds in runtime_errors.sol:93:18-30`)
 
         let res12 = await debug_buffer(conn, contract, `trunc_failure`, [BigInt(`999999999999999999999999`)], 0);
-        expect(res12).toEqual(`runtime_error: truncated type overflows in runtime_errors.sol:98:37-42,
-`)
-
+        expect(res12).toContain(`runtime_error: truncated type overflows in runtime_errors.sol:98:37-42`)
 
         let res13 = await debug_buffer(conn, contract, `out_of_bounds`, [19], 0);
-        expect(res13).toEqual(`runtime_error: array index out of bounds in runtime_errors.sol:104:16-21,
-`)
-
+        expect(res13).toContain(`runtime_error: array index out of bounds in runtime_errors.sol:104:16-21`)
 
         let res14 = await debug_buffer(conn, contract, `invalid_instruction`, [], 0);
-        expect(res14).toEqual(`runtime_error: reached invalid instruction in runtime_errors.sol:109:13-22,
-`)
-
+        expect(res14).toContain(`runtime_error: reached invalid instruction in runtime_errors.sol:109:13-22`)
 
         let res15 = await debug_buffer(conn, contract, `byte_cast_failure`, [33], 0);
-        expect(res15).toEqual(`runtime_error: bytes cast error in runtime_errors.sol:115:23-40,
-`)
+        expect(res15).toContain(`runtime_error: bytes cast error in runtime_errors.sol:115:23-40`)
 
         conn.disconnect();
     });

+ 1 - 0
src/emit/substrate/target.rs

@@ -1098,6 +1098,7 @@ impl<'a> TargetRuntime<'a> for SubstrateTarget {
                 .left()
                 .unwrap()
                 .into_int_value();
+            log_return_code(binary, "instantiation_nonce", nonce);
             let i256_t = binary.context.custom_width_int_type(256);
             binary
                 .builder

+ 2 - 1
tests/substrate_tests/calls.rs

@@ -791,7 +791,8 @@ fn log_api_call_return_values_works() {
     runtime.function("test", vec![]);
     assert_eq!(
         &runtime.printbuf,
-        r##"call: seal_instantiate=0,
+        r##"call: instantiation_nonce=1,
+call: seal_instantiate=0,
 print: hi!,
 call: seal_debug_message=0,
 call: seal_call=0,