Commit Graph

4454 Commits

Author SHA1 Message Date
Dave Collins
b92aa63e07
txscript: Remove CalcScriptInfo.
This removes the deprecated CalcScriptInfo function along with the
associated ScriptInfo struct and tests.
2019-06-24 15:13:33 -05:00
Dave Collins
41f1139e3b
txscript: Unexport IsStakeOutput
This unexports the previously deprecated isStakeOutput function and
updates all callers in the module accordingly.
2019-06-24 15:13:32 -05:00
Dave Collins
68e1655bba
txscript: Remove IsMultisigScript err return.
This removes the previously deprecated error return from the
IsMultisigScript function and updates all callers in the module and
tests accordingly.
2019-06-24 15:13:32 -05:00
Dave Collins
6dea351b42
txscript: Remove third GetPreciseSigOpCount param.
This removes the previously deprecated and unused third parameter of the
GetPreciseSigOpCount function.
2019-06-24 15:13:31 -05:00
Dave Collins
16b9f67d4f
txscript: Unexport HasP2SHScriptSigStakeOpCodes.
This unexports the previously deprecated HasP2SHScriptSigStakeOpCodes
function and updates all callers in the module accordingly.
2019-06-24 15:13:31 -05:00
Dave Collins
e0a318cd75
txscript: Remove DisableLog. 2019-06-24 15:13:30 -05:00
Dave Collins
ad0d98ce75
txscript; Use const for sighashall optimization.
This breaks the dependency on chaincfg.SigHashOptimization which is no
longer available in v2 of the chaincfg module.  The constant is set to
false to ensure the same semantics are kept and an additional comment
has been added regarding the status.
2019-06-24 15:13:30 -05:00
Dave Collins
7da223fd98
release: Freeze version 1 txscript module.
This serves as the final release of version 1 of the txscript module.
All future releases will be moving to version 2 of the module.

Consequently, it bumps the module version as follows:

- github.com/decred/dcrd/txscript@v1.1.0

It also removes the txscript override in the root module and updates it
so building the software will still produce binaries based on the v1
module until the v2 module is fully released.
2019-06-24 15:13:20 -05:00
Donald Adu-Poku
222e20680f blockchain: update CalcBlockSubsidy.
This betters CalcBlockSubsidy by allowing it to
calculate the requested block subsidy from a cached
subsidy entry regardless of its proximity to the
requested one.
2019-06-23 23:37:28 +00:00
Dave Collins
826d66bb56
mining: Overhaul background template generator.
Currently, block templates that provide work to PoW miners are generated
in response to them calling the getwork RPC.  However, unlike in a pure
PoW system, in Decred, when a new block is connected, a new block
template extending that block can't be generated until the minimum
number of required votes for it has been received.  This poses a
challenge for PoW miners because they are typically unaware of when
votes are received.  Consequently, miners poll getwork waiting until
they see a new template generated when a new block is connected.

Since only a minimum of 3 votes is required to build a template and new
templates are only generated in response to the miners calling getwork,
as just described, miners often, unfortunately, end up receiving a
template that only has 3 votes and begin working on it.  Worse, since
many new miners are not well versed in the intricacies of Decred voting,
they often aren't aware they really need to continue polling for new
votes and immediately switch over to the new template with more votes in
it in order to avoid receiving a reduced subsidy.  This often results in
new miners producing blocks with 3 votes.

Moreover, blocks with less than all 5 votes are not only undesirable for
the PoW miners because they receive reduced subsidy, they're also
undesirable for PoS stakers since it means the votes that were not
included don't receive the reward despite having voted simply due to a
propagation delay.

Another issue with the existing approach is that since it takes a bit of
time to generate and validate templates, particularly as the number of
transactions they include rises, miners periodically requesting updated
work to include the latest transactions have to wait for the template to
be built when they call getwork once the existing cached template is
expired.  This can result in undesirable delays for them.

In order to address the need to wait for templates to be built in that
case, there was recently some preliminary work that implemented a
background template generator which asynchronously generates templates
in response to events with the intention of allowing access to the
latest one without said random delays in the future.

