Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If contract instantiates sub-contracts, cargo contract instantiate returns wrong contract account id #777

Closed
cmichi opened this issue Oct 11, 2022 · 1 comment · Fixed by #965
Assignees
Labels
bug Something isn't working

Comments

@cmichi
Copy link
Collaborator

cmichi commented Oct 11, 2022

Bug

If a contract instantiates sub-contracts, cargo contract instantiate displays the wrong contract account id in its output. It displays the id of the sub-contract.

I'm referring to this output:

cargo contract instantiate …
 Dry-running new (skip with --skip-dry-run)
    Success! Gas required estimated at …
Confirm transaction details: (skip with --skip-confirm)
 Constructor new
        Args …
   Gas limit …
Submit? (Y/n): Y
   Code hash …
    Contract 5HjX3tcg1vccCnxdvPBE2S5mfXjC41dQ6D2wWWohvUdbneTn
      Events
      …

How to replicate

Our delegator example instantiates three sub-contracts in its constructor and shows the bug.

cd ink/examples/delegator/
/bin/bash ./build-all.sh

cargo contract upload --manifest-path=accumulator/Cargo.toml --suri //Alice
cargo contract upload --manifest-path=adder/Cargo.toml --suri //Alice
cargo contract upload --manifest-path=subber/Cargo.toml --suri //Alice

ACCUMULATOR_HASH=$(cat target/ink/accumulator/*.contract | jq -r .source.hash)
ADDER_HASH=$(cat target/ink/adder/*.contract | jq -r .source.hash)
SUBBER_HASH=$(cat target/ink/subber/*.contract | jq -r .source.hash)

cargo contract instantiate --manifest-path=Cargo.toml --suri //Alice --args 0 0 $ACCUMULATOR_HASH $ADDER_HASH $SUBBER_HASH

# Copy the `Contract` address from the above output 
export CONTRACT=…

cargo contract call -m switch --dry-run --suri //Alice  --contract $CONTRACT

The method switch exists only on the delegator/lib.rs contract, but we call the first instantiated sub-contract here (due to the bug) and it doesn't have a method switch. The command thus fails:

$ cargo contract call -m switch --dry-run  --contract $CONTRACT --suri //Alice
          Result ModuleError: Contracts::ContractTrapped: ["Contract trapped during execution."]
          …

Where to start looking

instantiate.rs:

let instantiated = result
    .find_first::<api::contracts::events::Instantiated>()?
    .ok_or_else(|| anyhow!("Failed to find Instantiated event"))?;

I think this should be find_last instead, as the Instantiated event from the first sub-contract is otherwise taken.

What else to think about

Above the code linked right above there is the same find_first for CodeStored:

// The CodeStored event is only raised if the contract has not already been uploaded.
let code_hash = result
    .find_first::<api::contracts::events::CodeStored>()?
    .map(|code_stored| code_stored.code_hash);

I'm not sure if it should be find_last there as well. For this to be faulty the contract would have to be able to upload another contract and I don't think that's possible right now.

@cmichi cmichi added the bug Something isn't working label Oct 11, 2022
cmichi added a commit to use-ink/ink that referenced this issue Oct 11, 2022
cmichi added a commit to use-ink/ink that referenced this issue Oct 11, 2022
cmichi added a commit to use-ink/ink that referenced this issue Oct 19, 2022
* Update `contracts-node.scale`

Executed for `substrate-contracts-node` v0.21.0:

            cargo install subxt-cli
            subxt metadata > contracts-node.scale

* Update dependency paths

* Fix wrong label

* Add helper function `extract_wasm`

* Add helper function `set_current_nonce`

* Fix issue with wrong contract id

Same issue as use-ink/cargo-contract#777.

* Implement cross-contract testing

* Adjust TODO comments

* Add E2E tests for `delegator`

* Adapt `contract-transfer` E2E tests for new API

* Rename `RETURN` to `ReturnType`

* Build additional contracts only once for all E2E tests

I had to remove `skip_build` as keeping it work would
become too complex right now. Will create a follow-up
ticket for it.

* Fix field name in error message
@lean-apple lean-apple self-assigned this Nov 7, 2022
@jjmiranda
Copy link

jjmiranda commented Feb 5, 2023

That's true, I spend a lot of hours to note this bug 👎
Revised the log, I notice about this error (reverse order):
system:ExtrinsicSuccess
[{"weight":{"refTime":"6,812,732,864","proofSize":"26,888"},"class":"Normal","paysFee":"Yes"}]
transactionPayment:TransactionFeePaid
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","98,993,850","0"]
balances:Reserved
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","100,340,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","100,340,000,000"]
balances:Reserved
["5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9","100,020,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9","100,020,000,000"]
balances:Reserved
["5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou","100,020,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou","100,020,000,000"]
contracts:Instantiated
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M"]
contracts:Instantiated
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9"]
balances:Reserved
["5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9","100,605,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9","100,605,000,000"]
balances:Endowed
["5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9","100,605,000,000"]
system:NewAccount
["5CjxfvE5faUodD83yEugiRrbezwptpGvRXJeDBKsjEp7YWS9"]
contracts:Instantiated
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou"]
balances:Reserved
["5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou","100,605,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou","100,605,000,000"]
balances:Endowed
["5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou","100,605,000,000"]
system:NewAccount
["5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou"]
balances:Reserved
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","100,605,000,000"]
balances:Transfer
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","100,605,000,000"]
balances:Endowed
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M","100,605,000,000"]
system:NewAccount
["5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M"]
balances:Withdraw
["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","98,993,850"]

cargo contract instantiate shows this accountID:
5EYCponRWoFwXLj7a8CgmupCSxsQkR7krzptBY4CkBR39Pou

This is the second system:NewAccount and first contracts:Instantiated.

The correct accountID for cargo contract instantiate would be:
5GCxJC3o2foWNpSkc5MUf1SiGXWqaTFGpzyFfaAoSzfokf8M
The first system:NewAccount

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants