Skip to content

Commit

Permalink
feat: ism deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt committed May 16, 2024
1 parent 78cc61f commit 751b222
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 167 deletions.
6 changes: 6 additions & 0 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v
sort-module-level-items = true

[[target.starknet-contract]]
# Enable Sierra codegen.
sierra = true

# Enable CASM codegen.
casm = true
# Emit Python-powered hints in order to run compiled CASM class with legacy Cairo VM.
casm-add-pythonic-hints = true
2 changes: 2 additions & 0 deletions rust/Cargo.lock

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

4 changes: 4 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.2.9", fea
ethers = "2.0.7"
serde = { version = "1.0.162", default-features = false, features = ["derive"] }
serde_json = "1.0.96"
futures = "0.3.30"

# utilities
thiserror = { version = "1.0.37" }
anyhow = { version = "1.0.71", features = ["backtrace"] }
eyre = { version = "0.6.8" }

# crypto
k256 = { version = "0.13.1", default-features = false, features = ["ecdsa"] }

rstest = "0.18.2"

[build-dependencies]
Expand Down
16 changes: 11 additions & 5 deletions rust/tests/contracts/strk/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use super::{hook::Hook, types::Codes, StarknetAccount};
use super::{
hook::Hook,
ism::Ism,
types::{Codes, CoreDeployments},
StarknetAccount,
};

pub fn deploy_core(
owner: StarknetAccount,
deployer: StarknetAccount,
owner: &StarknetAccount,
deployer: &StarknetAccount,
codes: &Codes,
domain: u32,
default_ism: String,
default_ism: Ism,
default_hook: Hook,
required_hook: Hook,
) {
) -> eyre::Result<CoreDeployments> {
// deploy mailbox
Ok(CoreDeployments::default())
}
1 change: 1 addition & 0 deletions rust/tests/contracts/strk/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum Hook {
},

Merkle {},
Igp(u32),

Pausable {},

Expand Down
173 changes: 173 additions & 0 deletions rust/tests/contracts/strk/ism.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use cainome::cairo_serde::ContractAddress;
use futures::{stream::FuturesUnordered, StreamExt};
use starknet::{accounts::Account, core::types::FieldElement, macros::felt};

use super::bind::multisig_ism::messageid_multisig_ism;
use super::bind::routing::domain_routing_ism;
use crate::validator::{self, TestValidators};

use super::{deploy_contract, types::Codes, StarknetAccount};

#[derive(Clone)]
pub enum Ism {
Routing(Vec<(u32, Self)>),

Multisig {
validators: validator::TestValidators,
},

Aggregate {
isms: Vec<Self>,
threshold: u8,
},

#[allow(dead_code)]
Mock,
}

impl Ism {
pub fn routing(isms: Vec<(u32, Self)>) -> Self {
Self::Routing(isms)
}

pub fn multisig(validators: validator::TestValidators) -> Self {
Self::Multisig { validators }
}
}

impl Ism {
async fn deploy_mock(codes: &Codes, deployer: &StarknetAccount) -> eyre::Result<FieldElement> {
let res = deploy_contract(codes.test_mock_ism, vec![], felt!("0"), deployer).await;
Ok(res.0)
}

async fn deploy_multisig(
codes: &Codes,
set: validator::TestValidators,
owner: &StarknetAccount,
deployer: &StarknetAccount,
) -> eyre::Result<FieldElement> {
let res = deploy_contract(
codes.ism_multisig,
vec![owner.address()],
felt!("0"),
deployer,
)
.await;

let contract = messageid_multisig_ism::new(res.0, owner);
contract
.set_validators(
&set.validators
.iter()
.map(|v| v.eth_addr())
.collect::<Vec<_>>(),
)
.send()
.await?;

Ok(res.0)
}

async fn deploy_routing(
codes: &Codes,
isms: Vec<(u32, Self)>,
owner: &StarknetAccount,
deployer: &StarknetAccount,
) -> eyre::Result<FieldElement> {
let res = deploy_contract(
codes.ism_routing,
vec![owner.address()],
felt!("0"),
deployer,
)
.await;

let futures = FuturesUnordered::new();

for i in isms.iter() {
let future = async move {
<Ism as Clone>::clone(&i.1)
.deploy(codes, owner, deployer)
.await
};
futures.push(future);
}

let results = futures.collect::<Vec<_>>().await;

let modules: Vec<_> = results
.iter()
.map(|a| ContractAddress(*a.as_ref().unwrap()))
.collect();

let contract = domain_routing_ism::new(res.0, owner);
contract
.initialize(&isms.iter().map(|i| i.0).collect::<Vec<_>>(), &modules)
.send()
.await?;

Ok(res.0)
}

async fn deploy_aggregate(
codes: &Codes,
isms: Vec<Self>,
threshold: u8,
owner: &StarknetAccount,
deployer: &StarknetAccount,
) -> eyre::Result<FieldElement> {
let futures = FuturesUnordered::new();

for i in isms.iter() {
let future = async move {
<Ism as Clone>::clone(&i)
.deploy(codes, owner, deployer)
.await
};
futures.push(future);
}

let results = futures.collect::<Vec<_>>().await;

let ism_addrs: Vec<_> = results.iter().map(|a| *a.as_ref().unwrap()).collect();

let res = deploy_contract(codes.ism_aggregate, ism_addrs, felt!("0"), deployer).await;

Ok(res.0)
}

pub async fn deploy(
self,
codes: &Codes,
owner: &StarknetAccount,
deployer: &StarknetAccount,
) -> eyre::Result<FieldElement> {
match self {
Self::Mock => Self::deploy_mock(codes, deployer).await,
Self::Multisig { validators: set } => {
Self::deploy_multisig(codes, set, owner, deployer).await
}
Self::Aggregate { isms, threshold } => {
Self::deploy_aggregate(codes, isms, threshold, owner, deployer).await
}
Self::Routing(isms) => Self::deploy_routing(codes, isms, owner, deployer).await,
}
}
}

