Skip to content

Update the transaction builder to make change outputs explicit#658

Merged
str4d merged 6 commits into
zcash:mainfrom
nuttycom:wallet/builder_explicit_change
Nov 4, 2022
Merged

Update the transaction builder to make change outputs explicit#658
str4d merged 6 commits into
zcash:mainfrom
nuttycom:wallet/builder_explicit_change

Conversation

@nuttycom
Copy link
Copy Markdown
Collaborator

@nuttycom nuttycom commented Oct 2, 2022

This introduces a FeeStrategy abstraction (and FutureFeeStrategy for zfuture) that can be used to compute fees and required change during the process of transaction build. This also modifies TransactionBuilder to require the caller to construct a change output directly, instead of doing so implicitly.

@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch 2 times, most recently from 82ff5fb to ea95514 Compare October 2, 2022 21:39
@nuttycom nuttycom marked this pull request as ready for review October 2, 2022 21:40
@nuttycom nuttycom requested review from daira and str4d October 2, 2022 21:40
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from ea95514 to 1c12d71 Compare October 4, 2022 00:25
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 4, 2022

Codecov Report

Base: 73.66% // Head: 72.17% // Decreases project coverage by -1.49% ⚠️

Coverage data is based on head (9c894eb) compared to base (0da4d27).
Patch coverage: 64.42% of modified lines in pull request are covered.

❗ Current head 9c894eb differs from pull request most recent head 28db1e3. Consider uploading reports for the commit 28db1e3 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #658      +/-   ##
==========================================
- Coverage   73.66%   72.17%   -1.50%     
==========================================
  Files         104      110       +6     
  Lines       10157    11097     +940     
==========================================
+ Hits         7482     8009     +527     
- Misses       2675     3088     +413     
Impacted Files Coverage Δ
zcash_client_backend/src/data_api/error.rs 16.32% <0.00%> (-0.35%) ⬇️
zcash_extensions/src/transparent/demo.rs 76.62% <ø> (ø)
...h_primitives/src/transaction/components/sapling.rs 86.57% <ø> (ø)
...imitives/src/transaction/components/transparent.rs 90.24% <ø> (ø)
zcash_primitives/src/transaction/components/tze.rs 44.70% <ø> (ø)
..._primitives/src/transaction/components/tze/fees.rs 0.00% <0.00%> (ø)
zcash_primitives/src/transaction/mod.rs 78.46% <ø> (ø)
zcash_client_backend/src/data_api/wallet.rs 52.86% <47.05%> (-0.95%) ⬇️
...ves/src/transaction/components/transparent/fees.rs 50.00% <50.00%> (ø)
.../src/transaction/components/transparent/builder.rs 81.37% <60.00%> (+33.54%) ⬆️
... and 19 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.

@nuttycom nuttycom marked this pull request as draft October 4, 2022 02:31
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from 1c12d71 to e849928 Compare October 4, 2022 02:54
@r3ld3v r3ld3v requested a review from ebfull October 4, 2022 14:10
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from e849928 to f90c64d Compare October 12, 2022 23:20
Comment thread zcash_client_backend/src/data_api/wallet.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/components/transparent/builder.rs Outdated
Copy link
Copy Markdown
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

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

Looks good so far.

@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch 3 times, most recently from de7dde5 to f807eae Compare October 31, 2022 19:30
@nuttycom nuttycom changed the title Add an abstraction for fee calculation. Update the transaction builder to make change outputs explicit Oct 31, 2022
@nuttycom nuttycom marked this pull request as ready for review October 31, 2022 19:32
Copy link
Copy Markdown
Contributor

@daira daira left a comment

Choose a reason for hiding this comment

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

utACK modulo the comments from my previous review. In particular there are still some bare uses of DEFAULT_FEE that assume fees are fixed without going through FixedFeeStrategy.

@nuttycom
Copy link
Copy Markdown
Collaborator Author

@daira the remaining uses of DEFAULT_FEE will be addressed in #689, which builds atop this PR.

Copy link
Copy Markdown
Contributor

@str4d str4d left a comment

Choose a reason for hiding this comment

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

Partial review of 6280ddebc556ef06064e603c2f04b4e43d24bd73.

Comment thread zcash_client_backend/src/data_api/error.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_client_backend/src/data_api/error.rs Outdated
Comment thread zcash_primitives/src/transaction/components/sapling/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/components/tze/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Copy link
Copy Markdown
Contributor

@str4d str4d left a comment

