Skip to content

Commit

Permalink
fix: messaging tutorial data (starkware-libs#1869)
Browse files Browse the repository at this point in the history
* fix: ensure correct subdmodule path and rev for solidity forge-std

* fix: adjust messaging guide for L1-L2 messaging

* fix: adjust messaging guide for L1-L3 messaging
  • Loading branch information
glihm authored Apr 23, 2024
1 parent b22e2aa commit 1000fcd
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "crates/katana/primitives/contracts/messaging/solidity/lib/forge-std"]
path = crates/katana/primitives/contracts/messaging/solidity/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "crates/katana/contracts/messaging/solidity/lib/forge-std"]
path = crates/katana/contracts/messaging/solidity/lib/forge-std
url = https://github.com/foundry-rs/forge-std
26 changes: 19 additions & 7 deletions crates/katana/contracts/messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ How to run the scripts:

- Start Anvil in a terminal.
- Start Katana in an other terminal on default port 5050 with the messaging configuration that is inside the:
`katana --messaging ~/dojo/crates/katana/core/contracts/messaging/anvil.messaging.json`
```bash
# From installed katana.
katana --messaging ~/dojo/crates/katana/contracts/messaging/anvil.messaging.json

# Dev mode
cargo run --bin katana -- --messaging ~/dojo/crates/katana/contracts/messaging/anvil.messaging.json
```
- Open an other terminal and `cd ~/dojo/crates/katana/core/contracts/messaging`.

Then you can then use pre-defined commands to interact with the contracts.
Expand All @@ -66,11 +72,7 @@ make -sC cairo/ send_msg_value_l1 value=2
```
Then you've to wait the message to be sent to L1, Katana will display it:
```
2023-12-15T15:16:18.435370Z INFO messaging: Message sent to settlement layer:
| hash | 0x62c7475daef517f6858a6f539bb4d2aa7eb1e23a7e8b1bc6a0834256d995e49d
| from_address | 0x4231f608ea4a233136f6cdfcd10eaad2e46362bbc4e5d5aa88d0d574ea120d8
| to_address | 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512
| payload | [0x2]
2024-04-22T23:42:52.478200Z INFO messaging: Message sent to settlement layer. hash=0xb5c9a1d3b8eb1c9d37ee5ffdacf09560a68d0c9e53fa4b1cc91d967095bc4ac7 from_address=0x609f8e7a76b6cc36f3ff86f09f6e5fdd0e6320f117d817e4344c1bf9fac7d67 to_address=0xe7f1725e7734ce288f8367e1bb143e90bb3f0512 payload=0x2
```
```
# Consume the messag previously sent. You can try to call it once and see the second one reverting.
Expand Down Expand Up @@ -109,7 +111,15 @@ You can also use the Makefile to setup the chains, but the flow is the following
How to run the scripts:

- Starts Katana (1) to simulate starknet on a new terminal with default port 5050.
- Starts Katana (2) for your appchain on a new terminal with port 6060 and the configuration for messaging: `katana --messaging crates/katana/core/contracts/messaging/l3.messaging.json -p 6060`
- Starts Katana (2) for your appchain on a new terminal with port 6060 and the configuration for messaging:
```bash
# From installed Katana.
katana --messaging crates/katana/contracts/messaging/l3.messaging.json -p 6060`
# Dev mode
cargo run --bin katana --features "starknet-messaging" -- --messaging crates/katana/contracts/messaging/l3.messaging.json -p 6060
```

- Open an other terminal and `cd ~/dojo/crates/katana/core/contracts/messaging`.

Then you can then use pre-defined commands to interact with the contracts.
Expand Down Expand Up @@ -139,3 +149,5 @@ make -sC ./cairo/ get_value_l2
# Try to change the value to see the transaction error.
make -sC cairo/ send_msg_l3 selector_str=msg_handler_value value=888
```

It's important to note that Dojo will support settlement. Hence, messaging will be done during the state update of the appchain on the base layer, and not with this custom solution that was developped for the demo.
2 changes: 1 addition & 1 deletion crates/katana/contracts/messaging/cairo/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.3.1
scarb 2.5.4
32 changes: 18 additions & 14 deletions crates/katana/contracts/messaging/cairo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ sierra = .contract_class.json
# ** L1 <-> L2 **
#
L1_CONTRACT_ADDR=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
C_MSG_L1_ADDR=0x04231f608ea4a233136f6cdfcd10eaad2e46362bbc4e5d5aa88d0d574ea120d8
C_MSG_L1_ADDR=0x0609f8e7a76b6cc36f3ff86f09f6e5fdd0e6320f117d817e4344c1bf9fac7d67

