Commit Graph

40 Commits

Author SHA1 Message Date
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
David Hill
5fc31ca578 multi: fix misspell linter warnings 2018-02-18 12:26:52 -06:00
Dave Collins
305a88cc4d
fullblocktests: Add tests for early final state.
This adds tests for the new consensus check which ensures the final
state is zero prior to stake validation height.
2018-02-15 09:53:09 -06:00
Donald Adu-Poku
e48b9ab823 mempool: Rename RelayNonStd config option.
This renames the mempool.Config.RelayNonStd option to AcceptNonStd which
more accurately describes its behavior since the mempool was refactored
into a separate package.

The reasoning for this change is that the mempool is not responsible for
relaying transactions (nor should it be).  Its job is to maintain a pool
of unmined transactions that are validated according to consensus and
policy configuration options which are then used to provide a source of
transactions that need to be mined.

Instead, it is the server that is responsible for relaying transactions.
While it is true that the current server code currently only relays txns
that were accepted to the mempool, this does not necessarily have to
be the case.  It would be entirely possible (and perhaps even a good
idea as something do in the future), to separate the relay policy from
the mempool acceptance policy (and thus indirectly the mining policy).
2018-02-14 15:55:22 +00:00
Donald Adu-Poku
bd8ff54a4b docs: Add fullblocktests entry and minor consistency updates 2017-12-31 12:25:01 +00:00
Donald Adu-Poku
2be3bf6639 sec256k1: update func signatures.
funcs implicitly use the sec256k1 curve instead of passing it as a
param.
2017-12-21 19:38:42 -06:00
David Hill
b13f5dfbda travis: add ineffassign linter 2017-12-01 09:14:04 -05:00
David Hill
20686cd775 travis: add gosimple linter 2017-11-20 18:49:55 -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
6da4c5e6ee
chaincfg: Introduce agenda for v5 lnfeatures vote.
This adds a new definition for the upcoming agenda vote to enable the
lnfeatures as defined by DCP0002 and DCP0003.  It does not include any
code to make decisions or bump the versions.  It is only the definition.
2017-09-13 17:38:46 -05:00
Jean-Christophe Mincke
5115a422f0 multi: Error descriptions are in lower case.
Functions fmt.Error(), errors.New() are inspected.

Files under the dcrd/vendor directory are ignored.
2017-09-08 18:03:59 +02:00
Dave Collins
fce24223cd
multi: Separate tx serialization type from version.
Decred's serialized format for transactions split the 32-bit version
field into two 16-bit components such that the upper bits are used to
encode a serialization type and the lower 16 bits are the actual
transaction version.

Unfortunately, when this was done, the in-memory transaction struct was
not also updated to hide this complexity, which means that callers
currently have to understand and take special care when dealing with the
version field of the transaction.

Since the main purpose of the wire package is precisely to hide these
details, this remedies the situation by introducing a new field on the
in-memory transaction struct named SerType which houses the
serialization type and changes the Version field back to having the
desired semantics of actually being the real transaction version.  Also,
since the maximum version can only be a 16-bit value, the Version field
has been changed to a uint16 to properly reflect this.

The serialization and deserialization functions now deal with properly
converting to and from these fields to the actual serialized format as
intended.

Finally, these changes also include a fairly significant amount of
related code cleanup and optimization along with some bug fixes in order
to allow the transaction version to be bumped as intended.

The following is an overview of all changes:
- Introduce new SerType field to MsgTx to specify the serialization type
- Change MsgTx.Version to a uint16 to properly reflect its maximum
  allowed value
- Change the semantics of MsgTx.Version to be the actual transaction
  version as intended
- Update all callers that had special code to deal with the previous
  Version field semantics to use the new semantics
- Switch all of the code that deals with encoding and decoding the
  serialized version field to use more efficient masks and shifts
  instead of binary writes into buffers which cause allocations
- Correct several issues that would prevent producing expected
  serializations for transactions with actual transaction versions that
  are not 1
