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

RFC: Contextual Difficulty #1246

Merged
merged 42 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e0e1459
State RFC: clean up trailing whitespace
teor2345 Nov 4, 2020
4d834e7
Difficulty Contextual RFC: Introduction
teor2345 Nov 2, 2020
b51ef91
Difficulty RFC: Add draft definitions
teor2345 Nov 2, 2020
f07c289
Difficulty RFC: Add relevant chain
teor2345 Nov 4, 2020
9af402d
Difficulty RFC: draft guide-level explanation
teor2345 Nov 4, 2020
b4f001d
WIP: Difficulty RFC: template with future work
teor2345 Nov 4, 2020
9ca30b8
Difficulty RFC: Fix formatting
teor2345 Nov 5, 2020
8983be3
Difficulty RFC: clarify relevant chain length assumption
teor2345 Nov 5, 2020
02dd6bc
Difficulty RFC: Split state service interface and contextual checks
teor2345 Nov 5, 2020
2476c0d
Difficulty RFC: Revised based on spec fixes
teor2345 Nov 12, 2020
e7665ce
Difficulty RFC: Explain how Zebra's calculations can match the spec
teor2345 Nov 12, 2020
ebc0fc2
Difficulty RFC: write most of the reference section
teor2345 Nov 12, 2020
eada42a
Difficulty RFC: Add a DifficultyAdjustmentContext struct
teor2345 Nov 12, 2020
22b3cdb
Difficulty RFC: add a memory usage note
teor2345 Nov 15, 2020
2eae0fd
Difficulty RFC: Improve difficulty adjustment explanation
teor2345 Nov 15, 2020
53b7a1c
Difficulty RFC: minor adjustments
teor2345 Nov 15, 2020
10b5ba2
Move the relevant chain impl from Difficulty to State RFC
teor2345 Nov 15, 2020
e94bea7
Rename DifficultyAdjustment to AdjustedDifficulty in the RFC
teor2345 Nov 15, 2020
123a3d1
Difficulty RFC: delete redundant initialiser
teor2345 Nov 15, 2020
f6106ec
Move more relevant chain from Difficulty to State RFC
teor2345 Nov 15, 2020
088df9b
Difficulty RFC: Summarise module structure in the one place
teor2345 Nov 15, 2020
6ebe996
Difficulty RFC: Create implementation notes subsections
teor2345 Nov 15, 2020
95d809d
Difficulty RFC: rename a check argument to expected_difficulty
teor2345 Nov 15, 2020
0de6cfc
Difficulty RFC: reword the incorrect ZIP note
teor2345 Nov 15, 2020
b368885
Difficulty RFC: fix a typo
teor2345 Nov 15, 2020
3224d2d
Difficulty RFC: adjusted to expected, fix arguments
teor2345 Nov 15, 2020
9079a5b
Difficulty RFC: move some TODOs to the tracking issue
teor2345 Nov 15, 2020
9a43c08
Difficulty RFC: cleanup the remaining sections
teor2345 Nov 15, 2020
8743861
Difficulty RFC: add consensus critical order of operations
teor2345 Nov 15, 2020
9a90a22
Difficulty RFC: Add missing brackets
teor2345 Nov 18, 2020
b6030bb
Difficulty RFC: Use the ValidateContextError type
teor2345 Nov 18, 2020
c7ed3d3
Difficulty RFC: Add missing commas
teor2345 Nov 18, 2020
630b0ca
Difficulty RFC: Add a comment on AdjustedDifficulty
teor2345 Nov 18, 2020
dd5829e
Difficulty RFC: Remove arguments from threshold_bits
teor2345 Nov 18, 2020
0175ec3
Difficulty RFC: capitalise Mainnet and Testnet
teor2345 Nov 18, 2020
205fa88
Difficulty RFC: make the median_time arg mut owned
teor2345 Nov 19, 2020
2980c20
Difficulty RFC: Replace array lengths with named constants
teor2345 Nov 19, 2020
8dcfdbf
Difficulty RFC: Return Duration from median_timespan_bounded
teor2345 Nov 19, 2020
bd1f330
Difficulty RFC: Add a median_timespan function
teor2345 Nov 19, 2020
8347e91
Difficulty RFC: fix a comment typo
teor2345 Nov 19, 2020
68535e3
Difficulty RFC: Move is_testnet_min_difficulty_block to NetworkUpgrade
teor2345 Nov 19, 2020
b6fa8bf
Merge branch 'main' into contextual-difficulty-rfc
teor2345 Nov 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions book/src/dev/rfcs/0005-state-updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ state service.

