-
Notifications
You must be signed in to change notification settings - Fork 831
Implement PartiallySignedTransaction::fee #1338
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
Conversation
9514408
to
67a900a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 67a900a8d5c6e3e6095cfa87f542bb6c37e6c07c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Apart from missing overflow handling (which is a critical bug) it looks quite good.
bitcoin/src/util/psbt/error.rs
Outdated
@@ -101,6 +103,7 @@ impl fmt::Display for Error { | |||
}, | |||
Error::CombineInconsistentKeySources(ref s) => { write!(f, "combine conflict: {}", s) }, | |||
Error::ConsensusEncoding => f.write_str("bitcoin consensus or BIP-174 encoding error"), | |||
Error::NegativeFee => f.write_str("negative fee is not allowed"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I think more context like "the PSBT has a negative fee which is not allowed" would be easier to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, done
bitcoin/src/util/psbt/mod.rs
Outdated
let mut inputs = 0; | ||
for utxo in self.iter_funding_utxos() { | ||
inputs += match utxo { | ||
Err(_) => return Err(Error::MissingUtxo), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ignore the error and why not use ?
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
bitcoin/src/util/psbt/mod.rs
Outdated
pub fn fee(&self) -> Result<u64, Error> { | ||
let mut inputs = 0; | ||
for utxo in self.iter_funding_utxos() { | ||
inputs += match utxo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overflow is not handled. I suggest explicitly checking and returning Overflow
error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, added an error variant
bitcoin/src/util/psbt/mod.rs
Outdated
Ok(utxo) => utxo.value, | ||
} | ||
} | ||
let outputs:u64 = self.unsigned_tx.output.iter().map(|out| out.value).sum(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again overflow not handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
assert!(t3.fee().is_err()); | ||
|
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test for overflow case when you fix it. This is a very important one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
bb0979e
to
cfa75a5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! My review comments are just suggested improvements to docs, the logic of this PR looks good to me.
ACK cfa75a5109f19c2f02c62f1d9fcb589a91c6453d
bitcoin/src/util/psbt/mod.rs
Outdated
/// Calculates fee | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible docs improvement, suggestion only.
/// Calculates transaction fee.
///
/// 'Fee' being the amount that will be paid for mining a transaction with the current inputs
/// and outputs i.e., the difference in value of the total inputs and the total outputs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, updated
bitcoin/src/util/psbt/mod.rs
Outdated
/// The function returns [`Error::MissingUtxo`] when UTXO information for at least one input | ||
/// is not present or is invalid. | ||
/// If calculated value is negative [`Error::NegativeFee`] is returned. | ||
/// If an integer overflow happens [`Error::FeeOverflow`] is returned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be rendered in HTML as a single sentence, perhaps either put it as a single sentence in code
/// The function returns [`Error::MissingUtxo`] when UTXO information for at least one input is
/// not present or is invalid. If calculated value is negative [`Error::NegativeFee`] is
/// returned. If an integer overflow happens [`Error::FeeOverflow`] is returned.
or as a list if that's preferred
/// - [`Error::MissingUtxo`] when UTXO information for any input is not present or is invalid.
/// - [`Error::NegativeFee`] if calculated value is negative.
/// - [`Error::FeeOverflow`] if an integer overflow occurs.
In case its useful for you, here is a shell command to build the docs using our custom conditional configuration option docsrs
:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appreciate your detailed suggestion, updated
Your CI fails are:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK cfa75a5109f19c2f02c62f1d9fcb589a91c6453d
Thank you!
cfa75a5
to
06c5e2a
Compare
Thanks, fixed. Unfortunately |
I assume you're saying you use M1, I think x68_64 binaries should still work even though they would be slightly less efficient. |
bitcoin/src/util/psbt/mod.rs
Outdated
/// - [`Error::MissingUtxo`] when UTXO information for any input is not present or is invalid. | ||
/// - [`Error::NegativeFee`] if calculated value is negative. | ||
/// - [`Error::FeeOverflow`] if an integer overflow occurs. | ||
pub fn fee(&self) -> Result<u64, Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait, sorry for being late I only realized now. We should return Result<Amount, Error>
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, I left calculations using u64
because psbt values are u64
(why btw?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's correct.
why
An oversight in API design. We have an issue to change it but nobody did it yet. (We have a bunch of APIs we want to improve so if you're surprised next time it's probably the same reason.)
@Kixunil sorry, I should have been be more specific. On M1 I see
I'll try to fix it later on. |
to calculate fee if previous outputs are available.
06c5e2a
to
c34d5f8
Compare
I think this could work: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c34d5f8 if CI passes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c34d5f8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK c34d5f8
to calculate fee if previous outputs are available.
Closes #1220