Commit graph

55 commits

Author SHA1 Message Date
Dave Collins
d58ea754f3 Add test and infrastructure for block scripts.
This commit adds a test for checking known good block scripts in a block
are valid.  As a part of this, it adds the infastructure needed to load a
saved transaction store from a file which contains all of the input
transactions needed.

It also contains some changes from running goimports as well as some other
cleanup.
2015-01-08 05:10:54 -06:00
Dave Collins
df065eee19 Provide a new median time source API.
This commit provides a new interface, MedianTimeSource, along with a
concrete implementation which allows improved accuracy of time by making
use of the median network time as calculated from multiple time samples.

The time samples are to be provided by callers and are intended to come
from remote clients.

The calculations performed in this implementation exactly mirror those in
Bitcoin Core because time calculations are part of the consensus rules and
hence need to match exactly.
2014-10-10 01:12:39 -05:00
Jonathan Gillham
f60e503308 Changed TxIn.PreviousOutpoint to TxIn.PreviousOutPoint after btcwire API change. 2014-10-01 13:53:47 +01:00
John C. Vernaleo
f5f03e8172 Match error names to btcdb updates. 2014-09-16 15:08:53 -04:00
Tomás Senart
4772d4a1a4 goimports -w . 2014-07-02 19:14:27 -05:00
Tomás Senart
e29f40274d Replace map[a]bool with map[a]struct{}
The later uses no memory storage for values and provides the same
functionality.
2014-07-02 17:56:22 +02:00
Dave Collins
933cdf50e8 Export constant for the maximum time offset.
This commit creates and exports a new constant, MaxTimeOffsetSeconds,
which is the maximum number of seconds a block timestamp is allowed to be
ahead of the current time.

Previously this value was hard coded into the consensus rule path, however
it is useful better to have it defined as a constant and exported so other
callers can access it.

No consensus rules have been changed with this commit.
2014-06-30 12:14:58 -05:00
Dave Collins
dbca1d59c3 Add a new behavior flag to provide a dry run.
This commit adds a new behavior flag, BFDryRun which allows the caller
to indicate all checks should be performed against the block as normal
except it will not modify any state.  This is useful to test that a block
is valid without actually modifying the current chain or memory state.

This commit also adds a few additional checks which were elided before
since they are implicitly handled by btcwire.  However, with the ability
to propose blocks which didn't necessarily come through the btcwire path,
these checks need to be enforced in the chain code as well.

As a part of adding the checks, three new error codes named
ErrBlockTooBig, ErrTooManyTransactions, and ErrTxTooBig have been
introduced.

Closes #5.
2014-06-29 17:49:16 -05:00
Dave Collins
ad275b34a8 Add a new behavior flag to disable the pow check.
This commit adds a new behavior flag, BFNoPoWCheck which allows the caller
to indicate the check which ensures a block hashes to a value less than
required target should not be performed.
2014-06-26 17:25:47 -05:00
Dave Collins
7d84d801d7 Make golint happy. 2014-06-26 16:10:10 -05:00
Dave Collins
6e0aab52df Improve the RuleError type to include error codes.
This commit changes the RuleError type to a struct which consists of an
error code and human-readable description.

From a usage perspective, existing code should not break since type
asserting an error to a RuleError still works in the same manner.  The
difference is the caller can now take that type asserted RuleError and
access the .ErrorCode field on it to programmatically identify the
specific rule that was violated.

ok @jrick
2014-06-24 19:14:24 -05:00
Dave Collins
4579cfb71b Refactor several network-specific params to btcnet.
This commit refactors the code to make use of the btcnet package for
network-specific parameters instead of switching on the specific network.

For example, the percentage of the network that needs to run version 2
blocks is different between testnet and mainnet.  Previously the code was
using a switch to choose these values.  With this refactor, those
parameters are part of the network parameters provided when creating a new
chain instance.

Another example is checkpoints, which have been moved to btcnet so they
can be specified by the caller instead of hard coded into this package.
As a result, the checkpoints for the standard networks are now specified
in the btcnet package.

This makes it easier to add new networks such as a testnet4 should it
become needed.  It also allows callers to define their own custom network
parameters without having to modify the code of the package to add new
switch cases for the custom network.
2014-05-27 10:11:55 -05:00
Josh Rickmar
79beb22aef Export CoinbaseMaturity as a const.
The unexported variable is still kept so that tests may modify it.

