]> Untitled Git - bdk/commitdiff
feat(testenv): add `make_checkpoint_tip`
author志宇 <hello@evanlinjin.me>
Mon, 25 Mar 2024 05:44:01 +0000 (13:44 +0800)
committer志宇 <hello@evanlinjin.me>
Tue, 16 Apr 2024 09:51:02 +0000 (17:51 +0800)
This creates a checkpoint linked list which contains all blocks.

crates/testenv/src/lib.rs

index 8cba86261570f8386753cd979a68694ca50360f5..4ae6ea6e3b2d8816d073bcebd8325a84f82cf858 100644 (file)
@@ -1,7 +1,9 @@
-use bdk_chain::bitcoin::{
-    address::NetworkChecked, block::Header, hash_types::TxMerkleNode, hashes::Hash,
-    secp256k1::rand::random, transaction, Address, Amount, Block, BlockHash, CompactTarget,
-    ScriptBuf, ScriptHash, Transaction, TxIn, TxOut, Txid,
+use bdk_chain::{
+    bitcoin::{
+        address::NetworkChecked, block::Header, hash_types::TxMerkleNode, hashes::Hash, secp256k1::rand::random, transaction, Address, Amount, Block, BlockHash, CompactTarget, ScriptBuf, ScriptHash, Transaction, TxIn, TxOut, Txid
+    },
+    local_chain::CheckPoint,
+    BlockId,
 };
 use bitcoincore_rpc::{
     bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},
@@ -234,6 +236,18 @@ impl TestEnv {
             .send_to_address(address, amount, None, None, None, None, None, None)?;
         Ok(txid)
     }
+
+    /// Create a checkpoint linked list of all the blocks in the chain.
+    pub fn make_checkpoint_tip(&self) -> CheckPoint {
+        CheckPoint::from_block_ids((0_u32..).map_while(|height| {
+            self.bitcoind
+                .client
+                .get_block_hash(height as u64)
+                .ok()
+                .map(|hash| BlockId { height, hash })
+        }))
+        .expect("must craft tip")
+    }
 }
 
 #[cfg(test)]