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

Validate miner transaction fees #3067

Merged
merged 15 commits into from
Nov 24, 2021
Merged

Conversation

oxarbitrage
Copy link
Contributor

@oxarbitrage oxarbitrage commented Nov 17, 2021

Motivation

We need to validate the last (i think) consensus rule for the coinbase subsidy which is directly related with the miner collected fees.

Close #1162

Specifications

Consensus rule: The total value in zatoshi of transparent outputs from a coinbase transaction, minus vbalanceSapling, minus vbalanceOrchard, MUST NOT be greater than the value in zatoshi of block subsidy plus the transaction fees paid by transactions in this block.

https://zips.z.cash/protocol/protocol.pdf#txnencodingandconsensus

Solution

This is the reasoning i used, i may be wrong.

The rule is:

valueBalanceTransparent - valueBalanceSapling - valueBalanceOrchard <= blockSubsidy + fees

Lets call valueBalanceTransparent - valueBalanceSapling - valueBalanceOrchard just valueBalance, then we have:

valueBalance - blockSubsidy <= fees

valueBalance - blockSubsidy - fees <= 0

valueBalance - blockSubsidy should be at least 0(outputs should at least cover block subsidy), so the fee should never be negative to hold the inequality.

Review

Anyone who already worked in consensus rules before should be able to review.

Reviewer Checklist

  • Code implements Specs and Designs
  • Tests for Expected Behaviour
  • Tests for Errors

jvff
jvff previously approved these changes Nov 18, 2021
Copy link
Contributor

@jvff jvff 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 to me overall. I added some comments, but they are all optional.

zebra-consensus/src/block/tests.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/tests.rs Outdated Show resolved Hide resolved
@oxarbitrage oxarbitrage requested a review from teor2345 November 18, 2021 23:12
@oxarbitrage
Copy link
Contributor Author

@teor2345 can you take a look at this one when you get some time? thanks :)

jvff
jvff previously approved these changes Nov 18, 2021
@oxarbitrage oxarbitrage marked this pull request as ready for review November 18, 2021 23:33
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

I think we need to validate the transaction fees.

Can we try validating the rule exactly as written?
If we change the equation, it's hard to get the overflow checks and field checks exactly right.

This is a merge blocker.

zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/check.rs Outdated Show resolved Hide resolved
@teor2345
Copy link
Contributor

Sorry for the long response, I'll try to summarise it here.

The consensus rule says that we need to verify the transaction fees:

Consensus rule: The total value in zatoshi of transparent outputs from a coinbase transaction, minus vbalanceSapling, minus vbalanceOrchard, MUST NOT be greater than the value in zatoshi of block subsidy plus the transaction fees paid by transactions in this block.

You can get the transaction fees from the transaction verifier response:

let response = result
.map_err(Into::into)
.map_err(VerifyBlockError::Transaction)?;

Here's the Response method that returns the transaction fee:

/// The miner fee for the transaction in this response.
///
/// Coinbase transactions do not have a miner fee.
pub fn miner_fee(&self) -> Option<Amount<NonNegative>> {

@teor2345
Copy link
Contributor

I opened PR #3093 to sum the miner fees from each transaction.

@mpguerra mpguerra linked an issue Nov 23, 2021 that may be closed by this pull request
9 tasks
teor2345
teor2345 previously approved these changes Nov 24, 2021
Copy link
Contributor

@teor2345 teor2345 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, I'm just going to delete a TODO, then merge.

zebra-consensus/src/block.rs Outdated Show resolved Hide resolved
zebra-consensus/src/block/tests.rs Show resolved Hide resolved
Copy link
Contributor

@teor2345 teor2345 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, thanks for all the tests!

@teor2345 teor2345 enabled auto-merge (squash) November 24, 2021 03:04
@teor2345 teor2345 merged commit a61eae0 into ZcashFoundation:main Nov 24, 2021
mpguerra added a commit that referenced this pull request May 19, 2023
mergify bot pushed a commit that referenced this pull request May 23, 2023
* ZIPs were updated to remove ambiguity, this was tracked in #1267.

* #2105 was fixed by #3039 and #2379 was closed by #3069

* #2230 was a duplicate of #2231 which was closed by #2511

* #3235 was obsoleted by #2156 which was fixed by #3505

* #1850 was fixed by #2944, #1851 was fixed by #2961 and #2902 was fixed by #2969

* We migrated to Rust 2021 edition in Jan 2022 with #3332

* #1631 was closed as not needed

* #338 was fixed by #3040 and #1162 was fixed by #3067

* #2079 was fixed by #2445

* #4794 was fixed by #6122

* #1678 stopped being an issue

* #3151 was fixed by #3934

* #3204 was closed as not needed

* #1213 was fixed by #4586

* #1774 was closed as not needed

* #4633 was closed as not needed

* Clarify behaviour of difficulty spacing

Co-authored-by: teor <[email protected]>

* Update comment to reflect implemented behaviour

Co-authored-by: teor <[email protected]>

* Update comment to reflect implemented behaviour when retrying block downloads

Co-authored-by: teor <[email protected]>

* Update `TODO` to remove closed issue and clarify when we might want to fix

Co-authored-by: teor <[email protected]>

* Update `TODO` to remove closed issue and clarify what we might want to change in future

Co-authored-by: teor <[email protected]>

* Clarify benefits of how we do block verification

Co-authored-by: teor <[email protected]>

* Fix rustfmt errors

---------

Co-authored-by: teor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants