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

Inbound FindBlocks and FindHeaders #1347

Merged
merged 29 commits into from
Nov 30, 2020
Merged

Conversation

oxarbitrage
Copy link
Contributor

@oxarbitrage oxarbitrage commented Nov 20, 2020

Motivation

Respond to peer FindBlocks and FindHeaders messages.

Solution

  1. Create a state function that calculates the intersection with known_blocks
  2. Create a state function that collects a list of hashes from the intersection to stop, the best tip, or the maximum response length
  3. Answer peer FindBlocks and FindHeaders using various requests that call those functions

The code in this pull request has:

Review

@oxarbitrage and @teor2345 have written most of this code.
@yaahc, @oxarbitrage and @teor2345 are going to review it.

Related Issues

Closes #1078 - FindBlocks
Closes #1079 - FindHeaders
Closes #1306 - state requests

Follow Up Work

Write unit tests and property tests for collect_chain_hashes - #1399
The other functions are trivially simple.

zebra-state/src/response.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebrad/src/components/inbound.rs Outdated 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.

There's some good work here, but we need to be careful about protocol conformance.

I don't think the original ticket listed all the Zcash network protocol requirements, so there are some gaps in this PR.

Here are the requirements that aren't implemented in this PR:

  • return hashes from the best chain, including non-finalized blocks
  • start from the first matching hash, ignoring all other hashes
  • the list includes the stop hash
  • the minimum size of a protocol response is 1 hash

We should describe these protocol requirements in the function documentation.
There aren't any tests yet.

I feel like this code would have been easier to write and review if we agreed on a design first. I tried to do a quick design sketch in #1306, but this PR seems to do some things differently.

We should also try harder to match the existing Zebra network protocol data structures:
https://github.com/ZcashFoundation/zebra/blob/main/zebra-network/src/protocol/internal/request.rs#L81

zebra-state/src/request.rs Outdated Show resolved Hide resolved
zebra-state/src/request.rs Outdated Show resolved Hide resolved
zebra-state/src/request.rs Outdated Show resolved Hide resolved
zebra-state/src/response.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebrad/src/components/inbound.rs Outdated Show resolved Hide resolved
zebrad/src/components/inbound.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated 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.

We're still making changes to the specification, and we're still struggling to follow the specification exactly. I feel like we're in a bit of a cycle where we're using PR suggestions to converge on a design, rather than just doing that design.

So I think we need to do a quick design for the find_chain_hashes function. As part of that design, we should add some new functions to split up its tasks.

So let's create separate functions:

  • find_chain_hashes: take the arguments from the FindBlocks and FindHeaders requests, and call other functions
  • find_chain_intersection: find the intersection height between the local best chain and the peer's known_blocks
  • collect_chain_hashes: create the list of hashes
    • starting the list
      • at the block after the intersection height
      • if there is no intersection, at the genesis block
    • stopping the list:
      • after we reach the stop hash
      • after we reach the max_len supplied to the function (FindBlocks: 500, FindHeaders: 160), or
      • after we reach the best tip

We should specify function names, arguments, return types, and documentation comments. The documentation comment should describe what the function does, how each argument is used, and what the return value means.

For an example, see the median time functions design:
https://github.com/ZcashFoundation/zebra/pull/1246/files#diff-3e636ad0596bc279407be7ac69096f4bea1da79cd5aa0ac76b7c4ef3f8b64ad9R442

@teor2345
Copy link
Contributor

@oxarbitrage it's the end of my day now, so I can't really do much more on the design or PR review right now.

But I can make this my top priority tomorrow - which starts in about 16-18 hours from this message :-)

yaahc
yaahc previously approved these changes Nov 26, 2020
Copy link
Contributor

@yaahc yaahc 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, just one style tweek that I find easier to read

zebra-state/src/service.rs Outdated Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
Also add a `best_chain_contains` function.
@teor2345 teor2345 marked this pull request as ready for review November 26, 2020 06:57
teor2345
teor2345 previously approved these changes Nov 26, 2020
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 the changes I've made are complete, but I'd like @yaahc and @oxarbitrage to double-check before we merge.

@teor2345 teor2345 changed the title Inbound FindBlocks Inbound FindBlocks and FindHeaders Nov 26, 2020
@teor2345 teor2345 added A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement labels Nov 26, 2020
@teor2345 teor2345 added this to the First Alpha Release milestone Nov 26, 2020
Copy link
Contributor Author

@oxarbitrage oxarbitrage 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 this looks great, thank you for putting all the pieces together and getting it sorted out.

@teor2345
Copy link
Contributor

I think this looks great, thank you for putting all the pieces together and getting it sorted out.

No worries!

Sorry we didn't have more time to work through the design and implementation changes together.

Copy link
Contributor

@yaahc yaahc 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, have a couple comments but I think this is more or less ready to merge

zebra-state/src/service.rs Show resolved Hide resolved
zebra-state/src/service.rs Outdated Show resolved Hide resolved
teor2345 and others added 2 commits November 30, 2020 10:24
@teor2345 teor2345 merged commit 4544463 into ZcashFoundation:main Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement
Projects
None yet
3 participants