|
| 1 | +Mocking interaction with L1 |
| 2 | +=========================== |
| 3 | + |
| 4 | +Abstract |
| 5 | +-------- |
| 6 | + |
| 7 | +In order to test interaction with L1 contracts, devnet client provides a way to mock the L1 interaction. |
| 8 | +Before taking a look at the examples, please get familiar with the `devnet postman docs <https://0xspaceshard.github.io/starknet-devnet-rs/docs/postman>`_ and messaging mechanism: |
| 9 | + |
| 10 | +- `Writing messaging contracts <https://book.cairo-lang.org/ch16-04-L1-L2-messaging.html>`_ |
| 11 | +- `Mechanism overview <https://docs.starknet.io/architecture-and-concepts/network-architecture/messaging-mechanism/>`_ |
| 12 | +- `StarkGate example <https://docs.starknet.io/architecture-and-concepts/network-architecture/messaging-mechanism/>`_ |
| 13 | + |
| 14 | +L1 network setup |
| 15 | +---------------- |
| 16 | + |
| 17 | +First of all you should deploy `messaging contract <https://github.com/0xSpaceShard/starknet-devnet-rs/blob/138120b355c44ae60269167b326d1a267f7af0a8/contracts/l1-l2-messaging/solidity/src/MockStarknetMessaging.sol>`_ |
| 18 | +on ethereum network or load the existing one. |
| 19 | + |
| 20 | +.. codesnippet:: ../../starknet_py/tests/e2e/docs/devnet_utils/test_l1_integration.py |
| 21 | + :language: python |
| 22 | + :dedent: 4 |
| 23 | + |
| 24 | + |
| 25 | +L2 -> L1 |
| 26 | +-------- |
| 27 | + |
| 28 | +Deploying L2 interaction contract |
| 29 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 30 | + |
| 31 | +Interaction with L1 is done by sending a message using `send_message_to_l1_syscall` function. |
| 32 | +So in order to test it, you need to deploy a contract that has this functionality. |
| 33 | +Example contract: `l1_l2.cairo <https://github.com/0xSpaceShard/starknet-devnet-js/blob/5069ec3397f31a408d3df2734ae40d93b42a0f7f/test/data/l1_l2.cairo>`_ |
| 34 | + |
| 35 | +.. codesnippet:: ../../starknet_py/tests/e2e/docs/devnet_utils/test_l1_integration.py |
| 36 | + :language: python |
| 37 | + :dedent: 4 |
| 38 | + :start-after: docs: messaging-contract-start |
| 39 | + :end-before: docs: messaging-contract-end |
| 40 | + |
| 41 | + |
| 42 | +Consuming message |
| 43 | +^^^^^^^^^^^^^^^^^ |
| 44 | + |
| 45 | +After deploying the contract, you need to flush the messages to the L1 network. |
| 46 | +And then you can consume the message on the L1 network. |
| 47 | + |
| 48 | +.. codesnippet:: ../../starknet_py/tests/e2e/docs/devnet_utils/test_l1_integration.py |
| 49 | + :language: python |
| 50 | + :dedent: 4 |
| 51 | + :start-after: docs: flush-1-start |
| 52 | + :end-before: docs: flush-1-end |
| 53 | + |
| 54 | +L1 -> L2 |
| 55 | +-------- |
| 56 | + |
| 57 | +Sending mock transactions from L1 to L2 does not require L1 node to be running. |
| 58 | + |
| 59 | +.. codesnippet:: ../../starknet_py/tests/e2e/docs/devnet_utils/test_l1_integration.py |
| 60 | + :language: python |
| 61 | + :dedent: 4 |
| 62 | + :start-after: docs: send-l2-start |
| 63 | + :end-before: docs: send-l2-end |
| 64 | + |
| 65 | + |
0 commit comments