Skip to content

Commit 943eea3

Browse files
authored
Merge pull request #15 from alexanderwiederin/btck_chain_iterator
feat: adapt bindings to btck_api changes (including chain, blocktreeentries and serialization callbacks)
2 parents 31a53eb + 03d67ec commit 943eea3

File tree

9 files changed

+1084
-689
lines changed

9 files changed

+1084
-689
lines changed

examples/src/silentpaymentscanner.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,19 @@ 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.block_index_tip();
165+
let chain = chainman.active_chain();
166+
let mut block_index = chain.tip();
167+
166168
loop {
167169
if block_index.height() <= 1 {
168170
break;
169171
}
170172
let spent_outputs = chainman.read_spent_outputs(&block_index).unwrap();
171-
let raw_block: Vec<u8> = chainman.read_block_data(&block_index).unwrap().into();
173+
let raw_block: Vec<u8> = chainman
174+
.read_block_data(&block_index)
175+
.unwrap()
176+
.try_into()
177+
.unwrap();
172178
let block: bitcoin::Block = deserialize(&raw_block).unwrap();
173179
// Should be the same size minus the coinbase transaction
174180
assert_eq!(block.txdata.len() - 1, spent_outputs.count());

fuzz/fuzz_targets/fuzz_target_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use libfuzzer_sys::fuzz_target;
55

66
fuzz_target!(|data: &[u8]| {
77
if let Ok(block) = Block::try_from(data) {
8-
let block_serialized: Vec<u8> = block.into();
8+
let block_serialized: Vec<u8> = block.try_into().unwrap();
99
assert!(data.len() >= block_serialized.len());
1010
}
1111
});

libbitcoinkernel-sys/bitcoin/src/bitcoin-chainstate.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@
1414
#include <windows.h>
1515
// clang-format on
1616
#include <codecvt>
17-
#include <shellapi.h>
1817
#include <locale>
18+
#include <shellapi.h>
1919
#endif
2020

2121
using namespace btck;
2222

23-
std::vector<unsigned char> hex_string_to_char_vec(std::string_view hex)
23+
std::vector<std::byte> hex_string_to_byte_vec(std::string_view hex)
2424
{
25-
std::vector<unsigned char> bytes;
25+
std::vector<std::byte> bytes;
2626
bytes.reserve(hex.length() / 2);
2727

2828
for (size_t i{0}; i < hex.length(); i += 2) {
29-
unsigned char byte;
30-
auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte, 16);
31-
if (ec == std::errc{} && ptr == hex.data() + i + 2) {
32-
bytes.push_back(byte);
29+
uint8_t byte_value;
30+
auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte_value, 16);
31+
32+
if (ec != std::errc{} || ptr != hex.data() + i + 2) {
33+
throw std::invalid_argument("Invalid hex character");
3334
}
35+
bytes.push_back(static_cast<std::byte>(byte_value));
3436
}
3537
return bytes;
3638
}
@@ -47,7 +49,7 @@ class KernelLog
4749
class TestValidationInterface : public ValidationInterface<TestValidationInterface>
4850
{
4951
public:
50-
TestValidationInterface() : ValidationInterface() {}
52+
TestValidationInterface() = default;
5153

5254
std::optional<std::string> m_expected_valid_block = std::nullopt;
5355

@@ -104,7 +106,7 @@ class TestValidationInterface : public ValidationInterface<TestValidationInterfa
104106
class TestKernelNotifications : public KernelNotifications<TestKernelNotifications>
105107
{
106108
public:
107-
void BlockTipHandler(btck_SynchronizationState, const btck_BlockIndex*, double) override
109+
void BlockTipHandler(btck_SynchronizationState, const BlockTreeEntry, double) override
108110
{
109111
std::cout << "Block tip changed" << std::endl;
110112
}
@@ -206,7 +208,7 @@ int main(int argc, char* argv[])
206208
continue;
207209
}
208210

209-
auto raw_block{hex_string_to_char_vec(line)};
211+
auto raw_block{hex_string_to_byte_vec(line)};
210212
std::unique_ptr<Block> block;
211213
try {
212214
block = std::make_unique<Block>(raw_block);

0 commit comments

Comments
 (0)