Skip to content

Commit d7bd4ad

Browse files
authored
refactor(target_chains/solana/cli): make the wormhole init idempotent (#2057)
1 parent d9d54f8 commit d7bd4ad

File tree

1 file changed

+72
-38
lines changed
  • target_chains/solana/cli/src

1 file changed

+72
-38
lines changed

target_chains/solana/cli/src/main.rs

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -126,44 +126,78 @@ fn main() -> Result<()> {
126126
let payer =
127127
read_keypair_file(&*shellexpand::tilde(&keypair)).expect("Keypair not found");
128128

129-
let initialize_instruction = initialize(
130-
wormhole,
131-
payer.pubkey(),
132-
0,
133-
GUARDIAN_EXPIRATION_TIME,
134-
&[hex::decode(INITIAL_GUARDIAN).unwrap().try_into().unwrap()],
135-
)
136-
.unwrap();
137-
process_transaction(&rpc_client, vec![initialize_instruction], &vec![&payer])?;
138-
139-
process_upgrade_guardian_set(
140-
&rpc_client,
141-
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_1).unwrap(),
142-
wormhole,
143-
&payer,
144-
true,
145-
)?;
146-
process_upgrade_guardian_set(
147-
&rpc_client,
148-
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_2).unwrap(),
149-
wormhole,
150-
&payer,
151-
false,
152-
)?;
153-
process_upgrade_guardian_set(
154-
&rpc_client,
155-
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_3).unwrap(),
156-
wormhole,
157-
&payer,
158-
false,
159-
)?;
160-
process_upgrade_guardian_set(
161-
&rpc_client,
162-
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_4).unwrap(),
163-
wormhole,
164-
&payer,
165-
false,
166-
)?;
129+
// Check whether the wormhole config account exists, if it does not exist, initialize the wormhole receiver
130+
let wormhole_config = BridgeConfig::key(&wormhole, ());
131+
132+
let wormhole_account_data = rpc_client.get_account_data(&wormhole_config);
133+
134+
let mut current_guardian_set_index = match wormhole_account_data {
135+
Ok(data) => {
136+
let config = BridgeConfig::try_from_slice(&data)?;
137+
println!("Wormhole already initialized. config: {:?}", config);
138+
config.guardian_set_index
139+
}
140+
Err(_) => {
141+
println!("Initializing wormhole receiver");
142+
let initialize_instruction = initialize(
143+
wormhole,
144+
payer.pubkey(),
145+
0,
146+
GUARDIAN_EXPIRATION_TIME,
147+
&[hex::decode(INITIAL_GUARDIAN).unwrap().try_into().unwrap()],
148+
)
149+
.expect("Failed to create initialize instruction");
150+
process_transaction(&rpc_client, vec![initialize_instruction], &vec![&payer])?;
151+
0
152+
}
153+
};
154+
155+
if current_guardian_set_index == 0 {
156+
println!("Upgrading guardian set from 0 to 1");
157+
process_upgrade_guardian_set(
158+
&rpc_client,
159+
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_1).unwrap(),
160+
wormhole,
161+
&payer,
162+
true,
163+
)?;
164+
current_guardian_set_index += 1;
165+
}
166+
167+
if current_guardian_set_index == 1 {
168+
println!("Upgrading guardian set from 1 to 2");
169+
process_upgrade_guardian_set(
170+
&rpc_client,
171+
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_2).unwrap(),
172+
wormhole,
173+
&payer,
174+
false,
175+
)?;
176+
current_guardian_set_index += 1;
177+
}
178+
179+
if current_guardian_set_index == 2 {
180+
println!("Upgrading guardian set from 2 to 3");
181+
process_upgrade_guardian_set(
182+
&rpc_client,
183+
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_3).unwrap(),
184+
wormhole,
185+
&payer,
186+
false,
187+
)?;
188+
current_guardian_set_index += 1;
189+
}
190+
191+
if current_guardian_set_index == 3 {
192+
println!("Upgrading guardian set from 3 to 4");
193+
process_upgrade_guardian_set(
194+
&rpc_client,
195+
&hex::decode(UPGRADE_GUARDIAN_SET_VAA_4).unwrap(),
196+
wormhole,
197+
&payer,
198+
false,
199+
)?;
200+
}
167201
}
168202

169203
Action::InitializePythReceiver {

0 commit comments

Comments
 (0)