Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Nov 21, 2024
1 parent 6dd2e79 commit 3b81952
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 109 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ build-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
# 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
20 changes: 11 additions & 9 deletions crates/jstzd/src/task/jstzd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
utils::{get_block_level, retry},
Task,
};
use anyhow::{anyhow, Result};
use anyhow::{bail, Context, Result};
use async_dropper_simple::{AsyncDrop, AsyncDropper};
use async_trait::async_trait;
use axum::{
Expand Down Expand Up @@ -85,8 +85,8 @@ impl Task for Jstzd {
let octez_client = OctezClient::new(config.octez_client_config.clone());
Self::wait_for_node(&octez_node).await?;

Self::import_activator(&octez_client).await;
Self::import_rollup_operator(&octez_client).await;
Self::import_activator(&octez_client).await?;
Self::import_rollup_operator(&octez_client).await?;
Self::activate_protocol(&octez_client, &config.protocol_params).await?;
let baker = OctezBaker::spawn(config.baker_config.clone()).await?;
Self::wait_for_block_level(&config.octez_node_config.rpc_endpoint, 3).await?;
Expand Down Expand Up @@ -153,21 +153,21 @@ impl Jstzd {
"unencrypted:edsk3gUfUPyBSfrS9CCgmCiQsTCHGkviBDusMxDJstFtojtc1zcpsh";
const ROLLUP_OPERATOR_ACCOUNT_ALIAS: &'static str = "bootstrap1";

async fn import_activator(octez_client: &OctezClient) {
async fn import_activator(octez_client: &OctezClient) -> Result<()> {
octez_client
.import_secret_key(Self::ACTIVATOR_ACCOUNT_ALIAS, Self::ACTIVATOR_ACCOUNT_SK)
.await
.expect("Failed to import account 'activator'");
.context("Failed to import account 'activator'")
}

async fn import_rollup_operator(octez_client: &OctezClient) {
async fn import_rollup_operator(octez_client: &OctezClient) -> Result<()> {
octez_client
.import_secret_key(
Self::ROLLUP_OPERATOR_ACCOUNT_ALIAS,
Self::ROLLUP_OPERATOR_ACCOUNT_SK,
)
.await
.expect("Failed to import account 'rollup_operator'");
.context("Failed to import account 'rollup_operator'")
}

async fn activate_protocol(
Expand Down Expand Up @@ -200,11 +200,13 @@ impl Jstzd {
/// Wait for the baker to bake at least `level` blocks.
async fn wait_for_block_level(node_endpoint: &Endpoint, level: i64) -> Result<()> {
let ready = retry(10, 1000, || async {
Ok(get_block_level(&node_endpoint.to_string()).await? >= level)
get_block_level(&node_endpoint.to_string())
.await
.map(|l| l >= level)
})
.await;
if !ready {
return Err(anyhow!("baker is not ready after retries"));
bail!("baker is not ready after retries");
}
Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions crates/jstzd/src/task/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use serde_json::Value;

pub async fn retry<'a, F>(retries: u16, interval_ms: u64, f: impl Fn() -> F) -> bool
Expand All @@ -21,11 +21,11 @@ pub async fn get_block_level(rpc_endpoint: &str) -> Result<i64> {
let blocks_head_endpoint = format!("{}/chains/main/blocks/head", rpc_endpoint);
let response: Value = reqwest::get(&blocks_head_endpoint).await?.json().await?;

if let Some(level) = response
let level = response
.get("header")
.and_then(|header| header.get("level"))
{
return level.as_i64().ok_or(anyhow!("Level is not a valid i64"));
}
bail!("Failed to extract level from the JSON response")
.ok_or_else(|| anyhow!("Failed to extract level from head block"))?;
level
.as_i64()
.ok_or_else(|| anyhow!("Level is not a valid i64"))
}
185 changes: 92 additions & 93 deletions crates/jstzd/tests/jstzd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ use jstzd::task::jstzd::{JstzdConfig, JstzdServer};
use jstzd::task::utils::retry;
use jstzd::{EXCHANGER_ADDRESS, JSTZ_NATIVE_BRIDGE_ADDRESS, JSTZ_ROLLUP_ADDRESS};
use octez::r#async::baker::{BakerBinaryPath, OctezBakerConfigBuilder};
use octez::r#async::client::{OctezClient, OctezClientConfig, OctezClientConfigBuilder};
use octez::r#async::client::{OctezClient, OctezClientConfigBuilder};
use octez::r#async::endpoint::Endpoint;
use octez::r#async::node_config::{OctezNodeConfigBuilder, OctezNodeRunOptionsBuilder};
use octez::r#async::protocol::{
BootstrapAccount, BootstrapContract, BootstrapSmartRollup, Protocol,
ProtocolParameter, ProtocolParameterBuilder, SmartRollupPvmKind,
BootstrapAccount, BootstrapContract, BootstrapSmartRollup, ProtocolParameterBuilder,
SmartRollupPvmKind,
};
use octez::r#async::rollup::{OctezRollupConfigBuilder, RollupDataDir};
use octez::unused_port;
use serde::Deserialize;
use serde_json::from_str;
use std::fs;
use std::path::Path;
use tempfile::NamedTempFile;
use tezos_crypto_rs::hash::SmartRollupHash;

Expand Down Expand Up @@ -200,7 +199,7 @@ async fn ensure_jstzd_components_are_up(

// check if rollup is running
let rollup_running =
retry(30, 1000, || rollup_health_check(&rollup_rpc_endpoint)).await;
retry(30, 1000, || rollup_health_check(rollup_rpc_endpoint)).await;
assert!(rollup_running);

assert!(jstzd.health_check().await);
Expand Down Expand Up @@ -297,69 +296,69 @@ async fn fetch_config_test(jstzd_config: JstzdConfig, jstzd_port: u16) {
);
}

async fn build_configs(
octez_node_rpc_endpoint: &Endpoint,
rollup_rpc_endpoint: &Endpoint,
) -> (JstzdConfig, OctezClientConfig, NamedTempFile) {
let run_options = OctezNodeRunOptionsBuilder::new()
.set_synchronisation_threshold(0)
.set_network("sandbox")
.build();
let octez_node_config = OctezNodeConfigBuilder::new()
.set_network("sandbox")
.set_rpc_endpoint(octez_node_rpc_endpoint)
.set_run_options(&run_options)
.build()
.unwrap();

let (rollup_kernel_installer, rollup_preimages_dir, rollup_parameters_ty) =
jstz_rollup_files();

let protocol_params =
build_protocol_params(&rollup_kernel_installer, &rollup_parameters_ty).await;

let octez_client_config =
OctezClientConfigBuilder::new(octez_node_rpc_endpoint.clone())
.build()
.unwrap();

let baker_config = OctezBakerConfigBuilder::new()
.set_binary_path(BakerBinaryPath::Env(protocol_params.protocol().clone()))
.set_octez_client_base_dir(
PathBuf::from(octez_client_config.base_dir())
.to_str()
.unwrap(),
)
.set_octez_node_endpoint(octez_node_rpc_endpoint)
.build()
.expect("Failed to build baker config");

let kernel_debug_file = NamedTempFile::new().unwrap();
let rollup_config = OctezRollupConfigBuilder::new(
octez_node_rpc_endpoint.clone(),
octez_client_config.base_dir().into(),
SmartRollupHash::from_base58_check(JSTZ_ROLLUP_ADDRESS).unwrap(),
JSTZ_ROLLUP_OPERATOR_ALIAS.to_string(),
rollup_kernel_installer,
)
.set_data_dir(RollupDataDir::TempWithPreImages {
preimages_dir: rollup_preimages_dir,
})
.set_rpc_endpoint(rollup_rpc_endpoint)
.set_kernel_debug_file(kernel_debug_file.path())
.build()
.expect("failed to build rollup config");

let config = jstzd::task::jstzd::JstzdConfig::new(
octez_node_config,
baker_config,
octez_client_config.clone(),
rollup_config,
protocol_params,
);

(config, octez_client_config, kernel_debug_file)
}
// async fn build_configs(
// octez_node_rpc_endpoint: &Endpoint,
// rollup_rpc_endpoint: &Endpoint,
// ) -> (JstzdConfig, OctezClientConfig, NamedTempFile) {
// let run_options = OctezNodeRunOptionsBuilder::new()
// .set_synchronisation_threshold(0)
// .set_network("sandbox")
// .build();
// let octez_node_config = OctezNodeConfigBuilder::new()
// .set_network("sandbox")
// .set_rpc_endpoint(octez_node_rpc_endpoint)
// .set_run_options(&run_options)
// .build()
// .unwrap();

// let (rollup_kernel_installer, rollup_preimages_dir, rollup_parameters_ty) =
// jstz_rollup_files();

// let protocol_params =
// build_protocol_params(&rollup_kernel_installer, &rollup_parameters_ty).await;

// let octez_client_config =
// OctezClientConfigBuilder::new(octez_node_rpc_endpoint.clone())
// .build()
// .unwrap();

// let baker_config = OctezBakerConfigBuilder::new()
// .set_binary_path(BakerBinaryPath::Env(protocol_params.protocol().clone()))
// .set_octez_client_base_dir(
// PathBuf::from(octez_client_config.base_dir())
// .to_str()
// .unwrap(),
// )
// .set_octez_node_endpoint(octez_node_rpc_endpoint)
// .build()
// .expect("Failed to build baker config");

// let kernel_debug_file = NamedTempFile::new().unwrap();
// let rollup_config = OctezRollupConfigBuilder::new(
// octez_node_rpc_endpoint.clone(),
// octez_client_config.base_dir().into(),
// SmartRollupHash::from_base58_check(JSTZ_ROLLUP_ADDRESS).unwrap(),
// JSTZ_ROLLUP_OPERATOR_ALIAS.to_string(),
// rollup_kernel_installer,
// )
// .set_data_dir(RollupDataDir::TempWithPreImages {
// preimages_dir: rollup_preimages_dir,
// })
// .set_rpc_endpoint(rollup_rpc_endpoint)
// .set_kernel_debug_file(kernel_debug_file.path())
// .build()
// .expect("failed to build rollup config");

// let config = jstzd::task::jstzd::JstzdConfig::new(
// octez_node_config,
// baker_config,
// octez_client_config.clone(),
// rollup_config,
// protocol_params,
// );

// (config, octez_client_config, kernel_debug_file)
// }

fn jstz_rollup_files() -> (PathBuf, PathBuf, PathBuf) {
(
Expand All @@ -369,31 +368,31 @@ fn jstz_rollup_files() -> (PathBuf, PathBuf, PathBuf) {
)
}

async fn build_protocol_params(
rollup_kernel_installer: &Path,
rollup_parameters_ty: &Path,
) -> ProtocolParameter {
ProtocolParameterBuilder::new()
.set_protocol(Protocol::Alpha)
.set_bootstrap_accounts([
// activator is given at least 12000 (6000 for bootstrap + 6000 for baking) tez for baking
BootstrapAccount::new(ACTIVATOR_PK, 15_000_000_000).unwrap(),
BootstrapAccount::new(JSTZ_ROLLUP_OPERATOR_PK, 60_000_000_000).unwrap(),
])
.set_bootstrap_smart_rollups([BootstrapSmartRollup::new(
JSTZ_ROLLUP_ADDRESS,
SmartRollupPvmKind::Wasm,
fs::read_to_string(rollup_kernel_installer)
.unwrap()
.as_str(),
from_str(fs::read_to_string(rollup_parameters_ty).unwrap().as_str())
.expect("failed to stringify JSON"),
)
.unwrap()])
.set_bootstrap_contracts(read_bootstrap_contracts().await)
.build()
.expect("failed to build protocol parameters")
}
// async fn build_protocol_params(
// rollup_kernel_installer: &Path,
// rollup_parameters_ty: &Path,
// ) -> ProtocolParameter {
// ProtocolParameterBuilder::new()
// .set_protocol(Protocol::Alpha)
// .set_bootstrap_accounts([
// // activator is given at least 12000 (6000 for bootstrap + 6000 for baking) tez for baking
// BootstrapAccount::new(ACTIVATOR_PK, 15_000_000_000).unwrap(),
// BootstrapAccount::new(JSTZ_ROLLUP_OPERATOR_PK, 60_000_000_000).unwrap(),
// ])
// .set_bootstrap_smart_rollups([BootstrapSmartRollup::new(
// JSTZ_ROLLUP_ADDRESS,
// SmartRollupPvmKind::Wasm,
// fs::read_to_string(rollup_kernel_installer)
// .unwrap()
// .as_str(),
// from_str(fs::read_to_string(rollup_parameters_ty).unwrap().as_str())
// .expect("failed to stringify JSON"),
// )
// .unwrap()])
// .set_bootstrap_contracts(read_bootstrap_contracts().await)
// .build()
// .expect("failed to build protocol parameters")
// }

async fn read_bootstrap_contracts() -> Vec<BootstrapContract> {
let mut contracts = vec![];
Expand Down

0 comments on commit 3b81952

Please sign in to comment.