Skip to content

Commit

Permalink
feat(jstzd): add build script for rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Dec 4, 2024
1 parent 5647e1c commit fcc31a2
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 25 deletions.
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.

13 changes: 10 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,6 @@ fmt-check: fmt-nix-check fmt-rust-check fmt-js-check

.PHONY: lint
lint:
@touch $(CLI_KERNEL_PATH)
@touch $(CLI_KERNEL_PATH) $(JSTZD_KERNEL_PATH)
@cargo clippy --all-targets -- --deny warnings
@rm -f $(CLI_KERNEL_PATH)
@rm -f $(CLI_KERNEL_PATH) $(JSTZD_KERNEL_PATH)
13 changes: 13 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 @@ -25,6 +26,18 @@ serde_json.workspace = true
tempfile.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
Expand Down
163 changes: 163 additions & 0 deletions crates/jstzd/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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
///
/// 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: {}",
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() -> PathBuf {{
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" }] }
]
}
]
}
]
}
]
}
2 changes: 1 addition & 1 deletion crates/jstzd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod task;
pub use config::BOOTSTRAP_CONTRACT_NAMES;
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
2 changes: 1 addition & 1 deletion crates/jstzd/tests/sandbox-params.json

Large diffs are not rendered by default.

Loading

0 comments on commit fcc31a2

Please sign in to comment.