Skip to content

Commit

Permalink
Update z4 doc
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhuachuang committed Jun 5, 2024
1 parent 7b520a6 commit 7d56ee4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
54 changes: 36 additions & 18 deletions content/blog/workshop-z4.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# use stable rustc
rustup default stable

# create new z4 game server directory
cargo new shoot
# install z4 command
cargo install z4

# generate new game with z4 command
z4 new --name my-game

cd my-game
```

In the demo project, we add z4 to dependency, because z4 not upload to crates.io, so, we use git path.
`z4-engine = { git = "https://github.com/zypher-game/z4" }`
In the demo project, we will see z4 in dependency.
`z4-engine = "0.1"`

## Step 1: Deploy standard Z4 game contract
We have two options:
Expand All @@ -55,10 +60,11 @@ npm install
# deploy stardard game contract
npm run deploy
```
Then, we will get the game contract address in public/xx.json
Then, we will get the game contract address in terminal output.

- we can also do some custom development based on the Z4 standard game contract.
Contract is [here](https://github.com/zypher-game/z4/blob/main/contracts/solidity/contracts/SimpleGame.sol).
And now the default contracts will at `contracts/` and only the solidity files, because solidity is default, you can choose other smart contract template when new a game project.

```solidity
contract SimpleGame is RoomMarket {
Expand Down Expand Up @@ -98,7 +104,12 @@ impl Handler for ShootHandler {
}

// create new game room
async fn create(peers: &[(Address, PeerId, [u8; 32])], _params: Vec<u8>, _rid: RoomId) -> (Self, Tasks<Self>) {
async fn create(
peers: &[(Address, PeerId, [u8; 32])],
_params: Vec<u8>,
_rid: RoomId,
_seed: [u8; 32],
) -> (Self, Tasks<Self>) {
// here we create ShootHandler
}

Expand Down Expand Up @@ -193,20 +204,27 @@ After we finished writing the circuit, we found that it is actually very difficu

## Step 4: Running Z4 node

After we have completed the logic of the game, we need to run it. Configure some parameters.
After we have completed the logic of the game, we need to run it. Configure some parameters in `.env` file, if not exists, copy `.env-template`.
in .env:
```
NETWORK=localhost
GAMES=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
SECRET_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 # Hardhat default sk!
# START_BLOCK=1 # if not set, will sync from latest block
# RPC_ENDPOINTS=https://xxx.com,https://xxxx.com
# URL_HTTP=http://127.0.0.1:8080
# URL_WEBSOCKET=ws://127.0.0.1:8000
# HTTP_PORT=8080
# WS_PORT=8000
# P2P_PORT=7364
# AUTO_STAKE=true # if true, will stake when starting with URL_HTTP & URL_WEBSOCKET
# ROOM_MARKET=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 # if game and room market not the same contract
# RUST_LOG=info
```
we need change games contract address, z4 node account secret key, and other configures. Next we run z4!

```rust
let mut config = Config::default();
config.http_port = http_port;
config.ws_port = Some(ws_port);
config.secret_key = secret_key.to_owned();
config.chain_network = "network";
config.chain_start_block = start_block;
config.games = vec![game.to_owned()];
config.auto_stake = true;
config.url_http = "server_url";
config.url_websocket = "server_url";

let config = Config::from_env().unwrap();
Engine::<ShootHandler>::init(config).run().await.expect("Down");
```

Expand Down
47 changes: 26 additions & 21 deletions content/zk/z4/based-uzkge.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ top = false
## Contents
In z4 engine, developers only need to implement an interface and configure some startup parameters, and then will get a complete z4 game node.

`z4 new my-game` will generate new Z4 game template.

### Handler

`Handler` trait is the core interface. Developers can implement the game logic in it, and then they can directly use the z4 engine to run it. This interface including `accept` room events from the chain, `create` room events when the room is accepted by this node, and also supporting events when nodes go online and offline (connected and disconnected with websocket), and finally how to handle user request events in general.
Expand Down Expand Up @@ -135,6 +137,25 @@ Supports three methods, corresponding to different return value types. `add_all`
### Run on Z4 !
Run the defined `Handler` to get a complete z4 node, it's simple!

let's create `.env` file in current directory.

```
NETWORK=localhost
GAMES=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
SECRET_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 # Hardhat default sk!
# START_BLOCK=1 # if not set, will sync from latest block
# RPC_ENDPOINTS=https://xxx.com,https://xxxx.com
# URL_HTTP=http://127.0.0.1:8080
# URL_WEBSOCKET=ws://127.0.0.1:8000
# HTTP_PORT=8080
# WS_PORT=8000
# AUTO_STAKE=true # if true, will stake when starting with URL_HTTP & URL_WEBSOCKET
# ROOM_MARKET=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 # if game and room market not the same contract
# RUST_LOG=info
```

And now, let's run z4 node!

```rust
use z4_engine::{Config, Engine};

Expand All @@ -145,27 +166,11 @@ impl Handler for MyHandler { ... }

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();

let network = std::env::var("NETWORK").unwrap();
let game = std::env::var("GAME").unwrap();
let secret_key = std::env::var("SECRET_KEY").unwrap();
let start_block = std::env::var("START_BLOCK").ok().map(|v| v.parse().unwrap());
let server = std::env::var("PUBLIC_SERVER").unwrap();
let http_port = std::env::var("HTTP_PORT").unwrap_or("8080".to_owned()).parse().unwrap();
let ws_port = std::env::var("WS_PORT").unwrap_or("8000".to_owned()).parse().unwrap();

let mut config = Config::default();
config.http_port = http_port;
config.ws_port = Some(ws_port);
config.secret_key = secret_key.to_owned();
config.chain_network = network;
config.chain_start_block = start_block;
config.games = vec![game.to_owned()];
config.auto_stake = true;
config.http = server;

Engine::<MyHandler>::init(config).run().await.expect("Down");
let config = Config::from_env().unwrap();
Engine::<MyHandler>::init(config)
.run()
.await
.expect("Down");
}
```

Expand Down
2 changes: 2 additions & 0 deletions content/zk/z4/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ It includes smart contracts, on-chain and off-chain interactions, RPC and P2P ne
Developers can freely and flexibly customize their own exclusive z4 nodes for their games, or they can use general zkVM game nodes without having to deal with any off-chain certification.

In this chapter, we will describe in detail how to customize z4 nodes and how to use general z4 nodes.

Firstly, let's install z4 command for better starting. `cargo install z4`.

0 comments on commit 7d56ee4

Please sign in to comment.