Commit graph

176 commits

Author SHA1 Message Date
Dave Collins
8be23c89ae Cleanup a few comments. 2013-10-11 10:24:13 -05:00
Dave Collins
39e7e5c4a1 Add minor optimization to transaction store fetch.
This commit adds a quick check to the transaction store fetch code which
simply returns an empty store if no hashes were requested rather than
bothering the db with an empty list.
2013-10-11 10:22:40 -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
32790d52d8 Add tests for HaveBlock.
This commit adds tests to ensure HaveBlock works properly with blocks on
the main chain, a side chain, orphans, and missing blocks.
2013-10-09 18:43:22 -05:00
Dave Collins
6d078d8115 Implement chain setup infrastructure in the tests.
This commit implents a basic infrastructure to be used throughout the
tests for creating a new chain instance that is ready to have tests run
against it.  It also returns a teardown function the caller can use to
clean up after it is done testing.  This paves the way for adding more
tests.
2013-10-09 18:29:53 -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
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
bce548cb99 Update reorg test to insert genesis block first.
btcdb was changed a while back to not insert the genesis block by default.
This commit modifies the reorg test to insert it as required so not all
blocks are orphans.
2013-10-06 14:23:09 -05:00
Dave Collins
4151416b16 Change processing block log message to trace.
When running debug level logging, the only thing that really matters is
whether the block was accepted or not.
2013-10-04 13:41:24 -05:00
Dave Collins
9841403c58 Correct comment on fetchTxListMain. 2013-10-04 12:11:58 -05:00
Dale Rahn
d58fb25d98 Update btcchain for btcdb API change. 2013-10-03 16:44:07 -04: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
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
b04b4c27ea Update the TODO section of README.md to latest. 2013-09-30 17:00:23 -05:00
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