Commit graph

41 commits

Author SHA1 Message Date
Dave Collins
73228aaebe Update for recent btcdb API changes.
Since the underlying database driver can now return an error when looking
up if blocks and transactions exist, the HaveBlock function now includes
an error return to allow any underlying errors to be exposed.
2014-07-07 11:48:41 -05:00
Tomás Senart
4772d4a1a4 goimports -w . 2014-07-02 19:14:27 -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
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
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
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
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
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
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
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
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
28f485a1d1 Fix a couple of style nits. 2014-01-09 11:36:50 -06:00
Dave Collins
18ac5c848a Add 2014 to copyright dates. 2014-01-08 23:52:54 -06:00
Dale Rahn
992d11830c Implement a fast path for the Initial Block Download.
It is not necessary to do all of the transaction validation on
blocks if they have been confirmed to be in the block chain leading
up to the final checkpoint in a given blockschain.

This algorithm fetches block headers from the peer, then once it has
established the full blockchain connection, it requests blocks.
Any blocks before the final checkpoint pass true for fastAdd on
btcchain operation, which causes it to do less valiation on the block.
2013-12-12 07:54:48 -05:00
Dave Collins
c88338d227 Slightly optimize GenerateInitialIndex.
Rather than fetching the hash of each block individually 2k+ times, make
use of the FetchHeightRange function so all of the most recent hashes can
be fetched at once.
2013-10-13 16:07:54 -05:00
Dave Collins
8be23c89ae Cleanup a few comments. 2013-10-11 10:24:13 -05:00
Owain G. Ainsworth
cdc3002c85 Fix previous. We care if we have the block OR it is an orphan, not both. 2013-10-09 16:35:33 +01:00
Dave Collins
b911e7e455 Replace HaveInventory with HaveBlock.
The original thought was that chain would also house the transaction
memory pool, but that ultimately was decided against.  As a result,
it only makes sense to query chain for blocks rather than generic
inventory.
2013-10-08 12:32:15 -05:00
Dave Collins
b06aa1672a Initial implementation of IsCurrent func.
This commit adds a new function, IsCurrent, which can be used to determine
whether or not the chain believes it is current.

