1

从 Polkadot JS api 调用合约调用时出现问题。

如果该函数在合约中包含 HashMap insert() 或 BTreeMap insert(),当通过 api 调用它时,我会得到:

{"ApplyExtrinsic":1}
[ An extrinsic failed.]
{"index":"0x0001","data":[{"Other":null},{"weight":10000000000,"class":"Normal","paysFee":"Yes"}]}

事件不会被正确触发。但如果我使用 HashMap/BTreeMap get() 或 contains_key(),我可以正确获取事件数据。

这是我在 lib.rs 中的合约代码:

/* 
    Using the standard library if we run the tests module, 
    or if we use a std feature flag within our code. 
    Otherwise the contract will always compile with no_std.
*/
#![cfg_attr(not(feature = "std"), no_std)]

extern crate hex;

use ink_lang as ink;

#[ink::contract(version = "0.1.0")]
mod roleContract {
    #[cfg(not(feature = "ink-as-dependency"))]
    use ink_core::storage;
    use ink_core::storage::BTreeMap;

    /// Defines the storage of your contract.
    /// Add new fields to the below struct in order
    /// to add new static storage fields to your contract.
    #[ink(storage)]
    struct RoleContract {
        /// Stores a single `bool` value on the storage.
        test_hashmap: storage::hash_map::HashMap<AccountId, u64>,
        test_btreemap: storage::BTreeMap<AccountId, u64>,
    }

    #[ink(event)]
    struct TestEvent {
        #[ink(topic)]
        isOk: u32,
    }

    impl RoleContract {
        /// Constructor that initializes the caller as roleType = 1 (System)
        #[ink(constructor)]
        fn newRoleContract(&mut self) {}

        /// Add caller type
        #[ink(message)]
    fn setType(&mut self) {
        let bytes: [u8; 32] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        let acc = AccountId::from(bytes);
        let default_value:u64 = 1;
        // self.test_hashmap.insert(acc, default_value);
        self.test_btreemap.insert(acc, default_value);

        self.env()
            .emit_event(
                TestEvent {
                    isOk: 1,
                }
            );
        }
    }
}

这是 api 调用的 js 文件:

async function main() {
    const provider = new WsProvider('ws://127.0.0.1:9944');
    const api = await ApiPromise.create({ provider: provider, types: {
        "Address": "AccountId",
        "LookupSource": "AccountId"
    } });

    const BOB="5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty";
    const ALICE = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";

    const keyring = new Keyring({ type: 'sr25519' });
    const alice=keyring.addFromUri("//Alice");
    
    const setTypeSelector="0xEE03889D"; //from abi

    const contractAddress = "0x33b757c837aaf6946de25ca35046fadb62e99848c7fe1d106404cb02a298bcf7";

    const callTx=api.tx.contracts.call(contractAddress, 0, 10000000000, setTypeSelector);

    console.log(callTx.toString());
    console.log(callTx.toHuman());
    console.log(callTx.toRawType());

    await callTx.signAndSend(alice, ({events=[], status}) => {
        if(status.isInBlock) {
        console.log("callTx (set caller's type) is in block");
        events.forEach(({event, phase}) => {
            console.log(event.toHuman());
            console.log(phase.toString());
            console.log(event.meta.documentation.toString());
            console.log(event.toString());
            if(event.section=="contracts" && event.method=="ContractExecution") {
            console.log("contract returned from", event.data[0].toHex(), "with pub key value", event.data[1]);
            }
        });

        } else {
        console.log('\n', "callTx (set caller's type) waiting to be in block");
        }
    });
}

这是节点终端信息:

