Skip to content

Commit 655dd60

Browse files
committed
Remove info, add height and hash queries
1 parent 831d2cb commit 655dd60

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

libbitcoinkernel-sys/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ unsafe impl Send for BlockIndex<'_> {}
628628
unsafe impl Sync for BlockIndex<'_> {}
629629

630630
#[derive(Debug, Clone)]
631-
pub struct BlockIndexInfo {
632-
pub height: i32,
631+
pub struct BlockHash {
632+
pub hash: [u8; 32],
633633
}
634634

635635
impl<'a> BlockIndex<'a> {
@@ -645,12 +645,16 @@ impl<'a> BlockIndex<'a> {
645645
})
646646
}
647647

648-
pub fn info(&self) -> BlockIndexInfo {
649-
let info = unsafe { kernel_get_block_index_info(self.inner) };
650-
let res = BlockIndexInfo {
651-
height: unsafe { (*info).height },
648+
pub fn height(&self) -> i32 {
649+
unsafe { kernel_block_index_get_height(self.inner) }
650+
}
651+
652+
pub fn info(&self) -> BlockHash {
653+
let hash = unsafe { kernel_block_index_get_block_hash(self.inner) };
654+
let res = BlockHash {
655+
hash: unsafe { (&*hash).hash },
652656
};
653-
unsafe { kernel_block_index_info_destroy(info) };
657+
unsafe { kernel_block_hash_destroy(hash) };
654658
return res;
655659
}
656660
}
@@ -912,8 +916,8 @@ impl<'a> ChainstateManager<'a> {
912916
})
913917
}
914918

915-
pub fn get_block_index_by_hash(&self, hash: [u8; 32]) -> Result<BlockIndex, KernelError> {
916-
let mut block_hash = kernel_BlockHash { hash };
919+
pub fn get_block_index_by_hash(&self, hash: BlockHash) -> Result<BlockIndex, KernelError> {
920+
let mut block_hash = kernel_BlockHash { hash: hash.hash };
917921
let inner = unsafe {
918922
kernel_get_block_index_by_hash(self.context.inner, self.inner, &mut block_hash)
919923
};

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn scan_txs(chainman: &ChainstateManager) {
171171
let (receiver, secret_scan_key) = parse_keys();
172172
let mut block_index = chainman.get_block_index_tip();
173173
loop {
174-
if block_index.info().height <= 1 {
174+
if block_index.height() <= 1 {
175175
break;
176176
}
177177
let undo = chainman.read_undo_data(&block_index).unwrap();

tests/test.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
mod tests {
33
use bitcoin::consensus::deserialize;
44
use libbitcoinkernel_sys::{
5-
register_validation_interface, unregister_validation_interface, verify, Block,
6-
BlockIndexInfo, BlockManagerOptions, BlockUndo, ChainParams, ChainType,
7-
ChainstateLoadOptions, ChainstateManager, ChainstateManagerOptions, Context,
8-
ContextBuilder, KernelError, KernelNotificationInterfaceCallbackHolder, Log, Logger,
9-
ProcessBlockError, ScriptPubkey, Transaction, TxOut, Utxo,
10-
ValidationInterfaceCallbackHolder, ValidationInterfaceWrapper, VERIFY_ALL_PRE_TAPROOT,
5+
register_validation_interface, unregister_validation_interface, verify, Block, BlockHash,
6+
BlockManagerOptions, BlockUndo, ChainParams, ChainType, ChainstateLoadOptions,
7+
ChainstateManager, ChainstateManagerOptions, Context, ContextBuilder, KernelError,
8+
KernelNotificationInterfaceCallbackHolder, Log, Logger, ProcessBlockError, ScriptPubkey,
9+
Transaction, TxOut, Utxo, ValidationInterfaceCallbackHolder, ValidationInterfaceWrapper,
10+
VERIFY_ALL_PRE_TAPROOT,
1111
};
1212
use std::fs::File;
1313
use std::io::{BufRead, BufReader};
@@ -212,11 +212,11 @@ mod tests {
212212
chainman.process_block(&block).unwrap();
213213
}
214214
let block_index_genesis = chainman.get_block_index_genesis();
215-
let info = block_index_genesis.info();
216-
assert_eq!(info.height, 0);
215+
let height = block_index_genesis.height();
216+
assert_eq!(height, 0);
217217
let block_index_1 = chainman.get_next_block_index(block_index_genesis).unwrap();
218-
let info = block_index_1.info();
219-
assert_eq!(info.height, 1);
218+
let height = block_index_1.height();
219+
assert_eq!(height, 1);
220220

221221
let block_index_tip = chainman.get_block_index_tip();
222222
let raw_block_tip: Vec<u8> = chainman.read_block_data(&block_index_tip).unwrap().into();
@@ -411,9 +411,8 @@ mod tests {
411411
is_send::<BlockUndo>();
412412
is_sync::<ChainstateManager>();
413413
is_send::<ChainstateManager>();
414-
is_sync::<BlockIndexInfo>();
415-
is_send::<BlockIndexInfo>();
416-
414+
is_sync::<BlockHash>();
415+
is_send::<BlockHash>();
417416
// is_sync::<Rc<u8>>(); // won't compile, kept as a failure case.
418417
// is_send::<Rc<u8>>(); // won't compile, kept as a failure case.
419418
}

0 commit comments

Comments
 (0)