Skip to content

Commit

Permalink
refactor(target_chains/solana/cli): make the wormhole init idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-bahjati committed Oct 24, 2024
1 parent d9d54f8 commit 28ea590
Showing 1 changed file with 72 additions and 38 deletions.
110 changes: 72 additions & 38 deletions target_chains/solana/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 28ea590

Please sign in to comment.