Choose a reason for hiding this comment

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

Reviewed 6280ddebc556ef06064e603c2f04b4e43d24bd73.

Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
Comment thread zcash_primitives/src/transaction/builder.rs Outdated
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch 2 times, most recently from 3a88999 to a55743d Compare November 2, 2022 01:36
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from a55743d to 324e1d5 Compare November 2, 2022 02:12
@nuttycom nuttycom requested a review from str4d November 2, 2022 02:12
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch 3 times, most recently from b7fbe73 to 47b43dd Compare November 2, 2022 14:26
This adds a fee calculation strategy abstraction that can be used to
dynamically compute fees so that the total fees required may be taken
taken into account during note selection, and also removes automatic
change creation from the transaction builder.

Change outputs must now be directly created by the caller by the caller.
This is a necessary prerequisite for permitting fees to adjust based
upon the contents of the transaction being constructed.

The initial implementation of the fee strategy simply uses the current
default fee.
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from 47b43dd to 9496fc6 Compare November 2, 2022 14:28
@nuttycom
Copy link
Copy Markdown
Collaborator Author

nuttycom commented Nov 2, 2022

force-pushed to address review comments.

Copy link
Copy Markdown
Contributor

@str4d str4d left a comment

Choose a reason for hiding this comment

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

Reviewed 9496fc6.

Comment thread zcash_primitives/src/transaction/fees.rs
Comment thread zcash_primitives/CHANGELOG.md Outdated
Comment thread zcash_primitives/src/transaction/fees.rs Outdated
Comment thread zcash_primitives/src/transaction/fees.rs Outdated
Comment thread zcash_primitives/src/transaction/fees.rs Outdated
Comment thread zcash_client_backend/src/fees.rs Outdated
Comment on lines +26 to +32
/// The amount of change and fees required to make a transaction's inputs and
/// outputs balance under a specific fee rule, as computed by a particular
/// [`ChangeStrategy`] that is aware of that rule.
pub struct TransactionBalance {
proposed_change: Vec<ChangeValue>,
fee_required: Amount,
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like this approach; it's going to enable note splitting easily. I'm now wondering about note merging; ChangeStrategy assumes that all of the provided inputs will be part of the transaction (which makes sense given that it is focused on change strategy), so does this mean to have a variable number of inputs beyond what is necessary, we will need a third layer around ChangeStrategy and FeeRule? Or would it make more sense to do both note merging and note splitting in the same place? I guess the outer layer could equivalently implement that itself...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Your inference is correct: we do need a third layer! See #689

Comment on lines +374 to +380
.map_err(|e| match e {
ChangeError::InsufficientFunds {
available,
required,
} => Error::InsufficientBalance(available, required),
ChangeError::StrategyError(e) => Error::BalanceError(e),
})?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We do this match in two places, so we should add an impl From<ChangeError> for Error.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Error handling changes substantially in #689 so let's wait to alter this.

.add_sapling_output(
Some(dfvk.to_ovk(Scope::Internal)),
dfvk.change_address().1,
*amount,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does this need to be dereferenced?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

proposed_change returns a slice, and so it's borrowed here.

match change_value {
ChangeValue::Sapling(amount) => {
builder
.add_sapling_output(Some(ovk), shielding_address.clone(), *amount, memo.clone())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

See above; proposed_change returns a slice.

@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch 2 times, most recently from ae95f77 to 11889f7 Compare November 3, 2022 15:32
@nuttycom nuttycom force-pushed the wallet/builder_explicit_change branch from 11889f7 to c92d81b Compare November 3, 2022 15:57
@nuttycom
Copy link
Copy Markdown
Collaborator Author

nuttycom commented Nov 3, 2022

updated to address requested changes.

Copy link
Copy Markdown
Contributor

@str4d str4d left a comment

Choose a reason for hiding this comment

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

utACK 9c894eb modulo changelog fixes.

Comment thread zcash_primitives/CHANGELOG.md Outdated
Comment thread zcash_primitives/CHANGELOG.md Outdated
Comment thread zcash_primitives/CHANGELOG.md Outdated
Copy link
Copy Markdown
Contributor

@str4d str4d left a comment

Choose a reason for hiding this comment

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

Re-utACK 28db1e3

@str4d str4d merged commit d4f4f5a into zcash:main Nov 4, 2022
@nuttycom nuttycom deleted the wallet/builder_explicit_change branch November 4, 2022 00:43
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.

3 participants