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

fix(consensus): Check that Zebra's state contains the social consensus chain on startup #6163

Merged
merged 16 commits into from
Feb 21, 2023

Conversation

teor2345
Copy link
Contributor

@teor2345 teor2345 commented Feb 15, 2023

Motivation

Zebra does not check that the state has followed the correct chain, but this is a new consensus requirement on mainnet.

Closes #5912

Specifications

A network upgrade is settled on a given network when there is a social consensus that it has activated with a given activation block hash. A full validator that potentially risks Mainnet funds or displays Mainnet transaction information to a user MUST do so only for a block chain that includes the activation block of the most recent settled network upgrade

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

Complex Code or Requirements

This PR adds another short startup task using the existing concurrent task framework and event loop.

Solution

Unrelated cleanups:

Testing

New tests:

  • check that this code is active in the existing cached state tests
  • check that it fails when the checkpoints don't match the previous cached state (and succeeds when they do)

I also did full and partial syncs locally, and they passed. (I had some unrelated issues with syncing due to proof errors, likely caused by disk corruption.)

Changelog

Added security and deprecation notes in this PR.

Review

This PR's code is ready for review. It is not on the critical path.

Reviewer Checklist

  • Will the PR name make sense to users?
    • Does it need extra CHANGELOG info? (new features, breaking changes, large changes)
  • Are the PR labels correct?
  • Does the code do what the ticket and PR says?
    • Does it change concurrent code, unsafe code, or consensus rules?
  • How do you know it works? Does it have tests?

Follow Up

@teor2345 teor2345 added C-bug Category: This is a bug A-consensus Area: Consensus rule updates P-Medium ⚡ C-security Category: Security issues I-consensus Zebra breaks a Zcash consensus rule I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data A-state Area: State / database changes I-lose-funds Zebra loses user funds A-concurrency Area: Async code, needs extra work to make it work properly. labels Feb 15, 2023
@teor2345 teor2345 self-assigned this Feb 15, 2023
@teor2345
Copy link
Contributor Author

This new task takes about one second on my machine, about the same time as validating the Zcash parameters:

2023-02-15T02:51:47.494988Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::commands::start: initializing verifiers
2023-02-15T02:51:47.494999Z  INFO {zebrad="f5dca6c" net="Main"}:crawl_and_dial{new_peer_interval=61s}: zebra_network::peer_set::initialize: starting the peer address crawler crawl_new_peer_interval=61s outbound_connections=18
2023-02-15T02:51:47.495024Z  INFO {zebrad="f5dca6c" net="Main"}:init{config=Config { checkpoint_sync: true, debug_skip_parameter_preload: false } network=Mainnet debug_skip_parameter_preload=false}: zebra_consensus::chain: starting state checkpoint validation...
2023-02-15T02:51:47.495058Z  INFO {zebrad="f5dca6c" net="Main"}:init{config=Config { checkpoint_sync: true, debug_skip_parameter_preload: false } network=Mainnet debug_skip_parameter_preload=false}: zebra_consensus::primitives::groth16::params: checking and loading Zcash Sapling and Sprout parameters
2023-02-15T02:51:47.498429Z  INFO {zebrad="f5dca6c" net="Main"}:init{config=Config { checkpoint_sync: true, debug_skip_parameter_preload: false } network=Mainnet debug_skip_parameter_preload=false}: zebra_consensus::chain: initializing chain verifier tip=Some((Height(1984325), block::Hash("00000000011c7c1efffc288d30fae56d9f3a82ad62c662e81c63c3c1984d755d"))) max_checkpoint_height=Height(1965904)
2023-02-15T02:51:47.498444Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::commands::start: initializing syncer
2023-02-15T02:51:47.498459Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::commands::start: initializing mempool
2023-02-15T02:51:47.498470Z  INFO {zebrad="f5dca6c" net="Main"}: zebra_rpc::server: Trying to open RPC endpoint at 0.0.0.0:28232...
2023-02-15T02:51:47.515981Z  INFO {zebrad="f5dca6c" net="Main"}: zebra_rpc::server: Opened RPC endpoint at 0.0.0.0:28232
2023-02-15T02:51:47.516147Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::commands::start: spawned initial Zebra tasks
2023-02-15T02:51:47.516252Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::components::mempool::crawler: initializing mempool crawler task
2023-02-15T02:51:47.516252Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::components::sync::gossip: initializing block gossip task
2023-02-15T02:51:47.516269Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::components::mempool::gossip: initializing transaction gossip task
2023-02-15T02:51:47.516268Z  INFO {zebrad="f5dca6c" net="Main"}: zebra_state::config: finished old database version cleanup task
2023-02-15T02:51:47.516272Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::components::mempool::queue_checker::initializing mempool queue checker task
2023-02-15T02:51:47.519140Z  INFO {zebrad="f5dca6c" net="Main"}:sync:try_to_sync: zebrad::components::sync::starting sync, obtaining new tips state_tip=Some(Height(1984325))
2023-02-15T02:51:47.520915Z  INFO {zebrad="f5dca6c" net="Main"}: zebrad::components::sync::progress: estimated progress to chain tip sync_percent=99.995% current_height=Height(1984325) network_upgrade=Nu5 remaining_sync_blocks=109 time_since_last_state_block=0s
2023-02-15T02:51:47.973306Z  INFO {zebrad="f5dca6c" net="Main"}:init{config=Config { checkpoint_sync: true, debug_skip_parameter_preload: false } network=Mainnet debug_skip_parameter_preload=false}: zebra_consensus::chain: finished state checkpoint validation
2023-02-15T02:51:48.466416Z  INFO {zebrad="f5dca6c" net="Main"}:init{config=Config { checkpoint_sync: true, debug_skip_parameter_preload: false } network=Mainnet debug_skip_parameter_preload=false}: zebra_consensus::primitives::groth16::params: Zcash Sapling and Sprout parameters downloaded and/or verified

