Commit graph

149 commits

Author SHA1 Message Date
Dave Collins
a22da99f91 Convert script errors to RuleErrors.
This commit modifies the error return type for errors during script
validation to use the RuleError so they are consistent with the rest of
the errors.  This also helps the calling code differentiate blocks
rejected due to script parsing and validation errors as opposed to
internal issues such as inability to read from the disk.

To accomplish this, two new two new RuleErrors, ErrScriptMalformed and
ErrScriptValidation, have been added.

Also, the errors for script parsing issues and script validation errors
have been improved to include both transaction hashes and indexes involved
in the validation effort.  Previously script parsing issues had almost no
additional information as to which transaction input/outputs the failing
script came from.
2014-06-26 23:47:14 -05:00
Dave Collins
cf3ad14d4d Remove TODO that is complete. 2014-06-26 20:31:32 -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
67394ec45d Modify ProcessBlock to accept behavior flags.
This commit change the ProcessBlock function to accept a new type named
BehaviorFlags in place of the current fastAdd parameter.

This has been done to pave the way for adding more control over the checks
that are performed such as avoiding the proof of work checks which will be
in an upcoming commit.  A bitmask was chosen because it will allow the
ProcessBlock API to remain a little more stable since new flag additions
that change the behavior are only likely to be used by new code because
adding flags does not change the existing behavior.

ok @jrick
2014-06-26 17:17:32 -05:00
Dave Collins
7d84d801d7 Make golint happy. 2014-06-26 16:10:10 -05:00
Dave Collins
415ac4596a Make orphan ntfns to a return val on ProcessBlock.
This commit changes the way that orphan blocks are identified by adding a
new boolean return value on ProcessBlock and removing the notification for
NTOrphanBlock.

This allows the calling code to identify orphan blocks immediately instead
of having to setup a seperate callback handler and implementing some type
of state tracking.  This, in turn, allows cleaner code for handling them.

In addition, the tests have been updated for the new function signature
and also now check that each block is or is not an orphan as expected
which makes the tests more robust.

ok @jrick
2014-06-25 17:46:00 -05:00
Dave Collins
0550bbbdc5 Add tests for new RuleError and ErrorCode types. 2014-06-24 19:14:30 -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
87bef61b30 Update tests to use btcnet genesis params. 2014-05-28 00:56:57 -05:00
Dave Collins
b0b090009a Update README.md for btcnet changes.
The sample code in doc.go was updated for the recent btcnet changes,
however the sample code in the README.md was not.
2014-05-28 00:56:06 -05:00
Dave Collins
f37a5e76b5 Update tests for recent API changes. 2014-05-27 10:15:35 -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
Dave Collins
d4082e4f24 Add checkpoint at block height 300255. 2014-05-23 13:44:23 -05:00
David Hill
0cdaefd4cc export HashMerkleBranches
ok @davecgh
2014-05-22 13:29:39 -04: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
96f9b88935 Export CalcPastMedianTime function.
This commit makes a slight variant of the existing calcPastMedianTime
function available to external callers.  The new exported version
calculates the past median time from the end of the current main chain
versus an arbitrary block node.
2014-03-15 15:04:59 -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
David Hill
936b4eaa2d add unit testing for BigToCompact, CompactToBig, and CalcWork 2014-03-11 22:33:56 -04: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
acdce27f9f Defer unlock of orphan mutex in addOrphanBlock.
This was pointed out by saracen on the btcd IRC channel.
2014-03-09 11:05:12 -05:00
Dave Collins
b041971ca8 Export CalcNextRequiredDifficutly function.
This commit exports a new variant of the existing internal
calcNextRequiredDifficulty function which calculates and returns the next
required difficulty for a block after the end of the current best chain
based on the difficulty retarget rules.

In order to support the exported function the internal one was slightly
modified to accept the block timestamp instead of an entire block since
the timestamp is all that is needed and the caller of the exported
function might next have a full block yet.
2014-03-02 18:43:41 -06: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
b25bf566b0 Rename findLatestKnownCheckpoint.
The name findLatestKnownCheckpoint is confusing and doesn't really convey
the fact the purpose of the function is to the find the checkpoint prior
to the current known block.

Therefore, rename the function to findPreviousCheckpoint for clarity.

