Commit Graph

6 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
Dave Collins
2296b31e5d
blockchain: Refactor main block index logic.
This refactors the block index logic into a separate struct and
introduces an individual lock for it so it can be queried independent of
the chain lock.

It also modifies the `newBlockNode` function to accept nil for the
ticket spend information parameter and updates all of the test code that
doesn't require it to use nil.
2018-01-30 13:21:57 -06:00
Dave Collins
4d20cc1217
stake/multi: Don't return errors for IsX functions.
This modifies the IsSStx, IsSSGen, and IsSSRtx functions to only return
a bool and introduces CheckSStx, CheckSSGen, and CheckSSRtx to return
the actual error as needed by consensus.

This is being done because "is" functions are much nicer to use when
they don't return an error and the callers that use them almost never
care why they aren't of the type, they just want to determine if they
are.  In the few cases where the caller does care, they can use of the
new check functions.

While here, also update the comments to call out what the more common
names for the transaction types are and to add comments to the test
functions for consistency.

Finally, it updates all callers in the repo accordingly.
2018-01-30 10:20:12 -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
David Hill
81e3f0983d gometalinter: run on subpkgs too (#878) 2017-10-10 17:20:40 -04:00
Dave Collins
42175088b6
blockchain: Implement enforced relative seq locks.
This repurposes the sequence number of version 2 transaction inputs to
provide consensus-enforced relative lock-time semantics so that a
transaction can require inputs to have a specified relative age before
they are allowed to be included in a block.  Each relative time lock can
either specify a relative number of seconds (with a granularity of 512
seconds and a maximum value of 33,553,920) or a specific number of
blocks (max 65535).

The number of seconds is calculated relative to the past median time of
the block before the one that contains the referenced output.  This is
done because, due to other changes that will also be included in the
same agenda vote, said time will become the earliest possible time the
block that contains the referenced output could have been (technically
it will be one second after that, but that complexity is ignored since
there is already a granularity involved anyways).

It is also possible to disable the behavior by setting bit 31 of the
sequence number (which all transactions currently do by default since
they are set to the max).

In order for the transaction to be permitted to the mempool, relayed,
considered for inclusion into block templates, and allowed into a block,
the specified relative time locks for all of its inputs must be
satisfied.

This only implements the required logic and tests to enforce the new
behavior.  Code to enforce the new behavior when considering candidate
transactions for acceptance to the mempool, relaying, and inclusion into
block templates will be added in a separate commit.

A consensus vote is required in order to reject blocks which contain
transactions that violate the new rules at a consensus level.  Code to
selectively enable consensus enforcement based on the result of an
agenda vote will be added in a separate commit.

In order to accomplish this new behavior, the concept of a sequence lock
is introduced which allows the minimum possible time and height at which
a transaction can be included into a block to be calculated from all
inputs with non-disabled relative time locks, and functions to calculate
and evaluate the sequence lock are added.

The following is an overview of the changes:

- Introduce a new struct named SequenceLock to represent the previously
  described sequence lock
- Define new constants related to sequence numbers named
  SequenceLockTimeDisabled, SequenceLockTimeIsSeconds,
  SequenceLockTimeMask, and SequenceLockTimeGranularity
- Add a new function named calcSequenceLock to calculate the sequence
  lock for a given transaction
- Add a new function named SequenceLockActive to determine if a given
  sequence lock is satisfied for a given block height and past median
  time
- Add a convenience function named LockTimeToSequence which can be used
  to convert a relative lock time to a sequence number
- Add a comprehensive set of tests for all of the new funcs
2017-09-21 15:58:36 -05:00