However, that preliminary work did not address the issues around vote
propagation such as the tendency for miners to end up with the first
block template only containing the 3 fastest-propagating votes, nor did
it have robust handling for all potential corner cases and error
conditions.

Consequently, this overhauls the background block template generator to
add support for intelligent vote propagation handling, handle chain
reorganization to alternative blocks in the case the current tip is
unable to obtain enough votes, provide a subscription for a stream of
template updates, handle template generation errors, consider the
synchronization state as a part of determining if the chain is current,
track the reason for template updates, block current template retrieval
during operations that are in the process of making it stale, and
correct several corner cases.

It should be noted that this only implements the infrastructure and does
not switch getwork or the CPU miner over to use the background templates
yet.  This will be done in future commits.

The following is a high-level overview of the semantics this implements:

- Generate new templates immediately when prior to stake validation
  height since no votes are required
- Do not generate templates for intermediate blocks during a chain
  reorganization
- Do not generate templates before the chain is considered synchronized
- Prefer to create templates with maximum votes through the use of a
  timeout once the minimum votes have been received to provide the votes
  an opportunity to propagate with a fallback for immediate generation
  as soon as all votes are received
  - In the case the timeout expires and a template is created with less
    than the maximum number of votes, generate a new template
    immediately upon receiving more votes for the block it extends
- In the event there are competing blocks at the current tip, prefer to
  build a template on the first seen block so long as it receives the
  minimum number of required votes within a few seconds
- Generate new templates immediately when a block is disconnected to
  support future planned chain invalidation capabilities
- Generate new templates periodically when there are new regular transactions
  to include
- Schedule retries in the rare event template generation fails
- Allow clients to subscribe for updates every time a new template is
  successfully generated along with a reason why it was generated
- Provide direct access to the most-recently generated template
  - Block while generating new templates that will make the current template
    stale (e.g. new parent or new votes)

The following is a high-level overview of the changes:

- Initialize PRNG once instead of at every invocation
- Implement an asynchronous queue for all events to ensure normal chain
  and vote processing is not blocked
- Provide ability to subscribe for all template updates over a single
  channel to ensure none can indadvertently be missed as is possible with
  the current design
  - Always deliver existing template upon registration
  - Drop templates if the receiver falls too far behind in the same way
    tickers work
- Introduce tracking the reason for template updates so that it can
  ultimately be presented to miners to allow them to make better
  decisions about when to force their workers to switch
- Consider the current network sync state when checking if the chain is
  current
- Introduce a regen handler state such as timeouts, blocks to monitor
  for votes, and the block the next template should extend
- Add logic for selectively reacting to votes on the block the current
  template is extending, the current tip, and alternate competing blocks
  at the current tip using the aforementioned semantics
- Perform all template generation in separate goroutines with a
  cancellable context
  - Cancel any in progress templates that are being generated whenever
    generating a new one
- Introduce blocking current template retrieval when a new template that
  would cause the existing one to become stale is being generated
- Modify periodic regen handling to use a resettable timer for better
  efficiency and more fine grained control
- Remove template pool as that is something that should be handled by
  the code that is actually handing the templates out
- Rename and export the event notification funcs to make it clear
  they're not internal functions and also make it easier to eventually
  move the code into the mining package
- Expand and improve comments
- Store and return template generation errors
  - Schedule retry in the case of failure
- Correct several cases that were not being handled correctly and some
  undesirable behaviors such as block disconnects (as opposed to
  reorgs), reorganization to side chains, and notifying with stale
  templates
2019-06-23 00:45:51 -05:00
Dave Collins
773db5b8db
stake: Prepare v1.2.0.
This updates the blockchain/stake dependencies and serves as a base for
blockchain/stake/v1.2.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/database@v1.1.0
- github.com/decred/dcrd/dcrutil@v1.3.0
- github.com/decred/dcrd/txscript v1.1.0

The full list of updated direct dependencies since the previous
blockchain/stake/v1.1.0 release are as follows:

