Skip to content

Commit

Permalink
feat: use custom contract address for any network
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and jacderida committed Nov 12, 2024
1 parent cba9834 commit 94df498
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 170 deletions.
34 changes: 17 additions & 17 deletions src/ansible/extra_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::inventory::VirtualMachine;
use crate::NodeType;
use crate::{ansible::provisioning::ProvisionOptions, CloudProvider, EvmNetwork};
use crate::{BinaryOption, Error, EvmCustomTestnetData, Result};
use crate::{BinaryOption, Error, Result};
use alloy::hex::ToHexExt;
use alloy::signers::local::PrivateKeySigner;
use serde_json::Value;
Expand Down Expand Up @@ -293,7 +293,6 @@ pub fn build_node_extra_vars_doc(
bootstrap_multiaddr: Option<String>,
node_instance_count: u16,
evm_network: EvmNetwork,
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<String> {
let mut extra_vars = ExtraVarsDocBuilder::default();
extra_vars.add_variable("provider", cloud_provider);
Expand Down Expand Up @@ -351,10 +350,14 @@ pub fn build_node_extra_vars_doc(

extra_vars.add_variable("rewards_address", &options.rewards_address);
extra_vars.add_variable("evm_network_type", &evm_network.to_string());
if let Some(evm_data) = evm_testnet_data {
extra_vars.add_variable("evm_rpc_url", &evm_data.rpc_url);
extra_vars.add_variable("evm_payment_token_address", &evm_data.payment_token_address);
extra_vars.add_variable("evm_data_payments_address", &evm_data.data_payments_address);
if let Some(evm_data_payment_token_address) = &options.evm_data_payments_address {
extra_vars.add_variable("evm_data_payments_address", evm_data_payment_token_address);
}
if let Some(evm_payment_token_address) = &options.evm_payment_token_address {
extra_vars.add_variable("evm_payment_token_address", evm_payment_token_address);
}
if let Some(evm_rpc_url) = &options.evm_rpc_url {
extra_vars.add_variable("evm_rpc_url", evm_rpc_url);
}

Ok(extra_vars.build())
Expand All @@ -377,7 +380,6 @@ pub fn build_uploaders_extra_vars_doc(
cloud_provider: &str,
options: &ProvisionOptions,
genesis_multiaddr: &str,
evm_testnet_data: Option<EvmCustomTestnetData>,
sk_map: &HashMap<VirtualMachine, Vec<PrivateKeySigner>>,
) -> Result<String> {
let mut extra_vars: ExtraVarsDocBuilder = ExtraVarsDocBuilder::default();
Expand All @@ -398,16 +400,14 @@ pub fn build_uploaders_extra_vars_doc(
&options.uploaders_count.unwrap_or(1).to_string(),
);
extra_vars.add_variable("evm_network_type", &options.evm_network.to_string());
if let Some(evm_testnet_data) = &evm_testnet_data {
extra_vars.add_variable("evm_rpc_url", &evm_testnet_data.rpc_url);
extra_vars.add_variable(
"evm_payment_token_address",
&evm_testnet_data.payment_token_address,
);
extra_vars.add_variable(
"evm_data_payments_address",
&evm_testnet_data.data_payments_address,
);
if let Some(evm_data_payment_token_address) = &options.evm_data_payments_address {
extra_vars.add_variable("evm_data_payments_address", evm_data_payment_token_address);
}
if let Some(evm_payment_token_address) = &options.evm_payment_token_address {
extra_vars.add_variable("evm_payment_token_address", evm_payment_token_address);
}
if let Some(evm_rpc_url) = &options.evm_rpc_url {
extra_vars.add_variable("evm_rpc_url", evm_rpc_url);
}

let mut serde_map = serde_json::Map::new();
Expand Down
37 changes: 17 additions & 20 deletions src/ansible/provisioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
error::{Error, Result},
funding::FundingOptions,
inventory::{DeploymentNodeRegistries, VirtualMachine},
print_duration, BinaryOption, CloudProvider, EvmCustomTestnetData, EvmNetwork, LogFormat,
NodeType, SshClient, UpgradeOptions,
print_duration, BinaryOption, CloudProvider, EvmNetwork, LogFormat, NodeType, SshClient,
UpgradeOptions,
};
use evmlib::common::U256;
use log::{debug, error, trace};
Expand All @@ -41,7 +41,11 @@ pub struct ProvisionOptions {
pub chunk_size: Option<u64>,
pub downloaders_count: u16,
pub env_variables: Option<Vec<(String, String)>>,
pub evm_data_payments_address: Option<String>,
pub evm_network: EvmNetwork,
pub evm_payment_token_address: Option<String>,
pub evm_rpc_url: Option<String>,
/// Used to fund the uploaders.
pub funding_wallet_secret_key: Option<String>,
pub gas_amount: Option<U256>,
pub interval: Duration,
Expand Down Expand Up @@ -72,7 +76,10 @@ impl From<BootstrapOptions> for ProvisionOptions {
chunk_size: bootstrap_options.chunk_size,
downloaders_count: 0,
env_variables: bootstrap_options.env_variables,
evm_data_payments_address: bootstrap_options.evm_data_payments_address,
evm_network: bootstrap_options.evm_network,
evm_payment_token_address: bootstrap_options.evm_payment_token_address,
evm_rpc_url: bootstrap_options.evm_rpc_url,
funding_wallet_secret_key: None,
gas_amount: None,
interval: bootstrap_options.interval,
Expand Down Expand Up @@ -102,7 +109,10 @@ impl From<DeployOptions> for ProvisionOptions {
chunk_size: deploy_options.chunk_size,
downloaders_count: deploy_options.downloaders_count,
env_variables: deploy_options.env_variables,
evm_data_payments_address: deploy_options.evm_data_payments_address,
evm_network: deploy_options.evm_network,
evm_payment_token_address: deploy_options.evm_payment_token_address,
evm_rpc_url: deploy_options.evm_rpc_url,
funding_wallet_secret_key: deploy_options.funding_wallet_secret_key,
gas_amount: None,
interval: deploy_options.interval,
Expand Down Expand Up @@ -278,11 +288,7 @@ impl AnsibleProvisioner {
Ok(())
}

pub fn provision_genesis_node(
&self,
options: &ProvisionOptions,
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<()> {
pub fn provision_genesis_node(&self, options: &ProvisionOptions) -> Result<()> {
let start = Instant::now();
let genesis_inventory = self
.ansible_runner
Expand All @@ -300,7 +306,6 @@ impl AnsibleProvisioner {
None,
1,
options.evm_network.clone(),
evm_testnet_data,
)?),
)?;
print_duration(start.elapsed());
Expand Down Expand Up @@ -344,7 +349,6 @@ impl AnsibleProvisioner {
options: &ProvisionOptions,
initial_contact_peer: &str,
node_type: NodeType,
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<()> {
let start = Instant::now();
let (inventory_type, node_count) = match &node_type {
Expand Down Expand Up @@ -393,7 +397,6 @@ impl AnsibleProvisioner {
Some(initial_contact_peer.to_string()),
node_count,
options.evm_network.clone(),
evm_testnet_data,
)?),
)?;
print_duration(start.elapsed());
Expand All @@ -404,7 +407,6 @@ impl AnsibleProvisioner {
&self,
options: &mut ProvisionOptions,
initial_contact_peer: &str,
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<()> {
let start = Instant::now();

Expand All @@ -431,12 +433,7 @@ impl AnsibleProvisioner {
error!("Failed to generate private node static inv with err: {err:?}")
})?;

self.provision_nodes(
options,
initial_contact_peer,
NodeType::Private,
evm_testnet_data,
)?;
self.provision_nodes(options, initial_contact_peer, NodeType::Private)?;

print_duration(start.elapsed());
Ok(())
Expand Down Expand Up @@ -466,13 +463,14 @@ impl AnsibleProvisioner {
&self,
options: &ProvisionOptions,
genesis_multiaddr: &str,
evm_testnet_data: Option<EvmCustomTestnetData>,
) -> Result<()> {
let start = Instant::now();

let sk_map = self
.deposit_funds_to_uploaders(&FundingOptions {
custom_evm_testnet_data: evm_testnet_data.clone(),
evm_data_payments_address: options.evm_data_payments_address.clone(),
evm_payment_token_address: options.evm_payment_token_address.clone(),
evm_rpc_url: options.evm_rpc_url.clone(),
uploaders_count: options.uploaders_count,
evm_network: options.evm_network.clone(),
funding_wallet_secret_key: options.funding_wallet_secret_key.clone(),
Expand All @@ -491,7 +489,6 @@ impl AnsibleProvisioner {
&self.cloud_provider.to_string(),
options,
genesis_multiaddr,
evm_testnet_data,
&sk_map,
)?),
)?;
Expand Down
20 changes: 11 additions & 9 deletions src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
ansible::{inventory::AnsibleInventoryType, provisioning::ProvisionOptions},
error::Result,
write_environment_details, BinaryOption, DeploymentType, EnvironmentDetails, EnvironmentType,
EvmCustomTestnetData, EvmNetwork, InfraRunOptions, LogFormat, NodeType, TestnetDeployer,
EvmNetwork, InfraRunOptions, LogFormat, NodeType, TestnetDeployer,
};
use colored::Colorize;

Expand All @@ -21,8 +21,10 @@ pub struct BootstrapOptions {
pub chunk_size: Option<u64>,
pub environment_type: EnvironmentType,
pub env_variables: Option<Vec<(String, String)>>,
pub evm_data_payments_address: Option<String>,
pub evm_network: EvmNetwork,
pub evm_custom_testnet_data: Option<EvmCustomTestnetData>,
pub evm_payment_token_address: Option<String>,
pub evm_rpc_url: Option<String>,
pub interval: Duration,
pub log_format: Option<LogFormat>,
pub max_archived_log_files: u16,
Expand Down Expand Up @@ -59,7 +61,9 @@ impl TestnetDeployer {
deployment_type: DeploymentType::Bootstrap,
environment_type: options.environment_type.clone(),
evm_network: options.evm_network.clone(),
evm_testnet_data: options.evm_custom_testnet_data.clone(),
evm_data_payments_address: options.evm_data_payments_address.clone(),
evm_payment_token_address: options.evm_payment_token_address.clone(),
evm_rpc_url: options.evm_rpc_url.clone(),
funding_wallet_address: None,
rewards_address: options.rewards_address.clone(),
},
Expand Down Expand Up @@ -113,7 +117,6 @@ impl TestnetDeployer {
&provision_options,
&options.bootstrap_peer,
NodeType::Generic,
options.evm_custom_testnet_data.clone(),
) {
Ok(()) => {
println!("Provisioned normal nodes");
Expand Down Expand Up @@ -148,11 +151,10 @@ impl TestnetDeployer {

self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision Private Nodes");
match self.ansible_provisioner.provision_private_nodes(
&mut provision_options,
&options.bootstrap_peer,
options.evm_custom_testnet_data.clone(),
) {
match self
.ansible_provisioner
.provision_private_nodes(&mut provision_options, &options.bootstrap_peer)
{
Ok(()) => {
println!("Provisioned private nodes");
}
Expand Down
44 changes: 29 additions & 15 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
ansible::{inventory::AnsibleInventoryType, provisioning::ProvisionOptions},
error::Result,
funding::get_address_from_sk,
get_evm_testnet_data, get_genesis_multiaddr, write_environment_details, BinaryOption,
get_genesis_multiaddr, get_local_evm_node_data, write_environment_details, BinaryOption,
DeploymentInventory, DeploymentType, EnvironmentDetails, EnvironmentType, EvmNetwork,
InfraRunOptions, LogFormat, NodeType, TestnetDeployer,
};
Expand All @@ -27,7 +27,10 @@ pub struct DeployOptions {
pub downloaders_count: u16,
pub environment_type: EnvironmentType,
pub env_variables: Option<Vec<(String, String)>>,
pub evm_data_payments_address: Option<String>,
pub evm_network: EvmNetwork,
pub evm_payment_token_address: Option<String>,
pub evm_rpc_url: Option<String>,
pub evm_node_vm_size: Option<String>,
pub funding_wallet_secret_key: Option<String>,
pub interval: Duration,
Expand Down Expand Up @@ -107,15 +110,17 @@ impl TestnetDeployer {
deployment_type: DeploymentType::New,
environment_type: options.environment_type.clone(),
evm_network: options.evm_network.clone(),
evm_testnet_data: None,
evm_data_payments_address: None,
evm_payment_token_address: None,
evm_rpc_url: None,
funding_wallet_address: None,
rewards_address: options.rewards_address.clone(),
},
)
.await?;

let mut provision_options = ProvisionOptions::from(options.clone());
let evm_testnet_data = if options.evm_network == EvmNetwork::Custom {
let local_evm_node_data = if options.evm_network == EvmNetwork::Custom {
self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision EVM Node");
self.ansible_provisioner
Expand All @@ -127,7 +132,7 @@ impl TestnetDeployer {
n += 1;

Some(
get_evm_testnet_data(&self.ansible_provisioner.ansible_runner, &self.ssh_client)
get_local_evm_node_data(&self.ansible_provisioner.ansible_runner, &self.ssh_client)
.map_err(|err| {
println!("Failed to get evm testnet data {err:?}");
err
Expand All @@ -140,22 +145,34 @@ impl TestnetDeployer {
let funding_wallet_address = if let Some(secret_key) = &options.funding_wallet_secret_key {
let address = get_address_from_sk(secret_key)?;
Some(address.encode_hex())
} else if let Some(emv_data) = &evm_testnet_data {
} else if let Some(emv_data) = &local_evm_node_data {
let address = get_address_from_sk(&emv_data.deployer_wallet_private_key)?;
Some(address.encode_hex())
} else {
log::error!("Funding wallet address not provided");
None
};

if let Some(custom_evm) = local_evm_node_data {
provision_options.evm_data_payments_address =
Some(custom_evm.data_payments_address.clone());
provision_options.evm_payment_token_address =
Some(custom_evm.payment_token_address.clone());
provision_options.evm_rpc_url = Some(custom_evm.rpc_url.clone());
provision_options.funding_wallet_secret_key =
Some(custom_evm.deployer_wallet_private_key.clone());
};

write_environment_details(
&self.s3_repository,
&options.name,
&EnvironmentDetails {
deployment_type: DeploymentType::New,
environment_type: options.environment_type.clone(),
evm_network: options.evm_network.clone(),
evm_testnet_data: evm_testnet_data.clone(),
evm_data_payments_address: provision_options.evm_data_payments_address.clone(),
evm_payment_token_address: provision_options.evm_payment_token_address.clone(),
evm_rpc_url: provision_options.evm_rpc_url.clone(),
funding_wallet_address,
rewards_address: options.rewards_address.clone(),
},
Expand All @@ -177,7 +194,7 @@ impl TestnetDeployer {
self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision Genesis Node");
self.ansible_provisioner
.provision_genesis_node(&provision_options, evm_testnet_data.clone())
.provision_genesis_node(&provision_options)
.map_err(|err| {
println!("Failed to provision genesis node {err:?}");
err
Expand All @@ -198,7 +215,6 @@ impl TestnetDeployer {
&provision_options,
&genesis_multiaddr,
NodeType::Bootstrap,
evm_testnet_data.clone(),
) {
Ok(()) => {
println!("Provisioned bootstrap nodes");
Expand All @@ -216,7 +232,6 @@ impl TestnetDeployer {
&provision_options,
&genesis_multiaddr,
NodeType::Generic,
evm_testnet_data.clone(),
) {
Ok(()) => {
println!("Provisioned normal nodes");
Expand Down Expand Up @@ -252,11 +267,10 @@ impl TestnetDeployer {

self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision Private Nodes");
match self.ansible_provisioner.provision_private_nodes(
&mut provision_options,
&genesis_multiaddr,
evm_testnet_data.clone(),
) {
match self
.ansible_provisioner
.provision_private_nodes(&mut provision_options, &genesis_multiaddr)
{
Ok(()) => {
println!("Provisioned private nodes");
}
Expand All @@ -272,7 +286,7 @@ impl TestnetDeployer {
self.ansible_provisioner
.print_ansible_run_banner(n, total, "Provision Uploaders");
self.ansible_provisioner
.provision_uploaders(&provision_options, &genesis_multiaddr, evm_testnet_data)
.provision_uploaders(&provision_options, &genesis_multiaddr)
.await
.map_err(|err| {
println!("Failed to provision uploaders {err:?}");
Expand Down
Loading

0 comments on commit 94df498

Please sign in to comment.