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

Track confirmation block hash and return via Confirm::get_relevant_txids #1796

Conversation

tnull
Copy link
Contributor

@tnull tnull commented Oct 24, 2022

Previously, Confirm::get_relevant_txids() only returned a list of transactions that have to be monitored for reorganization out of the chain. This interface however required a double bookkeeping: while we internally keep track of the best block, height, etc, it would also require the user to keep track which transaction was previously confirmed in which block and to take actions based on any change, e.g, to reconfirm them when the block would be reorged-out and the transactions had been reconfirmed in another block.

Here, we track the confirmation block hash internally and return it via Confirm::get_relevant_txids() to the user, which alleviates the requirement for double bookkeeping: the user can now simply check whether the given transactions are still confirmed via the given blocks, and take action if they are not.

@tnull tnull marked this pull request as draft October 24, 2022 12:18
@tnull
Copy link
Contributor Author

tnull commented Oct 24, 2022

Marked draft for now, will switch after concept ACK.

@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2022

Codecov Report

Base: 90.78% // Head: 91.23% // Increases project coverage by +0.44% 🎉

Coverage data is based on head (de9cd52) compared to base (e55e0d5).
Patch coverage: 89.39% of modified lines in pull request are covered.

❗ Current head de9cd52 differs from pull request most recent head 9685d6c. Consider uploading reports for the commit 9685d6c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1796      +/-   ##
==========================================
+ Coverage   90.78%   91.23%   +0.44%     
==========================================
  Files          87       89       +2     
  Lines       47604    50973    +3369     
  Branches    47604    50973    +3369     
==========================================
+ Hits        43218    46503    +3285     
- Misses       4386     4470      +84     
Impacted Files Coverage Δ
lightning/src/chain/chainmonitor.rs 97.78% <ø> (ø)
lightning/src/chain/mod.rs 68.18% <ø> (ø)
lightning/src/chain/channelmonitor.rs 90.69% <80.64%> (+0.04%) ⬆️
lightning/src/chain/onchaintx.rs 95.08% <90.00%> (+0.07%) ⬆️
lightning/src/ln/channel.rs 88.71% <100.00%> (+<0.01%) ⬆️
lightning/src/ln/channelmanager.rs 88.85% <100.00%> (+3.44%) ⬆️
lightning/src/ln/reorg_tests.rs 100.00% <100.00%> (ø)
lightning-net-tokio/src/lib.rs 76.73% <0.00%> (-0.31%) ⬇️
lightning/src/ln/functional_tests.rs 96.95% <0.00%> (-0.11%) ⬇️
lightning/src/lib.rs 100.00% <0.00%> (ø)
... and 12 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from c1d82ac to 0ff16b4 Compare October 24, 2022 13:27
@jkczyz jkczyz self-requested a review October 24, 2022 13:55
@tnull tnull marked this pull request as ready for review October 24, 2022 17:28
@tnull
Copy link
Contributor Author

tnull commented Oct 24, 2022

Marked draft for now, will switch after concept ACK.

Just discussed this with Matt in Discord, I'll count this as concept ACK.

@tnull tnull added this to the 0.0.113 milestone Oct 25, 2022
@arik-so
Copy link
Contributor

arik-so commented Oct 27, 2022

Would it be helpful to also return the confirmation height, so a user could trivially verify if a transaction has been dropped by knowing the depth of recent reorgs?

lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
lightning/src/ln/channelmanager.rs Outdated Show resolved Hide resolved
lightning/src/ln/reorg_tests.rs Outdated Show resolved Hide resolved
@tnull
Copy link
Contributor Author

tnull commented Oct 27, 2022

Would it be helpful to also return the confirmation height, so a user could trivially verify if a transaction has been dropped by knowing the depth of recent reorgs?

Not sure, as the height to compare it to is always due to change meaning it may be ambiguous what 'recent reorgs' are. So including height might invite users to do some comparisons that are likely gonna be race-y again?

Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to squash.

lightning/src/chain/channelmonitor.rs Outdated Show resolved Hide resolved
@TheBlueMatt
Copy link
Collaborator

This really needs some kind of testing. It should be pretty straightforward to just add relevant assertions in some of the existing reorg tests, I hope.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from cd2378a to c937807 Compare November 3, 2022 11:59
@tnull
Copy link
Contributor Author

tnull commented Nov 3, 2022

This really needs some kind of testing. It should be pretty straightforward to just add relevant assertions in some of the existing reorg tests, I hope.

Now added a test that we're indeed getting the expected confirmation block hash with c937807. Let me know if it's fine to squash.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from c937807 to 24c5dde Compare November 4, 2022 10:46
@@ -548,7 +552,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, conf_hash: BlockHash, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know its not really your fault, but man this is confusing - update_claims_view takes block hash and height parameters for "what block/height this tx was included in" but is often called without any txn and dummy (current block) values. Can we split the function in two and have the part that operates on txn_matched be separate? Should be a straightforward change and would make this PR much more clearly correct.

