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

Make getnetworksolps & getnetworkhashps RPCs more efficient by caching difficulties and times #7403

Closed
Tracked by #7366
teor2345 opened this issue Aug 28, 2023 · 0 comments
Labels
A-compatibility Area: Compatibility with other nodes or wallets, or standard rules A-concurrency Area: Async code, needs extra work to make it work properly. A-rpc Area: Remote Procedure Call interfaces A-state Area: State / database changes C-bug Category: This is a bug C-enhancement Category: This is an improvement C-tech-debt Category: Code maintainability issues I-slow Problems with performance or responsiveness I-usability Zebra is hard to understand or use S-needs-triage Status: A bug report needs triage

Comments

@teor2345
Copy link
Contributor

teor2345 commented Aug 28, 2023

Motivation

In ticket #6688 we did some quick fixes to speed up the getnetworksolps & getnetworkhashps RPCs.

This ticket is for a long-term fix that is even faster, and supports quick work calculations over the full block range.

Specifications

For each block other than the genesis block , nTime MUST be strictly greater than the median-time-past of that block.
For each block at block height 2 or greater on Mainnet , or block height 653606 or greater on Testnet , nTime MUST be less than or equal to the median-time-past of that block plus 90 · 60 seconds.

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

Read the state upgrade instructions before starting this ticket:
https://github.com/ZcashFoundation/zebra/blob/main/book/src/dev/state-db-upgrades.md#upgrading-the-state-database

Complex Code or Requirements

This ticket requires a state upgrade:

  • store the cumulative work for each height in the finalized state (16 bytes per difficulty, should use a compact encoding)
  • store the block times for each height in the finalized state (4 bytes per time)

Then changes to the RPC calculations:

  • do the work calculations by subtracting the end height cumulative work from the start height cumulative work
  • find the minimum and maximum times by reading all the times from the index (this will be fast enough because reading all the block headers only takes a few minutes)
complex optimisation - don't do this

If that's not fast enough, we can:

  • read the first PoWMedianBlockSpan/2 blocks' times: this is always the minimum because the "strictly greater than the median-time-past" rule requires the time to increase when the median increases
  • reading the last PoWMedianBlockSpan/2 * 4500 blocks' times: this is always the maximum because the "less than or equal to the median-time past plus 60*90 seconds" rule limits the maximum to 4500 greater than the median, and the "strictly greater than the median-time-past" rule guarantees regular time increases, so the median
  • and height 0..=2 on mainnet or height 0..=653606 on testnet, if they are inside the range, because these rules don't apply to them,
  • or reading the full range if it is smaller than these ranges

Then carefully test they produce the same results as the full range over a large number of random ranges. (Using a proptest?)

Testing

The original tests should pass before and after this change, but we should extend their ranges.

We should manually check that the RPC is faster on large ranges, using the RPC test cases from PR #7647.

Related Work

@teor2345 teor2345 added C-bug Category: This is a bug C-enhancement Category: This is an improvement S-needs-triage Status: A bug report needs triage P-Low ❄️ I-slow Problems with performance or responsiveness I-usability Zebra is hard to understand or use A-rpc Area: Remote Procedure Call interfaces A-state Area: State / database changes A-concurrency Area: Async code, needs extra work to make it work properly. A-compatibility Area: Compatibility with other nodes or wallets, or standard rules C-tech-debt Category: Code maintainability issues labels Aug 28, 2023
@mpguerra mpguerra added this to Zebra Aug 28, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New in Zebra Aug 28, 2023
@teor2345 teor2345 changed the title Make getnetworksolps & getnetworkhashps RPCs support the full height range Make getnetworksolps & getnetworkhashps RPCs more efficient by caching difficulties and times Oct 22, 2023
@mpguerra mpguerra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2024
@github-project-automation github-project-automation bot moved this from New to Done in Zebra Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-compatibility Area: Compatibility with other nodes or wallets, or standard rules A-concurrency Area: Async code, needs extra work to make it work properly. A-rpc Area: Remote Procedure Call interfaces A-state Area: State / database changes C-bug Category: This is a bug C-enhancement Category: This is an improvement C-tech-debt Category: Code maintainability issues I-slow Problems with performance or responsiveness I-usability Zebra is hard to understand or use S-needs-triage Status: A bug report needs triage
Projects
Archived in project
Development

No branches or pull requests

2 participants