dcrd/blockchain
Dave Collins 5a3f08293b
blockchain: Optimize old block ver upgrade checks.
Currently the code that rejects old block versions once the majority of
the network has upgraded is performed using a loop that iterates
backwards from latest enforced block version while checking for a
majority upgrade at each version.  This is inefficient for blocks early
in the chain since it means multiple versions need to be checked when it
isn't really necessary.

This optimizes the relevant code by taking into account that the
relevant enforcement semantics reduce to the following:

- Block versions greater than or equal to the latest enforced block
  version can never be rejected for being old
- Block versions must be rejected when the majority of the network has
  upgraded to ANY version greater than that version

The following timing information obtained from profiling shows the
overall reduction achieved with the optimized code for mainnet:

 existing: Total time to check old versions for 645603 headers: 10.61118s
optimized: Total time to check old versions for 645603 headers:  3.52555s
2022-04-06 12:48:47 -05:00
..
chaingen blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
fullblocktests blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
indexers indexers: fix indexer wait for sync. 2022-03-28 18:25:34 -05:00
internal build: update golangci-lint to v1.44.1 2022-02-18 12:48:40 -06:00
stake multi: Support module graph prune and lazy load. 2022-03-25 07:20:01 -05:00
standalone multi: Support module graph prune and lazy load. 2022-03-25 07:20:01 -05:00
testdata multi: Only use module-scoped data in tests. 2018-08-16 15:08:07 -05:00
agendas_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
bench_test.go blockchain: Use skip list for ancestor traversal. 2019-11-23 04:17:55 -06:00
blockindex_test.go blockchain: Add convenience ancestor of func. 2021-10-29 12:37:06 -05:00
blockindex.go blockchain: Use new ancestor of convenience func. 2021-10-29 12:37:24 -05:00
chain_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
chain.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
chainio_test.go blockchain: Use TempDir to create temp test dirs. 2022-03-17 20:17:26 -05:00
chainio.go chain: Revalidate blocks for new deployments. 2022-03-02 10:09:27 -06:00
chainquery.go multi: Allow discrete mining with invalidated tip. 2021-12-03 12:05:15 -06:00
chainview_test.go blockchain: removed unused params 2019-10-28 11:49:54 -05:00
chainview.go blockchain: Correct comment typos for find fork. 2021-11-24 23:53:41 -06:00
checkpoints.go blockchain: Set BFFastAdd based on assume valid. 2021-12-11 01:07:58 -06:00
common_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
compress_test.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
compress.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
difficulty_test.go blockchain: Remove unused error returns. 2020-12-07 18:32:27 -06:00
difficulty.go blockchain: Move diff retarget log to connect. 2021-10-19 20:07:30 -05:00
doc.go blockchain: Update README.md and doc.go. 2021-01-07 10:44:25 -06:00
error_test.go multi: avoid using subscriber lifecycle in catchup. 2021-10-12 18:52:50 -05:00
error.go blockchain: Fix several typos. 2021-09-16 11:10:08 -05:00
example_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
fullblocks_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
fullblocksstakeversion_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
go.mod multi: Support module graph prune and lazy load. 2022-03-25 07:20:01 -05:00
go.sum blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
headercmt_test.go multi: Implement header commitments and vote. 2019-10-12 12:06:02 -05:00
headercmt.go multi: Add isAutoRevocationsEnabled to CheckSSRtx. 2021-09-06 09:03:27 -05:00
log.go blockchain: Prepare v3.0.0. 2020-10-15 15:33:44 -05:00
mediantime_test.go blockchain: Consolidate tests into the main package. 2018-02-25 19:01:57 -06:00
mediantime.go multi: Correct typos. 2019-08-16 17:37:58 -05:00
notifications.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
process_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
process.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
prune.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
README.md multi: Update README.md files for go modules. 2021-01-22 14:39:23 -06:00
scriptval.go multi: DCP0009 Auto revocations consensus change. 2021-09-20 18:02:19 -05:00
sequencelock_test.go multi: Add isAutoRevocationsEnabled to CheckSSRtx. 2021-09-06 09:03:27 -05:00
sequencelock.go multi: Rework utxoset/view to use outpoints. 2021-01-14 17:25:06 -06:00
stakeext.go blockchain: Remove unused next lottery data func. 2021-10-30 13:30:58 -05:00
stakenode.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
stakeversion_test.go chaincfg: Start v3 module dev cycle. 2020-01-29 13:24:14 -06:00
stakeversion.go blockchain: Decouple processing and download logic. 2021-01-07 10:43:49 -06:00
subsidy.go multi: Start dcrutil v4 module dev cycle. 2020-12-16 16:30:24 -06:00
thresholdstate_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
thresholdstate.go chain: Add nextDeploymentVersion helper. 2022-03-02 10:09:27 -06:00
timesorter_test.go blockchain: Consolidate tests into the main package. 2018-02-25 19:01:57 -06:00
timesorter.go multi: Correct typos. 2019-08-16 17:37:58 -05:00
treasury_policy_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
treasury_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
treasury.go blockchain: Change tspend pass log level to debug. 2022-01-01 01:20:12 -06:00
upgrade_test.go blockchain: Prepare v4.0.0. 2021-12-17 13:37:03 -06:00
upgrade.go blockchain: Add ldbErr to error description. 2022-02-08 00:43:28 -06:00
utxobackend_test.go blockchain: Use TempDir to create temp test dirs. 2022-03-17 20:17:26 -05:00
utxobackend.go blockchain: Add ldbErr to error description. 2022-02-08 00:43:28 -06:00
utxobackenditerator.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxobackendtx.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxocache_test.go blockchain: Use TempDir to create temp test dirs. 2022-03-17 20:17:26 -05:00
utxocache.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxoentry_test.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxoentry.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxoio_test.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxoio.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
utxoviewpoint_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
utxoviewpoint.go blockchain: Wrap at max line length. 2021-12-08 18:25:38 -06:00
validate_test.go blockchain: Start v5 module dev cycle. 2022-03-24 12:44:05 -05:00
validate.go blockchain: Optimize old block ver upgrade checks. 2022-04-06 12:48:47 -05:00
votebits_test.go multi: Start blockchain/stake v4 module dev cycle. 2020-12-16 16:30:24 -06:00
votebits.go chaincfg: Start v3 module dev cycle. 2020-01-29 13:24:14 -06:00

