Commit Graph

3734 Commits

Author SHA1 Message Date
Dave Collins
a0bdb17891
chaingen: Add revocation generation infrastructure.
This adds infrastructure to track missed votes and automatically
generate revocations for them along with a new assertion helper named
AssertTipNumRevocations which, as it name suggests, allows the caller to
assert the header commits to the specified number of revocations.

This will be useful for porting the rest of the block validation tests
to the fullblocktests framework.
2018-03-05 12:40:36 -06:00
Markus Richter
33ee347667 addrmgr: Declutter package API.
Hide functions which are only used internally.
2018-03-05 12:29:40 -06:00
Josh Rickmar
eb9f01f8f3 connmgr: Use same Dial func signature as net.Dial 2018-03-05 12:29:26 -06:00
Dave Collins
03f77993bd
blockchain: Support testnet stake diff estimation.
Currently, the stake difficulty estimation function for algorithm
defined in DCP0001 only works with mainnet due to a limitation of only
working when the interval size is less than or equal to the ticket
maturity.

This generalizes the function to work with all values of interval size
and ticket maturity and adds a full suite of tests using the testnet
parameters to ensure proper functionality.

Of particular note is the case when the current height is before the
ticket maturity floor since that means some of the tickets being
estimated will mature during the interval and that there can be no
non-estimated immature tickets.

This also implies that, unlike when the ticket maturity is greater than
or equal to the interval size, the estimate may not exactly match the
real final value when given the exact same number of tickets to estimate
as real purchased tickets in the remainder of the interval depending on
whether or not the current height is before the ticket maturity floor.
This is the case because the number of real tickets that end up being
purchased before the maturity floor may not match the estimated
assumption of max tickets per block.
2018-03-05 12:07:58 -06:00
Dave Collins
9f7d6a1aac
mempool/mining: TxSource separation.
This finishes separating the mining code from the mempool that was
partially done in commit 9031d85 by extending the TxSource interface to
include the new functionality required by Decred.

The following is an overview of the changes to accomplish this:
- Move the VoteTx struct out of the mempool package in the mining
  package and rename it to VoteDesc instead to signify it is a vote
  descriptor and be consistent with the naming of TxDesc
- Update mempool to use the new mining.VoteDesc struct
- Rename CheckIfTxsExist to HaveAllTransactions to be more consistent
  with HaveTransaction in the TxSource interface as it now lives
  alongside it
- Add VoteHashesForBlock, VotesForBlocks, and IsTxTreeKnownInvalid
  to the TxSource interface
- Switch the mining code to call the functions on the TxSource interface
  instead and remove the reference to the concrete mempool
2018-03-05 11:38:36 -06:00
Dave Collins
1e466c62ea
mempool/mining: Clarify tree validity semantics.
This replaces the mempool IsTxTreeValid function with
IsTxTreeKnownInvalid instead which more accurately describes its
semantics and purpose.

The actual purpose of this function is related to mempool acceptance and
building block templates for mining where the assumption is that the
previous block's regular transaction tree will be valid and is only
invalid specifically if there are enough known votes that vote against
it.

This change is desirable because the function provides semantics that
are actually opposite to that of the consensus rules where a tree is
only valid if it has a majority of yes votes and thus claiming it's
valid when that is not actually known for certain yet can easily cause
confusion.
2018-03-05 11:38:06 -06:00
Dave Collins
5e29dc7393
mining: Use single uint64 coinbase extra nonce.
The current code creates block templates with coinbase transactions that
have 4 uint64 extra nonces, but only ever uses one of them.  Rather than
wasting the extra space, this modifies the code so it only uses a single
uint64 extra nonce.

It should be noted that realistically there isn't even a real need for
an extra nonce in the coinbase at all because there is extra space in
the header specifically for that purpose and miners can't modify the
coinbase via getwork anyways.