Update.
2013-10-03 10:05:44 -05:00
Dave Collins
667eaa1562 Move to a callback for notifications.
Rather than using a channel for notifictions, use a callback instead.
There are several issues that arise with async notifications via a
channel, so use a callback instead.  The caller can always make the
notifications async by using channels in the provided callback if
needed.
2013-10-02 21:16:38 -05:00
Dave Collins
5efc3ea23f Remove a couple of outdated comments. 2013-09-30 16:50:10 -05:00
Dale Rahn
00b183a8b5 Remove unreachable statement. ok davec@ 2013-09-19 15:09:18 -04:00
Dave Collins
fc1b2e54d3 Set best chain during GenerateInitialIndex.
This commit modifies the GenerateInitialIndex function to update the best
chain with each node as it is loaded.  This allows the best chain to be
set immediately upon generating the initial index which is a slight
performance optimization.
2013-08-28 10:42:38 -05:00
Dave Collins
d0be0ed5ed Add HaveInventory function.
This commit provides a new exported function, HaveInventory, which can
be used to determine if the already has the item referenced by passed
inventory vector.  Currently it only provides support for blocks and
cursory support for transactions in the main chain.  Types that are
unrecognized are returned as if they unknown as expected.
2013-08-22 16:14:10 -05:00
Dave Collins
5c6911c775 Add IsKnownOrphan function.
This commit provides a new exported function, IsKnownOrphan, which can
be used to determine if the passed hash is currently already known to the
chain as an orphan.
2013-08-22 14:47:58 -05:00
Dave Collins
9d5f855580 Export GetOrphanRoot.
This commit exposes GetOrphanRoot to callers.  It also modifies the code
to allow the function to be called in a concurrent safe manner.  This
allows the information to be safely obtained from multiple goroutines.
2013-08-22 14:47:24 -05:00
Dave Collins
f4bae7dc41 Send orphan root in notifies instead of its parent.
The notification for orphan blocks was sending the parent of orphan root
instead of the orphan root itself.
2013-08-22 14:37:06 -05:00
Dave Collins
2835238287 Correct child and orphan block removal logic.
It is not safe to remove elements from a slice while iterating them with
the range statement since it does not reevaluate the slice on each
iteration nor does it adjust the index for the modified slice.  This
commit modifies the code to use a for loop with an index (which does
reevaluate on every iteration) and manually adjusts the index when
elements are removed from the slice.
2013-08-17 14:05:36 -05:00
Dave Collins
c00de3ffd5 Fix a potential issue with removing orphan blocks.
The range loop is over a local var, so the local variable needs to be
updated and then set back into the chain instance after the range loop.
2013-08-04 13:41:05 -05:00
Dave Collins
0c2d4435ca Make use of removeChildNode function.
Rather than duplicating this code in the removeBlockNode function,
use the existing function.
2013-08-01 22:28:54 -05:00
Dave Collins
1a5683e220 Add optimized initial block index load function.
This commit adds a new optional function named GenerateInitialIndex.  It
generates the required number of initial block nodes in an optimized
fashion.  Since the memory block index is sparse and previous nodes are
dynamically loaded as needed, this function is not stricty needed.
However, during initial startup (when there are no nodes in memory yet),
dynamically loading all of the required nodes on the fly in the usual way
is much slower than preloading them.
2013-08-01 18:00:35 -05:00
Dave Collins
5b697947a7 Optimize getPrevNodeFromNode.
The recent pruning code made the parent hash for a node available directly
as a field in the node.  Make use of this in the getPrevNodeFromNode
function to avoid having to load the full block data associated with the
node to get the parent hash.
2013-08-01 12:34:59 -05:00
Dave Collins
2988289d2e Implement memory chain pruning.
This commit implements pruning for the block nodes that form the memory
chain which are no longer needed.  The validation of a block only requires
information from a set number of previous nodes.  The initial code did not
prune old nodes as they were no longer needed, but the vast majority of
the infrastructure to support it was already in place as that was always
the goal.  This commit finishes implementing the missing bits to make it
a reality.
2013-08-01 11:31:52 -05:00
Dave Collins
2d6a664d9d Greatly optimize best chain selection work values.
Previously, the code was using big rational numbers for work values which
resulted in carrying way too much precision around (and ultimately a lot
of extra memory and computation to carry that precision).  This commit
converts the work values to big integers and calculates them with integer
division.  This is acceptable because the numerator is multiplied by 2^256
which is higher than the maximum possible proof of work.  Therefore
anything after the decimal is superfluous precision for the purposes of
chain selection.

Also, add a check for negative difficulty values when calculating the work
value.  Negative values won't occur in practice with valid blocks, but
it's possible an invalid block could trigger the code path, so be safe and
check for it.
2013-07-31 13:07:06 -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
0e7791058a Add logging for chain forks and reorgs.
This commit adds info level log statements when a block causes a chain
fork, extends a side chain (fork), or causes a reorganize.  When a reorg
happens, the new and old chain heads along with the fork point are logged.
2013-07-29 22:32:12 -05:00
Dave Collins
1deeb05627 Add a few log error messages.
In addition to returning errors to the caller, log the error with a prefix
in a few key places that helps identify the origin for errors.  In some
cases, the underlying error comes from a different subsystem such as the
SQL database driver and the error messages can be fairly generic.
2013-07-24 12:31:14 -05:00
Dave Collins
f219ed5baf Add support for params based on active network.
This commit modifies the code to use params based on the active network
which paves the way for supporting the special rules and different genesis
blocks used by the test networks.
2013-07-24 12:26:17 -05:00
Dave Collins
aa5847f3cc Initial implementation. 2013-07-19 08:50:13 -05:00