Skip to content

Commit a532d37

Browse files
Refactor: adapt to btckApi changes and implement rust idioms
Breaking changes: - Replace BlockUndo with hierarchical BlockSpentOutputs/TransactionSpentOutputs/Coin design - Remove get_ prefixes from all methods following rust naming conventions - Introduce RefType<T, Owner> for zero-copy borrowing Improvements: - Add RefType wrapper for memory-safe borrowed references with compile-time lifetime guarantees - Update internal bindings from kernel_ to btck_ prefixed C API - Add input validation to verify function - Add transaction and block iteration methods (output_count, transaction_count, etc.) - Implement Clone traits using C copy functions - Leverage C Library's smart ownershop tracking (m_owned flags) for safe borrowing - Updated and improved documentation
1 parent ae32f30 commit a532d37

File tree

5 files changed

+670
-293
lines changed

5 files changed

+670
-293
lines changed

examples/src/silentpaymentscanner.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,22 @@ fn scan_tx(receiver: &Receiver, secret_scan_key: &SecretKey, scan_tx_helper: Sca
162162

163163
fn scan_txs(chainman: &ChainstateManager) {
164164
let (receiver, secret_scan_key) = parse_keys();
165-
let mut block_index = chainman.get_block_index_tip();
165+
let mut block_index = chainman.block_index_tip();
166166
loop {
167167
if block_index.height() <= 1 {
168168
break;
169169
}
170-
let undo = chainman.read_undo_data(&block_index).unwrap();
170+
let spent_outputs = chainman.read_spent_outputs(&block_index).unwrap();
171171
let raw_block: Vec<u8> = chainman.read_block_data(&block_index).unwrap().into();
172172
let block: bitcoin::Block = deserialize(&raw_block).unwrap();
173173
// Should be the same size minus the coinbase transaction
174-
assert_eq!(block.txdata.len() - 1, undo.n_tx_undo);
174+
assert_eq!(block.txdata.len() - 1, spent_outputs.count());
175175

176176
for i in 0..(block.txdata.len() - 1) {
177-
let transaction_undo_size: u64 = undo.get_transaction_undo_size(i.try_into().unwrap());
178-
let transaction_input_size: u64 = block.txdata[i + 1].input.len().try_into().unwrap();
179-
assert_eq!(transaction_input_size, transaction_undo_size);
177+
let tx_spent_outputs = spent_outputs.transaction_spent_outputs(i).unwrap();
178+
let coins_spent_count = tx_spent_outputs.count();
179+
let transaction_input_size = block.txdata[i + 1].input.len();
180+
assert_eq!(transaction_input_size, coins_spent_count);
180181
let mut scan_tx_helper = ScanTxHelper {
181182
ins: vec![],
182183
outs: block.txdata[i + 1]
@@ -186,21 +187,18 @@ fn scan_txs(chainman: &ChainstateManager) {
186187
.collect(),
187188
};
188189
for j in 0..transaction_input_size {
190+
let coin = tx_spent_outputs.coin(j).unwrap();
189191
scan_tx_helper.ins.push(Input {
190-
prevout: undo
191-
.get_prevout_by_index(i as u64, j)
192-
.unwrap()
193-
.get_script_pubkey()
194-
.get(),
195-
script_sig: block.txdata[i + 1].input[j as usize].script_sig.to_bytes(),
196-
witness: block.txdata[i + 1].input[j as usize].witness.to_vec(),
192+
prevout: coin.output().unwrap().script_pubkey().to_bytes(),
193+
script_sig: block.txdata[i + 1].input[j].script_sig.to_bytes(),
194+
witness: block.txdata[i + 1].input[j].witness.to_vec(),
197195
prevout_data: (
198-
block.txdata[i + 1].input[j as usize]
196+
block.txdata[i + 1].input[j]
199197
.previous_output
200198
.txid
201199
.to_byte_array()
202200
.to_vec(),
203-
block.txdata[i + 1].input[j as usize].previous_output.vout,
201+
block.txdata[i + 1].input[j].previous_output.vout,
204202
),
205203
});
206204
}
@@ -229,7 +227,6 @@ fn main() {
229227
let blocks_dir = data_dir.clone() + "/blocks";
230228
let chainman = ChainstateManager::new(
231229
ChainstateManagerOptions::new(&context, &data_dir, &blocks_dir).unwrap(),
232-
Arc::clone(&context),
233230
)
234231
.unwrap();
235232
chainman.import_blocks().unwrap();

fuzz/fuzz_targets/fuzz_target_chainman.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fuzz_target!(|data: ChainstateManagerInput| {
9191
.set_block_tree_db_in_memory(data.block_tree_db_in_memory)
9292
.set_chainstate_db_in_memory(data.chainstate_db_in_memory);
9393
chainman_opts.set_worker_threads(data.worker_threads);
94-
let chainman = match ChainstateManager::new(chainman_opts, Arc::clone(&context)) {
94+
let chainman = match ChainstateManager::new(chainman_opts) {
9595
Err(KernelError::Internal(_)) => {
9696
return;
9797
}

fuzz/fuzz_targets/fuzz_target_verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct VerifyInput {
1616
pub script_pubkey: Vec<u8>,
1717
pub amount: Option<i64>,
1818
pub tx_to: Vec<u8>,
19-
pub input_index: u32,
19+
pub input_index: usize,
2020
pub flags: Option<u32>,
2121
pub spent_outputs: Vec<UtxoWrapper>,
2222
}

0 commit comments

Comments
 (0)