8000 Implement PartiallySignedTransaction::fee by hashmap · Pull Request #1338 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 1 commit into from
Oct 24, 2022

Conversation

hashmap
Copy link
Contributor
@hashmap hashmap commented Oct 22, 2022

to calculate fee if previous outputs are available.
Closes #1220

@hashmap hashmap force-pushed the issue-1220-psbt-fee branch 2 times, most recently from 9514408 to 67a900a Compare October 22, 2022 19:20
apoelstra
apoelstra previously approved these changes Oct 23, 2022
Copy link
Member
@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 67a900a8d5c6e3e6095cfa87f542bb6c37e6c07c

Copy link
Collaborator
@Kixunil Kixunil left a 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.

@@ -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"),
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, done

let mut inputs = 0;
for utxo in self.iter_funding_utxos() {
inputs += match utxo {
Err(_) => return Err(Error::MissingUtxo),
Copy link
Collaborator

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 ? ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

pub fn fee(&self) -> Result<u64, Error> {
let mut inputs = 0;
for utxo in self.iter_funding_utxos() {
inputs += match utxo {
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Ok(utxo) => utxo.value,
}
}
let outputs:u64 = self.unsigned_tx.output.iter().map(|out| out.value).sum();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again overflow not handled.

Copy link
Contributor Author

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());

}

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@hashmap hashmap force-pushed the issue-1220-psbt-fee branch 3 times, most recently from bb0979e to cfa75a5 Compare October 23, 2022 17:59
tcharding
tcharding previously approved these changes Oct 23, 2022
Copy link
Member
@tcharding tcharding left a 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

Comment on lines 441 to 445
/// Calculates fee
///
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, updated

Comment on lines 445 to 448
/// 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.
Copy link
Member

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

Copy link
Contributor Author

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

@tcharding
Copy link
Member

Your CI fails are:

  • Remove clone when setting t4 in tests
  • Use max_value() instead of MAX because MAX is not available in Rust 1.41.1 (our current MSRV)

Kixunil
Kixunil previously approved these changes Oct 23, 2022
Copy link
Collaborator
@Kixunil Kixunil left a 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!

@hashmap hashmap dismissed stale reviews from Kixunil and tcharding via 06c5e2a October 24, 2022 08:49
@hashmap hashmap force-pushed the issue-1220-psbt-fee branch from cfa75a5 to 06c5e2a Compare October 24, 2022 08:49
@hashmap
Copy link
Contributor Author
hashmap commented Oct 24, 2022

Your CI fails are:

* Remove `clone` when setting `t4` in tests

* Use `max_value()` instead of `MAX` because `MAX` is not available in Rust 1.41.1 (our current MSRV)

Thanks, fixed. Unfortunately aarch64-apple-darwin binaries are available starting from Rust 1.49, will see how to get around it.

@Kixunil
Copy link
Collaborator
Kixunil commented Oct 24, 2022

Unfortunately aarch64-apple-darwin binaries are available starting from Rust 1.49

I assume you're saying you use M1, I think x68_64 binaries should still work even though they would be slightly less efficient.

/// - [`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> {
Copy link
Collaborator
@Kixunil Kixunil Oct 24, 2022

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.

Copy link
Contributor Author

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?)

Copy link
Collaborator

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.)

@hashmap
Copy link
Contributor Author
hashmap commented Oct 24, 2022

@Kixunil sorry, I should have been be more specific. On M1 I see

% RUSTUP_TOOLCHAIN=1.41.1 ./contrib/test.sh
+ CRATES=bitcoin
+ for crate in '${CRATES}'
+ cd bitcoin
+ ./contrib/test.sh
+ FEATURES='base64 bitcoinconsensus serde rand secp-recovery'
+ '[' '' = true ']'
+ cargo --version
info: syncing channel updates for '1.41.1-aarch64-apple-darwin'
info: latest update on 2020-02-27, rust version 1.41.1 (f3e1a954d 2020-02-24)
error: target 'aarch64-apple-darwin' not found in channel.  Perhaps check https://doc.rust-lang.org/nightly/rustc/platform-support.html for available targets

I'll try to fix it later on.

to calculate fee if previous outputs are available.
@hashmap hashmap force-pushed the issue-1220-psbt-fee branch from 06c5e2a to c34d5f8 Compare October 24, 2022 12:40
@Kixunil
Copy link
Collaborator
Kixunil commented Oct 24, 2022

I think this could work: rustup toolchain install 1.41.1-x86_64-apple-darwin
You probably need to run it with cargo +1.41.1 test --target x86_64-apple-darwin though, not sure.

Copy link
Collaborator
@Kixunil Kixunil left a 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

Copy link
Member
@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK c34d5f8

Copy link
Member
@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK c34d5f8

@apoelstra apoelstra merged commit 29d3bc0 into rust-bitcoin:master Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Method to calculate PartiallySignedTransaction fee
4 participants
0