- Simplify the various serialize functions to use a single func which
  accepts the serialization type to reduce code duplication
- Make serialization type switch usage more consistent with the rest of
  the code base
- Update the utxoview and related code to use uint16s for the
  transaction version as well since it should not care about the
  serialization type due to using its own
- Make code more consistent in how it uses bytes.Buffer
- Clean up several of the comments regarding hashes and add some new
  comments to better describe the serialization types
2017-08-07 14:10:27 -05:00
Dave Collins
ef806bb95c
fullblocktests: Sync upstream block validation.
Upstream commit f21410e47c.
2017-07-29 17:14:47 -05:00
Dave Collins
248b18172c
fullblocktests: Add more consensus tests.
This adds several new tests to the consensus tests based upon their
upstream equivalents as well as some Decred-specific cases and updates
the chaingen package accordingly.

The following is a summary of the new tests:

* Negative proof-of-work limit (bits header field)
* Spend from transaction that does not exist
* Max size block with canonical encoding which exceeds max size with
  non-canonical encoding
* Too many signature operations with offending sigop before invalid data
  push
* Max and too many signature operations via pay-to-script-hash redeem
  scripts
* Invalid opcode in dead execution path
* Reorganization of txns with OP_RETURN outputs
* Spend of an OP_RETURN output
* Transaction with multiple OP_RETURN outputs
* Large max-sized block reorganization test (disabled by default since
  it takes a long time and a lot of memory to run)

It also modifies a few tests to make them a little less reliant on the
specific parameters chosen such as replacing the number of votes with
the expected tickets per block divided by 2 instead of just subtracting
3 which assumes there are specifically 5 votes.

Finally, it adds some comments regarding some upstream tests that were
not included due to the specific conditions they test not applying to
Decred.
2017-07-29 14:17:40 -05:00
David Hill
a9234850e7 remove deadcode 2017-07-17 15:06:47 -05:00
David Hill
59db139d19 blockchain: check errors and remove ineffectual assignments. 2017-07-17 13:52:06 -05:00
Dave Collins
734684d288
multi: Implement stake difficulty change and vote.
This implements a new stake difficulty algorithm along with a voting
agenda for all networks to change and a comprehensive set of tests.  It
also implements estimation using the new algorithm for the
estimatestakediff RPC.

The following is an overview of the changes:

- Add new agenda to vote on changing the stake difficulty algorithm to
  all networks
  - The version on mainnet is version 4
  - The version on testnet and simnet is version 5
- Modifies the stake difficulty calculation function to calculate the
  difficulty based on the result of the vote
- Modifies the stake difficulty estimation function to calculate the
  difficulty based on the result of the vote
- Makes the stake difficulty estimation function concurrent safe
  - Calls it directly from the RPC server instead of going through block
    manager
  - Removes no longer needed code from the block manager
- Generate new version blocks and reject old version blocks after a
  super majority has been reached
  - New block version on mainnet is version 4
  - New block version on testnet and simnet is version 5
- Add tests for the supply estimation used in the new algorithm
- Add tests for the new algorithm calculations
- Add tests for the estimation based on the new algorithm
2017-04-26 13:00:28 -05:00
Dave Collins
05d8456d3e
multi: Rename vote choice IsIgnore to IsAbstain.
This renames the chaincfg parameter for the vote choice which represents
an abstaining vote to be named IsAbstain instead of IsIgnore since that
more accurately describes its intent and behavior.

It also updates the RPC server choice result field for isignore to be
named isabstain to match and bumps the major version accordingly.

Finally, it renames other internal variables which make use of the
choice to include the word abstain as well for clarity and renames a
couple of other internal variables.
2017-04-24 16:17:27 -05:00
Dave Collins
637e6d9e10
multi: Implement block size hard fork demo voting.
This implements a new voting agenda for the testnet and simnet networks
for increasing the maximum block size from 1MiB to 1.25MB.

The following is an overview of the changes:

- Bump the maximum protocol block size limit to 1.25MB
- Bump the protocol version to 4
- Add wire tests for v3 protocol sizes and update tests for latest
- Update all wire values that are defined in terms of the max block size
  to respect the protocol version