- github.com/decred/dcrd/chaincfg@v1.5.1
- github.com/decred/dcrd/database@v1.1.0
- github.com/decred/dcrd/dcrec@v1.0.0
- github.com/decred/dcrd/dcrec/edwards@v1.0.0
- github.com/decred/dcrd/dcrutil@v1.3.0
- github.com/decred/dcrd/txscript v1.1.0
2019-06-23 00:24:14 -05:00
Donald Adu-Poku
3c6ff2852d wire: assert MaxMessagePayload limit in tests.
This adds an additional check to Msg tests ensuring their max
payload lengths do not exceed MaxMessagePayload.
2019-06-20 12:28:41 -05:00
Dave Collins
b5dd4a030a
txscript: Prepare v1.1.0.
This updates the txscript dependencies and serves as a base for
txscript/v1.1.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/dcrec/secp256k1@v1.0.2
- github.com/decred/dcrd/dcrutil@v1.3.0
- golang.org/x/crypto@v0.0.0-20190611184440-5c40567a22f8

The full list of updated direct dependencies since the previous
txscripti/v1.0.2 release are as follows:

- github.com/decred/dcrd/chaincfg@v1.5.1
- github.com/decred/dcrd/dcrec@v1.0.0
- github.com/decred/dcrd/dcrec/edwards@v1.0.0
- github.com/decred/dcrd/dcrec/secp256k1@v1.0.2
- github.com/decred/dcrd/dcrutil@v1.3.0
- golang.org/x/crypto@v0.0.0-20190611184440-5c40567a22f8
2019-06-19 14:47:05 -05:00
Dave Collins
10147bc8f8
database: Use chaincfg/v2.
This updates the database module to use v2 of the chaincfg module and
bumps the root module to require dcrutil@v1.3.0 accordingly.

Since v2 of the chaincfg module is only used in the tests and standalone
utility, a major version bump of the database module is not required.
2019-06-19 14:18:43 -05:00
Dave Collins
ce852eb852
blockchain: Do coinbase nulldata check locally.
This refactors the consensus code which extracts the null data from the
coinbase from txscript.ExtractCoinbaseNullData so that it is performed
directly in the validation code where it more properly belongs.

The only reason the extraction was previously done in txscript is
because it was not possible to parse scripts outside of it, but that is
no longer the case now that txscript offers an exported tokenizer for
that purpose.

The extraction code is ever so slightly more efficient now that it no
longer needs to be as generic since it now has direct knowledge of the
conditions that need to be handled.

Great care was taken to ensure the semantics are not changed while
refactoring the code and no additional tests are added in this commit
because all of the conditions and code paths are covered by the tests
recently added to the full block tests.

While here, also perform some related code cleanup in the function and
improve the error messages .

Since the txscript.ExtractCoinbaseNullData is no longer necessary, this
deprecates the function and releated error code and constant so they can
be removed in the next major version of txscript.

Finally, since this relies on the script tokenizer which is not yet in a
released version of the txscript module, bump the requirement to include
an as yet unreleased version of txscript to ensure the next time the
blockchain module is released, it will require a newer version of
txscript to be released first.
2019-06-19 13:26:33 -05:00
Dave Collins
a0b84e4005
docs: Update for dcrutil v2 module. 2019-06-19 12:09:20 -05:00
Dave Collins
ed43e59df5
release: Introduce dcrutil v2 module. 2019-06-19 12:09:19 -05:00
Dave Collins
9b753598df
dcrutil: Update to use chaincfg/v2 module. 2019-06-19 12:09:18 -05:00
Dave Collins
e1840ea6cc
dcrutil: Use mock addr params in tests.
This implements a mock address params struct that implements the new
interface and modifies the test code to make use of the mock params
instead of chaincfg.  This completely decouples the tests, with the
exception of the example code, from chaincfg and therefore any updates
to it such as introducing new test networks will not require changes to
the tests.
2019-06-19 12:09:18 -05:00
Dave Collins
2779e67a20
dcrutil: Introduce AddressParams interface.
This introduces a new interface named AddressParams and updates the
functions that currently take a pointer to a chaincfg.Params struct to
accept the interface instead.

