Commit graph

60 commits

Author SHA1 Message Date
Dave Collins
5efc3ea23f Remove a couple of outdated comments. 2013-09-30 16:50:10 -05:00
Dave Collins
baa4c36618 Export ValdiateTransactionScript function.
This function allows the caller to execute and validate all scripts for
the passed transaction (which includes signature verification).
2013-09-30 16:47:37 -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
2f743b4821 Pre-allocate space for high use slices.
This commit modifies the code to choose sane defaults for the backing
arrays for slices that involve a lot of appends such block locators, hash
processing, and needed transactions.  This is an optimization to avoid
the overhead of growing the backing arrays and copying the data multiple
times in the most common case.  This also prevents a leak in Go GC which
will likely ultimatley be fixed, but the efficiecy gains alone are worth
the change.
2013-09-25 20:20:53 -05:00
Dave Collins
d1f1fe0752 Revert "Send notification in their own goroutine."
After discussion with others and thinking about the notification channel
some more, we've decided to leave it up to the caller to quickly handle
notifications.  While it is true that notification should be handled
quickly to not block the chain processing code unnecessarily, launching a
goroutine in chain means the notifications are no longer necessarily in
order.  Also, if the caller is not properly handling the notifications,
the goroutines end up sicking around forever.  By leaving it up to the
caller to quickly handle the notification or launch a goroutine as
necessary for the caller, it provides the flexibility to ensure proper
notification ordering as well as control over other things such as
how to handle backpressure.
2013-09-21 09:23:55 -05:00
Dale Rahn
00b183a8b5 Remove unreachable statement. ok davec@ 2013-09-19 15:09:18 -04:00
Dave Collins
b557a33f7c Send orphan hash in notifications instead of root.
Rather than sending the root of the an orphan chain when sending an orphan
notification, send the hash of the orphan block itself.  The caller can
then call the GetOrphanRoot function with the hash to get the root of the
orphan chain as needed.  This is being changed since it's cleaner and more
detministic sending the hash for the orphan block that was just processed
rather than sending a possibly different hash depending on whether there
is an orphan chain or not.
2013-09-09 19:35:36 -05:00
Dave Collins
0ba8cb9187 Send notification in their own goroutine.
Since the notification channel is provided by the caller and it may or may
not be buffered, send notifications in their own goroutine so the chain
processing code does not have to wait around on the caller to process the
notification before continuing.
2013-09-06 11:05:04 -05:00
Dave Collins
49f6b7d35d Add checkpoint at block 250000. 2013-08-29 12:01:19 -05:00
Dave Collins
4ac899e26f Clear disconnected transactions.
Rather than removing disconnected transactions from the node viewpoint
transaction store, clear the entry instead.  This is needed for the
connect code to update transactions that were removed on the other side of
a chain fork.
2013-08-29 08:56:27 -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
fc804aaec0 Add block locator infastructure.
This commit adds a new type, BlockLocator, and supporting infrastructure
to support generating block locators from a provided hash and getting
a full block locator for the latest known block.  This will likely be
expanded in the future to provide the opposite functionality as well.
That is to say the ability to lookup a block using a block locator.
2013-08-28 11:45:21 -05: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
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
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
716075d0d0 Remove tesnet specific rules unsupported blurb.
The testnet specific rules have been supported since commit
9787f46f6a.
2013-08-01 17:10:59 -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
455c92a716 Correct reg test proof of work limit comment. 2013-07-31 09:20:11 -05:00
Dave Collins
72feef5194 Clarify reg test proof of work limit comment. 2013-07-31 09:14:02 -05:00
Dave Collins
bb7f5da609 Print transaction hash in script val errors.
The txsha variable is already a pointer to the hash, so print the
variable, not the address of it.
2013-07-30 18:04:58 -05:00
Dave Collins
357160257c Allow checkpoints to work with all networks.
Previously the main network checkpoints were being used for unrecognized
networks.  This commit changes the code so that no checkpoints are used in
that scenario.
2013-07-30 15:29:15 -05:00
Dave Collins
1e891b4e0b Optimize proof of work limit bits handling.
Rather than converting the proof of work limit to its compact
representation multiple times during operation, do it once at package
initialization time and export it via the chain parameters.
2013-07-30 12:59: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
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
e5eaaee91c Remove a couple of error logs that are not needed. 2013-07-29 22:31:05 -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
Dave Collins
077e1ec336 Improve second coinbase error message.
Include the index at which the second coinbase transaction was found.
2013-07-29 17:02:42 -05:00
Dave Collins
882d1c1687 Ensure height is set on block as well as the node.
Even though the code currently makes heavy use of nodes which will have
the appropriate height, blocks which did not come from the database will
not have a height set.  As a result, this could possibly result in a
height of 0 being used when unintended.  By setting the block height in
the block as soon as its known (regardless of the source), we can future
proof against bugs that would likely be hard to track down.
2013-07-29 16:58:48 -05:00
Dave Collins
6409879e14 Modify count sigops calls for new btcscript API.
The btcscript API for GetSigOpCount and GetPreciseSigOpCount
recently changed to no longer return an error.  This change was necessary
to mirror the behavior of bitcoind signature operations counting.  This
commit modifies the code accordingly to use the new API.
2013-07-29 16:21:33 -05:00
Dave Collins
c3b330e42d Allow tx inputs to use txns earlier in same block.
It is acceptable for a transaction input in a block to reference the
output of another transaction in the same block only if the referenced
transaction comes before the transaction referencing it.
2013-07-29 15:48:52 -05:00
Dave Collins
e82c97be3b Update examples for recent btcdb changes.
Since creating a new database with btcdb no longer automatically inserts
the main network genesis block, update the examples accordingly.
2013-07-29 12:35:01 -05:00
Dave Collins
761f666c44 Use consistent form for Checkpoint comment. 2013-07-27 16:46:46 -05:00
Dave Collins
ace58495c8 Comment CheckpointConfirmations. 2013-07-27 16:45:59 -05:00
Dave Collins
b282678d9a Spend outputs while checking transaction inputs.
This commit modifies the double spend detection to handle double spends
within the same block as well as side chains when doing the checks before
reorganizing the chain.
2013-07-26 11:51:13 -05:00
Dave Collins
9787f46f6a Implement testnet specific rules.
This commit adds support for testnet specific rules when a new BlockChain
is created against testnet.
2013-07-24 17:03:29 -05:00