OPTS_L2 := --account katana-0 \
--rpc http://0.0.0.0:5050
--rpc http://0.0.0.0:5050 \
--poll-interval 1000 \
-w

setup_for_l1_messaging:
scarb build; \
Expand All @@ -38,29 +40,31 @@ send_msg_struct_l1:
# ** L2 <-> L3 **
#
ACCOUNT_L3=./account_l3.json
ACCOUNT_L3_ADDR=0x5686a647a9cdd63ade617e0baf3b364856b813b508f03903eb58a7e622d5855
L3_PRIVATE_KEY=0x33003003001800009900180300d206308b0070db00121318d17b5e6262150b
ACCOUNT_L3_ADDR=0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03
L3_PRIVATE_KEY=0x1800000000300000180000000000030000000000003006001800006600

# L2_APPCHAIN_MSG_ADDR=0x046c0ea3fb2ad27053e8af3c8cfab38a51afb9fe90fcab1f75446bd41f7d3796
# L2_APPCHAIN_MSG_CLASS_HASH=$(shell starkli class-hash target/dev/katana_messaging_appchain_messaging.contract_class.json)
L2_APPCHAIN_MSG_ADDR=0x0368407c4a0e075c693cf372dd735750df7289c6b5d06234615018456e658591
L2_APPCHAIN_MSG_CLASS_HASH=$(shell starkli class-hash target/dev/katana_messaging_appchain_messaging.contract_class.json)

L2_CONTRACT1_ADDR=0x054f66c104745e27ad5194815a6c4755cf2076c4809212101dfe31563f312a34
L2_CONTRACT1_ADDR=0x0450ae47f57d4a2165b015a4bf143cd53f60d61a74a0be998bf0a42c477f26ce
L2_CONTRACT1_CLASS_HASH=$(shell starkli class-hash target/dev/katana_messaging_contract_1.contract_class.json)

L3_C_MSG_ADDR=0x071278839029ab1f9fa0ce1ee01e38599736dd4e8fed2417158bec4ef5dc6d0f
L3_C_MSG_ADDR=0x039bb4ce38513597cf75eeacd7f3ed6ef058a61818c252612d134d95ed2e9051
L3_C_MSG_CLASS_HASH=$(shell starkli class-hash target/dev/katana_messaging_contract_msg_starknet.contract_class.json)

OPTS_L3 := --account ${ACCOUNT_L3} \
--rpc http://0.0.0.0:6060 \
--private-key ${L3_PRIVATE_KEY}
--private-key ${L3_PRIVATE_KEY} \
--poll-interval 1000 \
-w

setup_l2_messaging:
@set -x; \
scarb build; \
appchain_ch=$$(starkli class-hash ${build}ERC20${sierra}); \
starkli declare target/dev/katana_messaging_appchain_messaging.contract_class.json ${OPTS_L2}
starkli declare target/dev/katana_messaging_contract_1.contract_class.json ${OPTS_L2}
starkli deploy --salt 0x1234 ${L2_APPCHAIN_MSG_CLASS_HASH} ${ACCOUNT_L2_ADDR} ${ACCOUNT_L3_ADDR} ${OPTS_L2}
appchain_ch=$$(starkli class-hash ${build}appchain_messaging${sierra}); \
starkli declare target/dev/katana_messaging_appchain_messaging.contract_class.json ${OPTS_L2}; \
starkli declare target/dev/katana_messaging_contract_1.contract_class.json ${OPTS_L2}; \
starkli deploy --salt 0x1234 ${L2_APPCHAIN_MSG_CLASS_HASH} ${ACCOUNT_L2_ADDR} ${ACCOUNT_L3_ADDR} ${OPTS_L2}; \
starkli deploy --salt 0x1234 ${L2_CONTRACT1_CLASS_HASH} ${L2_APPCHAIN_MSG_ADDR} ${OPTS_L2}

