Commit Graph

31 Commits

Author SHA1 Message Date
Dave Collins
e99b8f3810
release: Introduce blockchain v2 module. 2019-08-08 13:10:22 -05:00
Dave Collins
469f985f86
blockchain: Remove deprecated code.
This removes the deprecated functions along with the additional code and
tests which are no longer necessary as a result of removing the
functions.

The following is a list of exported types and functions removed along
with what they were replaced by when applicable:

- DisableLog -> UseLogger(slog.Disabled)
- BlockOneCoinbasePaysTokens -> no alternative, internal only
- IsCoinBase -> standalone.IsCoinBaseTx
- IsCoinBaseTx -> standalone.IsCoinBaseTx
- BuildMerkleTreeStore -> standalone.CalcTxTreeMerkleRoot
- BuildMsgTxMerkleTreeStore -> standalone.CalcTxTreeMerkleRoot
- SubsidyCache -> standalone.SubsidyCache
- NewSubsidyCache -> standalone.NewSubsidyCache
- CalcBlockWorkSubsidy -> standalone.SubsidyCache.CalcWorkSubsidy
- CalcStakeVoteSubsidy -> standalone.SubsidyCache.CalcStakeVoteSubsidy
- CalcBlockTaxSubsidy -> standalone.SubsidyCache.CalcTreasurySubsidy
- CoinbasePaysTax -> no alternative, internal only
- CalculateAddedSubsidy -> no alternative, internal only
- BestPrevHash -> BestSnapshot.PrevHash
- TotalSubsidy -> BestSnapshot.TotalSubsidy
- FetchSubsidyCache -> standalone.NewSubsidyCache passed to chain
- HashToBig -> standalone.HashToBig
- CompactToBig -> standalone.CompactToBig
- BigToCompact -> standalone.BigToCompact
- CalcWork -> standalone.CalcWork
- CheckProofOfWork -> standalone.CheckProofOfWork
2019-08-08 13:10:21 -05:00
Dave Collins
2dac209198
blockchain: Use lastest major version deps.
This udpates the blockchain module to use the lastest module major
versions.

While here, it also corrects a few typos and updates some test function
names to more accurately reflect their purpose.

The updated direct dependencies are as follows:

- github.com/decred/dcrd/blockchain/stake/v2@v2.0.1
- github.com/decred/dcrd/chaincfg/v2@v2.2.0
- github.com/decred/dcrd/database/v2@v2.0.0
- github.com/decred/dcrd/dcrutil/v2@v2.0.0
- github.com/decred/dcrd/txscript/v2@v2.0.0
2019-08-08 13:10:20 -05:00
Dave Collins
25deee6195
blockchain: Allow named blocks in chaingen harness.
This introduces two new functions on the chaingen harness, named
AcceptBlock and RejectBlock, which allow ensuring a named block is
accepted or rejected, respectively, and redefines the existing Accepted
and Rejected functions which only deal with the tip block in terms of
the new functions.

It also renames Accepted and Rejected to AcceptTipBlock and
RejectTipBlock, respectively.

The motivation for this change is to make it easier to test future code
which will allow processing of headers and blocks independently as well
as processing blocks out of order so long as their headers are already
known.
2019-03-28 16:55:57 -05:00
Dave Collins
10f23b0845
blockchain: Use harness in force head reorg tests.
This refactors the force head reorganization tests in blockchain to use
the recently introduced chaingen harness.
2019-01-29 13:26:48 -06:00
Dave Collins
6f9b6f1c6e
multi: Use regression test network in unit tests.
This modifies the majority of the tests that make use of chain
parameters and the RPC tests to use the resurrected regression test
network.

It also bumps the affected module versions as follows:

- github.com/decred/dcrd/txscript@v1.0.2
- github.com/decred/dcrd/blockchain/stake@v1.0.3
- github.com/decred/dcrd/mempool@v1.0.2

The blockchain and dcrutil modules are also affected, but since their
version has already been bumped since their last release tags, they are
not bumped again.
2018-10-09 19:40:10 -05:00
Dave Collins
e7495b5b38
blockchain: Use temp dirs for fullblocks test.
This modifies the fullblocktests that create a temporary database to
make use of ioutil.TempDir instead of a relative directory path.  This
allows the tests to be run from any path without failure.
2018-08-16 16:16:53 -05:00
Dave Collins
46b081c96d
blockchain: Optimize reorg to use known status.
This optimizes the chain reorganization logic by making use of the
block index status flags for known valid and invalid blocks.