blockchain

Build Status ISC License Doc

Package blockchain implements Decred block handling and chain selection rules.

The Decred block handling and chain selection rules are an integral, and quite likely the most important, part of Decred. At its core, Decred is a distributed consensus of which blocks are valid and which ones will comprise the main block chain (public ledger) that ultimately determines accepted transactions, so it is extremely important that fully validating nodes agree on all rules.

At a high level, this package provides support for inserting new blocks into the block chain according to the aforementioned rules. It includes functionality such as rejecting duplicate blocks, ensuring blocks and transactions follow all rules, and best chain selection along with reorganization.

Since this package does not deal with other Decred specifics such as network communication or wallets, it provides a notification system which gives the caller a high level of flexibility in how they want to react to certain events such as newly connected main chain blocks which might result in wallet updates.

A comprehensive suite of tests is provided to ensure proper functionality.

Decred Chain Processing Overview

Before a block is allowed into the block chain, it must go through an intensive series of validation rules. The following list serves as a general outline of those rules to provide some intuition into what is going on under the hood, but is by no means exhaustive:

  • Reject duplicate blocks
  • Perform a series of sanity checks on the block and its transactions such as verifying proof of work, timestamps, number and character of transactions, transaction amounts, script complexity, and merkle root calculations
  • Compare the block against predetermined checkpoints for expected timestamps and difficulty based on elapsed time since the checkpoint
  • Perform a series of more thorough checks that depend on the block's position within the block chain such as verifying block difficulties adhere to difficulty retarget rules, timestamps are after the median of the last several blocks, all transactions are finalized, checkpoint blocks match, and block versions are in line with the previous blocks
  • Determine how the block fits into the chain and perform different actions accordingly in order to ensure any side chains which have higher difficulty than the main chain become the new main chain
  • When a block is being connected to the main chain (either through reorganization of a side chain to the main chain or just extending the main chain), perform further checks on the block's transactions such as verifying transaction duplicates, script complexity for the combination of connected scripts, coinbase maturity, double spends, and connected transaction values
  • Run the transaction scripts to verify the spender is allowed to spend the coins
  • Insert the block into the block database

Processing Order

This package supports headers-first semantics such that block data can be processed out of order so long as the associated header is already known.

The headers themselves, however, must be processed in the correct order since headers that do not properly connect are rejected. In other words, orphan headers are not allowed.

The processing code always maintains the best chain as the branch tip that has the most cumulative proof of work, so it is important to keep that in mind when considering errors returned from processing blocks.

Notably, due to the ability to process blocks out of order, and the fact blocks can only be fully validated once all of their ancestors have the block data available, it is to be expected that no error is returned immediately for blocks that are valid enough to make it to the point they require the remaining ancestor block data to be fully validated even though they might ultimately end up failing validation. Similarly, because the data for a block becoming available makes any of its direct descendants that already have their data available eligible for validation, an error being returned does not necessarily mean the block being processed is the one that failed validation.

Installation and Updating

This package is part of the github.com/decred/dcrd/blockchain/v3 module. Use the standard go tooling for working with modules to incorporate it.

Examples

  • ProcessBlock Example Demonstrates how to create a new chain instance and use ProcessBlock to attempt to add a block to the chain. This example intentionally attempts to insert a duplicate genesis block to illustrate how an invalid block is handled.

License

Package blockchain is licensed under the copyfree ISC License.