ok @davecgh
2014-04-14 08:45:43 -05:00
Dave Collins
958ea38fdd Export CheckProofOfWork function. 2014-03-28 14:32:12 -05:00
Dave Collins
38a694e309 Correct maturity debug print.
This commit corrects the debug log print to use the hash of referenced
coinbase transaction instead of the hash of the current transation when a
coinbase maturity is not met.
2014-03-28 13:09:53 -05:00
Dave Collins
67ebfa0e80 Update checkBIP0030 to not use deprecated function.
Rather than using the deprecated TxShas function on a btcutil.Block,
convert the checkBIP0030 to use the newer preferred method of ranging over
the Transactions to obtain the cached hash of each transaction.

This is also a little more efficient since it can avoid creating and
caching an extra slice to keep the hashes in addition to having the hash
cached with each transaction.
2014-03-24 13:15:16 -05:00
Dave Collins
6dd7785276 Export coinbase script length limits.
This commit exports the MinCoinbaseScriptLen and MaxCoinbaseScriptLen
constants.
2014-03-15 15:03:03 -05:00
Dave Collins
1676ecd7a6 Export CheckConnectBlock function.
This commit makes a slight variant of the existing checkConnectBlock
function available to external callers.  The new exported version checks
if the passed block will connect to the end of the current main chain.

In order to support this, a few other small modifications have been made
to the initial index generation and the existing checkConnectBlock since
it previously made some assumptions about the state of genesis block which
can no longer be assumed due accepting blocks from callers directly.

Also, add a quick test to ensure the new function fails when checking if
the genesis block will connect again when it's already inserted.
2014-03-11 13:18:27 -05:00
Dave Collins
b59a15d07e Export CountP2SHSigOps. 2014-03-01 21:50:11 -06:00
Dave Collins
ec641751a8 Change BuildMerkleTreeStore to accept transactions.
This commit modifies the BuildMerkleTreeStore function to accept a slice
of btcutil.Tx transactions as opposed to a full block.  This allows more
flexibility when calculating merkle roots since a full block may not be
created yet (particularly when generating blocks that need to be solved in
mining).

Previously, the BuildMerkleTreeStore function accepted a btcutil.Block
because originally the block itself cached the transaction hashes and it
was necessary to have access to the block to make use of the cached
transactions.  However, the code has since been improved such that it
caches transaction hashes directly in each btcutil.Tx.  This means the
code can remain as efficient as before while allowing the individual
transacitons to be passed.
2014-03-01 21:16:19 -06:00
Dave Collins
82edb203e5 Export MaxSigOpsPerBlock. 2014-02-28 12:24:05 -06:00
Dave Collins
87ecac2072 Export CountSigOps function. 2014-02-28 12:16:56 -06:00
Dave Collins
a56dfc7ff4 Export CalcBlockSubsidy function. 2014-02-25 00:32:22 -06:00
Dave Collins
1c052a01d8 Reject blocks with high precision timestamps.
This commit adds an additional sanity check to ensure the block that is
being processed does not contain a timestamp with a precision higher than
one second.  This is necessary because the consensus rules only deal with
whole seconds in the time comparisons whereas the internal data structures
make use of Go time.Time values which support up to nanosecond precision.

Also, add a test to ensure the new functionality works as expected.

ok @owainga
2014-02-24 10:42:50 -06:00
Dave Collins
bfef4e4a31 Make regtest subsidy halving interval 150.
This commit moves the subsidy halving interval to the chain params so it
can be configured per network.  With that change it sets the regression
test halving interval to 150 to match the regression test params of the
reference implementation.

This was pointed out by @flammit.
2014-02-23 14:32:56 -06:00
Dave Collins
b7ef1f0946 Include transaction hash in failed input val error. 2014-02-03 20:03:06 -06:00
Dave Collins
1626994433 Avoid copying block headers in convenience locals.
This commit modifies local variables that are used for more convenient
access to a block's header to use pointers.  This avoids copying the
header multiple times.
2014-01-19 12:42:45 -06:00
Dave Collins
18ac5c848a Add 2014 to copyright dates. 2014-01-08 23:52:54 -06:00
Dave Collins
5295be070d Make the CheckBlockSanity function context free.
Rather than defining CheckBlockSanity as a member of a BlockChain
instance, define it at the root level so it is truly context free as
intended.  In order to make it context free, the proof of work limit is
now a required parameter.
2013-11-07 16:06:01 -06:00
Dave Collins
09b53a8fca Export the CheckBlockSanity function. 2013-11-07 15:39:48 -06:00
David Hill
36941cc427 Remove the satoshi consts and use them from btcutil instead. 2013-10-29 16:08:12 -04:00
David Hill
8271a11808 add SatoshiPerBitcent constant and export SatoshiPerBitcoin and
MaxSatoshi
2013-10-29 14:57:31 -04:00
Dave Collins
6165e9b95b Convert API to use new btcutil.Tx.
This is part of the ongoing transaction hash optimization effort noted in
conformal/btcd#25.
2013-10-28 15:17:53 -05:00
Dave Collins
b6e4ae4441 Optimize duplicate transaction input check.
Profiling showed the duplicate transaction input check was taking around
6% of the total CheckTransactionSanity processing time.  This was largely
due to using fmt.Sprintf to generate the map key.

