Skip to content

Commit

Permalink
tests: verifier: adopt multiverifier interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akirillo committed Sep 1, 2023
1 parent 8e9dcf0 commit 126d761
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 326 deletions.
23 changes: 5 additions & 18 deletions starknet_scripts/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use tracing::{debug, info};
use crate::{
cli::{Contract, DeployArgs},
commands::utils::{
deploy_darkpool, deploy_merkle, deploy_nullifier_set, deploy_verifier, dump_deployment,
initialize, setup_account, MERKLE_HEIGHT,
deploy_darkpool, deploy_merkle, deploy_nullifier_set, dump_deployment, initialize,
setup_account, MERKLE_HEIGHT,
},
};

Expand Down Expand Up @@ -66,33 +66,20 @@ pub async fn deploy_and_initialize(args: DeployArgs) -> Result<()> {
);

if should_initialize {
// Deploy verifier
let verifier_class_hash_hex = if let Some(verifier_class_hash) = verifier_class_hash
{
verifier_class_hash
} else {
format!("{verifier_class_hash_felt:#64x}")
};
let (verifier_address, _, _) = deploy_verifier(
Some(verifier_class_hash_hex),
FieldElement::ZERO, /* salt */
&artifacts_path,
&account,
)
.await?;

// Initialize darkpool
debug!("Initializing darkpool contract...");
let calldata = vec![
merkle_class_hash_felt,
nullifier_set_class_hash_felt,
verifier_address,
verifier_class_hash_felt,
FieldElement::from(MERKLE_HEIGHT),
// TODO: Need to get circuit params! Prob best to read them in from file.
];
let initialization_result =
initialize(&account, darkpool_address, calldata).await?;

// TODO: Parameterize verifiers

info!(
"Darkpool contract initialized!\n\
Transaction hash: {:#64x}\n",
Expand Down
81 changes: 13 additions & 68 deletions starknet_scripts/src/commands/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ADDR_BOUND: FieldElement = FieldElement::from_mont([
pub const DARKPOOL_CONTRACT_NAME: &str = "renegade_contracts_Darkpool";
pub const MERKLE_CONTRACT_NAME: &str = "renegade_contracts_Merkle";
pub const NULLIFIER_SET_CONTRACT_NAME: &str = "renegade_contracts_NullifierSet";
pub const VERIFIER_CONTRACT_NAME: &str = "renegade_contracts_Verifier";
pub const VERIFIER_CONTRACT_NAME: &str = "renegade_contracts_MultiVerifier";

pub const SIERRA_FILE_EXTENSION: &str = "sierra.json";
pub const CASM_FILE_EXTENSION: &str = "casm.json";
Expand Down Expand Up @@ -171,11 +171,14 @@ pub async fn deploy(
account: &ScriptAccount,
class_hash: FieldElement,
calldata: &[FieldElement],
salt: FieldElement,
) -> Result<InvokeTransactionResult> {
let contract_factory = ContractFactory::new(class_hash, account);
let deploy_result = contract_factory
.deploy(calldata, salt, false /* unique */)
.deploy(
calldata,
FieldElement::ZERO, /* salt */
false, /* unique */
)
.send()
.await?;

Expand Down Expand Up @@ -206,13 +209,12 @@ pub async fn initialize(
// Taken from https://github.com/xJonathanLEI/starknet-rs/blob/master/starknet-accounts/src/factory/mod.rs
pub fn calculate_contract_address(
class_hash: FieldElement,
salt: FieldElement,
constructor_calldata: &[FieldElement],
) -> FieldElement {
compute_hash_on_elements(&[
PREFIX_CONTRACT_ADDRESS,
FieldElement::ZERO, /* deployer address */
salt,
FieldElement::ZERO, /* salt */
class_hash,
compute_hash_on_elements(constructor_calldata),
]) % ADDR_BOUND
Expand Down Expand Up @@ -278,19 +280,9 @@ pub async fn deploy_darkpool(
let calldata = vec![account.address()];
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(
account,
darkpool_class_hash_felt,
&calldata,
FieldElement::ZERO, /* salt */
)
.await?;
} = deploy(account, darkpool_class_hash_felt, &calldata).await?;

let darkpool_address = calculate_contract_address(
darkpool_class_hash_felt,
FieldElement::ZERO, /* salt */
&calldata,
);
let darkpool_address = calculate_contract_address(darkpool_class_hash_felt, &calldata);

Ok((
darkpool_address,
Expand Down Expand Up @@ -321,19 +313,9 @@ pub async fn deploy_merkle(
debug!("Deploying merkle contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(
account,
merkle_class_hash_felt,
&[],
FieldElement::ZERO, /* salt */
)
.await?;
} = deploy(account, merkle_class_hash_felt, &[]).await?;

let merkle_address = calculate_contract_address(
merkle_class_hash_felt,
FieldElement::ZERO, /* salt */
&[],
);
let merkle_address = calculate_contract_address(merkle_class_hash_felt, &[]);

Ok((merkle_address, merkle_class_hash_felt, transaction_hash))
}
Expand All @@ -357,50 +339,13 @@ pub async fn deploy_nullifier_set(
debug!("Deploying nullifier set contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(
account,
nullifier_set_class_hash_felt,
&[],
FieldElement::ZERO, /* salt */
)
.await?;
} = deploy(account, nullifier_set_class_hash_felt, &[]).await?;

let nullifier_set_address = calculate_contract_address(
nullifier_set_class_hash_felt,
FieldElement::ZERO, /* salt */
&[],
);
let nullifier_set_address = calculate_contract_address(nullifier_set_class_hash_felt, &[]);

Ok((
nullifier_set_address,
nullifier_set_class_hash_felt,
transaction_hash,
))
}

pub async fn deploy_verifier(
verifier_class_hash: Option<String>,
salt: FieldElement,
artifacts_path: &str,
account: &ScriptAccount,
) -> Result<(FieldElement, FieldElement, FieldElement)> {
let (verifier_sierra_path, verifier_casm_path) =
get_artifacts(artifacts_path, VERIFIER_CONTRACT_NAME);
let verifier_class_hash_felt = get_or_declare(
verifier_class_hash,
verifier_sierra_path,
verifier_casm_path,
account,
)
.await?;

// Deploy verifier
debug!("Deploying verifier contract...");
let InvokeTransactionResult {
transaction_hash, ..
} = deploy(account, verifier_class_hash_felt, &[], salt).await?;

let verifier_address = calculate_contract_address(verifier_class_hash_felt, salt, &[]);

Ok((verifier_address, verifier_class_hash_felt, transaction_hash))
}
Loading

0 comments on commit 126d761

Please sign in to comment.