However, it is still necessary to insert for the current code in order
to ensure every block template has a unique merkle root since that is
what is used to uniquely identify the block template.
2018-03-04 16:40:01 -06:00
Dave Collins
1275d1ffe9
main: Make func definition style consistent.
This modifies all files in the main package so that function definitions
are on the a single line as is the style used in the upstream code as
well as throughout its code base.  This helps minimize the differences
between them and the upstream code in order to facilitate easier syncs
due to less merge conflicts as a result of superfluous changes.
2018-03-04 14:31:43 -06:00
Donald Adu-Poku
0943f94912 dcrjson: add StartAutoBuyerCmd & StopAutoBuyerCmd. 2018-03-03 19:57:30 -06:00
Donald Adu-Poku
2c146946bc mempool: Optimize orphan map limiting.
This optimizes the way in which the mempool oprhan map is limited in the
same way the server block manager maps were previously optimized.

Previously the code would read a cryptographically random value large
enough to construct a hash, find the first entry larger than that value,
and evict it.

That approach is quite inefficient and could easily become a
bottleneck when processing transactions due to the need to read from a
source such as /dev/urandom and all of the subsequent hash comparisons.

Luckily, strong cryptographic randomness is not needed here. The primary
intent of limiting the maps is to control memory usage with a secondary
concern of making it difficult for adversaries to force eviction of
specific entries.

Consequently, this changes the code to make use of the pseudorandom
iteration order of Go's maps along with the preimage resistance of the
hashing function to provide the desired functionality.  It has
previously been discussed that the specific pseudorandom iteration order
is not guaranteed by the Go spec even though in practice that is how it
is implemented.  This is not a concern however because even if the
specific compiler doesn't implement that, the preimage resistance of the
hashing function alone is enough.

The following is a before and after comparison of the function for both
speed and memory allocations:

benchmark                    old ns/op     new ns/op     delta
----------------------------------------------------------------
BenchmarkLimitNumOrphans     3727          243           -93.48%

benchmark                    old allocs    new allocs    delta
-----------------------------------------------------------------
BenchmarkLimitNumOrphans     4             0             -100.00%
2018-03-03 23:51:56 +00:00
Dave Collins
2027fc339a
mining/mempool: Move priority code to mining pkg.
This moves the priority-related code from the mempool package to the
mining package and also exports a new constant named UnminedHeight which
takes the place of the old unexported mempoolHeight.

Even though the mempool makes use of the priority code to make decisions
about what it will accept, priority really has to do with mining since
it influences which transactions will end up into a block.  This change
also has the side effect of being a step towards enabling separation of
the mining code into its own package which, as previously mentioned,
needs access to the priority calculation code as well.

Finally, the mempoolHeight variable was poorly named since what it
really represents is a transaction that has not been mined into a block
yet.  Renaming the variable to more accurately reflect its purpose makes
it clear that it belongs in the mining package which also needs the
definition now as well since the priority calculation code relies on it.
This will also benefit an outstanding PR which needs access to the same
value.
2018-03-03 00:18:01 -06:00
Jonathan Gillham
ba2bd63533
mining: Stop transactions losing their dependants.
This fixes a bug where a transaction would lose reference to other
transactions dependant on it when being considered for inclusion in a
new block template. The issue only occurs when the transaction being
considered triggers a change of priority queue ordering to ordering by
fee. It results in none of the dependant transactions being considered
for inclusion in the new block template.
2018-03-02 23:55:41 -06:00
Dave Collins
1f2ae2f78f
mining: Fix duplicate txns in the prio heap.
This prevents the ability for duplicate transactions to be added to the
mining priority heap when a transaction spends multiple outputs from the
same input transaction by converting the dependency tracking to use a
map keyed by the transaction hash instead of a linked list.
2018-03-02 23:35:48 -06:00
Dave Collins
74d130057a
multi: Rename BIP0111Version to NodeBloomVersion.
This renames the BIP0111Version constant to NodeBloomVersion to better
describe its purpose.
2018-03-02 22:29:14 -06:00
Dave Collins
621362ea8b
rpcserver: Correct getblockheader result text.
The getblockheader RPC with a verbose parameter of false returns the
serialized header not its hash.
2018-03-02 00:13:47 -06:00
Markus Richter
5f7cd6c46b addrmgr: Improve test coverage.
Make sure the peers.json file is actually written and read in unit test.

