Commit graph

99 commits

Author SHA1 Message Date
Dave Collins
84f6089bc9 Rework and improve async script validation logic.
The previous script validation logic entailed starting up a hard-coded
number of goroutines to process the transaction scripts in parallel.  In
particular, one goroutine (up to 8 max) was started per transaction in a
block and another one was started for each input script pair in the
each transaction.  This resulted in 64 goroutines simultaneously running
scripts and verifying cryptographic signatures.  This could easily lead to
the overall system feeling sluggish.

Further the previous design could also result in bursty behavior since the
number of inputs to a transaction as well as its complexity can vary
widely between transactions.  For example, starting 2 goroutines (one to
process the transaction and one for actual script pair validation) to
verify a transaction with a single input was not desirable.

Finally, the previous design validated all transactions and inputs
regardless of a failure in one of the other scripts.  This really didn't
have a big impact since it's quite rare that blocks with invalid
verifications are being processed, but it was a potential way DoS vector.

This commit changes the logic in a few ways to improve things:

- The max number of validation goroutines is now based on the number of
  cores in the system
- All transaction inputs from all transactions in the block are collated
  into a single list which is fed through the aforementioned validation
  goroutines
- The validation CPU usage is much more consistent due to the collation of
  inputs
- A validation error in any goroutine immediately stops validation of all
  remaining inputs
- The errors have been improved to include context about what tx script
  pair failed as opposed to showing the information as a warning

This closes conformal/btcd#59.
2014-01-16 20:02:30 -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
55331de532 Configure TravisCI to pull pkgs needed for tests. 2013-12-10 16:10:14 -06:00
Dave Collins
decc1e8c69 Add support for TravisCI.
Also add TravisCI build status badge to README.md.
2013-12-09 04:44:34 -06:00
Dave Collins
f7f51a1e43 Use btclog for logging. 2013-11-21 17:44:35 -06:00
Owain G. Ainsworth
8a19f269ca use passed in parameter and not global in log message.
Should shut up the race detector (and make the printf more correct)
2013-11-21 18:23:45 +00:00
Dave Collins
2300b35731 Add checkpoint at block height 267300. 2013-11-12 20:11:55 -06:00
Dave Collins
d0bbcacfc0 Modify the tests to use memory database backend.
This commit modifies the tests to setup a chain instance backed by the new
memory database backend for btcdb.  This allows the tests to avoid
creating and cleaning up files and also allows the tests to run faster
since it can all happen in memory.

The chainSetup function has also been changed to provide logic to switch
on the database type to allow for easy changing of the backend to a
different database type as needed.  For example, it could be useful to
provide extra testing against new database backends.
2013-11-08 02:52:50 -06:00
Dave Collins
5295be070d Make the CheckBlockSanity function context free.
Rather than defining CheckBlockSanity as a member of a BlockChain
instance, define it at the root level so it is truly context free as
intended.  In order to make it context free, the proof of work limit is
now a required parameter.
2013-11-07 16:06:01 -06:00
Dave Collins
09b53a8fca Export the CheckBlockSanity function. 2013-11-07 15:39:48 -06:00
David Hill
36941cc427 Remove the satoshi consts and use them from btcutil instead. 2013-10-29 16:08:12 -04:00
David Hill
8271a11808 add SatoshiPerBitcent constant and export SatoshiPerBitcoin and
MaxSatoshi
2013-10-29 14:57:31 -04:00
Dave Collins
6165e9b95b Convert API to use new btcutil.Tx.
This is part of the ongoing transaction hash optimization effort noted in
conformal/btcd#25.
2013-10-28 15:17:53 -05:00
Dave Collins
b6e4ae4441 Optimize duplicate transaction input check.
Profiling showed the duplicate transaction input check was taking around
6% of the total CheckTransactionSanity processing time.  This was largely
due to using fmt.Sprintf to generate the map key.

This commit modifies the check instead to use the actual output as a map
key.

The following benchmark results show the difference:

Before: BenchmarkOldDuplicatInputCheck    100000     21787 ns/op
After:  BenchmarkNewDuplicatInputCheck   2000000       937 ns/op

Closes #2
2013-10-26 14:31:15 -05:00
Dave Collins
89d86d07ac ValidateTransactionScripts now accepts script flags.
This commit modifies the ValidateTransactionScripts API to accept the
recently added ScriptFlags from btcscript.  This provides flexibility to
the caller to choose validation behavior based on those new flags.
2013-10-25 10:49:06 -05:00
Dave Collins
b7b9823f6c Fix potential issue with dependent orphans.
This commit modifies the main processing loop for orphan dependencies
(orphans that are processed due to their parents showing up) to use an
index based for loop over range.  Since the Go range statement does not
reevaluate on every iteration, it was previously possible under certain
circumstances for the slice to be changed out from under the range
statement while processing the orphan blocks.
2013-10-15 09:50:51 -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
d7e057a020 Make use of new spent vs unspent btcdb APIs.
This commit modifies the code to make use of the new btcd APIs that allow
fetching of transaction lists which either do or do not include fully
spent transactions.  It is more efficient to avoid fetching fully spent
transactions from the database when they aren't needed.
2013-10-13 02:40:02 -05:00
Dave Collins
9a29855c16 Convert inability to find input tx to a RuleError.
This commit modifies the errors that result from missing expected input
transactions to a RuleError.  This allows the caller to detect a block was
rejected due to a rule violation as opposed to an unexpected error.
2013-10-13 02:38:46 -05:00
Dave Collins
e5ba199eed Add example to BlockLocator comments. 2013-10-13 02:06:42 -05:00
Dale Rahn
b680d3539f Revive old validate Tx in parallel code, faster...
requested by davec, cleanup/polish in-tree.
2013-10-11 18:31:54 -04:00
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