2020-06-28 23:02:16.360 main WARN sc_cli::commands::run_cmd  Running in --dev mode, RPC CORS has been disabled.
2020-06-28 23:02:16.361 main INFO sc_cli::runner  Substrate Node
2020-06-28 23:02:16.361 main INFO sc_cli::runner  ✌️  version 2.0.0-rc4-f8924ad-x86_64-macos
2020-06-28 23:02:16.361 main INFO sc_cli::runner  ❤️  by Substrate DevHub <https://github.com/substrate-developer-hub>, 2017-2020
2020-06-28 23:02:16.361 main INFO sc_cli::runner   Chain specification: Development
2020-06-28 23:02:16.361 main INFO sc_cli::runner    Node name: gentle-scissors-2232
2020-06-28 23:02:16.361 main INFO sc_cli::runner   Role: AUTHORITY
2020-06-28 23:02:16.361 main INFO sc_cli::runner   Database: RocksDb at /Users/yunchen/Library/Application Support/node-template/chains/dev/db
2020-06-28 23:02:16.361 main INFO sc_cli::runner  ⛓  Native runtime: node-template-1 (node-template-1.tx1.au1)
2020-06-28 23:02:16.423 main INFO sc_service::client::client   Initializing Genesis block/state (state: 0x5a18…3dac, header-hash: 0xb7e5…ced3)
2020-06-28 23:02:16.425 main INFO afg   Loading GRANDPA authority set from genesis on what appears to be first startup.
2020-06-28 23:02:16.479 main INFO sc_consensus_slots  ⏱  Loaded block-time = 6000 milliseconds from genesis on first-launch
2020-06-28 23:02:16.490 main INFO sc_service::builder   Highest known block at #0
2020-06-28 23:02:16.490 main WARN sc_service::builder  Using default protocol ID "sup" because none is configured in the chain specs
2020-06-28 23:02:16.490 main INFO sub-libp2p    Local node identity is: 12D3KooWNF5VhkLcDuiWiB68y2wJPAMkDJ6Hu5r2UxBHZJe9ii9p (legacy representation: QmdfNEfNzcZzDnHSBKgLJk1neFQEZu79MC5HXvtez9XNJe)
2020-06-28 23:02:16.830 tokio-runtime-worker INFO substrate_prometheus_endpoint::known_os  〽️ Prometheus server started at 127.0.0.1:9615
2020-06-28 23:02:18.012 tokio-runtime-worker INFO sc_basic_authorship::basic_authorship   Starting consensus session on top of parent 0xb7e5451c29add89bbfb47e8cd6cbb7c2cc11440e1d32f50072f640073beeced3
2020-06-28 23:02:18.022 tokio-blocking-driver INFO sc_basic_authorship::basic_authorship   Prepared block for proposing at 1 [hash: 0x199fc86def9f870b809eacd2cecdff1b0e9915f4a7ea472edd70954a8c05f75e; parent_hash: 0xb7e5…ced3; extrinsics (1): [0xea7f…7053]]
2020-06-28 23:02:18.025 tokio-runtime-worker INFO sc_consensus_slots   Pre-sealed block for proposal at 1. Hash now 0x5c0973ca64179295e3476df7f7a3788579472b468b5e575670aabb5709941a6d, previously 0x199fc86def9f870b809eacd2cecdff1b0e9915f4a7ea472edd70954a8c05f75e.
2020-06-28 23:02:18.025 tokio-runtime-worker INFO substrate  ✨ Imported #1 (0x5c09…1a6d)
2020-06-28 23:02:21.832 tokio-runtime-worker INFO substrate   Idle (0 peers), best: #1 (0x5c09…1a6d), finalized #0 (0xb7e5…ced3), ⬇ 0 ⬆ 0
2020-06-28 23:02:24.007 tokio-runtime-worker INFO sc_basic_authorship::basic_authorship   Starting consensus session on top of parent 0x5c0973ca64179295e3476df7f7a3788579472b468b5e575670aabb5709941a6d
2020-06-28 23:02:24.013 tokio-blocking-driver INFO sc_basic_authorship::basic_authorship   Prepared block for proposing at 2 [hash: 0xc4fec08e86666a4a689ccd118e48c9a357a2b672658ed3cc1fb509f3e54c0b11; parent_hash: 0x5c09…1a6d; extrinsics (2): [0x36ad…cca7, 0xe274…07bf]]
2020-06-28 23:02:24.016 tokio-runtime-worker INFO sc_consensus_slots   Pre-sealed block for proposal at 2. Hash now 0xfdda7c33ffd204f51a310d9c757a50e37cc3388473c92f9eb5f173f6f0e0cb92, previously 0xc4fec08e86666a4a689ccd118e48c9a357a2b672658ed3cc1fb509f3e54c0b11.
2020-06-28 23:02:24.017 tokio-runtime-worker INFO substrate  ✨ Imported #2 (0xfdda…cb92)
2020-06-28 23:02:26.835 tokio-runtime-worker INFO substrate   Idle (0 peers), best: #2 (0xfdda…cb92), finalized #0 (0xb7e5…ced3), ⬇ 0 ⬆ 0
2020-06-28 23:02:30.006 tokio-runtime-worker INFO sc_basic_authorship::basic_authorship   Starting consensus session on top of parent 0xfdda7c33ffd204f51a310d9c757a50e37cc3388473c92f9eb5f173f6f0e0cb92
2020-06-28 23:02:30.013 tokio-blocking-driver INFO sc_basic_authorship::basic_authorship   Prepared block for proposing at 3 [hash: 0xe89c521e9c2d830ca61ca4476fbaa458a1ed96915400a6ac29e04b2eeb44e86b; parent_hash: 0xfdda…cb92; extrinsics (2): [0xfb4f…e5d3, 0x5838…935c]]
2020-06-28 23:02:30.016 tokio-runtime-worker INFO sc_consensus_slots   Pre-sealed block for proposal at 3. Hash now 0xd19ff3df953bf6e3883a21752be156ce5ba35bf26cff82fa007b39740d3bbeca, previously 0xe89c521e9c2d830ca61ca4476fbaa458a1ed96915400a6ac29e04b2eeb44e86b.
2020-06-28 23:02:30.018 tokio-runtime-worker INFO substrate  ✨ Imported #3 (0xd19f…beca)
2020-06-28 23:02:31.837 tokio-runtime-worker INFO substrate   Idle (0 peers), best: #3 (0xd19f…beca), finalized #1 (0x5c09…1a6d), ⬇ 0 ⬆ 0
2020-06-28 23:02:36.009 tokio-runtime-worker INFO sc_basic_authorship::basic_authorship   Starting consensus session on top of parent 0xd19ff3df953bf6e3883a21752be156ce5ba35bf26cff82fa007b39740d3bbeca
2020-06-28 23:02:36.014 tokio-blocking-driver DEBUG runtime  DispatchError
2020-06-28 23:02:36.014 tokio-blocking-driver DEBUG runtime  ran out of gas during contract execution
2020-06-28 23:02:36.014 tokio-blocking-driver DEBUG runtime  PostInfo: 
2020-06-28 23:02:36.014 tokio-blocking-driver DEBUG runtime  actual_weight=
2020-06-28 23:02:36.014 tokio-blocking-driver DEBUG runtime  10000000000
2020-06-28 23:02:36.014 tokio-blocking-driver INFO sc_basic_authorship::basic_authorship   Prepared block for proposing at 4 [hash: 0x45ce7a7667492d24c87c5ae63a59ca492c99c27d9ecbbb1219275bb9c0e884cc; parent_hash: 0xd19f…beca; extrinsics (2): [0x1ece…074f, 0xbc82…2313]]
2020-06-28 23:02:36.017 tokio-runtime-worker INFO sc_consensus_slots   Pre-sealed block for proposal at 4. Hash now 0xd8ded9ec97729f678ac5468f6fb13cc45850689a8eff2fa17b99f8c0b690301b, previously 0x45ce7a7667492d24c87c5ae63a59ca492c99c27d9ecbbb1219275bb9c0e884cc.
2020-06-28 23:02:36.017 tokio-runtime-worker INFO substrate  ✨ Imported #4 (0xd8de…301b)
2020-06-28 23:02:36.840 tokio-runtime-worker INFO substrate   Idle (0 peers), best: #4 (0xd8de…301b), finalized #1 (0x5c09…1a6d), ⬇ 0 ⬆ 0
2020-06-28 23:02:41.844 tokio-runtime-worker INFO substrate   Idle (0 peers), best: #4 (0xd8de…301b), finalized #2 (0xfdda…cb92), ⬇ 0 ⬆ 0
2020-06-28 23:02:42.008 tokio-runtime-worker INFO sc_basic_authorship::basic_authorship   Starting consensus session on top of parent 0xd8ded9ec97729f678ac5468f6fb13cc45850689a8eff2fa17b99f8c0b690301b
2020-06-28 23:02:42.009 tokio-blocking-driver INFO sc_basic_authorship::basic_authorship   Prepared block for proposing at 5 [hash: 0xcbeceaea766dc66a8137c15bdeeca7a7647360109596cb89ee7514ae2ce48381; parent_hash: 0xd8de…301b; extrinsics (1): [0x5fd0…a889]]
2020-06-28 23:02:42.012 tokio-runtime-worker INFO sc_consensus_slots   Pre-sealed block for proposal at 5. Hash now 0x0e8547c91b8e1429e4d763d9c042d49d522b832c8e0f26958946a4e86d834da0, previously 0xcbeceaea766dc66a8137c15bdeeca7a7647360109596cb89ee7514ae2ce48381.
2020-06-28 23:02:42.013 tokio-runtime-worker INFO substrate  ✨ Imported #5 (0x0e85…4da0)

任何人都知道问题是什么?

4

0 回答 0