Also, update some comments to follow suit.
2014-02-21 15:16:41 -06:00
Dave Collins
50b6e10b57 Reject blocks that fork before previous checkpoint.
This commit adds an additional check to the block acceptance rules which
prevents new blocks that fork the main chain before the previous known
good checkpoint.  This prevents storage of new, otherwise valid, blocks
from building off of old blocks which are likely at a much easier
difficulty and therefore could be used to waste cache and disk space.

Note this is slightly different than the other existing check which
prevents blocks with a timestamp before the timestamp of the latest known
good checkpoint since that check effectively prevents old side chain
blocks that already existed (per the claimed timestamp).

ok drahn@
2014-02-21 15:11:22 -06:00
Dave Collins
149d8176b0 Fix an issue causing excessive memory consumption.
This commit resolves an issue where the block node index was forcing
entire blocks to be kept in memory thereby forcing excessive memory usage.
For example, prior to this change, the memory usage could consume upwards
of 1.5GB while importing bootstrap.dat via the addblock utility.  With
this change the entire import takes <150MB.  This also has the same memory
reduction to btcd since it uses the same code path.
2014-02-09 01:36:56 -06:00
Dave Collins
190ef70ace Export CalcWork and BlocksPerRetarget.
This commit makes the CalcWork function and BlocksPerRetarget variable
available to external packages.
2014-02-07 16:29:19 -06:00
David Hill
e1f66f6103 gofmt 2014-02-04 16:16:53 -05:00
Dave Collins
bb4ea7d59a Include in/out script bytes in script val error. 2014-02-03 20:27:13 -06:00
Dave Collins
b7ef1f0946 Include transaction hash in failed input val error. 2014-02-03 20:03:06 -06:00
Dave Collins
be2e4c5860 Add new function to get the checkpoints.
Previously there was only a function to get the latest checkpoint.  This
commit exposes a new function named Checkpoints which returns a slice of
checkpoints ordered by height for the active network or nil when
checkpoints are disabled.
2014-01-31 19:52:20 -06:00
Dave Collins
6a9997583a Prune the block node index in fast add mode too.
The recent addition of the fast add path to support headers first was not
running the block node index pruning code which removes unneeded block
nodes from memory.  This resulted in higher memory usage than needed in
fast add mode.
2014-01-28 13:46:55 -06:00
Dave Collins
7390a62a8d Correct internal node children on reorg.
Previously the code was only adding a new block node as a child in the
inernal node index for the cases it extended the main chain or a side
chain and not for a node which caused a reorg.  This resulted in the block
node pruning code not clearing the parent link of reorged nodes which
ultimately led to a sanity check error accordingly.

This commit resolves the issue by ensuring new block nodes are added as
children of their respective parents in all cases.

Closes #4.
2014-01-28 13:44:39 -06:00
David Hill
ad65bee735 fix typo 2014-01-22 13:17:41 -05:00
Dave Collins
d0d74b4e3e Add checkpoint at block height 279000. 2014-01-20 11:03:11 -06:00
Dave Collins
e2dba2a074 Update for recent btcdb API changes. 2014-01-19 20:13:53 -06:00
Dave Collins
583406ee51 Use block headers to generate node index.
This commit changes the node index creation to use block headers instead
of full blocks.  This speeds up the initial node index generation since it
doesn't require loading a bunch of full blocks at startup.
2014-01-19 13:04:07 -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
be277b7230 Update for recent btcwire API changes. 2014-01-19 12:32:30 -06:00
Dave Collins
1b54badde1 Improve efficiency of checkpoint tracking.
Previously the code performed a database query for every checkpoint (going
backwards) to find the latest known checkpoint on every block.  This was
particularly noticabled near the beginning of the block chain when there
are still several checkpoints that haven't been reached yet.

This commit changes the logic to cache the latest known checkpoint while
keeping track of when it needs to be updated once a new later known
checkpoint has been reached.

While here, also add a log message when a checkpoint has been reached and
verified.
2014-01-17 09:59:52 -06:00
Dave Collins
4c13410e62 Update README.md TODO. 2014-01-17 00:51:42 -06:00
Dave Collins
03394d6309 Bring README.md and doc.go examples up to date.
Since sqlite is now deprecated, update the examples to use levelb.  Also,
add false to the ProcessBlock calls for the fastAdd parameter.
2014-01-17 00:48:32 -06:00