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

Cross-contract instantiation failed with CalleeTrapped #1160

Open
ltfschoen opened this issue Jun 14, 2023 · 0 comments
Open

Cross-contract instantiation failed with CalleeTrapped #1160

ltfschoen opened this issue Jun 14, 2023 · 0 comments

Comments

@ltfschoen
Copy link

ltfschoen commented Jun 14, 2023

In summary, I think there is a bug, where it says there's a max of 4x topics during build if you use >4x topics, but if you only use 4x topics for an event then it'll build successfully but then generate error Cross-contract instantiation failed with CalleeTrapped during instantiation, then if you change to <4x topics it'll successfully build and instantiate.

Detailed report is as follows:

I'm using the latest cargo-contract 3.0.1-unknown-x86_64-unknown-linux-gnu

If in my code I use 5x topics like this

    #[ink(event)]
    pub struct NewOracleMarketGuessForMarketId {
        #[ink(topic)]
        id_market: MarketGuessId,
        #[ink(topic)]
        oracle_owner: OracleOwner,
        #[ink(topic)]
        block_number_guessed: BlockNumber,
        #[ink(topic)]
        block_number_entropy: BlockNumber,
        #[ink(topic)]
        block_number_end: BlockNumber,
    }

And I emit that event with:

    impl OracleContract {
        #[ink(constructor)]
        pub fn new(
            id_market: MarketGuessId,
            block_number_guessed: BlockNumber,
            block_number_entropy: BlockNumber,
            block_number_end: BlockNumber,
        ) -> Self {
            let mut instance = Self::default();
            ...
            ink::env::debug_println!("77");
            instance.env().emit_event(NewOracleMarketGuessForMarketId {
                id_market: id_market.clone(),
                oracle_owner: caller,
                block_number_guessed,
                block_number_entropy,
                block_number_end,
            });

Then when I build the contract with

cd $PARENT_DIR/dapps/xcm/unnamed
PROJECT_ROOT=$PARENT_DIR/dapps/xcm/unnamed
cargo contract build \
    --manifest-path $PARENT_DIR/dapps/xcm/unnamed/Cargo.toml
cargo contract build \
    --manifest-path $PARENT_DIR/dapps/xcm/unnamed/oracle_contract/Cargo.toml

It outputs an error since I've tried to use 5x event topics when only 4x topics are allowed

error[E0277]: the trait bound `EventTopics<5>: RespectTopicLimit<4>` is not satisfied
  --> /app/dapps/xcm/unnamed/oracle_contract/lib.rs:26:5
   |
26 |     /// Emitted when create new market guess for market id.
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `RespectTopicLimit<4>` is not implemented for `EventTopics<5>`
   |
   = help: the following other types implement trait `RespectTopicLimit<N>`:
             <EventTopics<5> as RespectTopicLimit<10>>
             <EventTopics<5> as RespectTopicLimit<11>>
             <EventTopics<5> as RespectTopicLimit<12>>
             <EventTopics<5> as RespectTopicLimit<5>>
             <EventTopics<5> as RespectTopicLimit<6>>
             <EventTopics<5> as RespectTopicLimit<7>>
             <EventTopics<5> as RespectTopicLimit<8>>
             <EventTopics<5> as RespectTopicLimit<9>>
note: required by a bound in `EventRespectsTopicLimit`
  --> /usr/local/cargo/git/checkouts/ink-c26a90e68a5d1f57/d372cce/crates/ink/src/codegen/event/topics.rs:45:43
   |
45 |     <Event as EventLenTopics>::LenTopics: RespectTopicLimit<LEN_MAX_TOPICS>,
   |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EventRespectsTopicLimit`

So I think that's an easy fix, I just comment out one of the #[ink(topic)], so I'm only using 4x topics instead of 5x topics.

    /// Emitted when create new market guess for market id.
    #[ink(event)]
    pub struct NewOracleMarketGuessForMarketId {
        #[ink(topic)]
        id_market: MarketGuessId,
        #[ink(topic)]
        oracle_owner: OracleOwner,
        #[ink(topic)]
        block_number_guessed: BlockNumber,
        #[ink(topic)]
        block_number_entropy: BlockNumber,
        // #[ink(topic)]
        block_number_end: BlockNumber,
    }

That change builds successfully,
but when I then try to instantiate it with:

    cargo contract instantiate \
        --manifest-path $PARENT_DIR/dapps/xcm/unnamed/Cargo.toml \
        --suri //Alice \
        --constructor new \
        --args "$CODE_HASH" "[109, 121, 95, 105, 100]" "100" "228" "500" \
        --execute

It outputs the following error

 Dry-running new (skip with --skip-dry-run)
          Result ModuleError: Contracts::ContractTrapped: ["Contract trapped during execution."]
    Gas Consumed Weight { ref_time: 4480251726, proof_size: 71307 }
    Gas Required Weight { ref_time: 10884204205, proof_size: 267367 }
 Storage Deposit StorageDeposit::Charge(0)
   Debug Message 77
                 panicked at 'Cross-contract instantiation failed with CalleeTrapped', /usr/local/cargo/git/checkouts/ink-c26a90e68a5d1f57/d372cce/crates/env/src/call/create_builder.rs:260:17
ERROR: Pre-submission dry-run failed. Use --skip-dry-run to skip this step.

So I then I try only using 3x topics (even though it said there was max. 4x topics) by commenting out two of the #[ink(topic)] lines instead of only commenting out one:

    /// Emitted when create new market guess for market id.
    #[ink(event)]
    pub struct NewOracleMarketGuessForMarketId {
        #[ink(topic)]
        id_market: MarketGuessId,
        #[ink(topic)]
        oracle_owner: OracleOwner,
        #[ink(topic)]
        block_number_guessed: BlockNumber,
        // #[ink(topic)]
        block_number_entropy: BlockNumber,
        // #[ink(topic)]
        block_number_end: BlockNumber,
    }

And that builds successfully, and it also instantiates successfully

So I think it's a bug in ink! or a bug in cargo-contract where user is told that the max amount of topics they can use is one more than the actual amount of topics they can use.

it's telling me that the max. amount of topics is 4x when building, but when it instantiates it just panics with error Cross-contract instantiation failed with CalleeTrapped, and the solution to that error is to use "one less" than it tells me is the max. amount of topics during build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant