Commit graph

18 commits

Author SHA1 Message Date
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