Also some minor edits:

- Capitalize Tor.
- Capitalize JSON.
- Proper capitalization at the beginning of a sentence.
2018-03-01 22:05:29 -06:00
Markus Richter
cbc3c88533 docs: document how to use go test -coverprofile
Also some cleanups:

- Use proper punctuation.
- Use nicer link for Bitcoin white paper.
- Remove white spaces at the end of lines.
- Format paragraph.
2018-03-01 20:26:39 -06:00
Markus Richter
353053d236 Adjust README.md for new Go version
- Capitalize Go.
- No text lines longer than 80 characters.
2018-03-01 20:15:08 -06:00
Markus Richter
bca8d18cb1 docs: Remove carriage return.
Bite the bullet now and remove carriage return (`\r`) from all
documentation files to allow further edits without messy diffs.

No further changes have been made besides `sed -i 's/\r//g' docs/*`.
2018-03-01 20:14:55 -06:00
Donald Adu-Poku
365a0db683 chaingen: Track expected blk heights separately. 2018-03-01 20:13:56 -06:00
Dave Collins
f92d36303c
rpcserver: Add dcrd version info to getversion RPC.
This adds the dcrd version info as an additional key to the JSON-RPC
getversion response to accompany the existing RPC API version
information and bumps the JSON-RPC minor version to account for the
backwards-compatible change.
2018-03-01 19:41:07 -06:00
Dave Collins
01fd2927fe
build: Correct semver build handling.
The semver spec allow the build portion to contain multiple identifiers
separated by periods, however, the current code is stripping periods.
2018-03-01 19:41:05 -06:00
Markus Richter
d59ea167d9 addrmgr: Remove obsolete coverage script. 2018-03-01 18:34:55 -06:00
Markus Richter
1e42b8524d multi: Properly capitalize Decred.
Decred is inconsistencly capitalized in the code base,
change all occurences of decred to Decred.
2018-03-01 17:41:35 -06:00
Dave Collins
2b8bde41ea
blockchain: Remove unused GetTopBlock function. 2018-03-01 12:41:48 -06:00
Dave Collins
a6ddf038f2
mining: Obtain block by hash instead of top block.
This modifies the mining code to request the desired blocks from chain
by hash as opposed to using a separate function to get the top block and
removes the now unused plubming through block manager.

Not only is this faster because it avoids the need to go through block
manager, it is also more robust since it does not rely on the tip of the
chain not having changing for correctness.
2018-03-01 12:41:46 -06:00
Dave Collins
6573428e11
multi: Implement getchaintips JSON-RPC.
This implements the getchaintips JSON-RPC and updates the associated
JSON-RPC API documentation accordingly.

It should be noted that until the entire block index is loaded into
memory, the chain tips tracking currently only works with tips that have
been seen since the daemon was started.
2018-03-01 12:29:36 -06:00
Dave Collins
c9cfd21a1b
blockchain: Cache tip and parent at init.
This modifies the startup logic to cache the tip and parent blocks in
the main chain block cache at initialization time which is desirable
because all of the transactions which come in to the mempool need access
to the these blocks to construct their utxo views due to the potential
for invalidation of the outputs in the previous block through voting.

Without caching them, the blocks end up being loaded from the database
again every time a new transaction enters the mempool until the blocks
eventually get cached when a new block is connected which is majorly
inefficient.
2018-03-01 12:05:18 -06:00
Donald Adu-Poku
60c7bc0e10 blockchain: move block validation rule tests into fullblocktests (2/x).
This PR moves the following block validation rule tests to
fullblocktests.