In particular, validation is now skipped for blocks that are either
already known valid or invalid.  When validating blocks, the result is
stored into the block index for the block accordingly, and in the case
of blocks that fail validation, all of the descendants of the invalid
block are marked as having an invalid ancestor.

It also introduces a new error named ErrKnownInvalidBlock which is
returned in the case a forced reorg is attempted to an invalid block and
adds a test to ensure it works as intended.
2018-07-26 15:20:28 -05:00
Dave Collins
9ef7db9b23
blockchain: Refactor to use new chain view.
This refactors and simplifies the code in blockchain to use the new more
efficient chain views.

An overview of the logic changes are as follows:

- Remove inMainChain from block nodes since that can now be efficiently
  determined by using the chain view
- Track the best chain via a chain view instead of a single block node
  - Use the tip of the best chain view everywhere bestNode was used
  - Update chain view tip instead of updating best node
- Remove height map and associated lock in favor of chain view
  - Use chain view NodeByHeight everywhere height map was used
- Change reorg logic to use more efficient chain view fork finding logic
- Change block locator code over to use more efficient chain view logic
  - Remove now unused block-index-based block locator code
  - Move BlockLocator definition to chain.go
  - Move BlockLocatorFromHash and LatestBlockLocator to chain.go
    - Update both to use more efficient chain view logic
- Rework several functions to use chain view for main chain detection
  - fetchMainChainBlockByNode
  - BlockByHeight
  - MainChainHasBlock
  - findPreviousCheckpoint
  - IsCheckpointCandidate
2018-07-20 17:19:52 -05:00
Dave Collins
eec4f96b0d
blockchain: Add negative tests for forced reorg.
This adds tests which ensure invalid blocks are rejected when attempting
to force a reorganization to them.

It also creates the invalid blocks in such a way that some of them have
block index entries while others do not to ensure both scenarios are
handled correctly.
2018-07-09 23:47:26 -05:00
Dave Collins
4f6ed9afdb
multi: Refactor and optimize inv discovery.
This refactors the code that locates blocks (inventory discovery) out of
server and into blockchain where it can make use of the fact that all
block nodes are now in memory and more easily be tested.  As an aside,
it really belongs in blockchain anyways since it's purely dealing with
the block index and best chain.

In order to do this reasonably efficiently, a new memory-only height
to block node mapping for the main chain is introduced to allow
efficient forward traversal of the main chain.  This is ultimately
intended to be replaced by a chain view.

Since the network will be moving to header-based semantics, this also
provides an additional optimization to allow headers to be located
directly versus needing to first discover the hashes and then fetch the
headers.

The new functions are named LocateBlocks and LocateHeaders. The former
returns a slice of located hashes and the latter returns a slice of
located headers.

Finally, it also updates the RPC server getheaders call and related
plumbing to use the new LocateHeaders function.

A comprehensive suite of tests is provided to ensure both functions
behave correctly for both correct and incorrect block locators.
2018-06-01 13:20:04 -05:00
Dave Collins
66010e4134
multi: Return fork len from ProcessBlock.
This modifies the ProcessBlock function in the blockchain package to
return the fork length for the connected block and updates all callers
and tests accordingly.  Several of the internal functions which
ProcessBlock calls are also updated in order to bubble the necessary
information back up so it can be returned.  It does not make any
behavioral changes.

This is being done to better expose information about the position of
the block within the chain to callers without them having to make
additional queries.
2018-05-27 20:20:43 -05:00
Dave Collins
6327ed7fab
blockchain: CheckConnectBlockTemplate with tests.
This removes the CheckConnectBlock function in favor of a new function
named CheckConnectBlockTemplate which more accurately reflects its
purpose of testing block template proposals and uses this fact to be
more restrictive about the allowed inputs and to avoid performing the
proof of work check.

In particular, the provided block template must only build from the
current tip or its parent or an error is returned.

All mining code has been updated to call the new function accordingly.

Finally, it also adds a full suite of tests for the new function using
the chaingen framework in order to ensure proper functionality.
2018-05-03 13:53:24 -05:00
Dave Collins
8c69c29d69
blockchain: Consolidate tests into the main package.
Putting the test code in the same package makes it easier for forks
since they don't have to change the import paths as much and it also
gets rid of the need for internal_test.go to bridge.
2018-02-25 19:01:57 -06:00
Dave Collins
1492e551ca
blockchain: Use chaingen for forced reorg tests.
This replaces and improves the tests for the ForceHeadReorganization
function with new tests based on the dynamic chaingen infrastructure
versus uses static test data that gets out of date as data structures
change and is much more difficult to update.

