Skip to content

Commit

Permalink
feat(sandbox): update sandbox to deploy native bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Jul 31, 2024
1 parent e10c281 commit 31ceb82
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 44 deletions.
1 change: 0 additions & 1 deletion crates/jstz_cli/src/sandbox/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub const SANDBOX_OCTEZ_NODE_RPC_PORT: u16 = 18730;
pub const SANDBOX_JSTZ_NODE_PORT: u16 = 8933;
pub const SANDBOX_OCTEZ_SMART_ROLLUP_PORT: u16 = 8932;
pub const SANDBOX_BOOTSTRAP_ACCOUNT_XTZ_AMOUNT: u64 = 4000000000000;
pub const SANDBOX_BOOTSTRAP_ACCOUNT_CTEZ_AMOUNT: u64 = 100000000000;
pub const SANDBOX_BOOTSTRAP_ACCOUNTS: [SandboxBootstrapAccount; 5] = [
SandboxBootstrapAccount {
address: "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx",
Expand Down
38 changes: 7 additions & 31 deletions crates/jstz_cli/src/sandbox/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use indicatif::{ProgressBar, ProgressStyle};
use jstz_rollup::{
deploy_ctez_contract, rollup::make_installer, BootstrapAccount, BridgeContract,
JstzRollup,
};
use jstz_rollup::{rollup::make_installer, Exchanger, JstzRollup, NativeBridge};
use nix::{
sys::signal::{kill, Signal},
unistd::Pid,
Expand Down Expand Up @@ -47,9 +44,7 @@ use crate::{
term::{self, styles},
};

use super::{
SANDBOX_BOOTSTRAP_ACCOUNT_CTEZ_AMOUNT, SANDBOX_BOOTSTRAP_ACCOUNT_XTZ_AMOUNT,
};
use super::SANDBOX_BOOTSTRAP_ACCOUNT_XTZ_AMOUNT;

fn octez_node_endpoint() -> String {
format!(
Expand Down Expand Up @@ -220,16 +215,6 @@ const ACTIVATOR_ACCOUNT_SK: &str =

const OPERATOR_ADDRESS: &str = "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"; // bootstrap1

fn ctez_bootstrap_accounts() -> Vec<BootstrapAccount> {
SANDBOX_BOOTSTRAP_ACCOUNTS
.iter()
.map(|account| BootstrapAccount {
address: account.address.to_string(),
amount: SANDBOX_BOOTSTRAP_ACCOUNT_CTEZ_AMOUNT,
})
.collect::<Vec<BootstrapAccount>>()
}

fn cached_identity_path() -> PathBuf {
jstz_home_dir().join("octez-node-identity.json")
}
Expand Down Expand Up @@ -455,20 +440,14 @@ fn start_sandbox(
// 5. Deploy bridge
progress_step(log_file, progress);
debug!(log_file, "Deploying bridge...");
let client = cfg.octez_client_sandbox()?;

let ctez_address = deploy_ctez_contract(
&cfg.octez_client_sandbox()?,
OPERATOR_ADDRESS,
ctez_bootstrap_accounts(),
)?;
let exchanger = Exchanger::deploy(&client, OPERATOR_ADDRESS)?;
debug!(log_file, "Exchanger deployed at {}", exchanger);

progress_step(log_file, progress);

let bridge = BridgeContract::deploy(
&cfg.octez_client_sandbox()?,
OPERATOR_ADDRESS,
&ctez_address,
)?;
let bridge = NativeBridge::deploy(&client, OPERATOR_ADDRESS, &exchanger)?;

debug!(log_file, "Bridge deployed at {}", bridge);

Expand All @@ -478,7 +457,7 @@ fn start_sandbox(

let preimages_dir = TempDir::with_prefix("jstz_sandbox_preimages")?.into_path();

let installer = make_installer(&jstz_kernel_path(), &preimages_dir, &bridge)?;
let installer = make_installer(&jstz_kernel_path(), &preimages_dir, &exchanger)?;
debug!(
log_file,
"Installer kernel created with preimages at {:?}", preimages_dir
Expand Down Expand Up @@ -508,7 +487,6 @@ fn start_sandbox(

// 9. Set the rollup address in the bridge
progress_step(log_file, progress);
bridge.set_rollup(&cfg.octez_client_sandbox()?, OPERATOR_ADDRESS, &rollup)?;
debug!(log_file, "`jstz_bridge` `rollup` address set to {}", rollup);

Ok(sandbox)
Expand All @@ -519,7 +497,6 @@ fn format_sandbox_bootstrap_accounts() -> Table {
table.set_titles(Row::new(vec![
Cell::new("Address"),
Cell::new("XTZ Balance"),
Cell::new("CTEZ Balance"),
]));

for (i, bootstrap_account) in SANDBOX_BOOTSTRAP_ACCOUNTS.iter().enumerate() {
Expand All @@ -530,7 +507,6 @@ fn format_sandbox_bootstrap_accounts() -> Table {
bootstrap_account.address
)),
Cell::new(&SANDBOX_BOOTSTRAP_ACCOUNT_XTZ_AMOUNT.to_string()),
Cell::new(&SANDBOX_BOOTSTRAP_ACCOUNT_CTEZ_AMOUNT.to_string()),
]));
}

Expand Down
53 changes: 53 additions & 0 deletions crates/jstz_rollup/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::BootstrapAccount;

const CTEZ_CONTRACT: &str = include_str!("../../../contracts/jstz_ctez.tz");
const BRIDGE_CONTRACT: &str = include_str!("../../../contracts/jstz_bridge.tz");
const EXCHANGER_CONTRACT: &str = include_str!("../../../contracts/exchanger.tz");
const NATIVE_BRDIGE_CONTRACT: &str =
include_str!("../../../contracts/jstz_native_bridge.tz");

impl BootstrapAccount {
fn as_michelson_elt(&self) -> String {
Expand Down Expand Up @@ -36,6 +39,56 @@ pub fn deploy_ctez_contract(
client.originate_contract("jstz_ctez", operator_address, CTEZ_CONTRACT, &init_storage)
}

#[derive(Debug, Clone, PartialEq, Eq, Deref, DerefMut)]
pub struct Exchanger(String);

impl Exchanger {
pub fn deploy(client: &OctezClient, operator: &str) -> Result<Self> {
let storage_init = "Unit";
client
.originate_contract("exchanger", operator, EXCHANGER_CONTRACT, storage_init)
.map(Exchanger)
}
}

impl Display for Exchanger {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<ContractKt1Hash> for Exchanger {
fn from(hash: ContractKt1Hash) -> Self {
Self(hash.to_base58_check())
}
}

pub struct NativeBridge(String);

impl NativeBridge {
pub fn deploy(
client: &OctezClient,
operator: &str,
exchanger: &Exchanger,
) -> Result<NativeBridge> {
let storage_init = format!("(Pair \"{}\" None)", exchanger.0);
client
.originate_contract(
"jstz_native_bridge",
operator,
NATIVE_BRDIGE_CONTRACT,
&storage_init,
)
.map(NativeBridge)
}
}

impl Display for NativeBridge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Deref, DerefMut)]
pub struct BridgeContract(String);

Expand Down
18 changes: 10 additions & 8 deletions crates/jstz_rollup/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use figment::{
Figment,
};
use jstz_rollup::{
deploy_ctez_contract, rollup, BootstrapAccount, BridgeContract, JstzRollup,
deploy_ctez_contract, rollup, BootstrapAccount, BridgeContract, Exchanger, JstzRollup,
};
use octez::{OctezClient, OctezRollupNode, OctezThread};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -285,14 +285,15 @@ fn import_keys(cfg: &Config, alias: Option<String>, secret_key: &str) -> Result<

fn make_installer(
kernel: PathBuf,
bridge: ContractKt1Hash,
exchanger: ContractKt1Hash,
output: PathBuf,
) -> Result<()> {
let bridge = BridgeContract::from(bridge);
let exchanger = Exchanger::from(exchanger);

print!("Building installer...");

let installer = rollup::make_installer(&kernel, &output.join("preimages"), &bridge)?;
let installer =
rollup::make_installer(&kernel, &output.join("preimages"), &exchanger)?;
fs::write(output.join("installer.wasm"), installer)?;

println!(" done");
Expand Down Expand Up @@ -353,24 +354,25 @@ fn deploy(
cfg: &Config,
operator: Option<Tz1AddressOrAlias>,
kernel: PathBuf,
bridge: ContractKt1Hash,
exchanger: ContractKt1Hash,
output: PathBuf,
) -> Result<()> {
let client = cfg.octez_client();
let operator = Operator::try_from(operator)?;
let bridge = BridgeContract::from(bridge);
let exchanger = Exchanger::from(exchanger);

print!("Building installer...");

let installer = rollup::make_installer(&kernel, &output.join("preimages"), &bridge)?;
let installer =
rollup::make_installer(&kernel, &output.join("preimages"), &exchanger)?;
fs::write(output.join("installer.wasm"), &installer)?;

println!(" done");

println!("Deploying rollup...");

let rollup_address = JstzRollup::deploy(&client, &operator.to_string(), &installer)?;
bridge.set_rollup(&client, &operator.to_string(), &rollup_address)?;
// bridge.set_rollup(&client, &operator.to_string(), &rollup_address)?;

println!("\tAddress: {}", rollup_address);

Expand Down
8 changes: 4 additions & 4 deletions crates/jstz_rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use tezos_smart_rollup_installer_config::binary::owned::{
OwnedBytes, OwnedConfigInstruction, OwnedConfigProgram,
};

use crate::BridgeContract;
use crate::Exchanger;

const TICKETER_PATH: RefPath = RefPath::assert_from(b"/ticketer");
const ROLLUP_MICHELSON_TYPE: &str = "(pair bytes (ticket (pair nat (option bytes))))";
const ROLLUP_MICHELSON_TYPE: &str = "(pair address (ticket (pair nat (option bytes))))";

pub fn make_installer(
kernel_file: &Path,
preimages_dir: &Path,
bridge_contract: &BridgeContract,
exchanger: &Exchanger,
) -> Result<Vec<u8>> {
let root_hash = preimages::content_to_preimages(kernel_file, preimages_dir)?;

Expand All @@ -43,7 +43,7 @@ pub fn make_installer(
// 2. Set `jstz` ticketer as the bridge contract address
OwnedConfigInstruction::set_instr(
OwnedBytes(bincode::serialize(&ContractKt1Hash::from_base58_check(
bridge_contract,
exchanger,
)?)?),
OwnedPath::from(TICKETER_PATH),
),
Expand Down

0 comments on commit 31ceb82

Please sign in to comment.