Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jstzd: add rollup build script and spawn rollup in jstzd #670

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ node_modules/

# Required by the CLI
crates/jstz_cli/jstz_kernel.wasm
crates/jstzd/resources/jstz_rollup/jstz_kernel.wasm

**/*/.DS_Store
6 changes: 6 additions & 0 deletions Cargo.lock

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

15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ else
PROFILE_TARGET_DIR := $(PROFILE)
endif

JSTZD_KERNEL_PATH := crates/jstzd/resources/jstz_rollup/jstz_kernel.wasm
CLI_KERNEL_PATH := crates/jstz_cli/jstz_kernel.wasm

.PHONY: all
all: build test check

.PHONY: build
build: build-cli-kernel
build: build-cli-kernel build-jstzd-kernel
@cargo build $(PROFILE_OPT)

.PHONY: build-bridge
Expand All @@ -33,6 +34,12 @@ build-bridge:
build-kernel:
@cargo build --package jstz_kernel --target wasm32-unknown-unknown $(PROFILE_OPT)

.PHONY: build-jstzd-kernel
build-jstzd-kernel: build-kernel
@cp target/wasm32-unknown-unknown/$(PROFILE_TARGET_DIR)/jstz_kernel.wasm $(JSTZD_KERNEL_PATH)

# TODO: Remove once jstzd replaces the sandbox
# https://linear.app/tezos/issue/JSTZ-205/remove-build-for-jstz-cli
.PHONY: build-cli-kernel
build-cli-kernel: build-kernel
@cp target/wasm32-unknown-unknown/$(PROFILE_TARGET_DIR)/jstz_kernel.wasm $(CLI_KERNEL_PATH)
Expand Down Expand Up @@ -114,6 +121,8 @@ fmt-check: fmt-nix-check fmt-rust-check fmt-js-check

.PHONY: lint
lint:
@touch $(CLI_KERNEL_PATH)
@touch $(CLI_KERNEL_PATH)
# Jstzd has to processes a non-empty kernel in its build script
@echo "ignore" > $(JSTZD_KERNEL_PATH)
@cargo clippy --all-targets -- --deny warnings
@rm -f $(CLI_KERNEL_PATH)
@rm -f $(CLI_KERNEL_PATH) $(JSTZD_KERNEL_PATH)
17 changes: 17 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors.workspace = true
version.workspace = true
edition.workspace = true
repository.workspace = true
include = ["resources/", "src"]

[dependencies]
anyhow.workspace = true
Expand All @@ -23,14 +24,30 @@ reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
tezos_crypto_rs.workspace = true
tokio.workspace = true

[build-dependencies]
anyhow.workspace = true
bincode.workspace = true
hex.workspace = true
tempfile.workspace = true
tezos_crypto_rs.workspace = true
tezos-smart-rollup-host.workspace = true
tezos-smart-rollup-installer.workspace = true
tezos-smart-rollup-installer-config.workspace = true
jstz_kernel = { path = "../jstz_kernel" }


[dev-dependencies]
assert_cmd.workspace = true
predicates.workspace = true
rand.workspace = true
tezos_crypto_rs.workspace = true

[features]
ignore-flaky-tests = []

[[bin]]
name = "jstzd"
path = "src/main.rs"
162 changes: 162 additions & 0 deletions crates/jstzd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use anyhow::Result;
use jstz_kernel::TICKETER;
use std::{
env, fs,
path::{Path, PathBuf},
};
use tezos_crypto_rs::hash::ContractKt1Hash;
use tezos_smart_rollup_host::path::OwnedPath;
use tezos_smart_rollup_installer::{
installer, preimages, KERNEL_BOOT_PATH, PREPARE_KERNEL_PATH,
};
use tezos_smart_rollup_installer_config::binary::owned::{
OwnedBytes, OwnedConfigInstruction, OwnedConfigProgram,
};

include!("build_config.rs");

/// The jstz kernel path used to generate the rollup installer / preimages.
/// generated by running `make build build-jstzd-kernel`
const JSTZ_KERNEL_PATH: &str = "./resources/jstz_rollup/jstz_kernel.wasm";
const JSTZ_PARAMETERS_TY_PATH: &str = "./resources/jstz_rollup/parameters_ty.json";
/// Generated file that contains path getter functions
const JSTZ_ROLLUP_PATH: &str = "jstz_rollup_path.rs";

/// Build script that generates and saves the following files in OUT_DIR:
///
/// Files copied:
/// - parameters_ty.json: JSON file containing parameter types
///
/// Files generated:
/// - kernel_installer.hex: Hex-encoded kernel installer binary
/// - preimages/: Directory containing kernel preimages
/// - jstz_rollup_path.rs: Generated Rust code with path getters
zcabter marked this conversation as resolved.
Show resolved Hide resolved
///
/// The generated jstz_rollup_path.rs provides the following functions:
/// - kernel_installer_path(): Path to the kernel installer hex file
/// - parameters_ty_path(): Path to the parameters type JSON file
/// - preimages_path(): Path to the preimages directory
fn main() {
println!("cargo:rerun-if-changed={}", JSTZ_KERNEL_PATH);
println!("cargo:rerun-if-changed={}", JSTZ_PARAMETERS_TY_PATH);

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

// 1. Copy parameters_ty.json to OUT_DIR
fs::copy(JSTZ_PARAMETERS_TY_PATH, out_dir.join("parameters_ty.json"))
.expect("Failed to copy parameters_ty.json to OUT_DIR");

// 2. Create preimages directory and the kernel installer in OUT_DIR
let preimages_dir = out_dir.join("preimages");
fs::create_dir_all(&preimages_dir).expect("Failed to create preimages directory");
let kernel_installer =
make_kernel_installer(PathBuf::from(JSTZ_KERNEL_PATH).as_path(), &preimages_dir)
.expect("Failed to make kernel installer");

// 3. Save hex-encoded kernel installer to OUT_DIR
fs::write(out_dir.join("kernel_installer.hex"), kernel_installer)
.expect("Failed to write kernel_installer.hex");

// 4. Generate path getter code in OUT_DIR
generate_code(&out_dir);

println!(
"cargo:warning=Build script output directory: {}",
ryutamago marked this conversation as resolved.
Show resolved Hide resolved
out_dir.display()
);
}

/// Builds the kernel installer and generates preimages
///
/// # Arguments
/// * `kernel_file` - Path to the kernel wasm file
/// * `preimages_dir` - Directory where preimages will be saved
///
/// # Returns
/// Hex-encoded kernel installer string
fn make_kernel_installer(kernel_file: &Path, preimages_dir: &Path) -> Result<String> {
if !kernel_file.exists() {
return Err(anyhow::anyhow!(
"kernel file not found: {}",
kernel_file.display()
));
}
let root_hash = preimages::content_to_preimages(kernel_file, preimages_dir)?;
let installer_program = OwnedConfigProgram(vec![
// 1. Prepare kernel installer
OwnedConfigInstruction::reveal_instr(
root_hash,
OwnedPath::from(PREPARE_KERNEL_PATH),
),
OwnedConfigInstruction::move_instr(
OwnedPath::from(PREPARE_KERNEL_PATH),
OwnedPath::from(KERNEL_BOOT_PATH),
),
// 2. Set `jstz` ticketer as the bridge contract address
OwnedConfigInstruction::set_instr(
OwnedBytes(bincode::serialize(&ContractKt1Hash::from_base58_check(
EXCHANGER_ADDRESS,
)?)?),
OwnedPath::from(TICKETER),
),
]);
let installer = installer::with_config_program(installer_program);
Ok(hex::encode(&installer))
}

/// Generates Rust code for path getters to access files in OUT_DIR
///
/// Generates the following functions:
/// - kernel_installer_path(): Path to the kernel installer hex file
/// - parameters_ty_path(): Path to the parameters type JSON file
/// - preimages_path(): Path to the preimages directory
fn generate_code(out_dir: &Path) {
let mut code = String::new();
code.push_str(&generate_path_getter_code(
out_dir,
"kernel_installer",
"kernel_installer.hex",
));
code.push_str(&generate_path_getter_code(
out_dir,
"parameters_ty",
"parameters_ty.json",
));
code.push_str(&generate_path_getter_code(
out_dir,
"preimages",
"preimages",
));

fs::write(out_dir.join(JSTZ_ROLLUP_PATH), code).expect("Failed to write paths.rs");
}

/// Generates a path getter function
///
/// # Arguments
/// * `out_dir` - The output directory
/// * `fn_name` - The name of the function to generate (e.g., "kernel_installer" generates kernel_installer_path())
/// * `path_suffix` - The path component to append to out_dir
///
/// # Example
/// ```
/// // Generates:
/// // const KERNEL_INSTALLER_PATH: &str = "/path/to/out_dir/kernel_installer.hex";
/// // pub fn kernel_installer_path() -> PathBuf { PathBuf::from(KERNEL_INSTALLER_PATH) }
/// generate_path_getter_code(out_dir, "kernel_installer", "kernel_installer.hex");
/// ```
fn generate_path_getter_code(out_dir: &Path, fn_name: &str, path_suffix: &str) -> String {
let name_upper = fn_name.to_uppercase();
format!(
r#"
const {}_PATH: &str = "{}";
pub fn {}_path() -> std::path::PathBuf {{
std::path::PathBuf::from({}_PATH)
}}
"#,
&name_upper,
out_dir.join(path_suffix).to_str().expect("Invalid path"),
fn_name,
&name_upper,
)
}
1 change: 1 addition & 0 deletions crates/jstzd/build_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const EXCHANGER_ADDRESS: &str = "KT1F3MuqvT9Yz57TgCS3EkDcKNZe9HpiavUJ";
42 changes: 42 additions & 0 deletions crates/jstzd/resources/jstz_rollup/parameters_ty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"prim": "or",
"args": [
{
"prim": "pair",
"args": [
{ "prim": "address" },
{
"prim": "ticket",
"args": [
{
"prim": "pair",
"args": [
{ "prim": "nat" },
{ "prim": "option", "args": [{ "prim": "bytes" }] }
]
}
]
}
]
},
{
"prim": "pair",
"args": [
{ "prim": "address" },
{ "prim": "option", "args": [{ "prim": "address" }] },
{
"prim": "ticket",
"args": [
{
"prim": "pair",
"args": [
{ "prim": "nat" },
{ "prim": "option", "args": [{ "prim": "bytes" }] }
]
}
]
}
]
}
]
}
21 changes: 20 additions & 1 deletion crates/jstzd/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::path::{Path, PathBuf};

use crate::task::jstzd::JstzdConfig;
use crate::{EXCHANGER_ADDRESS, JSTZ_NATIVE_BRIDGE_ADDRESS};
use crate::{EXCHANGER_ADDRESS, JSTZ_NATIVE_BRIDGE_ADDRESS, JSTZ_ROLLUP_ADDRESS};
use anyhow::{Context, Result};
use octez::r#async::endpoint::Endpoint;
use octez::r#async::protocol::{BootstrapContract, ProtocolParameter};
use octez::r#async::rollup::{OctezRollupConfigBuilder, RollupDataDir};
use octez::{
r#async::{
baker::{BakerBinaryPath, OctezBakerConfig, OctezBakerConfigBuilder},
Expand All @@ -14,6 +16,7 @@ use octez::{
unused_port,
};
use serde::Deserialize;
use tezos_crypto_rs::hash::SmartRollupHash;
use tokio::io::AsyncReadExt;

const ACTIVATOR_PUBLIC_KEY: &str =
Expand Down Expand Up @@ -65,6 +68,21 @@ pub(crate) async fn build_config(
&octez_client_config,
)?;

// TODO: https://linear.app/tezos/issue/JSTZ-238/deserialize-rollup-config
// Dummy rollup config for now
let octez_node_endpoint = octez_node_config.rpc_endpoint.clone();
let octez_rollup_config = OctezRollupConfigBuilder::new(
octez_node_endpoint,
octez_client_config.base_dir().into(),
SmartRollupHash::from_base58_check(JSTZ_ROLLUP_ADDRESS).unwrap(),
"bootstrap1".to_string(),
"dummy-kernel".into(),
)
.set_data_dir(RollupDataDir::Temp)
.set_rpc_endpoint(&Endpoint::localhost(8000))
.build()
.unwrap();

let protocol_params = build_protocol_params(config.protocol).await?;
let server_port = config.server_port.unwrap_or(unused_port());
Ok((
Expand All @@ -73,6 +91,7 @@ pub(crate) async fn build_config(
octez_node_config,
baker_config,
octez_client_config,
octez_rollup_config,
protocol_params,
),
))
Expand Down
5 changes: 4 additions & 1 deletion crates/jstzd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ pub mod docker;
pub mod task;

pub use config::BOOTSTRAP_CONTRACT_NAMES;
pub mod jstz_rollup_path {
include!(concat!(env!("OUT_DIR"), "/jstz_rollup_path.rs"));
}
use std::process::exit;

pub const EXCHANGER_ADDRESS: &str = "KT1F3MuqvT9Yz57TgCS3EkDcKNZe9HpiavUJ";
include!("../build_config.rs");
pub const JSTZ_ROLLUP_ADDRESS: &str = "sr1PuFMgaRUN12rKQ3J2ae5psNtwCxPNmGNK";
pub const JSTZ_NATIVE_BRIDGE_ADDRESS: &str = "KT1GFiPkkTjd14oHe6MrBPiRh5djzRkVWcni";

Expand Down
Loading
Loading