Skip to content

Bound incoming HTLC witnessScript to min/max limits #447

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

Merged

Conversation

ariard
Copy link

@ariard ariard commented Jan 14, 2020

Fix a crash where previously we weren't able to detect any accepted
HTLC if its witness-encoded cltv expiry was different from expected
ACCEPTED_HTLC_SCRIPT_WEIGHT. This should work for any cltv expiry
included between 0 and 16777216 on mainnet, testnet and regtest.

@@ -397,6 +396,20 @@ enum InputMaterial {
}
}

#[cfg(test)]
pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm, can we just drop these constants from channelmonitor and replace them with an fn weight_to_type(weight: usize) -> type enum.

Copy link
Author

Choose a reason for hiding this comment

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

You mean type_to_weight ? As these constants are used as type assertions based on a known weight

@TheBlueMatt
Copy link
Collaborator

TheBlueMatt commented Jan 14, 2020 via email

@ariard ariard force-pushed the 2020-01-fix-weight-computation branch from 0ae2166 to 0eb0621 Compare January 17, 2020 17:20
@ariard
Copy link
Author

ariard commented Jan 17, 2020

Updated at 0eb0621, introducing HTLCType::weight_to_htlctype

}

impl HTLCType {
pub(super) fn weight_to_htlctype(witness_script_len: usize) -> HTLCType {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should return an Option instead of having a NoneHTLC enum

Copy link
Author

@ariard ariard Jan 17, 2020

Choose a reason for hiding this comment

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

I disagree a bit here, I don't find it makes callsites clearer, like "else if (if let Some(htlc_type) = weight_to_htlctype(script.len()) { htlc_type == HTLCType::Offered } else { false })"

Copy link
Collaborator

Choose a reason for hiding this comment

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

You should be able to make the callsets else if weight_to_htlctype(script.len()) == Some(HTLCType::Offered), which reads fine to me?

Copy link
Author

Choose a reason for hiding this comment

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

My periodic reminder of Rust greatness.

}

impl HTLCType {
pub(super) fn weight_to_htlctype(witness_script_len: usize) -> HTLCType {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could use some at least basic docs, like "checks if a given tx weight is potentially is pre-signed HTLC transaction"

@ariard ariard force-pushed the 2020-01-fix-weight-computation branch from 0eb0621 to 8c17afd Compare January 17, 2020 21:06
@ariard
Copy link
Author

ariard commented Jan 17, 2020

Updated at 8c17afd, including #446

else if HTLCType::weight_to_htlctype(inp.witness.last().unwrap().len()) == Some(HTLCType::AcceptedHTLC) { write!(f, "timeout-")?; break }
}
}
write!(f, " tx")?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: awkward space here.

Copy link
Author

Choose a reason for hiding this comment

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

Hmm it was intentional at first, but I'm fine too with "preimage-timeout-tx". Corrected

The logger which decides what to refer to an on-chain claim tx was
assuming that all inputs would have a witness. While this was fine
for the one-input case, it broke the fuzzer which was connecting a
consensus-invalid transaction. Further, in the case we have multiple
inputs, some may not have a witness, which we shouldn't crash on.

This fixes 9df0250.
@ariard ariard force-pushed the 2020-01-fix-weight-computation branch from 8c17afd to c96653b Compare January 17, 2020 21:28

impl HTLCType {
/// Check if a given tx witnessScript weight matchs one of a pre-signed HTLC
pub(crate) fn weight_to_htlctype(witness_script_len: usize) -> Option<HTLCType> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wait, why is this called "weight", its redeemScript length.

Copy link
Author

Choose a reason for hiding this comment

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

The spec refers to weight (BOLT3), I always use it at so but yes you're right it's false. Keep witnessScript tho

}

impl HTLCType {
/// Check if a given tx witnessScript weight matchs one of a pre-signed HTLC
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

Fix a crash where previously we weren't able to detect any accepted
HTLC if its witness-encoded cltv expiry was different from expected
ACCEPTED_HTLC_SCRIPT_WEIGHT. This should work for any cltv expiry
included between 0 and 16777216 on mainnet, testnet and regtest.
@ariard ariard force-pushed the 2020-01-fix-weight-computation branch from c96653b to fbc7885 Compare January 17, 2020 21:39
@TheBlueMatt TheBlueMatt merged commit 0b5b282 into lightningdevkit:master Jan 17, 2020
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.

2 participants