- Update the MaxSigOpsPerBlock constant to maintain existing value
  regardless of the raised max protocol block size
- Decouple the maximum tx size from the block size by creating a chain
  parameter for it with the current sizes so it is not a part of the
  hard fork vote
- Add definition for new version 4 stake vote along with agenda to vote
  on block size to the testnet and simnet networks
- Convert the MaximumBlockSize chain parameter to a slice in order to
  hold the different possible sizes
- Adds a new function that returns the maximum block size based upon the
  result of the vote
- Change the existing context-free block size check to enforce the max
  protocol size and introduce a context-specific check which enforces a
  restricted size based upon the network consensus parameters and the
  result of the vote
- Set the max allowed size in generated block templates based upon the
  network params and result of the vote
- Generate version 4 blocks and reject version 3 blocks after a super
  majority has been reached
2017-02-13 13:28:21 -06: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
Dave Collins
a62155587d
chaingen: Add package for generating test chains.
This introduces a new package named chaingen that is useful for
generating full-blown valid test chains.  It is primarily a refactor of
the generator code that was previously in fullblocktests.

It also updates the fullblocktests package to make use of this new
chaingen package and removes the stakeversiontests package in favor of
performing all of the same tests inline making use of the new chaingen
package.
2017-02-12 10:28:58 -06:00
Dave Collins
574620e459 fullblocktests: Generate subsidy for voted block. (#519)
This tweaks the full block test infrastructure to generate the pos
subsidy based upon the block being voted on as opposed to the current
one so it matches the consensus rules.
2016-12-20 10:38:43 -06:00
Dave Collins
7056dec96b fullblocktests: Limit tickets to target pool size. (#518)
This modifies the full block consensus tests to only purchase tickets
until the target pool size is reached during the initial creation of the
blocks needed to reach stake validation height.

This helps make the code a little more flexible since it will allow many
more blocks to be created without spiking the ticket price as would
happen prior to this change.

For a little intuition, once stake validation height is reached the
tests intentionally purchase the same number of tickets as are voting on
each block in order to achieve a steady state, so if the number of
tickets in the pool are already higher than the target pool size prior
to reaching that steady state, the ticket price would keep rising until
the outputs used to purchase the tickets no longer had enough funds
available to purchase them.
2016-12-20 10:38:23 -06:00
Dave Collins
face803542 blockchain: Make params used in tests match. (#517)
This updates the various definitions of the simnet params used
exclusively in the tests to match their definitions used by the actual
simnet parameters defined in chaincfg.

While it would certainly be easier to simply use the params defined in
chaincfg, that also would not allow the tests to detect them being
inadvertently changed.
2016-12-20 10:37:22 -06:00
Dave Collins
698fee123b multi: Restore correct upstream majority version code. (#490)
This reverts to the correct upstream majority version code which
properly detects when certain thresholds of the network have been
updated in order to determine when to start rejecting old version
blocks and enforcing the rules in the new version block.
2016-11-25 20:59:46 -06:00
Dave Collins
60c812e735
multi: Upstream parameter abstraction sync
Upstream commit a7b35d9f9e.

Also, the merge commit contains necessary decred-specific alterations.
It also has the side effect of correcting a couple of min reduction bugs
even though they can't currently ever be hit due to the min reduction
interval flag being disabled for all networks.
2016-11-16 19:34:01 -06: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
a0c19ada7b
wire: Export transaction tree constants.
This exports the transaction tree constants from the wire package
instead of from dcrutil.  The tree is a fundamental part of a
transaction and therefore its definitions belong in wire alongside the
other constants that are also related to fundamental transaction fields.

Further, the wire package most definitely should not depend on dcrutil
since that would create a cyclic import cycle.
2016-11-14 14:35:22 -06:00
Marco Peereboom
f96f6222b6 ErrBadStakevaseScrVal -> ErrBadStakebaseScrVal (#444)
Fixes #393
2016-11-10 11:47:23 -05:00
Marco Peereboom
2d242b224e Add StakeVersion bits to various subsystems. (#441)
Bump block version and add stake version to wire.  Currently
StakeVersions are unused and once the enforcement logic goes in the
versions will bump again.

This fixes #435
2016-11-07 13:06:17 -06:00
John C. Vernaleo
13c3301cb4 fullblocktests: Add additional tests
* Test sub-second time precision
* Test difficulty less than zero
* Test difficulty above maximum
2016-11-02 00:39:32 -05:00
pedro martelletto
3e036a2a82 fullblocktests: Add several negative tests.
* Fraudulent transaction due to invalid index
* Transaction with no inputs
* Transaction with no outputs
* Transaction output with negative vlaue
* Transaction output with too large value
* Block with multiple transactions that sum to too large value
* Stakebase script too small and too large
* Input transactions with null outpoints
* Blocks with duplicate tx inputs
2016-11-02 00:36:36 -05:00
Alex Yocom-Piatt
f13d36dd1a fullblocktests: Add malformed coinbase tests
* Test no proof-of-work subsidy output in the coinbase
* Test invalid script type in the coinbase block commitment output
* Test too few bytes for the coinbase height commitment
* Test invalid block height in the coinbase commitment
2016-11-02 00:17:52 -05:00
Javed Khan
0d34382958 fullblocktests: Add block validation tests
* Test block with sbits underflow
* Test stake transaction pos limit
* Test stakebase script len checks
* Test stakebase unknown signature script
* Test stake transaction in regular tx tree
2016-11-02 00:17:51 -05:00
David Hill
e4c6cf1998 fullblocktests: Add additional tests
* Orphan with null prev hash
* Valid orphan due to undelivered block
* Duplicate regular block
* Duplicate orphan block
2016-11-02 00:17:51 -05:00
John C. Vernaleo
7c52a6317a fullblocktests: Add some voting tests.
* Vote with null ticket reference
* Regular tx in the stake tree
* Too many votes
* Too few votes
* Specified number of votes in header does not match included votes
2016-11-02 00:07:32 -05:00
Josh Rickmar
0a7a9530e6 fullblocktests: Add additional validation tests.
* Test early vote bits rule
* Invalid dev-org output script
* Invalid dev-org output script version
2016-11-02 00:07:32 -05:00
Dave Collins
0a38390437 blockchain: Add block validation infrastructure.
This adds a full-blown testing infrastructure in order to test consensus
validation rules.  It is built around the idea of dynamically generating
full blocks that link together to form a block chain.  In order to
properly test the rules, each test instance starts with a valid block
that is then modified to be invalid in the specific way needed to test a
specific rule.

Since the intent of this framework is to be able to detect any
regressions and/or otherwise inadvertent changes to the consensus rules,
it intentionally does not rely on any logic from the blockchain package
and instead has local implementations of everything necessary to build
valid blocks including things such as the initial premine block,
proof-of-work and proof-of-stake difficulty calculations, purchasing
stake tickets, and calculating lottery winners and casting the
appropriate votes.

In addition, blocks which exercise following rules have been added as an
initial starting point.  These are not exhaustive by any means, but they
serve to show how the framework is used and pave the way for more
exhaustive tests to be added over time.

* Invalid premine block
* Valid premine block
* Enough valid blocks to reach ticket maturity
* Enough valid blocks to reach stake enabled height
* Enough valid blocks to reach stake validation height
* Enough valid block to have a stable base of mature coinbases to spend
  for futher tests
* Basic forking and chain reorganization
* Double spend in regular transaction tree
* Too much proof-of-work coinbase (extending main chain, in block that
  forces a reorg, and in a valid fork)
* Too much dev-org coinbase (same scenarios as pow coinbase above)
* Max and too many signature operations via various combinations of
  OP_CHECKSIG, OP_MULTISIG, OP_CHECKSIGVERIFY, and OP_MULTISIGVERIFY
* Too many signature operations with offending sigop after invalid data
  push
* Attempt to spend tx created on a different forks
* Attempt to spend immature coinbase (on main chain and fork)
* Max size block and block that exceeds the max size
* Children of rejected blocks are either orphans or rejected
* Coinbase script too small and too large
* Max length coinbase script
* Attempt to spend tx in blocks that failed to connect
* Valid non-coinbase tx in place of coinbase
* Block with no transactions
* Invalid proof-of-work
* Block with a timestamp too far in the future
* Invalid regular tree merkle root
* Invalid proof-of-work limit (Bits header field)
* Two coinbase transactions
* Duplicate txns in the regular tree
* Timestamp exactly at and one second after the median time
* Spend from transaction index that is out of range
* Transaction that spends more that its inputs provide
* Non-final coinbase
* Spend from transaction earlier in same block
* Spend from transaction later in same block
* Double spend transaction from earlier in same block
* Coinbase that pays more than subsidy + fees
* Coinbase that includes subsidy + fees
* Spend from transaction that does not exist
2016-11-02 00:07:32 -05:00
Dave Collins
9634a8cb0d
fullblocktests: Add missing doc.go file. 2016-10-23 13:31:26 -05:00
Dave Collins
f21410e47c
blockchain: Add block validation infrastructure.
This adds a full-blown testing infrastructure in order to test consensus
validation rules.  It is built around the idea of dynamically generating
full blocks that target specific rules linked together to form a block
chain.  In order to properly test the rules, each test instance starts
with a valid block that is then modified in the specific way needed to
test a specific rule.

Blocks which exercise following rules have been added for this initial
version.  These tests were largely ported from the original Java-based
'official' block acceptance tests as well as some additional tests
available in the Core python port.  It is expected that further tests
can be added over time as consensus rules change.

* Enough valid blocks to have a stable base of mature coinbases to spend
  for futher tests
* Basic forking and chain reorganization
* Double spends on forks
* Too much proof-of-work coinbase (extending main chain, in block that
  forces a reorg, and in a valid fork)
* Max and too many signature operations via various combinations of
  OP_CHECKSIG, OP_MULTISIG, OP_CHECKSIGVERIFY, and OP_MULTISIGVERIFY
* Too many and max signature operations with offending sigop after
  invalid data push
* Max and too many signature operations via pay-to-script-hash redeem
  scripts
* Attempt to spend tx created on a different fork
* Attempt to spend immature coinbase (on main chain and fork)
* Max size block and block that exceeds the max size
* Children of rejected blocks are either orphans or rejected
* Coinbase script too small and too large
* Max length coinbase script
* Attempt to spend tx in blocks that failed to connect
* Valid non-coinbase tx in place of coinbase
* Block with no transactions
* Invalid proof-of-work
* Block with a timestamp too far in the future
* Invalid merkle root
* Invalid proof-of-work limit (bits header field)
* Negative proof-of-work limit (bits header field)
* Two coinbase transactions
* Duplicate transactions
* Spend from transaction that does not exist
* Timestamp exactly at and one second after the median time
* Blocks with same hash via merkle root tricks
* Spend from transaction index that is out of range
* Transaction that spends more that its inputs provide
* Transaction with same hash as an existing tx that has not been
  fully spent (BIP0030)
* Non-final coinbase and non-coinbase txns
* Max size block with canonical encoding which exceeds max size with
  non-canonical encoding
* Spend from transaction earlier in same block
* Spend from transaction later in same block
* Double spend transaction from earlier in same block
* Coinbase that pays more than subsidy + fees
* Coinbase that includes subsidy + fees
* Invalid opcode in dead execution path
* Reorganization of txns with OP_RETURN outputs
* Spend of an OP_RETURN output
* Transaction with multiple OP_RETURN outputs
* Large max-sized block reorganization test (disabled by default since
  it takes a long time and a lot of memory to run)

Finally, the README.md files in the main and docs directories have been
updated to reflect the use of the new testing framework.
2016-10-17 12:16:53 -05:00