Copy link
Contributor Author

@tnull tnull Nov 7, 2022

Choose a reason for hiding this comment

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

Now split the method into update_claims_view_from_requests and update_claims_view_from_matched_txn in 0f9fc8a. I also DRYed the onchain event processing that would always run into process_onchain_events_awaiting_threshold_conf.

/// `conf_height` represents the height at which the transactions in `txn_matched` were
/// confirmed. This does not need to equal the current blockchain tip height, which should be
/// provided via `cur_height`, however it must never be higher than `cur_height`.
pub(crate) fn update_claims_view<B: Deref, F: Deref, L: Deref>(&mut self, txn_matched: &[&Transaction], requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(&mut self,
requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B,
Copy link
Collaborator

@TheBlueMatt TheBlueMatt Nov 7, 2022

Choose a reason for hiding this comment

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

Should drop the conf_height - nothing is being confirmed. Can also drop the corresponding docs which talk about txn_matched.

Can you fix the above docs on conf_height which mentions txn_matched?

Copy link
Contributor Author

@tnull tnull Nov 7, 2022

Choose a reason for hiding this comment

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

Done hope I'm not misinterpreting something here:

$ git diff-tree -U2 0f9fc8a 84073d
diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs
index ee557eb7..8fbfa215 100644
--- a/lightning/src/chain/onchaintx.rs
+++ b/lightning/src/chain/onchaintx.rs
@@ -552,7 +552,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
        /// preimage after force-close.
        ///
-       /// `conf_height` represents the height at which the transactions in `txn_matched` were
-       /// confirmed. This does not need to equal the current blockchain tip height, which should be
-       /// provided via `cur_height`, however it must never be higher than `cur_height`.
+       /// `conf_height` represents the minimal height at which the request could get confirmed. This
+       /// does not need to equal the current blockchain tip height, which should be provided via
+       /// `cur_height`, however it must never be higher than `cur_height`.
        pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(&mut self,
                requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B,

@TheBlueMatt
Copy link
Collaborator

Basically LGTM, feel free to squash from my PoV, but will need re-review from multiple reviewers here.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from 0f9fc8a to 84073d6 Compare November 7, 2022 18:48
@tnull
Copy link
Contributor Author

tnull commented Nov 7, 2022

Squashed commits.

@tnull tnull requested review from jkczyz and wpaulino November 7, 2022 19:29
Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

LGTM

lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from 84073d6 to 1a9fec5 Compare November 8, 2022 08:59
jkczyz
jkczyz previously approved these changes Nov 8, 2022
Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

Just some nits.

lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
lightning/src/ln/reorg_tests.rs Outdated Show resolved Hide resolved
lightning/src/ln/reorg_tests.rs Outdated Show resolved Hide resolved
@tnull
Copy link
Contributor Author

tnull commented Nov 8, 2022

Addressed nits, squashed again (and accidentally rebased on main in the process... Sorry 😢 )

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from e6b7bc2 to 1a80eac Compare November 8, 2022 18:30
@tnull
Copy link
Contributor Author

tnull commented Nov 8, 2022

Recovered the most recent base before the accident. So the latest squash diff is:

> git diff-tree -U2  1a9fec5 1a80eac
diff --git a/lightning/src/chain/onchaintx.rs b/lightning/src/chain/onchaintx.rs
index aa1116ae..14b5a298 100644
--- a/lightning/src/chain/onchaintx.rs
+++ b/lightning/src/chain/onchaintx.rs
@@ -555,10 +555,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
 	/// does not need to equal the current blockchain tip height, which should be provided via
 	/// `cur_height`, however it must never be higher than `cur_height`.
-	pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(&mut self,
-		requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32, broadcaster: &B,
-		fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
-		where B::Target: BroadcasterInterface,
-			  F::Target: FeeEstimator,
-			  L::Target: Logger,
+	pub(crate) fn update_claims_view_from_requests<B: Deref, F: Deref, L: Deref>(
+		&mut self, requests: Vec<PackageTemplate>, conf_height: u32, cur_height: u32,
+		broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+	) where
+			B::Target: BroadcasterInterface,
+			F::Target: FeeEstimator,
+			L::Target: Logger,
 	{
 		log_debug!(logger, "Updating claims view at height {} with {} claim requests", cur_height, requests.len());
@@ -654,10 +655,11 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
 	/// confirmed. This does not need to equal the current blockchain tip height, which should be
 	/// provided via `cur_height`, however it must never be higher than `cur_height`.
-	pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(&mut self,
-		txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash, cur_height: u32,
-		broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L)
-		where B::Target: BroadcasterInterface,
-			  F::Target: FeeEstimator,
-			  L::Target: Logger,
+	pub(crate) fn update_claims_view_from_matched_txn<B: Deref, F: Deref, L: Deref>(
+		&mut self, txn_matched: &[&Transaction], conf_height: u32, conf_hash: BlockHash,
+		cur_height: u32, broadcaster: &B, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
+	) where
+			B::Target: BroadcasterInterface,
+			F::Target: FeeEstimator,
+			L::Target: Logger,
 	{
 		log_debug!(logger, "Updating claims view at height {} with {} matched transactions in block {}", cur_height, txn_matched.len(), conf_height);
diff --git a/lightning/src/ln/reorg_tests.rs b/lightning/src/ln/reorg_tests.rs
index 6d4fe16f..1e26d339 100644
--- a/lightning/src/ln/reorg_tests.rs
+++ b/lightning/src/ln/reorg_tests.rs
@@ -285,7 +285,6 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
 			assert_eq!(relevant_txids.len(), 1);
 			let block_hash_opt = relevant_txids[0].1;
-			assert!(block_hash_opt.is_some());
 			let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
-			assert_eq!(block_hash_opt.unwrap(), expected_hash);
+			assert_eq!(block_hash_opt, Some(expected_hash));
 			let txid = relevant_txids[0].0;
 			assert_eq!(txid, chan.3.txid());
@@ -364,7 +363,6 @@ fn do_test_unconf_chan(reload_node: bool, reorg_after_reload: bool, use_funding_
 			assert_eq!(relevant_txids.len(), 1);
 			let block_hash_opt = relevant_txids[0].1;
-			assert!(block_hash_opt.is_some());
 			let expected_hash = nodes[0].get_block_header(chan_conf_height).block_hash();
-			assert_eq!(block_hash_opt.unwrap(), expected_hash);
+			assert_eq!(block_hash_opt, Some(expected_hash));
 			let txid = relevant_txids[0].0;
 			assert_eq!(txid, chan.3.txid());

lightning/src/chain/mod.rs Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
@TheBlueMatt
Copy link
Collaborator

FWIW, in the future, the update_claims_view split could be in a separate commit. Not worth trying to split it now that its done, though.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from 1a80eac to ee34469 Compare November 8, 2022 19:28
@tnull
Copy link
Contributor Author

tnull commented Nov 8, 2022

FWIW, in the future, the update_claims_view split could be in a separate commit. Not worth trying to split it now that its done, though.

Alright, split it out again.

@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from ee34469 to 2084f11 Compare November 8, 2022 20:46
TheBlueMatt
TheBlueMatt previously approved these changes Nov 8, 2022
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Two doc nits, feel free to ignore unless/until you have more review comments from others.

lightning/src/chain/mod.rs Outdated Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
jkczyz
jkczyz previously approved these changes Nov 8, 2022
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
lightning/src/chain/onchaintx.rs Outdated Show resolved Hide resolved
@tnull tnull dismissed stale reviews from jkczyz and TheBlueMatt via de9cd52 November 9, 2022 08:27
@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from 2084f11 to de9cd52 Compare November 9, 2022 08:27
Previously, `Confirm::get_relevant_txids()` only returned a list of
transactions that have to be monitored for reorganization out of the
chain. This interface however required double bookkeeping: while we
internally keep track of the best block, height, etc, it would also
require the user to keep track which transaction was previously
confirmed in which block and to take actions based on any change, e.g,
to reconfirm them when the block would be reorged-out and the
transactions had been reconfirmed in another block.

Here, we track the confirmation block hash internally and return it via
`Confirm::get_relevant_txids()` to the user, which alleviates the
requirement for double bookkeeping: the user can now simply check
whether the given transaction is still confirmed and in the given block,
and take action if not.

We also split `update_claims_view`: Previously it was one, now it's two
methods: `update_claims_view_from_matched_txn` and
`update_claims_view_from_requests`.
@tnull tnull force-pushed the 2022-10-track-confirmation-block-hash branch from de9cd52 to 9685d6c Compare November 9, 2022 10:13
@tnull
Copy link
Contributor Author

tnull commented Nov 9, 2022

Addressed nits and squashed again, CI should be happy now.

@jkczyz
Copy link
Contributor

jkczyz commented Nov 9, 2022

Addressed nits and squashed again, CI should be happy now.

Didn't we go with two commits earlier?

@TheBlueMatt
Copy link
Collaborator

Yea, we did split it before, but not sure its worth bothering to hold this up more for that - the total diff is only +129 −60 so its not like its big enough that its hard to read.

@TheBlueMatt TheBlueMatt merged commit b6fce3d into lightningdevkit:main Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants