Commit Graph

22 Commits

Author SHA1 Message Date
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
2f4fee5e3a
blockchain: Make consensus votes network agnostic.
This modifies the various code which deals with choosing which set of
consensus rules to use based upon the result of consensus votes to
remove the hard coded network checks.  Not only does it simplify the
existing code, but it makes it less error prone and cumbersome to
introduce new consensus votes in the future.

In order to accomplish this, introduce a new map to associate each
vote/agenda ID to the version that defines it within a given set of
chain parameters and populate the map when creating the chain instance.
Then, when the version is needed to make a decision regarding which
consensus rules are active, query the version from the map by ID.  A
missing entry means there is no voting agenda for the given network
(such as will be the case on simnet), and therefore the new rules will
always be in effect for that network.

An important point is that it is theoretically possible that the same ID
could be used in multiple consensus votes since they are only
technically unique per deployment version, although in practice reusing
IDs would be confusing.  Given the former, and since duplicate IDs would
break this logic otherwise, the chain instance will now return an error
if a duplicate ID is detected.
2019-01-30 09:19:47 -06:00
Dave Collins
42bc847479
multi: Cleanup and optimize tx input check code.
This optimizes and cleans up significant portions of the
CheckTransactionInputs function to avoid a lot extra allocations, remove
redundant checks, better document its semantics, and make the code
easier to reason about.  In addition, it renames a lot of the error
constants involved in said function to use the ticket/vote/revocation
terminology and improve their readability.  Even though several of the
checks have been rearranged for efficiency and readability purposes, all
consensus semantics have been retained.

One of the primary changes is to reduce the reliance on the stake package
for consensus validation.  Not only is more desirable to have the bulk
of the validation related to the blockchain in the blockchain package,
but it also allows the code to be more specific, which enables better
optimization opportunities as well as eliminates the need for a lot of
redundant checks that are simply unnecessary.

The stake identification functions are also part of consensus and have
not been modified, however, the actual validation related to all of
their inputs, such as ensuring commitments are observed, are now in the
blockchain package itself.

Since the only thing using the related verification functions is now
done in blockchain, this also removes the VerifyStakingPkhsAndAmounts,
VerifySStxAmounts, and related tests from the stake package.

Another significant change is the addition of new functions for
efficiently and specifically identifying the form of the scripts
required by stake transactions in order to reduce the dependence on the
standard code in txscript.  Standard code should _NOT_ be used in
consensus code as the two are not the same thing.

It should be noted that these changes break compatibility with the
current v1 blockchain and stake modules, so they will need a major
version bump prior to the next release.

Finally, all tests in the repository have been updated for the error
name changes as well as change in expected error in some cases due to
the reordering of the validation checks.
2018-09-26 09:23:03 -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
Donald Adu-Poku
0ca75aba1a blockchain: rename ErrRegTxSpendStakeOut to ErrRegTxCreateStakeOut. 2018-05-07 18:19:32 -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
132eb4b150
blockchain: Add block validation status to index.
This adds a new field to the block node struct named status that
consists of bit flags for keeping tracking of the validation state of
each block.  The status is only stored in memory as of this commit, but
it will be stored as a part of the block index in the upcoming block
index migration code.

Since the field will be updated after node creation, this also
introduces some new functions for interacting with the status field in a
concurrent-safe fashion.

This is largely based on upstream commits 2492af0 and fb0d13c; however,
it does not include some of the logic in the reorganization paths those
commits include as that approach doesn't match the intended direction
this package is moving towards.
2018-02-22 13:57:15 -06:00
Dave Collins
79689af3a8
blockchain: Correct error stringer tests.
This adds missing error entries to the tests and also adds an additional
check to ensure that all possibilities are tested to prevent drift in the
future.
2018-02-21 18:26:54 -06:00
Dave Collins
0536ed4db6
blockchain: Combine ErrDoubleSpend & ErrMissingTx.
This replaces the ErrDoubleSpend and ErrMissingTx error codes with a
single error code named ErrMissingTxOut and updates the relevant errors
and expected test results accordingly.