setup_l3_messaging:
Expand All @@ -82,7 +86,7 @@ consume_msg_from_l3_usage:

consume_msg_from_l3:
starkli invoke ${L2_CONTRACT1_ADDR} consume_message \
${ACCOUNT_L3_ADDR} \
${L3_C_MSG_ADDR} \
$(payload) \
${OPTS_L2}

Expand Down
5 changes: 3 additions & 2 deletions crates/katana/contracts/messaging/cairo/account_l2.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"variant": {
"type": "open_zeppelin",
"version": 1,
"public_key": "0x2b191c2f3ecf685a91af7cf72a43e7b90e2e41220175de5c4f7498981b10053"
"public_key": "0x2b191c2f3ecf685a91af7cf72a43e7b90e2e41220175de5c4f7498981b10053",
"legacy": false
},
"deployment": {
"status": "deployed",
"class_hash": "0x4d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27f0bb5bc5f",
"class_hash": "0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c",
"address": "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
}
}
7 changes: 4 additions & 3 deletions crates/katana/contracts/messaging/cairo/account_l3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"variant": {
"type": "open_zeppelin",
"version": 1,
"public_key": "0x4c0f884b8e5b4f00d97a3aad26b2e5de0c0c76a555060c837da2e287403c01d"
"public_key": "0x2b191c2f3ecf685a91af7cf72a43e7b90e2e41220175de5c4f7498981b10053",
"legacy": false
},
"deployment": {
"status": "deployed",
"class_hash": "0x4d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27f0bb5bc5f",
"address": "0x5686a647a9cdd63ade617e0baf3b364856b813b508f03903eb58a7e622d5855"
"class_hash": "0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c",
"address": "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ mod appchain_messaging {
);

match starknet::call_contract_syscall(to_address, selector, payload) {
Result::Ok(span) => self
Result::Ok(_) => self
.emit(MessageExecuted { from_address, to_address, selector, payload, }),
Result::Err(e) => {
panic(e)
Expand Down
7 changes: 4 additions & 3 deletions crates/katana/contracts/messaging/cairo/src/contract_1.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ mod contract_1 {

// Will revert in case of failure if the message is not registered
// as consumable.
let msg_hash = messaging.consume_message_from_appchain(from_address, payload,);
// msg successfully consumed, we can proceed and process the data
// in the payload.
let _msg_hash = messaging.consume_message_from_appchain(from_address, payload,);

// msg successfully consumed, we can proceed and process the data
// in the payload.
}

/// An example function to test how appchain contract can trigger
Expand Down
6 changes: 3 additions & 3 deletions crates/katana/contracts/messaging/l3.messaging.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"chain": "starknet",
"rpc_url": "http://127.0.0.1:5050",
"contract_address": "0x046c0ea3fb2ad27053e8af3c8cfab38a51afb9fe90fcab1f75446bd41f7d3796",
"sender_address": "0x5686a647a9cdd63ade617e0baf3b364856b813b508f03903eb58a7e622d5855",
"private_key": "0x33003003001800009900180300d206308b0070db00121318d17b5e6262150b",
"contract_address": "0x0368407c4a0e075c693cf372dd735750df7289c6b5d06234615018456e658591",
"sender_address": "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03",
"private_key": "0x1800000000300000180000000000030000000000003006001800006600",
"interval": 2,
"from_block": 0
}
2 changes: 1 addition & 1 deletion crates/katana/contracts/messaging/solidity/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export $(shell sed 's/=.*//' .env)
# Addresses fixed here for easy testing.
C_MSG_L2_ADDR=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
L2_ACCOUNT=0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03
L2_CONTRACT_ADDR=0x0429a64d97c1422a37a09fc7406f35c264be59b744aaff5a79d59393eb1bc7e1
L2_CONTRACT_ADDR=0x609f8e7a76b6cc36f3ff86f09f6e5fdd0e6320f117d817e4344c1bf9fac7d67

deploy_messaging_contracts:
forge script --broadcast --rpc-url ${ETH_RPC_URL} script/LocalTesting.s.sol:LocalSetup
Expand Down
1 change: 1 addition & 0 deletions crates/katana/contracts/messaging/solidity/lib/forge-std
Submodule forge-std added at 5dd1c6

0 comments on commit 1000fcd

Please sign in to comment.