It also removes the now unused static reorgto180.bz2 test data.
2018-02-23 21:01:02 -06:00
Josh Rickmar
6842aa006d Merge remaining dcrutil code into a dcrd package.
This merge commit adds the following code from the
github.com/decred/dcrutil package into a new
github.com/decred/dcrd/dcrutil package:

* Address handling
* Amount type
* AppDataDir func
* bitflags functions
* Block wrapper type
* Hash160 func
* Tx wrapper type
* WIF type

as well as all tests for this code.

The old github.com/decred/dcrutil/hdkeychain package has also been
merged and moved to github.com/decred/dcrd/dcrutil/hdkeychain.

dcrd packages have been updated to use the new packages and the dep
files have been updated for this change.
2017-10-11 22:06:36 -04:00
Dave Collins
df18b9ffc6
blockchain: Modify tests to use chaincfg params.
Rather than redefining the simnet parameters in the tests, just use the
existing chaincfg simnet parameters.  Since some of the tests rely on
the modified legacy version, introduce a clone function for the params
and simply override the parameters that are different.

This is far preferable to redefining them which is cumbersome to update
when the chaincfg params add new definitions.
2017-09-08 17:14:45 -05:00
Donald Adu-Poku
1c618ec5e1 multi: Replace DecodeNetworkAddress with DecodeAddress 2017-07-21 23:41:24 -05:00
David Hill
59db139d19 blockchain: check errors and remove ineffectual assignments. 2017-07-17 13:52:06 -05:00
David Hill
3428a4011f adjust for dcrutil Block changes. (#700) 2017-05-18 16:42:56 -04:00
Dave Collins
c162fbde71
multi: Upstream chainhash abstraction sync
Contains the following commits:

- 711f33450c
- b6b1e55d1e
  - Reverted because Travis is already at a more recent version
- bd4e64d1d4

Also, the merge commit contains the necessary decred-specific
alterations, converts all other references to sha to hash to keep with
the spirit of the merged commits, and various other cleanup intended to
bring the code bases more in line with one another.
2016-11-16 12:48:40 -06:00
Dave Collins
748ecb2a52
blockchain: Associate time src with chain instance.
Upstream commit 00ebb9d14d

Also, the merge commit contains the necessary decred-specific
alterations.
2016-11-11 18:06:09 -06:00
Dave Collins
b6d426241d blockchain: Rework to use new db interface.
This commit is the first stage of several that are planned to convert
the blockchain package into a concurrent safe package that will
ultimately allow support for multi-peer download and concurrent chain
processing.  The goal is to update btcd proper after each step so it can
take advantage of the enhancements as they are developed.

In addition to the aforementioned benefit, this staged approach has been
chosen since it is absolutely critical to maintain consensus.
Separating the changes into several stages makes it easier for reviewers
to logically follow what is happening and therefore helps prevent
consensus bugs.  Naturally there are significant automated tests to help
prevent consensus issues as well.

The main focus of this stage is to convert the blockchain package to use
the new database interface and implement the chain-related functionality
which it no longer handles.  It also aims to improve efficiency in
various areas by making use of the new database and chain capabilities.

The following is an overview of the chain changes:

- Update to use the new database interface
- Add chain-related functionality that the old database used to handle
  - Main chain structure and state
  - Transaction spend tracking
- Implement a new pruned unspent transaction output (utxo) set
  - Provides efficient direct access to the unspent transaction outputs
  - Uses a domain specific compression algorithm that understands the
    standard transaction scripts in order to significantly compress them
  - Removes reliance on the transaction index and paves the way toward
    eventually enabling block pruning
- Modify the New function to accept a Config struct instead of
  inidividual parameters
- Replace the old TxStore type with a new UtxoViewpoint type that makes
  use of the new pruned utxo set
- Convert code to treat the new UtxoViewpoint as a rolling view that is
  used between connects and disconnects to improve efficiency
- Make best chain state always set when the chain instance is created
  - Remove now unnecessary logic for dealing with unset best state
- Make all exported functions concurrent safe
  - Currently using a single chain state lock as it provides a straight
    forward and easy to review path forward however this can be improved
    with more fine grained locking
- Optimize various cases where full blocks were being loaded when only
  the header is needed to help reduce the I/O load
- Add the ability for callers to get a snapshot of the current best
  chain stats in a concurrent safe fashion
  - Does not block callers while new blocks are being processed
- Make error messages that reference transaction outputs consistently
  use <transaction hash>:<output index>
- Introduce a new AssertError type an convert internal consistency
  checks to use it
- Update tests and examples to reflect the changes
- Add a full suite of tests to ensure correct functionality of the new
  code

The following is an overview of the btcd changes:

- Update to use the new database and chain interfaces
- Temporarily remove all code related to the transaction index
- Temporarily remove all code related to the address index
- Convert all code that uses transaction stores to use the new utxo
  view
- Rework several calls that required the block manager for safe
  concurrency to use the chain package directly now that it is
  concurrent safe
- Change all calls to obtain the best hash to use the new best state
  snapshot capability from the chain package
- Remove workaround for limits on fetching height ranges since the new
  database interface no longer imposes them
- Correct the gettxout RPC handler to return the best chain hash as
  opposed the hash the txout was found in
- Optimize various RPC handlers:
  - Change several of the RPC handlers to use the new chain snapshot
    capability to avoid needlessly loading data
  - Update several handlers to use new functionality to avoid accessing
    the block manager so they are able to return the data without
    blocking when the server is busy processing blocks
  - Update non-verbose getblock to avoid deserialization and
    serialization overhead
  - Update getblockheader to request the block height directly from
    chain and only load the header
  - Update getdifficulty to use the new cached data from chain
  - Update getmininginfo to use the new cached data from chain
  - Update non-verbose getrawtransaction to avoid deserialization and
    serialization overhead
  - Update gettxout to use the new utxo store versus loading
    full transactions using the transaction index

The following is an overview of the utility changes:
- Update addblock to use the new database and chain interfaces
- Update findcheckpoint to use the new database and chain interfaces
- Remove the dropafter utility which is no longer supported

NOTE: The transaction index and address index will be reimplemented in
another commit.
2016-08-18 15:42:18 -04:00
Dave Collins
bd4e64d1d4 chainhash: Abstract hash logic to new package. (#729)
This is mostly a backport of some of the same modifications made in
Decred along with a few additional things cleaned up.  In particular,
this updates the code to make use of the new chainhash package.

Also, since this required API changes anyways and the hash algorithm is
no longer tied specifically to SHA, all other functions throughout the
code base which had "Sha" in their name have been changed to Hash so
they are not incorrectly implying the hash algorithm.

The following is an overview of the changes:

- Remove the wire.ShaHash type
- Update all references to wire.ShaHash to the new chainhash.Hash type
- Rename the following functions and update all references:
  - wire.BlockHeader.BlockSha -> BlockHash
  - wire.MsgBlock.BlockSha -> BlockHash
  - wire.MsgBlock.TxShas -> TxHashes
  - wire.MsgTx.TxSha -> TxHash
  - blockchain.ShaHashToBig -> HashToBig
  - peer.ShaFunc -> peer.HashFunc
- Rename all variables that included sha in their name to include hash
  instead
- Update for function name changes in other dependent packages such as
  btcutil
- Update copyright dates on all modified files
- Update glide.lock file to use the required version of btcutil
2016-08-08 14:04:33 -05:00
Dave Collins
00ebb9d14d blockchain: Associate time src with chain instance.
Rather than making the caller to pass in the median time source on
ProcessBlock and IsCurrent, modify the Config struct to include the
median time source and associate it with the chain instance when it is
created.

This is being done because both the ProcessBlock and IsCurrent functions
require access to the blockchain state already, it is a little bit safer
to ensure the time source matches the chain instance state, it
simplifies the caller logic, and it also allows its use within the logic
of the blockchain package itself which will be required by upcoming
rule change warning logic that is part of BIP9.
2016-07-14 13:10:47 -05:00
Dave Collins
eb882f39f8 multi: Fix several misspellings in the comments.
This commit corrects several typos in the comments found by misspell.
2016-02-25 11:17:12 -06:00
John C. Vernaleo
5076a00512 Initial Decred Commit.
Includes work by cjepson, ay-p, jolan, and jcv.

Initial conceptual framework by tacotime.
2016-02-07 14:00:12 -05:00
Dave Collins
6e402deb35 Relicense to the btcsuite developers.
This commit relicenses all code in this repository to the btcsuite
developers.
2015-05-01 12:00:56 -05:00
Dave Collins
c6bc8ac1eb Update btcnet path import paths to new location. 2015-02-05 23:24:53 -06:00
Dave Collins
03433dad6a Update btcwire path import paths to new location. 2015-02-05 15:16:39 -06:00
Dave Collins
b69a849114 Import btcchain repo into blockchain directory.
This commit contains the entire btcchain repository along with several
changes needed to move all of the files into the blockchain directory in
order to prepare it for merging.  This does NOT update btcd or any of the
other packages to use the new location as that will be done separately.

- All import paths in the old btcchain test files have been changed to
  the new location
- All references to btcchain as the package name have been changed to
  blockchain
2015-01-30 15:49:59 -06:00