|
| 1 | +use crate::Migrate; |
| 2 | +use anyhow::Context; |
| 3 | +use aptos_framework_pre_l1_merge_release::maptos_framework_release_util::{ |
| 4 | + LocalAccountReleaseSigner, OverrideAccountAddressReleaseSigner, |
| 5 | +}; |
| 6 | +use clap::Parser; |
| 7 | +use movement_core::{ |
| 8 | + movement::{Celestia, Eth}, |
| 9 | + Config as MovementCoreConfig, |
| 10 | +}; |
| 11 | +use mtma_node_null_core::Config as MtmaNodeNullConfig; |
| 12 | +use mtma_types::movement::aptos_sdk::types::{ |
| 13 | + account_address::AccountAddress, account_config::aptos_test_root_address, LocalAccount, |
| 14 | +}; |
| 15 | +use mtma_types::movement::movement_config::Config as MovementConfig; |
| 16 | +use serde::{Deserialize, Serialize}; |
| 17 | +use std::fmt::Debug; |
| 18 | + |
| 19 | +/// Errors thrown when working with the [Config]. |
| 20 | +#[derive(Debug, thiserror::Error)] |
| 21 | +pub enum MigrateConfigError { |
| 22 | + #[error("failed to build from config: {0}")] |
| 23 | + Build(#[source] Box<dyn std::error::Error + Send + Sync>), |
| 24 | +} |
| 25 | + |
| 26 | +/// The config for the migration. |
| 27 | +/// |
| 28 | +/// All fields should be easily statically encodable to a CLI argument. |
| 29 | +/// This is the frontend for the core API. |
| 30 | +#[derive(Parser, Debug, Serialize, Deserialize, Clone)] |
| 31 | +#[clap(help_expected = true)] |
| 32 | +pub struct Config { |
| 33 | + /// The config for the mtma-node-null. |
| 34 | + #[clap(flatten)] |
| 35 | + mtma_node_null: MtmaNodeNullConfig, |
| 36 | + /// The movement config. |
| 37 | + #[clap(flatten)] |
| 38 | + movement_core: MovementCoreConfig, |
| 39 | + /// The account address override for the release signer. |
| 40 | + #[clap(long)] |
| 41 | + pub account_address: Option<AccountAddress>, |
| 42 | +} |
| 43 | + |
| 44 | +impl Default for Config { |
| 45 | + fn default() -> Self { |
| 46 | + let mut movement_core = MovementCoreConfig { |
| 47 | + movement_config_string: None, |
| 48 | + setup: false, |
| 49 | + celestia: Celestia::Local, |
| 50 | + eth: Eth::Local, |
| 51 | + biarritz_rc1_to_pre_l1_merge: false, |
| 52 | + ping_rest_api: false, |
| 53 | + ping_faucet: false, |
| 54 | + }; |
| 55 | + |
| 56 | + // Create a default MovementConfig |
| 57 | + let movement_config = MovementConfig::default(); |
| 58 | + |
| 59 | + // Hard-code the core resources address |
| 60 | + let account_address = Some(aptos_test_root_address()); |
| 61 | + |
| 62 | + // Set the movement config |
| 63 | + movement_core.set_movement_config(movement_config); |
| 64 | + |
| 65 | + Self { mtma_node_null: MtmaNodeNullConfig::default(), movement_core, account_address } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Config { |
| 70 | + /// Builds the release signer from the config. |
| 71 | + pub fn build_release_signer(&self) -> Result<LocalAccountReleaseSigner, MigrateConfigError> { |
| 72 | + // load the signer |
| 73 | + let raw_private_key = self |
| 74 | + .movement_core |
| 75 | + .try_movement_signer_identifier() |
| 76 | + .map_err(|e| MigrateConfigError::Build(e.into()))? |
| 77 | + .try_raw_private_key() |
| 78 | + .context("failed to load raw private key") |
| 79 | + .map_err(|e| MigrateConfigError::Build(e.into()))?; |
| 80 | + |
| 81 | + // get the raw private key |
| 82 | + let private_key_hex = hex::encode(raw_private_key); |
| 83 | + let root_account = LocalAccount::from_private_key(private_key_hex.as_str(), 0) |
| 84 | + .context("failed to build root account") |
| 85 | + .map_err(|e| MigrateConfigError::Build(e.into()))?; // sequence number 0 is fine because the release API loads sequence numbers |
| 86 | + |
| 87 | + Ok(LocalAccountReleaseSigner::new(root_account, None)) |
| 88 | + } |
| 89 | + |
| 90 | + /// Builds the [Migrate] struct from the config. |
| 91 | + pub fn build( |
| 92 | + &self, |
| 93 | + ) -> Result< |
| 94 | + Migrate<OverrideAccountAddressReleaseSigner<LocalAccountReleaseSigner>>, |
| 95 | + MigrateConfigError, |
| 96 | + > { |
| 97 | + let mtma_node_null = |
| 98 | + self.mtma_node_null.build().map_err(|e| MigrateConfigError::Build(e.into()))?; |
| 99 | + let local_signer = self.build_release_signer()?; |
| 100 | + let signer = OverrideAccountAddressReleaseSigner::core_resource_account(local_signer); |
| 101 | + Ok(Migrate { release_signer: signer, mtma_node_null }) |
| 102 | + } |
| 103 | +} |
0 commit comments