[blockchain] Fix receiving a coinbase using Electrum/Esplora

Closes #107
This commit is contained in:
Alekos Filini 2020-10-13 11:56:59 +02:00
parent 848b52c50e
commit 0caad5f3d9
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F
3 changed files with 26 additions and 6 deletions

View File

@ -243,6 +243,11 @@ pub trait ElectrumLikeSync {
// look for our own inputs
for (i, input) in tx.input.iter().enumerate() {
// skip coinbase inputs
if input.previous_output.is_null() {
continue;
}
// the fact that we visit addresses in a BFS fashion starting from the external addresses
// should ensure that this query is always consistent (i.e. when we get to call this all
// the transactions at a lower depth have already been indexed, so if an outpoint is ours
@ -312,7 +317,7 @@ pub trait ElectrumLikeSync {
sent: outgoing,
height,
timestamp: 0,
fees: inputs_sum - outputs_sum,
fees: inputs_sum.saturating_sub(outputs_sum), // if the tx is a coinbase, fees would be negative
};
info!("Saving tx {}", txid);
updates.set_tx(&tx)?;

View File

@ -520,6 +520,21 @@ pub fn bdk_blockchain_tests(attr: TokenStream, item: TokenStream) -> TokenStream
assert_eq!(wallet.get_balance().unwrap(), 0);
assert_eq!(new_details.received, 0);
}
#[test]
#[serial]
fn test_sync_receive_coinbase() {
let (wallet, descriptors, mut test_client) = init_single_sig();
let wallet_addr = wallet.get_new_address().unwrap();
wallet.sync(noop_progress(), None).unwrap();
assert_eq!(wallet.get_balance().unwrap(), 0);
test_client.generate(1, Some(wallet_addr));
wallet.sync(noop_progress(), None).unwrap();
assert!(wallet.get_balance().unwrap() > 0);
}
}
};

View File

@ -341,7 +341,7 @@ impl TestClient {
.unwrap();
if let Some(num) = meta_tx.min_confirmations {
self.generate(num);
self.generate(num, None);
}
let monitor_script = Address::from_str(&meta_tx.output[0].to_address)
@ -464,9 +464,9 @@ impl TestClient {
block.header.block_hash().to_hex()
}
pub fn generate(&mut self, num_blocks: u64) {
let our_addr = self.get_new_address(None, None).unwrap();
let hashes = self.generate_to_address(num_blocks, &our_addr).unwrap();
pub fn generate(&mut self, num_blocks: u64, address: Option<Address>) {
let address = address.unwrap_or_else(|| self.get_new_address(None, None).unwrap());
let hashes = self.generate_to_address(num_blocks, &address).unwrap();
let best_hash = hashes.last().unwrap();
let height = self.get_block_info(best_hash).unwrap().height;
@ -505,7 +505,7 @@ impl TestClient {
pub fn reorg(&mut self, num_blocks: u64) {
self.invalidate(num_blocks);
self.generate(num_blocks);
self.generate(num_blocks, None);
}
pub fn get_node_address(&self, address_type: Option<AddressType>) -> Address {