diff --git a/packages/pocket-ic/HOWTO.md b/packages/pocket-ic/HOWTO.md index 8c9a10a8ea0..e3e6b8dc6ff 100644 --- a/packages/pocket-ic/HOWTO.md +++ b/packages/pocket-ic/HOWTO.md @@ -585,9 +585,20 @@ To mine blocks with rewards credited to a given `bitcoin_address: String`, you c .unwrap(); let mut n = 101; // must be more than 100 (Coinbase maturity rule) - btc_rpc - .generate_to_address(n, &Address::from_str(&bitcoin_address).unwrap()) - .unwrap(); + // retry generating blocks until the bitcoind is up and running + let start = std::time::Instant::now(); + loop { + match btc_rpc.generate_to_address(n, &Address::from_str(&bitcoin_address).unwrap()) { + Ok(_) => break, + Err(bitcoincore_rpc::Error::JsonRpc(err)) => { + if start.elapsed() > std::time::Duration::from_secs(30) { + panic!("Timed out when waiting for bitcoind; last error: {}", err); + } + std::thread::sleep(std::time::Duration::from_millis(100)); + } + Err(err) => panic!("Unexpected error when talking to bitcoind: {}", err), + } + } ``` For an example of a test canister that can be deployed to an application subnet of the PocketIC instance, diff --git a/rs/pocket_ic_server/tests/bitcoin_integration_tests.rs b/rs/pocket_ic_server/tests/bitcoin_integration_tests.rs index 59a97ab7dba..876becbd392 100644 --- a/rs/pocket_ic_server/tests/bitcoin_integration_tests.rs +++ b/rs/pocket_ic_server/tests/bitcoin_integration_tests.rs @@ -140,9 +140,20 @@ rpcauth=ic-btc-integration:cdf2741387f3a12438f69092f0fdad8e$62081498c98bee09a0dc // `n` must be more than 100 (Coinbase maturity rule) so that the reward for the first block can be sent out let mut n = 101; - btc_rpc - .generate_to_address(n, &Address::from_str(&bitcoin_address).unwrap()) - .unwrap(); + // retry generating blocks until the bitcoind is up and running + let start = std::time::Instant::now(); + loop { + match btc_rpc.generate_to_address(n, &Address::from_str(&bitcoin_address).unwrap()) { + Ok(_) => break, + Err(bitcoincore_rpc::Error::JsonRpc(err)) => { + if start.elapsed() > std::time::Duration::from_secs(30) { + panic!("Timed out when waiting for bitcoind; last error: {}", err); + } + std::thread::sleep(std::time::Duration::from_millis(100)); + } + Err(err) => panic!("Unexpected error when talking to bitcoind: {}", err), + } + } let reward = 50 * 100_000_000; // 50 BTC