This removes the tight coupling between the two packages at the API
boundary and allows callers to easily provide custom values without
having to create and register and entire chaincfg network as previously
required.

It also updates the tests to make use of the interface.
2019-06-19 12:09:17 -05:00
Dave Collins
423c3344d6
dcrutil: Accept magic bytes directly in NewWIF.
This modifies NewWIF to accept the required network magic bytes directly
versus a chaincfg.Params struct in order to remove the tight coupling
between the two packages at the API boundary and allows callers to
easily provide custom values without having to create and register and
entire chaincfg network as previously required.

It also removes the now unused error return and updates the tests
accordingly.
2019-06-19 12:09:16 -05:00
Dave Collins
5ca5f814cd
dcrutil: Require network on WIF decode.
This modifies DecodeWIF to accept the required network parameters for
the provided encoded WIF string and introduces a new error to indicate
the provided WIF string is for the wrong network.

This means that DecodeWIF will now return ErrWrongWIFNetwork if the WIF
string being decoded is not for the provided network.  This differs from
the previous behavior that would accept arbirtary network bytes and
relied on globally-registered networks in chaincfg to determine which
network the string was for followed by requiring the caller to check if
the WIF was for the specific network it desired upon return.

Finally, it removes the no longer necessary IsForNet method and updates
the documentation accordingly.
2019-06-19 12:09:16 -05:00
Dave Collins
736e0317b8
dcrutil: Don't store net ref in addr impls.
This modifies the concrete address implementations to only store the
necessary individual parameters instead of the entire params struct to
further decouple from chaincfg.
2019-06-19 12:09:15 -05:00
Dave Collins
07786d83e1
dcrutil: Rename EncodeAddress to Address.
This renames the EncodeAddress method on the Address interface to
Address and updates all of the concrete implementations and tests
accordingly.
2019-06-19 12:09:14 -05:00
Dave Collins
37531d2b35
dcrutil: Remove Net from Address interface.
This removes the no longer necessary Net method from the Address
interface and all concrete address implementations.
2019-06-19 12:09:14 -05:00
Dave Collins
54cd596803
dcrutil: Remove DSA from Address interface.
This removes the DSA method from the Address interface in favor of only
defining it on the specific concrete address implementations to which it
applies.  It also modifies the remaining instances to remove the network
parameters since they are already available on the type.

Finnaly, it updates the example code accordingly.
2019-06-19 12:09:13 -05:00
Dave Collins
b13eb4511f
dcrutil: Remove IsForNet from Address interface.
This removes the no longer necessary IsForNet method from the Address
interface and all concrete address implementations.

It also updates the tests accordingly.
2019-06-19 12:09:12 -05:00
Dave Collins
0ba5ac74e5
dcrutil: Require network on address decode.
This modifies DecodeAddress to accept the required network parameters
for the provided encoded address and return an error when the provided
address is for the wrong network.

This means that DecodeAddress will now return ErrUnknownAddressType if
the address being decoded is not for the provided network.  This differs
from the previous behavior relied on globally-registered networks in
chaincfg to determine which network the encoded address was for followed
by requiring the caller to check if the address was for the specific
network it desired upon return.

It also updates the tests and example accordingly.
2019-06-19 12:09:12 -05:00
Dave Collins
ff1268a1ef
dcurtil: Remove unused ErrMissingDefaultNet. 2019-06-19 12:09:11 -05:00
Dave Collins
be589d216c
dcrutil: Remove unused ErrAddressCollision. 2019-06-19 12:09:10 -05:00
Dave Collins
2142c4037c
dcrutil: Make docs example testable and correct it.
This adds a tested example for address decoding and removes the previous
non-functioning version of it from the pacakge documentation.  The
previous example appers to have been missed when porting the code and
was no longer correct for Decred.
2019-06-19 12:09:10 -05:00
Dave Collins
eab91970f5
release: Freeze version 1 dcrutil module.
This serves as the final release of version 1 of the dcrutil module.
All future releases will be moving to version 2 of the module.