pub fn prepare_routing_ism(info: Vec<(u32, TestValidators)>) -> Ism {
let mut isms = vec![];

for (domain, set) in info {
isms.push((
domain,
Ism::Aggregate {
isms: vec![Ism::multisig(set)],
threshold: 1,
},
));
}

Ism::routing(isms)
}
1 change: 1 addition & 0 deletions rust/tests/contracts/strk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod bind;
mod deploy;
mod hook;
mod ism;
mod setup;
mod types;
mod utils;
Expand Down
1 change: 1 addition & 0 deletions rust/tests/contracts/strk/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::validator::TestValidators;
use super::{
deploy_core, get_dev_account,
hook::Hook,
ism::prepare_routing_ism,
types::{Codes, CoreDeployments},
StarknetAccount,
};
Expand Down
55 changes: 29 additions & 26 deletions rust/tests/contracts/strk/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::BTreeMap;

use starknet::{accounts::SingleOwnerAccount, providers::AnyProvider, signers::LocalWallet};
use starknet::{
accounts::SingleOwnerAccount, core::types::FieldElement, providers::AnyProvider,
signers::LocalWallet,
};

pub type StarknetAccount = SingleOwnerAccount<AnyProvider, LocalWallet>;

Expand All @@ -15,32 +18,32 @@ impl FromIterator<(String, u64)> for CodesMap {

#[derive(serde::Serialize, serde::Deserialize)]
pub struct Codes {
pub mailbox: String,
pub mailbox: FieldElement,
#[serde(rename = "validator_announce")]
pub va: String,
pub va: FieldElement,

pub hook_aggregate: String,
pub hook_merkle: String,
pub hook_pausable: String,
pub hook_routing: String,
pub hook_routing_custom: String,
pub hook_routing_fallback: String,
pub hook_aggregate: FieldElement,
pub hook_merkle: FieldElement,
pub hook_pausable: FieldElement,
pub hook_routing: FieldElement,
pub hook_routing_custom: FieldElement,
pub hook_routing_fallback: FieldElement,

pub igp: String,
pub igp_oracle: String,
pub igp: FieldElement,
pub igp_oracle: FieldElement,

pub ism_aggregate: String,
pub ism_multisig: String,
pub ism_routing: String,
pub ism_aggregate: FieldElement,
pub ism_multisig: FieldElement,
pub ism_routing: FieldElement,

pub test_mock_hook: String,
pub test_mock_ism: String,
pub test_mock_msg_receiver: String,
pub test_mock_hook: FieldElement,
pub test_mock_ism: FieldElement,
pub test_mock_msg_receiver: FieldElement,

pub warp_strk20: String,
pub warp_native: String,
pub warp_strk20: FieldElement,
pub warp_native: FieldElement,

pub strk20_base: String,
pub strk20_base: FieldElement,
}

impl TryFrom<CodesMap> for Codes {
Expand All @@ -55,13 +58,13 @@ impl TryFrom<CodesMap> for Codes {
}
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(Default, serde::Serialize, serde::Deserialize)]
pub struct CoreDeployments {
pub mailbox: String,
pub default_ism: String,
pub default_hook: String,
pub required_hook: String,
pub msg_receiver: String,
pub mailbox: FieldElement,
pub default_ism: FieldElement,
pub default_hook: FieldElement,
pub required_hook: FieldElement,
pub msg_receiver: FieldElement,
}

#[derive(serde::Serialize, serde::Deserialize)]
Expand Down
Loading

0 comments on commit 751b222

Please sign in to comment.