Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto: some dry-by fixes that improve the onchain log #224

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions lampo-bitcoind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ impl Backend for BitcoinCore {
tx.txid(),
)));
}
TxResult::Discarded => {}
TxResult::Discarded => handler.emit(Event::OnChain(
OnChainEvent::UnconfirmedTransaction(txid.clone()),
)),
}
}
txs.clear();
Expand Down Expand Up @@ -426,74 +428,72 @@ impl Backend for BitcoinCore {
let best_block = self.get_best_block();
let Ok((block_hash, height)) = best_block else {
// SAFETY: if we are in this block the error will be always not null
log::error!(target: "lampo_bitcoind", "Impossible get the inforamtion of the last besh block: {}", best_block.err().unwrap());
break;
log::error!(target: "bitcoind", "Impossible get the inforamtion of the last besh block: {}", best_block.err().unwrap());
continue;
Comment on lines -429 to +432
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should break out of the loop when we don't get the best block to remove further errors. It is also breaking tests.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nop because if the bitcoin core it is not ready this function will return an error but we should just be able to retry, no?

};
if height.is_none() {
log::warn!(target: "lampo_bitcoind", "height is none for the best block found `{block_hash}`");
let Some(height) = height else {
log::warn!(target: "bitcoind", "height is none for the best block found `{block_hash}`");
continue;
}
let height = height.unwrap();
};

if !self.others_txs.lock().unwrap().borrow().is_empty() {
let start: u64 = self.best_height.borrow().clone().into();
let end: u64 = height.into();
log::trace!(target: "lampo_bitcoind", "Scan blocks in range [{start}..{end}]");
log::trace!(target: "bitcoind", "Scan blocks in range [{start}..{end}]");
for height in start..end + 1 {
log::trace!(target: "lampo_bitcoind", "Looking at block with height {height}");
log::trace!(target: "bitcoind", "Looking at block with height {height}");
let block_hash = self.get_block_hash(height).unwrap();
let Ok(lampo_common::backend::BlockData::FullBlock(block)) =
self.get_block(&block_hash)
else {
log::warn!(target: "lampo_bitcoind", "Impossible retrieval the block information with hash `{block_hash}`");
log::warn!(target: "bitcoind", "Impossible retrieval the block information with hash `{block_hash}`");
continue;
};
if self.best_height.borrow().lt(&height.into()) {
*self.best_height.borrow_mut() = height.into();
*self.last_bloch_hash.borrow_mut() = Some(block_hash);
log::trace!(target: "lampo_bitcoind", "new best block with hash `{block_hash}` at height `{height}`");
log::trace!(target: "bitcoind", "new best block with hash `{block_hash}` at height `{height}`");
handler.emit(Event::OnChain(OnChainEvent::NewBestBlock((
block.header,
// SAFETY: the height should be always a valid u32
Height::from_consensus(height as u32).unwrap(),
))));

self.handler.borrow().clone().map(|handler| {
handler.emit(Event::OnChain(OnChainEvent::NewBlock(block.clone())));
});
handler.emit(Event::OnChain(OnChainEvent::NewBlock(block.clone())));
let _ = self.find_tx_in_block(&block);
}
}
// ok when the wallet is full in sync with the blockchain, we can query the
// bitcoind wallet for ours transaction.
//
// This is the only place where we can query because otherwise the we can
// This is the only place where we can query because otherwise we can
// confuse ldk when we send a new best block with height X and a Confirmed transaction
// event at height Y, where Y > X. In this way ldk think that a reorgs happens.
//
// The reorgs do not happens, it is only that the bitcoind wallet is able to answer quickly
// while the lampo wallet is still looking for external transaction inside the blocks.
// The reorgs do not happens commonly, it is only that the bitcoind wallet is able
// to answer quickly while the lampo wallet is still looking
// for external transaction inside the blocks.
let _ = self.process_transactions();
} else if self.best_height.borrow().lt(&height.into()) {
log::trace!(target: "lampo_bitcoind", "New best block at height {height}, out current best block is {}", self.best_height.borrow());
log::trace!(target: "bitcoind", "New best block at height {height}, out current best block is {}", self.best_height.borrow());
*self.best_height.borrow_mut() = height.into();
*self.last_bloch_hash.borrow_mut() = Some(block_hash);
let Ok(lampo_common::backend::BlockData::FullBlock(block)) =
self.get_block(&block_hash)
else {
log::warn!(target: "lampo_bitcoind", "Impossible retrieval the block information with hash `{block_hash}`");
log::warn!(target: "bitcoind", "Impossible retrieval the block information with hash `{block_hash}`");
continue;
};
handler.emit(Event::OnChain(OnChainEvent::NewBestBlock((
block.header,
// SAFETY: the height should be always a valid u32
Height::from_consensus(height).unwrap(),
))));
handler.emit(Event::OnChain(OnChainEvent::NewBlock(block.clone())));

let _ = self.find_tx_in_block(&block);
log::trace!(target: "lampo_bitcoind", "new best block with hash `{block_hash}` at height `{}`", height);
log::trace!(target: "bitcoind", "new best block with hash `{block_hash}` at height `{}`", height);
}

// Emit new Best block!
std::thread::sleep(self.pool_time);
}
}))
Expand Down
4 changes: 2 additions & 2 deletions lampod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ impl LampoDeamon {

log::info!(target: "lampo", "Stating onchaind");
let _ = self.onchain_manager().backend.clone().listen();
log::info!(target: "lampo", "Starting channel manager");
let _ = self.channel_manager().listen();
log::info!(target: "lampo", "Starting peer manager");
let _ = self.peer_manager().run();
log::info!(target: "lampo", "Starting channel manager");
let _ = self.channel_manager().listen();
Ok(std::thread::spawn(move || {
let _ = background_processor.join();
Ok(())
Expand Down
16 changes: 12 additions & 4 deletions lampod/src/ln/channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,23 @@ impl LampoChannelManager {
self.load_channel_monitors(true).unwrap();
}
std::thread::spawn(move || {
log::info!(target: "lampo_channel_manager", "listening on chain event on the channel manager");
log::info!(target: "manager", "listening on chain event on the channel manager");
let events = self.handler().events();
loop {
let Ok(Event::OnChain(event)) = events.recv() else {
continue;
};
log::trace!(target: "lampo_channel_manager", "event received {:?}", event);
log::trace!(target: "channel_manager", "event received {:?}", event);
match event {
OnChainEvent::NewBestBlock((hash, height)) => {
log::debug!(target: "lampo_channel_manager", "new best block with hash `{}` at height `{height}`", hash.block_hash());
log::info!(target: "channel_manager", "new best block with hash `{}` at height `{height}`", hash.block_hash());
self.chain_monitor()
.best_block_updated(&hash, height.to_consensus_u32());
self.manager()
.best_block_updated(&hash, height.to_consensus_u32());
}
OnChainEvent::ConfirmedTransaction((tx, idx, header, height)) => {
log::debug!(target: "lampo_channel_manager", "confirmed transaction with txid `{}` at height `{height}`", tx.txid());
log::info!(target: "channel_manager", "confirmed transaction with txid `{}` at height `{height}`", tx.txid());
self.chain_monitor().transactions_confirmed(
&header,
&[(idx as usize, &tx)],
Expand All @@ -159,6 +159,14 @@ impl LampoChannelManager {
height.to_consensus_u32(),
);
}
OnChainEvent::UnconfirmedTransaction(txid) => {
log::info!(target: "channel_manager", "transaction with txid `{txid}` is still unconfirmed");
self.chain_monitor().transaction_unconfirmed(&txid);
self.manager().transaction_unconfirmed(&txid);
}
OnChainEvent::DiscardedTransaction(txid) => {
log::warn!(target: "channel_manager", "transaction with txid `{txid}` discarded");
}
_ => continue,
}
}
Expand Down
Loading