Skip to content

Commit 059bc20

Browse files
authored
fix(cli): Wait for program confirmation before IDL init during deploy (#3976)
* fix(cli): Wait for program confirmation before IDL init during deploy * fix(cli): Check for existing IDL account and upgrade instead of create On redeployment, the IDL account already exists. This commit checks if the IDL account exists and calls idl_upgrade if it does, or idl_init if it doesn't. Also reduces retry count from 30 to 5 for faster feedback.
1 parent 45beb40 commit 059bc20

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

cli/src/lib.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,12 +3624,55 @@ fn deploy(
36243624

36253625
// Upload the IDL to the cluster by default (unless no_idl is set)
36263626
if !no_idl {
3627-
idl_init(
3628-
cfg_override,
3629-
program_id,
3630-
idl_filepath.display().to_string(),
3631-
None,
3632-
)?;
3627+
// Wait for the program to be confirmed before initializing IDL to prevent
3628+
// race condition where the program isn't yet available in validator cache
3629+
let client = create_client(&url);
3630+
let max_retries = 5;
3631+
let retry_delay = std::time::Duration::from_millis(500);
3632+
let cache_delay = std::time::Duration::from_secs(2);
3633+
3634+
println!("Waiting for program {} to be confirmed...", program_id);
3635+
3636+
for attempt in 0..max_retries {
3637+
if let Ok(account) = client.get_account(&program_id) {
3638+
if account.executable {
3639+
println!("Program confirmed on-chain");
3640+
std::thread::sleep(cache_delay);
3641+
break;
3642+
}
3643+
}
3644+
3645+
if attempt == max_retries - 1 {
3646+
return Err(anyhow!(
3647+
"Timeout waiting for program {} to be confirmed",
3648+
program_id
3649+
));
3650+
}
3651+
3652+
std::thread::sleep(retry_delay);
3653+
}
3654+
3655+
// Check if IDL account already exists
3656+
let idl_address = IdlAccount::address(&program_id);
3657+
let idl_account_exists = client.get_account(&idl_address).is_ok();
3658+
3659+
if idl_account_exists {
3660+
// IDL account exists, upgrade it
3661+
idl_upgrade(
3662+
cfg_override,
3663+
program_id,
3664+
idl_filepath.display().to_string(),
3665+
None,
3666+
)?;
3667+
} else {
3668+
// IDL account doesn't exist, create it
3669+
idl_init(
3670+
cfg_override,
3671+
program_id,
3672+
idl_filepath.display().to_string(),
3673+
None,
3674+
)?;
3675+
}
36333676
}
36343677
}
36353678
}

0 commit comments

Comments
 (0)