This commit modifies the check instead to use the actual output as a map
key.

The following benchmark results show the difference:

Before: BenchmarkOldDuplicatInputCheck    100000     21787 ns/op
After:  BenchmarkNewDuplicatInputCheck   2000000       937 ns/op

Closes #2
2013-10-26 14:31:15 -05:00
Dave Collins
9a29855c16 Convert inability to find input tx to a RuleError.
This commit modifies the errors that result from missing expected input
transactions to a RuleError.  This allows the caller to detect a block was
rejected due to a rule violation as opposed to an unexpected error.
2013-10-13 02:38:46 -05:00
Dave Collins
8be23c89ae Cleanup a few comments. 2013-10-11 10:24:13 -05:00
Dave Collins
e888372019 Optimize transaction lookups.
This commit modifies the transaction lookup code to use a set instead of a
slice (list).  This allows the lookup to automatically prevent duplicate
requests to the database.

Previously, the code simply added every referenced transaction to a list
without checking for duplicates, which led to multiple requests against
the database for the same transaction.  It also meant the request list
could grow quite large with all of the duplicates using far more memory
than required.

While the end result was accurate, operating that way is not as efficient
as only requesting unique transactions.
2013-10-10 12:23:46 -05:00
Dave Collins
0b334bc841 Correct reading of serialized height in coinbase.
This commit corrects the reading of the serialized height in coinbase
transactions for block height of version 2 or greater.  On mainnet, the
serialized height is always 3 bytes and will continue to be so for
something like  another ~159 years, so there was no issue with mainnet.
However on testnet, there are some version 2 blocks which are low enough
in the chain to only take 2 bytes to serialize.

In addition, this commit adds a full tests for the relavant function
including negative tests and variable length serialized lengths for block
heights.

Closes #1.
2013-10-06 14:27:53 -05:00
Dave Collins
e54fb96d80 Test tx lookup validity in CheckInputTransactions.
This commit modifies CheckInputTransactions to ensure that not only must a
transaction exist in the transaction store, it must also not have any
errors associated with it or be nil.
2013-10-02 10:26:44 -05:00
Dave Collins
3c7511a34b Export the CheckTransactionInputs function. 2013-09-30 16:44:01 -05:00
Dave Collins
e576962cb3 Export the CheckTransactionSanity function. 2013-09-30 16:43:10 -05:00
Dave Collins
6695cd15bb Export the IsCoinbase function. 2013-09-30 16:42:28 -05:00
Dave Collins
4eb135618a Export the IsFinalizedTransaction function. 2013-09-30 16:40:19 -05:00
Dave Collins
fc69776371 Expose a transaction store and related functions.
Several of the functions require a map of contextual transaction data to
use as a source for referenced transactions.  This commit exports the
underlying TxData type and creates a new type TxStore, which is a map of
points to the under TxData.  In addition, this commit exposes a new
function, FetchTransactionStore, which returns a transaction store
(TxStore) containing all of the transactions referenced by the passed
transaction, as well as the existing transaction if it already exists.

This paves the way for subsequent commits which will expose some of the
functions which depend on this transaction store.
2013-09-30 16:37:11 -05:00
Dave Collins
117765ba7c Correct error message in other input tx lookup. 2013-08-28 13:50:47 -05:00
Dave Collins
ffa56b437c Correct error msg for missing input transaction.
The error message for missing input transaction had the referenced and
referencing transactions backwards.
2013-08-28 13:46:17 -05:00
Dave Collins
10a62a37a3 Update for latest btcutil, btcscript, and btcwire.
This commit updates the calls into btcutil, btcscript, and btcwire for the
latest API changes which remove the need for the protocol version for
serialization and deserialization of blocks and transactions.
2013-08-05 18:53:50 -05:00
Dave Collins
cef901ebad Export chain parameters.
This commit exports the chain parameters used depending on the specific
bitcoin network so callers can have access to this information if desired.
2013-07-30 12:38:27 -05:00
Dave Collins
829f187e47 Use network-specific genesis hash.
The test in checkConnectBlock for whether or not the node is the genesis
block needs to account for the genesis block of the specified network.
2013-07-29 20:21:24 -05:00
Dave Collins
04d88d01ec Clarify mismatched merkle root error message. 2013-07-29 19:50:00 -05:00