@@ -286,11 +286,49 @@ impl BitcoinCore {
286286 /// Fund the node's wallet.
287287 ///
288288 /// This can be useful before using [`BitcoinCore::create_mempool_transaction`].
289+ ///
290+ /// We intentionally generate blocks in smaller batches instead of a single
291+ /// `generatetoaddress(101, ...)` call. On slower or contended CI hosts,
292+ /// generating 101 blocks in one RPC request can exceed the JSON-RPC client
293+ /// timeout and fail even though partial mining progress is happening.
294+ ///
295+ /// To absorb transient Bitcoin Core warmup/RPC startup conditions in CI,
296+ /// this method retries the full funding flow a few times with a short delay.
289297 pub fn fund_wallet ( & self ) -> Result < ( ) , corepc_node:: Error > {
290- let client = & self . bitcoind . client ;
291- let address = client. new_address ( ) ?;
292- client. generate_to_address ( 101 , & address) ?;
293- Ok ( ( ) )
298+ const ATTEMPTS : u8 = 6 ;
299+ const RETRY_DELAY_SECS : u64 = 2 ;
300+ const FUNDING_BLOCKS_TOTAL : usize = 101 ;
301+ const FUNDING_BLOCKS_BATCH_SIZE : usize = 16 ;
302+
303+ let mut last_error = None ;
304+
305+ for attempt in 1 ..=ATTEMPTS {
306+ let client = & self . bitcoind . client ;
307+
308+ let result = ( || -> Result < ( ) , corepc_node:: Error > {
309+ let address = client. new_address ( ) ?;
310+ let mut remaining = FUNDING_BLOCKS_TOTAL ;
311+
312+ while remaining > 0 {
313+ let to_generate = remaining. min ( FUNDING_BLOCKS_BATCH_SIZE ) ;
314+ client. generate_to_address ( to_generate, & address) ?;
315+ remaining -= to_generate;
316+ }
317+ Ok ( ( ) )
318+ } ) ( ) ;
319+
320+ match result {
321+ Ok ( ( ) ) => return Ok ( ( ) ) ,
322+ Err ( error) => {
323+ last_error = Some ( error) ;
324+ if attempt < ATTEMPTS {
325+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( RETRY_DELAY_SECS ) ) ;
326+ }
327+ }
328+ }
329+ }
330+
331+ Err ( last_error. expect ( "fund_wallet must produce an error before exhausting retries" ) )
294332 }
295333
296334 /// Return the hash of the most recent block.
@@ -432,6 +470,9 @@ impl TemplateProvider {
432470 /// Fund the node's wallet.
433471 ///
434472 /// This can be useful before using [`TemplateProvider::create_mempool_transaction`].
473+ ///
474+ /// This delegates to [`BitcoinCore::fund_wallet`], including its built-in
475+ /// block-batch generation and retry behavior for transient CI startup issues.
435476 pub fn fund_wallet ( & self ) -> Result < ( ) , corepc_node:: Error > {
436477 self . bitcoin_core . fund_wallet ( )
437478 }
0 commit comments