starkware-libs / cairo-lang

Apache License 2.0
1.33k stars 262 forks source link

Calling on the Ethereum L1 bridge contract from the doc example fails with "INVALID_MESSAGE_TO_CONSUME" error #141

Open 0xrubes opened 1 year ago

0xrubes commented 1 year ago

It seems like the example from the docs regarding L1-communication is broken.

I deployed the contract on L2 (0x07cc62824cca70ec4f407e0d7a99d18e0540e0128e836d67629a694a941238a5), provided a 251 bit USERID (3211681899000825905828277986971367691877225602563943946254945768308385816794) and set the balance and called the withdraw function, just as in the example. However, my call on the provided Ethereum L1 sample contract failed with an INVALID_MESSAGE_TO_CONSUME error. Looking at the contract in the block explorer, this seems to have happened for every user that has tried it so far. I'm not sure where the issue is, but would appreciate if someone can take a look at this :)

t4sk commented 1 year ago

This address worked for me 0xde29d060D45901Fb19ED6C6e959EB22d8626708e

I found it here https://github.com/starknet-edu/starknet-messaging-bridge/blob/8e02e58017ed31a86d2a680c2b377ec8a8776e41/README.md#exercises--contract-addresses

Example

%lang starknet

from starkware.cairo.common.alloc import alloc
from starkware.starknet.common.messages import send_message_to_l1

@external
func send_message{syscall_ptr: felt*}(l1_contract_addr: felt, x: felt, y: felt) -> () {
    let payload_size = 2;
    let (payload: felt*) = alloc();
    assert payload[0] = x;
    assert payload[1] = y;

    send_message_to_l1(l1_contract_addr, payload_size, payload);
    return ();
}
pragma solidity ^0.8;

interface IStarknetCore {
    function consumeMessageFromL2(
        uint256 fromAddress,
        uint256[] calldata payload
    ) external returns (bytes32);
}

contract L2ToL1 {
    IStarknetCore starknetCore;

    uint256 public sum;

    constructor(IStarknetCore _starknetCore) {
        starknetCore = _starknetCore;
    }

    // 0xde29d060D45901Fb19ED6C6e959EB22d8626708e
    function add(
        uint256 l2ContractAddress,
        uint256 x,
        uint256 y
    ) external {
        uint256 payloadSize = 2;

        uint256[] memory payload = new uint256[](payloadSize);
        payload[0] = x;
        payload[1] = y;

        // This call reverts if the message doesn't exist on L1.
        starknetCore.consumeMessageFromL2(l2ContractAddress, payload);

        // If we got to this point everything is ok and we can compute
        //  the sum and store it in the storage variable.
        sum = x + y;
    }
}