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

Stop double-spends by checking nullifiers and UTXO spends in each non-finalized chain #2231

Closed
5 tasks
teor2345 opened this issue Jun 1, 2021 · 2 comments · Fixed by #2511
Closed
5 tasks
Assignees
Labels
A-consensus Area: Consensus rule updates A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement NU Sprout Network Upgrade: Sprout specific tasks (before Overwinter) NU-1 Sapling Network Upgrade: Sapling specific tasks NU-5 Network Upgrade: NU5 specific tasks

Comments

@teor2345
Copy link
Contributor

teor2345 commented Jun 1, 2021

Motivation

Zebra needs to check the double-spend consensus rule for shielded and transparent inputs, in each non-finalized chain.

Consensus Rules

Nullifiers

A nullifier MUST NOT repeat either within a transaction, or across transactions in a valid blockchain. Sprout and Sapling and Orchard nullifiers are considered disjoint, even if they have the same bit pattern.

https://zips.z.cash/protocol/nu5.pdf#nullifierset

A transaction is not valid if it would have added a nullifier to the nullifier set that already exists in the set

https://zips.z.cash/protocol/nu5.pdf#commitmentsandnullifiers

A note can change from being unspent to spent as a node’s view of the best valid block chain is extended by new transactions. Also, block chain reorganizations can cause a node to switch to a different best valid block chain that does not contain the transaction in which a note was output

https://zips.z.cash/protocol/nu5.pdf#decryptivk

Note: reorganizations can also change the best chain to one where a note was spent

UTXO spends

Validation state associated with transparent inputs and outputs, such as the UTXO (Unspent Transaction Output) set, is not described in this document; it is used in essentially the same way as in Bitcoin.

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

each output of a particular transaction can only be used as an input once in the block chain.
Any subsequent reference is a forbidden double spend - an attempt to spend the same satoshis twice.

https://developer.bitcoin.org/devguide/block_chain.html#introduction

Any input within this block can spend an output which also appears in this block (assuming the spend is otherwise valid).
However, the TXID corresponding to the output must be placed at some point before the TXID corresponding to the input.
This ensures that any program parsing block chain transactions linearly will encounter each output before it is used as an input.

https://developer.bitcoin.org/reference/block_chain.html#merkle-trees

Existing Design

pub fn push(&mut self, block: Arc<Block>)
...
Add nullifiers to the appropriate self.<version>_nullifiers

https://github.com/ZcashFoundation/zebra/blob/main/book/src/dev/rfcs/0005-state-updates.md#pub-fn-pushmut-self-block-arcblock

Solution

Design Change

  • Inserts into Chain.<version>_nullifiers should return an error if the key already exists
  • Before adding a block to a non-finalized chain, check that its nullifiers are not in the finalized state

This design change enforces the double-spend rules for each shielded pool.

  • Check transparent spends against previous transactions in the block, the relevant non-finalized chain, and the finalized state

Testing

  • Test that duplicate sprout, sapling, and orchard nullifiers are rejected by the non-finalized state
  • Test that duplicate transparent UTXOs are rejected by the non-finalized state

Alternatives

This is a required consensus rule. There aren't really any alternatives to returning an error here.

@teor2345 teor2345 added A-consensus Area: Consensus rule updates NU-1 Sapling Network Upgrade: Sapling specific tasks NU Sprout Network Upgrade: Sprout specific tasks (before Overwinter) A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement S-needs-triage Status: A bug report needs triage NU-5 Network Upgrade: NU5 specific tasks P-Medium labels Jun 1, 2021
@mpguerra mpguerra mentioned this issue Jun 1, 2021
53 tasks
@mpguerra mpguerra removed the S-needs-triage Status: A bug report needs triage label Jun 4, 2021
@teor2345 teor2345 changed the title Stop double-spends by checking nullifiers in each non-finalized chain Stop double-spends by checking nullifiers and UTXOs in each non-finalized chain Jun 24, 2021
@teor2345 teor2345 changed the title Stop double-spends by checking nullifiers and UTXOs in each non-finalized chain Stop double-spends by checking nullifiers and UTXO spends in each non-finalized chain Jun 24, 2021
@teor2345
Copy link
Contributor Author

teor2345 commented Jun 29, 2021

This ticket is blocked by #2417, because they both modify similar code in the Chain struct and functions.

#2417 returns errors when blocks are added to a chain. But I'm not sure if this ticket will error during pre-commit or the actual commit. So there might also be a merge conflict there.

@teor2345
Copy link
Contributor Author

This got accidentally closed by #2504.

@teor2345 teor2345 reopened this Jul 21, 2021
@mpguerra mpguerra removed a link to a pull request Jul 21, 2021
3 tasks
mergify bot pushed a commit that referenced this issue 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
A-consensus Area: Consensus rule updates A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement NU Sprout Network Upgrade: Sprout specific tasks (before Overwinter) NU-1 Sapling Network Upgrade: Sapling specific tasks NU-5 Network Upgrade: NU5 specific tasks
Projects
None yet
2 participants