Once upon a time, the code relied on a transaction index, so it was able
to definitively differentiate between a transaction output that
legitimately did not exist and one that had already been spent.

However, since the code now uses a pruned utxoset, it is no longer
possible to reliably differentiate since once all outputs of a
transaction are spent, it is removed from the utxoset completely.
Consequently, a missing transaction could be either because the
transaction never existed or because it is fully spent.

Also, while here, consistently use the LookupEntry function on the
UtxoView instead of directly accessing the internal map as intended.
2018-02-21 14:01:05 -06:00
Dave Collins
693fc12e39
blockchain: Validate early final state is zero.
This modifies the checkBlockHeaderSanity function to include an
additional check which ensures the final state is zero prior to stake
validation height.

It should be noted that this is technically a hard fork since before
this change it was possible to use any value for the final state prior
to stake validation height.  However, no blocks on mainnet nor testnet
ever used non-zero final states for blocks in that range and they are
buried behind checkpoints in the minimum required version of the
software due to the previous consensus votes, so it isn't possible to
trigger a reorg that would fork old nodes off the network anyways, so it
is safe to retroactively apply this check.
2018-02-15 09:52:34 -06:00
Dave Collins
53b592bcf7
blockchain: Remove unused error code.
This removes the ErrUnparseableSSGen error code since it is no longer
used.
2018-02-02 14:34:48 -06:00
David Hill
ea5e13c291 blockchain: add missing error code entries 2017-07-25 11:43:14 -04:00
Marco Peereboom
6967358649 blockchain: Implement configurable voting on top of PoS. (#542)
Port BIP0009 over from btcd.  The following is an overview of the
changes:
- Add new configuration options to the chaincfg package which allows the
  rule deployments to be defined per chain
- Implement code to calculate the threshold state as required by voting
  - Use threshold state caches that are stored to the database in order
    to accelerate startup time
  - Remove caches that are invalid due to definition changes in the
    params including additions, deletions, and changes to existing
    entries
  - Verify that PoW and PoS majorities have been reached.
- Add tests for new error type
- Add tests for threshold state
- Deployments are per stake version.
- Add human readable vote/choice infrastructure.
- Add RPC command to interrogate vote status (getvoteinfo).
2017-02-13 13:32:40 -05:00
Marco Peereboom
e3eb40b4e8 Implement soft-forking mechanism. (#524)
* Bump block header version to 3.
* Enforce new rules once block version gets to 3.
* Use Voter Versions to control bumping of Stake Version.
* Calculate appropriate stake version based on history and prior
  intervals in the mining code.
* Add bunch of UT to validate code.

This is the minimally correct working implementation.  This code
desperately needs to memoize the questions that are asked repeatedly.

Fixes #523
2016-12-23 15:42:52 -06:00
Dave Collins
dc489f378d blockchain: Remove unnecessary RuleError.GetCode. (#459)
This function had several issues:

- It is completely unnecessary since the ErrorCode field is
  intentionally exported so it is accessible
- It goes against effective Go naming guidelines which specifically
  state getters should not be named GetFoo
- The comment on it regarding implementing the error interface was
  incorrect
2016-11-11 13:12:57 -05:00
Marco Peereboom
f96f6222b6 ErrBadStakevaseScrVal -> ErrBadStakebaseScrVal (#444)
Fixes #393
2016-11-10 11:47:23 -05:00
John C. Vernaleo
6beae08edf Remove unused ErrBIP0030
Closes #356
2016-09-28 15:47:14 -04: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
John C. Vernaleo
1954bf24bb Work on improving the use of analysis tools in goclean.sh
Corrected or added many comments.

Update test code to follow format go vet wants.

The code doesn't pass the checks 100% yet.  That will come in a later
commit but these all seemed related so I'd rather get them in now.
2016-02-12 15:24:32 -05: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
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