* **block chain**: A sequence of valid blocks linked by inclusion of the
previous block hash in the subsequent block. Chains are rooted at the
*genesis* block and extend to a *tip*.
genesis block and extend to a **tip**.

* **chain state**: The state of the ledger after application of a particular
sequence of blocks (state transitions).

* **cumulative difficulty**: The cumulative proof-of-work from genesis to the
chain tip.
* **block work**: The approximate amount of work required for a miner to generate
a block hash that passes the difficulty filter. The number of block header
attempts and the mining time are proportional to the work value. Numerically
higher work values represent longer processing times.

* **best chain**: The chain with the greatest cumulative difficulty. This chain
* **cumulative work**: The sum of the **block work** of all blocks in a chain, from
genesis to the chain tip.

* **best chain**: The chain with the greatest **cumulative work**. This chain
represents the consensus state of the Zcash network and transactions.

* **side chain**: A chain which is not contained in the best chain.
Expand Down Expand Up @@ -70,6 +75,11 @@ state service.
be lower during the initial sync, and after a chain reorganization, if the new
best chain is at a lower height.

* **relevant chain**: The relevant chain for a block starts at the previous
block, and extends back to genesis.

* **relevant tip**: The tip of the relevant chain.

# Guide-level explanation
[guide-level-explanation]: #guide-level-explanation

Expand Down Expand Up @@ -230,7 +240,6 @@ chains, so that the map ordering is the ordering of worst to best chains.
### The `Chain` type
[chain-type]: #chain-type


The `Chain` type represents a chain of blocks. Each block represents an
incremental state update, and the `Chain` type caches the cumulative state
update from its root to its tip.
Expand Down Expand Up @@ -706,6 +715,59 @@ These updates can be performed in a batch or without necessarily iterating
over all transactions, if the data is available by other means; they're
specified this way for clarity.

## Accessing previous blocks for contextual validation
[previous-block-context]: #previous-block-context

The state service performs contextual validation of blocks received via the
`CommitBlock` request. Since `CommitBlock` is synchronous, contextual validation
must also be performed synchronously.

The relevant chain for a block starts at its previous block, and follows the
chain of previous blocks back to the genesis block.

### Relevant chain iterator
[relevant-chain-iterator]: #relevant-chain-iterator

The relevant chain can be retrieved from the state service as follows:
* if the previous block is the finalized tip:
* get recent blocks from the finalized state
* if the previous block is in the non-finalized state:
* get recent blocks from the relevant chain, then
* get recent blocks from the finalized state, if required

The relevant chain can start at any non-finalized block, or at the finalized tip.

### Relevant chain implementation
[relevant-chain-implementation]: #relevant-chain-implementation

The relevant chain is implemented as a `StateService` iterator, which returns
`Arc<Block>`s.

The chain iterator implements `ExactSizeIterator`, so Zebra can efficiently
assert that the relevant chain contains enough blocks to perform each contextual
validation check.

```rust
impl StateService {
/// Return an iterator over the relevant chain of the block identified by
/// `hash`.
///
/// The block identified by `hash` is included in the chain of blocks yielded
/// by the iterator.
pub fn chain(&self, hash: block::Hash) -> Iter<'_> { ... }
}

impl Iterator for Iter<'_> {
type Item = Arc<Block>;
...
}
impl ExactSizeIterator for Iter<'_> { ... }
impl FusedIterator for Iter<'_> {}
```

For further details, see [PR 1271].

[PR 1271]: https://github.com/ZcashFoundation/zebra/pull/1271

## Request / Response API
[request-response]: #request-response
Expand Down
Loading