Consequently, it bumps the module version as follows:

- github.com/decred/dcrd/dcrutil@v1.3.0

It also removes the dcrutil override in the root module and updates it
so building the software will still produce binaries based on the v1
module until the v2 module is fully released.
2019-06-19 12:09:05 -05:00
Dave Collins
e62a21858b
fullblocktests: Add coinbase nulldata tests.
This adds two new consensus tests to the full block tests for the
coinbase nulldata height commitment.  The first new test ensures
coinbase transactions with the wrong script version are rejected and the
second test ensures data pushes that exceed the maximum allowed number
of bytes for the coinbase nulldata push are rejected.
2019-06-18 14:47:32 -05:00
David Hill
893aa30dce multi: Use https links where available. 2019-06-18 14:20:06 -05:00
David Hill
2036772446 addrmgr: drop container/list. 2019-06-18 14:44:55 -04:00
Dave Collins
c33be8c68c
multi: Update all modules for chaincfg v1.5.1.
This updates all of the modules that still rely on v1 of the chaincfg
module to use v1.5.1 which builds correctly with the major API bump
introduced in the edwards v1.0.0 module.

While here, it also updates all references to the v0 edwards and dcrec
modules to their tagged v1 counterparts, tidies all of the modules via
go mod tidy, and removes all unnecessary indirect entries from the mod
files to keep the modules using the versions the dependencies are tested
with.

The primary motivation for this change is that the chaincfg/v2 module
requires edwards/v1 which is not API compatible with edwards/v0.
Consequently, in order for each module to be incrementally updated to
use chaincfg/v2, all of its dependencies must also be able to build with
edwards/v1 which is the case as of chaincfg v1.5.1.
2019-06-17 15:54:31 -05:00
David Hill
542c5e01e7 secp256k1: Introduce v2 module 2019-06-17 16:15:34 -04:00
David Hill
1d6a39546a secp256k1: unexport NAF 2019-06-17 12:12:59 -04:00
David Hill
c7a988b7cb schnorr: add signature IsEqual and Verify methods 2019-06-17 12:12:59 -04:00
David Hill
9149b2ba68 schnorr: unexport functions 2019-06-17 12:12:59 -04:00
David Hill
6b4d1a47b7 schnorr: remove curve param 2019-06-17 12:12:59 -04:00
David Hill
1cf1fc5998 release: freeze version 1 dcrec/secp256k1 module.
This serves as the final release of version 1 of the dcrec/secp256k1 module.
All future releases will be moving to version 2 of the module.
2019-06-17 12:12:59 -04:00
Dave Collins
2968070c7b
chaincfg: Add addr params accessor funcs.
This defines several new functions on the Params struct which return the
magic prefix bytes for addresses.

The new functions are:

- AddrIDPubKeyV0
- AddrIDPubKeyHashECDSAV0
- AddrIDPubKeyHashEd25519V0
- AddrIDPubKeyHashSchnorrV0
- AddrIDScriptHashV0

This will allow the parameters to be used directly as input to an
interface in the future.
2019-06-14 17:55:37 -05:00
Dave Collins
6bb0838e84
docs: Update for dcrec/edwards v2 module.
This updates the docs/README.md file, module hierarchy graphviz, and
module hierarchy diagram to reflect the new module version.
2019-06-14 11:34:48 -05:00
David Hill
bb479d1209 edwards: Introduce v2 module. 2019-06-14 08:21:57 -04:00
David Hill
88c18b4490 edwards: add Sign method to PrivateKey 2019-06-14 08:21:54 -04:00
David Hill
aa76fb64a1 edwards: add signature IsEqual and Verify methods 2019-06-14 08:21:53 -04:00
David Hill
f0ef1e4e2c edwards: unexport a slew of funcs 2019-06-14 08:21:50 -04:00
David Hill
06102241bf edwards: unexport EncodedBytesToBigIntPoint 2019-06-14 08:21:44 -04:00
David Hill
be5b1ad1ed edwards: remove curve param 2019-06-14 08:21:40 -04:00