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

feerate of unsigned transaction can be overestimated #1406

Closed
jp1ac4 opened this issue Oct 31, 2024 · 0 comments · Fixed by #1452
Closed

feerate of unsigned transaction can be overestimated #1406

jp1ac4 opened this issue Oct 31, 2024 · 0 comments · Fixed by #1452
Labels
Bug Something isn't working as expected

Comments

@jp1ac4
Copy link
Collaborator

jp1ac4 commented Oct 31, 2024

This is taken from #1371 (comment).

This issue is about how we estimate the feerate of an unsigned transaction after creation. It's different from #1132 and #1371, which are about how we set the fee when creating a transaction.

The issue concerns the following function:

pub fn unsigned_tx_max_vbytes(&self, tx: &bitcoin::Transaction, use_primary_path: bool) -> u64 {
let witness_factor: u64 = WITNESS_SCALE_FACTOR.try_into().unwrap();
let num_inputs: u64 = tx.input.len().try_into().unwrap();
let max_sat_weight: u64 = self.max_sat_weight(use_primary_path).try_into().unwrap();
// Add weights together before converting to vbytes to avoid rounding up multiple times.
let tx_wu = tx
.weight()
.to_wu()
.checked_add(max_sat_weight.checked_mul(num_inputs).unwrap())
.unwrap();
tx_wu
.checked_add(witness_factor.checked_sub(1).unwrap())
.unwrap()
.checked_div(witness_factor)
.unwrap()
}
}

It seems to estimate the weight to be 2 less than the true weight of the signed transaction. I think this is because we call tx.weight() where all inputs of tx have empty witnesses, and so the transaction is serialized as non-Segwit:
https://docs.rs/bitcoin/0.31.0/src/bitcoin/blockdata/transaction.rs.html#752-753
https://docs.rs/bitcoin/0.31.0/src/bitcoin/blockdata/transaction.rs.html#968-979

This function is used by the GUI to display the estimated feerate of an unsigned PSBT as well as by the daemon to perform sanity checks on a PSBT after creation. By underestimating the weight, we may overestimate the feerate.

The suggested fix is to use tx.weight() + 2 if tx has >= 1 input and all these inputs have empty witnesses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working as expected
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants