Skip to content

Commit

Permalink
fix: refacto README + add new scripts + fix sozo issues with migratio…
Browse files Browse the repository at this point in the history
…n on sepolia
  • Loading branch information
glihm committed Sep 6, 2024
1 parent f005215 commit de8095d
Show file tree
Hide file tree
Showing 36 changed files with 573 additions and 216 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ bindings
justfile
spawn-and-move-db
types-test-db
examples/spawn-and-move/manifests/saya/**
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scarb 2.7.0
starknet-foundry 0.30.0
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/saya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true
anyhow.workspace = true
clap.workspace = true
console.workspace = true
dojo-utils.workspace = true
katana-primitives.workspace = true
katana-rpc-api.workspace = true
saya-core.workspace = true
Expand Down
276 changes: 130 additions & 146 deletions bin/saya/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,183 +19,167 @@ Ensure you have the following set up:
Below is a command-line example that demonstrates how to run the Saya executable with necessary parameters:

```bash
cargo run --bin saya -- --rpc-url http://localhost:5050 --da-chain celestia --celestia-node-url http://127.0.0.1:26658 --celestia-namespace mynm --celestia-node-auth-token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.....
cargo run --bin saya -- \
--rpc-url http://localhost:5050 \
--da-chain celestia \
--celestia-node-url http://127.0.0.1:26658 \
--celestia-namespace mynm \
--celestia-node-auth-token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.....
```

## Detailed Workflow

1. Prepare fact registry contract
1. Setup your environment:
* For now starknet foundry is required until Sozo supports deploying non-dojo contracts. Please refer to [install instructions](https://foundry-rs.github.io/starknet-foundry/getting-started/installation.html#installation-via-asdf).
* Setup some environment variables for `sozo`:
```bash
# Use private key or keystore, only one of them is required. For the keystore, please refer to Starkli documentation: https://book.starkli.rs/signers#encrypted-keystores.
export STARKNET_RPC_URL="https://api.cartridge.gg/x/starknet/sepolia"
export DOJO_ACCOUNT_ADDRESS="<YOUR_ACCOUNT_ADDRESS>"
export DOJO_PRIVATE_KEY="<YOUR_PRIVATE_KEY>"
```
* `sncast` doesn't support environment variables, for now, so you may have to set the options manually.
Declare or use already declared `class-hash`: `0x7f6076572e04d7182a1c5c9f1f4c15aafcb069b1bfdb3de4d7c9e47c99deeb4`.
During this tutorial, we will export environment variables, so you must remain in the same shell session.
2. Prepare fact registry contract
Declare or use already declared `class-hash`: `0x7f6076572e04d7182a1c5c9f1f4c15aafcb069b1bfdb3de4d7c9e47c99deeb4`.
Deploy or use already deployed `contract`: `0x217746a5f74c2e5b6fa92c97e902d8cd78b1fabf1e8081c4aa0d2fe159bc0eb`.
```bash
export SAYA_FACT_REGISTRY_ADDRESS="0x217746a5f74c2e5b6fa92c97e902d8cd78b1fabf1e8081c4aa0d2fe159bc0eb"
```
In the repository https://github.com/HerodotusDev/integrity run
```bash
```bash
fact_registry/1-declare.sh # extract `class-hash`
fact_registry/1-deploy.sh <CLASS_HASH> # use at <FACT_REGISTRY>
```

2. Spawn world
```
You must choose a world's name as you may deploy the exact same code as an other person trying this example. The world's name must fit into 31 characters.
3. Spawn world
```bash
cargo run -r --bin sozo -- \
build \
--manifest-path examples/spawn-and-move/Scarb.toml

cargo run -r --bin sozo -- \
migrate apply \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url <SEPOLIA_ENDPOINT> \
--private-key <SEPOLIA_PRIVATE_KEY> \
--account-address <SEPOLIA_ACCOUNT_ADDRESS> \
--fee-estimate-multiplier 20 \
--name <WORLD_NAME>
```
You must choose a different world seed as an other person trying this example will have the same world's address. To modify the world's seed, modify the `seed` parameter in the `examples/spawn-and-move/dojo_saya.toml` file.
Once the migration is done, please take note of the address of the world as it will be re-used in the commands below.
Then execute this command, being at the root of the repository:
1. Set world configs
```bash
bash bin/saya/scripts/1_world_setup.sh
```
```bash
cargo run -r --bin sozo -- \
execute <WORLD_ADDRESS> set_differ_program_hash \
-c 0xa73dd9546f9858577f9fdbe43fd629b6f12dc638652e11b6e29155f4c6328 \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url <SEPOLIA_ENDPOINT> \
--private-key <SEPOLIA_PRIVATE_KEY> \
--account-address <SEPOLIA_ACCOUNT_ADDRESS> \
--fee-estimate-multiplier 20 \
--world <WORLD_ADDRESS> \
--wait

cargo run -r --bin sozo -- \
execute <WORLD_ADDRESS> set_merger_program_hash \
-c 0xc105cf2c69201005df3dad0050f5289c53d567d96df890f2142ad43a540334 \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url <SEPOLIA_ENDPOINT> \
--private-key <SEPOLIA_PRIVATE_KEY> \
--account-address <SEPOLIA_ACCOUNT_ADDRESS> \
--fee-estimate-multiplier 20 \
--world <WORLD_ADDRESS> \
--wait

cargo run -r --bin sozo -- \
execute <WORLD_ADDRESS> set_facts_registry \
-c 0x217746a5f74c2e5b6fa92c97e902d8cd78b1fabf1e8081c4aa0d2fe159bc0eb \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url <SEPOLIA_ENDPOINT> \
--private-key <SEPOLIA_PRIVATE_KEY> \
--account-address <SEPOLIA_ACCOUNT_ADDRESS> \
--fee-estimate-multiplier 20 \
--world <WORLD_ADDRESS> \
--wait
```
4. Preparing Piltover Contract
```bash
sncast -a <SAYA_SNCAST_ACCOUNT_NAME> -u <SEPOLIA_ENDPOINT> deploy \
--class-hash <SAYA_PILTOVER_CLASS_HASH> \
-c <SEPOLIA_ACCOUNT_ADDRESS> 0 <SAYA_FORK_BLOCK_NUMBER + 1> 0
```
Save the address of the deployed contract, we will use it later as SAYA_PILTOVER_ADDRESS
```bash
sncast -a <SAYA_SNCAST_ACCOUNT_NAME> -u <SEPOLIA_ENDPOINT> --wait invoke \
--contract-address <SAYA_PILTOVER_ADDRESS> --function set_program_info -c 0x042066b8031c907125abd1acb9265ad2ad4b141858d1e1e3caafb411d9ab71cc 42
```
```bash
sncast -a <SAYA_SNCAST_ACCOUNT_NAME> -u <SEPOLIA_ENDPOINT> --wait invoke \
--contract-address <SAYA_PILTOVER_ADDRESS> --function set_facts_registry -c <SAYA_FACT_REGISTRY_ADDRESS>
```
5. Start katana
Once the migration is done, the world address and the block number at which the world was deployed will be printed,
you can setup your environment variable like so:
```bash
export DOJO_WORLD_ADDRESS="<WORLD_ADDRESS>"
export SAYA_FORK_BLOCK_NUMBER="<WORLD_DEPLOYMENT_BLOCK_NUMBER>"
```
Start a local instance of Katana configured to work with the newly deployed contract. You should wait your world to be integrated into the latest block (and not the pending).
Once block in which the transaction that deploys the world is mined, you can start `katana` in forking mode.
Once those variables are exported, you can run the following command to configure the world:
```bash
bash bin/saya/scripts/2_world_config.sh
```
```bash
cargo run -r --bin katana -- \
--rpc-url <SEPOLIA_ENDPOINT> \
--fork-block-number <LATEST_BLOCK>
```

5. Run transactions on `katana`

Finally, modify the state of the world using specific actions:
4. Preparing Piltover Contract
The current Piltover contract is under [Cartridge github](https://github.com/cartridge-gg/piltover) and the class hash is `0x01dbe90a725edbf5e03dcb1f116250ba221d3231680a92894d9cc8069f209bd6`.
```bash
cargo run -r --bin sozo -- execute dojo_examples::actions::actions spawn \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url http://localhost:5050 \
--private-key <SEPOLIA_PRIVATE_KEY> \
--account-address <SEPOLIA_ACCOUNT_ADDRESS> \
--world <WORLD_ADDRESS> \
--wait
```
At the moment, we don't have a piltover maintained by Dojo community to receive all state updates for multiple
appchain, this is coming soon.

Before running `saya`, we can check the actual value for some models on Sepolia, to then see them updated by the proof being verified and the state of the world being updated.
In the `spawn-and-move` example, the `Position` model is used to store some data associated with the player,
being the contract address of the contract that called `spawn` (hence, your account address).
By default on Sepolia, it should be set like to unknown position, being like:
In the meantime, if you need to test the piltover contract, you can deploy your own piltover contract using the following command:
```bash
bash bin/saya/scripts/3_piltover.sh
```

```bash
cargo run -r --bin sozo -- model get Position <ACCOUNT_ADDRESS> \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url <SEPOLIA_ENDPOINT> \
--world <WORLD_ADDRESS>
```
5. Start katana

```json
// Expected on Sepolia as we've executed the transaction on the Katana shard.
{
player : <SEPOLIA_ACCOUNT_ADDRESS>,
vec : {
x : 0,
y : 0
Start a local instance of Katana configured to work with the newly deployed contract. You should wait your world to be integrated into the **latest block** (and not the pending one).
Once the block in which the transaction that deploys the world is mined, you can start `katana` in forking mode.

If you need to start an other terminal, you can first print the variables you need to set:
```bash
echo $STARKNET_RPC_URL
echo $SAYA_FORK_BLOCK_NUMBER
```
Then start katana with the following command:
```bash
cargo run -r --bin katana -- \
--rpc-url $STARKNET_RPC_URL \
--fork-block-number $SAYA_FORK_BLOCK_NUMBER
```

6. Run transactions on `katana`

Finally, modify the state of the world using specific actions and granting some permissions:

```bash
cargo run -r --bin sozo -- auth grant writer ns:dojo_examples,actions \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url http://localhost:5050 \
--wait
cargo run -r --bin sozo -- execute actions spawn \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url http://localhost:5050 \
--wait
```

Before running `saya`, we can check the actual value for some models on Sepolia, to then see them updated by the proof being verified and the state of the world being updated.
In the `spawn-and-move` example, the `Position` model is used to store some data associated with the player,
being the contract address of the contract that called `spawn` (hence, your account address).
By default on Sepolia, it should be set like to unknown position, being like:

```bash
cargo run -r --bin sozo -- model get Position <ACCOUNT_ADDRESS> \
--manifest-path examples/spawn-and-move/Scarb.toml \
--rpc-url http://localhost:5050
```

```json
// Expected on Sepolia as we've executed the transaction on the Katana shard.
{
player : <SEPOLIA_ACCOUNT_ADDRESS>,
vec : {
x : 0,
y : 0
}
}
}

// Expected on Katana.
{
player : <SEPOLIA_ACCOUNT_ADDRESS>,
vec : {
x : 10,
y : 10
// Expected on Katana.
{
player : <SEPOLIA_ACCOUNT_ADDRESS>,
vec : {
x : 10,
y : 10
}
}
}
```
```
6. Run saya
8. Run saya
The <PROVER_URL> could be `http://prover.visoft.dev:3618` if you have a registered key or a link to a self hosted instance of `https://github.com/neotheprogramist/http-prover`.
The <PROVER_KEY> is the private key produced by `keygen` installed with `cargo install --git https://github.com/neotheprogramist/http-prover keygen`. Pass the public key to server operator or the prover program.
The <PROVER_URL> could be `http://localhost:3618` if you have a registered key or a link to a self hosted instance of `https://github.com/cartridge-gg/http-prover`.
The <PROVER_KEY> is the private key produced by `keygen` installed with `cargo install --git https://github.com/cartridge-gg/http-prover keygen`. Pass the public key to server operator or the prover program.
If you are on an `amd64` architecture, go ahead and run the `http-prover` locally to see how it works and run this whole pipeline locally.
If not (this includes Apple Silicon), some emulation will take place to run the prover on your machine, and this is very very slow.
You can also use the service provided by cartridge by asking to pre-register your key to the service on the Cartridge discord to experiment with Saya.
It's important that the `--start-block` of Saya is the first block produced by Katana as for now Katana is not fetching events from the forked network.
If you are on an `amd64` architecture, go ahead and run the `http-prover` locally to see how it works and run this whole pipeline locally.
If not (this includes Apple Silicon), some emulation will take place to run the prover on your machine, and this is very very slow.
Starknet sepolia network chain id is `0x00000000000000000000000000000000000000000000534e5f5345504f4c4941`.
It's important that the `--start-block` of Saya is the first block produced by Katana as for now Katana is not fetching events from the forked network. To get this value, you can add one to the `SAYA_FORK_BLOCK_NUMBER` value.
```bash
cargo run -r --bin saya -- \
--mode persistent \
--rpc-url http://localhost:5050 \
--registry <SAYA_FACT_REGISTRY_ADDRESS> \
--piltover <SAYA_PILTOVER_ADDRESS> \
--world <SAYA_WORLD_ADDRESS> \
--prover-url <SAYA_PROVER_URL> \
--store-proofs \
--starknet-url <SEPOLIA_ENDPOINT> \
--signer-key <SEPOLIA_ACCOUNT_PRIVATE_KEY> \
--signer-address <SEPOLIA_ACCOUNT_ADDRESS> \
--private-key <SAYA_PROVER_KEY> \
--start-block <SAYA_FORK_BLOCK_NUMBER + 1>
```
```bash
cargo run -r --bin saya -- \
--mode persistent \
--rpc-url http://localhost:5050 \
--registry $SAYA_FACT_REGISTRY_ADDRESS \
--settlement-contract $SAYA_PILTOVER_ADDRESS \
--prover-url <PROVER_URL> \
--store-proofs \
--private-key <PROVER_PRIVATE_KEY> \
--start-block $(($SAYA_FORK_BLOCK_NUMBER + 1))
```
After this command, Saya will pick up the blocks with transactions, generate the proof for the state transition, and send it to the base layer world contract.
After this command, Saya will pick up the blocks with transactions, generate the proof for the state transition, and send it to the base layer world contract.
Once the world on Sepolia is updated, you can issue again the `model get` command as seen before, and you should see the `katana` shard state reflected on Sepolia.
Once the world on Sepolia is updated, you can issue again the `model get` command as seen before, and you should see the `katana` shard state reflected on Sepolia.
Ensure to replace placeholders (`<>`) with appropriate values for your configuration and environment. This documentation provides a comprehensive overview for developers and operators to effectively utilize the Saya service in blockchain applications.
Ensure to replace placeholders (`<>`) with appropriate values for your configuration and environment. This documentation provides a comprehensive overview for developers and operators to effectively utilize the Saya service in blockchain applications.
Loading

0 comments on commit de8095d

Please sign in to comment.