4 [![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)](https://travis-ci.org/btcsuite/btcd)
5 [![ISC License](http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
6 [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](http://godoc.org/github.com/btcsuite/btcd/blockchain)
8 Package blockchain implements bitcoin block handling and chain selection rules.
9 The test coverage is currently only around 60%, but will be increasing over
10 time. See `test_coverage.txt` for the gocov coverage report. Alternatively, if
11 you are running a POSIX OS, you can run the `cov_report.sh` script for a
12 real-time report. Package blockchain is licensed under the liberal ISC license.
14 There is an associated blog post about the release of this package
15 [here](https://blog.conformal.com/btcchain-the-bitcoin-chain-package-from-bctd/).
17 This package has intentionally been designed so it can be used as a standalone
18 package for any projects needing to handle processing of blocks into the bitcoin
21 ## Installation and Updating
24 $ go get -u github.com/btcsuite/btcd/blockchain
27 ## Bitcoin Chain Processing Overview
29 Before a block is allowed into the block chain, it must go through an intensive
30 series of validation rules. The following list serves as a general outline of
31 those rules to provide some intuition into what is going on under the hood, but
32 is by no means exhaustive:
34 - Reject duplicate blocks
35 - Perform a series of sanity checks on the block and its transactions such as
36 verifying proof of work, timestamps, number and character of transactions,
37 transaction amounts, script complexity, and merkle root calculations
38 - Compare the block against predetermined checkpoints for expected timestamps
39 and difficulty based on elapsed time since the checkpoint
40 - Save the most recent orphan blocks for a limited time in case their parent
41 blocks become available
42 - Stop processing if the block is an orphan as the rest of the processing
43 depends on the block's position within the block chain
44 - Perform a series of more thorough checks that depend on the block's position
45 within the block chain such as verifying block difficulties adhere to
46 difficulty retarget rules, timestamps are after the median of the last
47 several blocks, all transactions are finalized, checkpoint blocks match, and
48 block versions are in line with the previous blocks
49 - Determine how the block fits into the chain and perform different actions
50 accordingly in order to ensure any side chains which have higher difficulty
51 than the main chain become the new main chain
52 - When a block is being connected to the main chain (either through
53 reorganization of a side chain to the main chain or just extending the
54 main chain), perform further checks on the block's transactions such as
55 verifying transaction duplicates, script complexity for the combination of
56 connected scripts, coinbase maturity, double spends, and connected
58 - Run the transaction scripts to verify the spender is allowed to spend the
60 - Insert the block into the block database
64 * [ProcessBlock Example](http://godoc.org/github.com/btcsuite/btcd/blockchain#example-BlockChain-ProcessBlock)
65 Demonstrates how to create a new chain instance and use ProcessBlock to
66 attempt to attempt add a block to the chain. This example intentionally
67 attempts to insert a duplicate genesis block to illustrate how an invalid
70 * [CompactToBig Example](http://godoc.org/github.com/btcsuite/btcd/blockchain#example-CompactToBig)
71 Demonstrates how to convert the compact "bits" in a block header which
72 represent the target difficulty to a big integer and display it using the
75 * [BigToCompact Example](http://godoc.org/github.com/btcsuite/btcd/blockchain#example-BigToCompact)
76 Demonstrates how to convert how to convert a target difficulty into the
77 compact "bits" in a block header which represent that target difficulty.
79 ## GPG Verification Key
81 All official release tags are signed by Conformal so users can ensure the code
82 has not been tampered with and is coming from the btcsuite developers. To
83 verify the signature perform the following:
85 - Download the public key from the Conformal website at
86 https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
88 - Import the public key into your GPG keyring:
90 gpg --import GIT-GPG-KEY-conformal.txt
93 - Verify the release tag with the following command where `TAG_NAME` is a
94 placeholder for the specific tag:
102 Package blockchain is licensed under the [copyfree](http://copyfree.org) ISC