@teor2345

This comment was marked as outdated.

@teor2345

This comment was marked as outdated.

@codecov
Copy link

codecov bot commented Feb 15, 2023

Codecov Report

Merging #6163 (eb59780) into main (cbc4b44) will increase coverage by 0.10%.
The diff coverage is 50.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6163      +/-   ##
==========================================
+ Coverage   77.96%   78.07%   +0.10%     
==========================================
  Files         304      304              
  Lines       39197    39251      +54     
==========================================
+ Hits        30560    30644      +84     
+ Misses       8637     8607      -30     

Copy link
Contributor

@arya2 arya2 left a comment

Choose a reason for hiding this comment

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

This is looking good!

zebra-consensus/src/chain.rs Outdated Show resolved Hide resolved
zebra-consensus/src/config.rs Outdated Show resolved Hide resolved
zebra-consensus/src/chain.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Feb 16, 2023
@teor2345
Copy link
Contributor Author

Coverage failed downloading Zcash parameters:

thread 'main' panicked at 'error downloading Sapling parameter files after 3 retries. IoError(Custom { kind: InvalidData, error: "sapling-spend.params failed validation:\nexpected: 8270785a1a0d0bc77196f000ee6d221c9c9894f55307bd9357c3f0105d31ca63991ab91324160d8f53e2bbd3c2633a6eb8bdf5205d822e7f3f73edac51b2b70c hashing 47958396 bytes,\nactual: 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce hashing 0 bytes from "https://download.z.cash/downloads/sapling-spend.params.part.1 + [https://download.z.cash/downloads/sapling-spend.params.part.2](https://download.z.cash/downloads/sapling-spend.params.part.2/)"" })

https://github.com/ZcashFoundation/zebra/actions/runs/4192197818/jobs/7267473489#step:9:18

It's possible the download server is very slow right now.

@teor2345 teor2345 removed the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Feb 17, 2023
@teor2345 teor2345 changed the title fix(consensus): Validate state blocks against checkpoints on startup fix(consensus): Check that previous state followed the correct chain Feb 17, 2023
@teor2345 teor2345 requested review from arya2 and removed request for a team February 17, 2023 01:42
arya2
arya2 previously approved these changes Feb 17, 2023
Copy link
Contributor

@arya2 arya2 left a comment

Choose a reason for hiding this comment

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

There are "missing documentation for a struct field" warnings in the BackgroundTaskHandles, but this looks perfect otherwise!

zebrad/tests/common/sync.rs Show resolved Hide resolved
@teor2345
Copy link
Contributor Author

There are "missing documentation for a struct field" warnings in the BackgroundTaskHandles, but this looks perfect otherwise!

Oops, that should be fixed now!

arya2
arya2 previously approved these changes Feb 17, 2023
Copy link
Contributor

@arya2 arya2 left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you!

@teor2345 teor2345 changed the title fix(consensus): Check that previous state followed the correct chain fix(consensus): Check that Zebra's state contains the social consensus chain on startup Feb 17, 2023
@mpguerra
Copy link
Contributor

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Feb 17, 2023

refresh

✅ Pull request refreshed

@teor2345
Copy link
Contributor Author

@Mergifyio update

@mergify
Copy link
Contributor

mergify bot commented Feb 17, 2023

update

✅ Branch has been successfully updated

@teor2345
Copy link
Contributor Author

Failed due to bug #5940, that fix is in the main branch now.

@teor2345 teor2345 dismissed arya2’s stale review February 19, 2023 20:29

This approval should have been dismissed by the main branch update, this is a bug in GitHub's latest pusher setting.

@teor2345 teor2345 removed the C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG label Feb 20, 2023
@teor2345
Copy link
Contributor Author

@arya2 this just needs a re-approval after a main branch merge.

@teor2345
Copy link
Contributor Author

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Feb 21, 2023

refresh

✅ Pull request refreshed

mergify bot added a commit that referenced this pull request Feb 21, 2023
@teor2345
Copy link
Contributor Author

Unrelated failure in the getblocktemplate tests, I'll open another ticket.

@teor2345
Copy link
Contributor Author

@Mergifyio refresh

@mergify
Copy link
Contributor

mergify bot commented Feb 21, 2023

refresh

✅ Pull request refreshed

mergify bot added a commit that referenced this pull request Feb 21, 2023
@mergify mergify bot merged commit 4daedbc into main Feb 21, 2023
@mergify mergify bot deleted the startup-validate-checkpoints branch February 21, 2023 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: Async code, needs extra work to make it work properly. A-consensus Area: Consensus rule updates A-state Area: State / database changes C-bug Category: This is a bug C-security Category: Security issues I-consensus Zebra breaks a Zcash consensus rule I-invalid-data Zebra relies on invalid or untrusted data, or sends invalid data I-lose-funds Zebra loses user funds
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On startup, check that the finalized state blocks match the checkpoint hashes
3 participants