- `ErrBlockOneOutputs`
  - Create a premine block with one premine output removed.
  - Create a premine block with a bad spend script.
  - Create a premine block with an incorrect pay to amount.

- `ErrVotesMismatch`
  - Create block with a header that commits to more votes than the block
actually contains.

- `ErrIncongruentVotebit`
  - Attempt to add block with incorrect votebits set.
    - 4x Voters, 2x Nay 2x Yea, but block header says Yea
    - 3x Voters, 2x Nay 1x Yea, but block header says Yea
    - 3x Voters, 1x Nay 2x Yea, but block header says Nay
- `ErrSStxCommitment`
  - Attempt to add block with a bad ticket purchase commitment.

- `ErrSSGenPayeeOuts`
  - Attempt to add a block with a bad vote payee output.
  - Attempt to add a block with an incorrect vote payee output amount.

- `ErrBadCoinbaseFraudProof`
  - Create block with an invalid coinbase transaction.

- `ErrFraudBlockIndex`
  - Create block with an invalid transaction block height.

- `ErrFraudAmountIn`
  - Create block with an invalid transaction amount.

- `ErrExpiredTx`
  - Create block with an expired transaction.

- `ErrBadBlockHeight`
  - Create block with an invalid block height.

- `ErrPoolSize`
  - Create block with an invalid ticket pool size.

- `ErrInvalidFinalState`
  - Create block with an invalid final state.

- `ErrScriptMalformed`
  - Create block with a malformed spend script.
2018-02-28 20:43:28 -06:00
Dave Collins
f2f0ec5866
blockchain: Add tests for chain tip tracking.
This adds code to test the chain tip tracking functionality of the block
index.
2018-02-28 10:55:51 -06:00
Dave Collins
1a583c75b7
blockchain: Simplify chain tip tracking.
This simplifies the recently added chain tip tracking by allowing the
block index to handle it when a node is added to it.

This is possible since the block index does not support nodes that do
not connect to an existing node (with the exception of the genesis
block), and thus all new nodes are either extending an existing chain or
are on a side chain, but in either case, are a new chain tip.  In the
case the node is extending a chain, the parent is no longer a tip so it
can be removed.
2018-02-27 21:26:21 -06:00
Dave Collins
d4e73ee358
blockchain: Remove unused threshold serialization.
This removes the unused threshold state serialization code and
associated tests that appear to be left over from initial development
but were never needed due to other caches used.
2018-02-27 00:55:13 -06:00
David Hill
d19ad36d9b dep: sync 2018-02-26 19:33:48 -06:00
Dave Collins
1172773907
chaingen: Accept mungers for create premine block.
This modifies the CreatePremineBlock in chaingen to accept mungers
similar to NextBlock.
2018-02-26 17:55:19 -06:00
Dave Collins
1510308810
blockchain: Simplify voter version calculation.
This simplifies, optimizes, and improves the robustness of the voter
version calculation as follows:

- Modifies calcVoterVersion to make use of the AncestorNode function to directly
  iterate back to the end of the prevoius stake version interval instead
  of manually adjusting and calling findStakeVersionPriorNode.
- Adds an optimization to skip any attempts to count votes before stake
  validation height since there can't possibly be any votes prior to
  that point.
- Modifies calcVoterVersionInterval to explicitly assert the assumptions
  at the beginning of the function to prevent invalid behavior instead
  of only commenting them and then indirectly asserting it via the total
  number of found votes.
2018-02-26 13:40:47 -06:00
Dave Collins
1d39c089f9
blockchain: Switch tip generation to chain tips.
This modifies the TipGeneration function to use the new chain tip
functionality in order to determine the entire generation of blocks
stemming from the current tip as opposed to the children.
2018-02-26 13:06:52 -06:00
Dave Collins
c5168a0394
blockchain: Add chain tip tracking.
This adds logic to keep track of all known chain tips.  Currently, this
will only work with chain tips that have been seen since the daemon was
loaded, however, when moving to the full block index in memory, it will
pick end up tracking all chain tips in the block index.

