From 28ea590d2c728a1f7d6dc48af7df726ba2a6c8cd Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 24 Oct 2024 10:33:00 +0200 Subject: [PATCH] refactor(target_chains/solana/cli): make the wormhole init idempotent --- target_chains/solana/cli/src/main.rs | 110 ++++++++++++++++++--------- 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/target_chains/solana/cli/src/main.rs b/target_chains/solana/cli/src/main.rs index 6c626c25be..53195925d4 100644 --- a/target_chains/solana/cli/src/main.rs +++ b/target_chains/solana/cli/src/main.rs @@ -126,44 +126,78 @@ fn main() -> Result<()> { let payer = read_keypair_file(&*shellexpand::tilde(&keypair)).expect("Keypair not found"); - let initialize_instruction = initialize( - wormhole, - payer.pubkey(), - 0, - GUARDIAN_EXPIRATION_TIME, - &[hex::decode(INITIAL_GUARDIAN).unwrap().try_into().unwrap()], - ) - .unwrap(); - process_transaction(&rpc_client, vec![initialize_instruction], &vec![&payer])?; - - process_upgrade_guardian_set( - &rpc_client, - &hex::decode(UPGRADE_GUARDIAN_SET_VAA_1).unwrap(), - wormhole, - &payer, - true, - )?; - process_upgrade_guardian_set( - &rpc_client, - &hex::decode(UPGRADE_GUARDIAN_SET_VAA_2).unwrap(), - wormhole, - &payer, - false, - )?; - process_upgrade_guardian_set( - &rpc_client, - &hex::decode(UPGRADE_GUARDIAN_SET_VAA_3).unwrap(), - wormhole, - &payer, - false, - )?; - process_upgrade_guardian_set( - &rpc_client, - &hex::decode(UPGRADE_GUARDIAN_SET_VAA_4).unwrap(), - wormhole, - &payer, - false, - )?; + // Check whether the wormhole config account exists, if it does not exist, initialize the wormhole receiver + let wormhole_config = BridgeConfig::key(&wormhole, ()); + + let wormhole_account_data = rpc_client.get_account_data(&wormhole_config); + + let mut current_guardian_set_index = match wormhole_account_data { + Ok(data) => { + let config = BridgeConfig::try_from_slice(&data)?; + println!("Wormhole already initialized. config: {:?}", config); + config.guardian_set_index + } + Err(_) => { + println!("Initializing wormhole receiver"); + let initialize_instruction = initialize( + wormhole, + payer.pubkey(), + 0, + GUARDIAN_EXPIRATION_TIME, + &[hex::decode(INITIAL_GUARDIAN).unwrap().try_into().unwrap()], + ) + .expect("Failed to create initialize instruction"); + process_transaction(&rpc_client, vec![initialize_instruction], &vec![&payer])?; + 0 + } + }; + + if current_guardian_set_index == 0 { + println!("Upgrading guardian set from 0 to 1"); + process_upgrade_guardian_set( + &rpc_client, + &hex::decode(UPGRADE_GUARDIAN_SET_VAA_1).unwrap(), + wormhole, + &payer, + true, + )?; + current_guardian_set_index += 1; + } + + if current_guardian_set_index == 1 { + println!("Upgrading guardian set from 1 to 2"); + process_upgrade_guardian_set( + &rpc_client, + &hex::decode(UPGRADE_GUARDIAN_SET_VAA_2).unwrap(), + wormhole, + &payer, + false, + )?; + current_guardian_set_index += 1; + } + + if current_guardian_set_index == 2 { + println!("Upgrading guardian set from 2 to 3"); + process_upgrade_guardian_set( + &rpc_client, + &hex::decode(UPGRADE_GUARDIAN_SET_VAA_3).unwrap(), + wormhole, + &payer, + false, + )?; + current_guardian_set_index += 1; + } + + if current_guardian_set_index == 3 { + println!("Upgrading guardian set from 3 to 4"); + process_upgrade_guardian_set( + &rpc_client, + &hex::decode(UPGRADE_GUARDIAN_SET_VAA_4).unwrap(), + wormhole, + &payer, + false, + )?; + } } Action::InitializePythReceiver {