Also, note that nothing makes use of the functionality as of this commit
yet.  It will be useful in the future to allow things such as removal of
the more expensive per-node children tracking, mining code improvements,
and enhanced fork-related interrogation ability.
2018-02-26 12:58:35 -06:00
Dave Collins
479bfdead8
blockchain: Reduce GetGeneration to TipGeneration.
This replaces the GetGeneration function which allowed getting the
entire generation (all children stemming from the same parent) of an
arbitrary bock with TipGeneration which only returns the same
information for the tip block.

This is being done because the function is only used for mining purposes
to get the generation of the current tip.  The code is simplified by
reducing its scope to its actual purpose as an initial benefit.  It also
provides much better optimization opportunities later.
2018-02-26 11:41:01 -06:00
Dave Collins
5d06f6b858
fullblocktests: Use new exported IsSolved func. 2018-02-25 21:19:17 -06:00
Dave Collins
71922ca909
chaingen: Export func to check if block is solved.
This adds a new exported function named IsSolved which allows consumers
of chaingen to easily check if a block is solved according to the target
difficulty specified by the bits in its header.
2018-02-25 21:14:03 -06:00
Dave Collins
111dfdafb6
fullblocktests: Improve vote on wrong block tests.
This improves the test which checks for votes on the wrong block by
creating a new commitment script and testing 3 different scenarios:

- Ticket that commits to the parent of the correct block to ensure that
  the code under test is not just failing due to a hash that doesn't
  exist
- Ticket that commits to the correct block hash and wrong block height
- Ticket the commits to the correct block height and wrong block hash
2018-02-25 19:21:55 -06:00
Dave Collins
6512184190
chaingen: Export vote commitment script function.
This exports the VoteCommitmentScript function so callers can make use
of it to create new vote commitment scripts.
2018-02-25 19:21:54 -06: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
1f66f20be7
chaingen: Break dependency on blockchain.
This copies the minor bits of code referenced by chaingen from
blockchain to break the dependency on the blockchain package.

The intent of chaingen is that it is totally separate code from
the blockchain package so that accidental consensus changes are much
more difficult to slip through.
2018-02-25 16:26:34 -06:00
Dave Collins
6c07fdb361
blockchain: Remove duplicate val tests.
This removes some tests from validate_test.go that are already tested by
the dynamic full blocks tests.
2018-02-25 16:14:14 -06:00
Dave Collins
0aa73c02b4
chaingen: Prevent dup block names in NextBlock.
This modifies the chaingen test harness to error when a duplicate block
name is provided to NextBlock to help prevent misuse.

It also orrects a couple of comment typos.
2018-02-25 16:00:06 -06:00
Dave Collins
b661ebdfd5
blockchain: Remove redundant stake ver calc func.
This removes the calcStakeVersionByNode in favor of just using
calcStakeVersion directly since the latter already takes the node and
the former is a simple pass through with no additional handling.
2018-02-25 15:42:09 -06:00
Dave Collins
7cd713dddf
fullblocktests: Cleanup after refactor.
This corrects various comment issues introduced with the name refactor
as well as a few block names.
2018-02-24 20:12:36 -06:00
Donald Adu-Poku
6da8225f09 blockchain: move block validation rule tests into fullblocktests. 2018-02-25 02:03:59 +00:00
Dave Collins
ea1d3750ba
blockchain: Remove dry run flag.
This switches block template proposal checks over to use
CheckConnectBlock now that all of the core checks needed for block
templates is included there, and, more importantly, removes the the
BFDryRun flag since it was only used to test block template proposals
and it is much simpler to allow the main chain processing paths to run
without the complication of selectively processing code for dry run
behavior.
2018-02-24 12:53:05 -06:00