Commit graph

2317 commits

Author SHA1 Message Date
Dave Collins
491acd4ca6 blockchain: Rework to use new db interface.
This commit is the first stage of several that are planned to convert
the blockchain package into a concurrent safe package that will
ultimately allow support for multi-peer download and concurrent chain
processing.  The goal is to update btcd proper after each step so it can
take advantage of the enhancements as they are developed.

In addition to the aforementioned benefit, this staged approach has been
chosen since it is absolutely critical to maintain consensus.
Separating the changes into several stages makes it easier for reviewers
to logically follow what is happening and therefore helps prevent
consensus bugs.  Naturally there are significant automated tests to help
prevent consensus issues as well.

The main focus of this stage is to convert the blockchain package to use
the new database interface and implement the chain-related functionality
which it no longer handles.  It also aims to improve efficiency in
various areas by making use of the new database and chain capabilities.

The following is an overview of the chain changes:

- Update to use the new database interface
- Add chain-related functionality that the old database used to handle
  - Main chain structure and state
  - Transaction spend tracking
- Implement a new pruned unspent transaction output (utxo) set
  - Provides efficient direct access to the unspent transaction outputs
  - Uses a domain specific compression algorithm that understands the
    standard transaction scripts in order to significantly compress them
  - Removes reliance on the transaction index and paves the way toward
    eventually enabling block pruning
- Modify the New function to accept a Config struct instead of
  inidividual parameters
- Replace the old TxStore type with a new UtxoViewpoint type that makes
  use of the new pruned utxo set
- Convert code to treat the new UtxoViewpoint as a rolling view that is
  used between connects and disconnects to improve efficiency
- Make best chain state always set when the chain instance is created
  - Remove now unnecessary logic for dealing with unset best state
- Make all exported functions concurrent safe
  - Currently using a single chain state lock as it provides a straight
    forward and easy to review path forward however this can be improved
    with more fine grained locking
- Optimize various cases where full blocks were being loaded when only
  the header is needed to help reduce the I/O load
- Add the ability for callers to get a snapshot of the current best
  chain stats in a concurrent safe fashion
  - Does not block callers while new blocks are being processed
- Make error messages that reference transaction outputs consistently
  use <transaction hash>:<output index>
- Introduce a new AssertError type an convert internal consistency
  checks to use it
- Update tests and examples to reflect the changes
- Add a full suite of tests to ensure correct functionality of the new
  code

The following is an overview of the btcd changes:

- Update to use the new database and chain interfaces
- Temporarily remove all code related to the transaction index
- Temporarily remove all code related to the address index
- Convert all code that uses transaction stores to use the new utxo
  view
- Rework several calls that required the block manager for safe
  concurrency to use the chain package directly now that it is
  concurrent safe
- Change all calls to obtain the best hash to use the new best state
  snapshot capability from the chain package
- Remove workaround for limits on fetching height ranges since the new
  database interface no longer imposes them
- Correct the gettxout RPC handler to return the best chain hash as
  opposed the hash the txout was found in
- Optimize various RPC handlers:
  - Change several of the RPC handlers to use the new chain snapshot
    capability to avoid needlessly loading data
  - Update several handlers to use new functionality to avoid accessing
    the block manager so they are able to return the data without
    blocking when the server is busy processing blocks
  - Update non-verbose getblock to avoid deserialization and
    serialization overhead
  - Update getblockheader to request the block height directly from
    chain and only load the header
  - Update getdifficulty to use the new cached data from chain
  - Update getmininginfo to use the new cached data from chain
  - Update non-verbose getrawtransaction to avoid deserialization and
    serialization overhead
  - Update gettxout to use the new utxo store versus loading
    full transactions using the transaction index

The following is an overview of the utility changes:
- Update addblock to use the new database and chain interfaces
- Update findcheckpoint to use the new database and chain interfaces
- Remove the dropafter utility which is no longer supported

NOTE: The transaction index and address index will be reimplemented in
another commit.
2016-04-11 16:47:27 -05:00
David Hill
123ff368f4 mempool: Create and use mempoolPolicy. (#571)
mempoolPolicy contains the values that configure the mempool policy.
This decouples the values from the internals of btcd to move closer
to a mempool package.
2016-04-11 16:37:52 -05:00
Dave Collins
5ff5fc5fa2 txscript: Correct comments on alt stack methods. (#657) 2016-04-11 14:22:25 -05:00
Dave Collins
1d83cd5721 chaincfg: Consolidate tests into the chaincfg pkg. (#662)
Putting the test code in the same package makes it easier for forks
since they don't have to change the import paths as much and it also
gets rid of the need for internal_test.go to bridge.

This same thing should probably be done for the majority of the code
base.
2016-04-11 14:21:40 -05:00
Dave Collins
c7e5d56b58 chaincfg: Register networks instead of hard coding. (#660)
This modifies the chaincfg package to register the default network
params via the init function instead of manually hard coding their data
into the maps.  This is less error prone when adding new default
networks.

A new function named mustRegister has been introduced that panics if
there are any errors when registering the network that the new code
makes use of and appropriate tests have been added.
2016-04-11 12:34:28 -05:00
Dave Collins
23f59144c7 server: Optimize map limiting in block manager. (#658)
This optimizes the way in which the maps are limited by the block
manager.

Previously the code would read a cryptographically random value large
enough to construct a hash, find the first entry larger than that value,
and evict it.

That approach is quite inefficient and could easily become a bottleneck
when processing transactions due to the need to read from a source such
as /dev/urandom and all of the subsequent hash comparisons.

Luckily, strong cryptographic randomness is not needed here. The primary
intent of limiting the maps is to control memory usage with a secondary
concern of making it difficult for adversaries to force eviction of
specific entries.

Consequently, this changes the code to make use of the pseudorandom
iteration order of Go's maps along with the preimage resistance of the
hashing function to provide the desired functionality.  It has
previously been discussed that the specific pseudorandom iteration order
is not guaranteed by the Go spec even though in practice that is how it
is implemented.  This is not a concern however because even if the
specific compiler doesn't implement that, the preimage resistance of the
hashing function alone is enough.

Thanks to @Roasbeef for pointing out the efficiency concerns and the
fact that strong cryptographic randomness is not necessary.
2016-04-11 10:29:07 -05:00
Dave Collins
a3fa066745 mining: Export block template fields. (#659)
This simply exports and adds some comments to the fields of the
BlockTemplate struct.

This is primarily being done as a step toward being able to separate the
mining code into its own package, but also it makes sense on its own
because code that requests new block template necessarily examines the
returned fields which implies they should be exported.
2016-04-11 10:27:29 -05:00
David Hill
cab74feb59 Keep track of recently rejected transactions. (#484)
This prevents the node from repeatedly requesting and rejecting the
same transaction as different peers inv the same transaction.

Idea from Bitcoin Core commit 0847d9cb5fcd2fdd5a21bde699944d966cf5add9

Also, limit the number of both requested blocks and transactions.
2016-04-10 01:12:57 -05:00
David Hill
d1e493f4ee config: New option --blocksonly (#553)
The --blocksonly configuration option disables accepting transactions
from remote peers.  It will still accept, relay, and rebroadcast
valid transactions sent via RPC or websockets.
2016-04-07 18:16:46 -05:00
David Hill
7b31349023 Cleanup and optimize handleBroadcastMsg 2016-04-07 14:50:42 -04:00
Dave Collins
69839adc1c TravisCI: Remove external go vet reference. (#655)
The vet tool moved into the Go source tree as of Go 1.5.  Its previous
location in the x/tools repo was deprecated at that time and has now
been removed.

This commit updates the .travis.yml configuration to avoid fetching vet
from the old location and to simply use the version now available as
part of the standard Go install.

Also, while here, remove the check for changing the tool path since it
is no longer needed.
2016-04-07 13:21:14 -05:00
David Hill
8a58f8cf3a peer: Implement sendheaders support (BIP0130).
This modifies the peer package to add support for the sendheaders
protocol message introduced by BIP0030.

NOTE: This does not add support to btcd itself. That requires the server
and sync code to make use of the new functionality exposed by these
changes.  As a result, btcd will still be using protocol version 70011.
2016-04-06 16:56:48 -05:00
David Hill
c1861bc8fa peer: declare QueueMessage()'s doneChan as send only.
This ensures the channel passed to QueueMessage is writable and that
QueueMessage will not read from the channel (write-only).

This change is merely a safety change.  If a user of the API passes
a read-only channel to QueueMessage, it will now be caught at compile
time instead of panicking during runtime.

Also update internal functions.
2016-04-06 13:50:27 -05:00
Jonathan Gillham
391d5e4a01 server: Stop main loop from blocking when RPC server is not running.
When the RPC server is not running a buffered transaction notification

channel fills and eventually blocks.  This commit ensures that the

channel continues to be drained irrespective of the RPC server status.
2016-04-06 13:20:01 -05:00
Dave Collins
e08038115b TravisCI: Remove Go 1.4.3 tests.
Since the latest golint no longer works with on Go 1.4 and Go 1.4 is no
longer officially supported by btcsuite, remove it from the
configurations tested by TravisCI.
2016-03-29 15:52:20 -05:00
Dave Collins
37938375dc docs: Update READMEs with current details.
This commit updates the main README.md and docs/README.md files to
replace the references to the now dead btcgui project with the
Windows-only Paymetheus project.

While here, it also updates some information to make it more current and
accurately describe the current status.
2016-03-11 14:53:15 -06:00
Jonathan Gillham
5c59b685e6 server: Appropriately name inbound peers map in peerState. 2016-02-27 15:52:40 +00:00
Dave Collins
f389742b39 multi: Update with result of gofmt -s.
This commit updates the code to make use of the most recent simplified
output from gofmt.
2016-02-25 13:02:54 -06:00
Dave Collins
d4852101d4 Update TravisCI and README for Go 1.6.
Now that Go 1.6 has been released, update the required Go version in the
README to 1.5 and add Go 1.6 to the configurations tested by TravisCI.

Also, while here, update the Go 1.4 and 1.5 versions tested by TravisCI
to the latest point releases.
2016-02-25 12:43:57 -06:00
Dave Collins
eb882f39f8 multi: Fix several misspellings in the comments.
This commit corrects several typos in the comments found by misspell.
2016-02-25 11:17:12 -06:00
Tibor Bősze
ef9c50be57 Fix typos in comments 2016-02-22 19:57:29 +01:00
Jouke Hofman
c17ff82061 wire: Export (read|write)(VarInt|VarBytes). 2016-02-22 18:11:58 +01:00
Dave Collins
f4d551c08d findcheckpoint: Update to allow first checkpoint.
This updates the findcheckpoint utility to work when there are not
already any checkpoints.  This doesn't really matter for Bitcoin at the
current time, but if a new testnet is created it will not have any
checkpoints to start with and this change also means the utility can
work for alts.

While here, switch a couple of error prints to ensure they contain a
final newline.
2016-02-19 15:53:06 -06:00
Dave Collins
f45db028db database: Remove tx log in favor of new ldb txns.
This removes the intermediate transaction log that was introduced as a
part of the database cache as a workaround for leveldb batches causing
massive memory usage spikes in favor of the recently introduced leveldb
transaction interface which no longer has the memory usage issues.

This approach is preferred because it can avoid the extra memory needed
for the transaction log and therefore all of the intermediate states as
well.  As a result, the default cache size has been doubled since it
equals roughly the same amount of overall memory usage and the flush
interval has been raised as well.
2016-02-16 11:46:43 -06:00
Tibor Bősze
c75fea9c94 Implement banning based on dynamic ban scores
Dynamic ban scores consist of a persistent and a decaying component. The
persistent score can be used to create simple additive banning policies
simlar to those found in other bitcoin node implementations. The
decaying score enables the creation of evasive logic which handles
misbehaving peers (especially application layer DoS attacks) gracefully
by disconnecting and banning peers attempting various kinds of flooding.
Dynamic ban scores allow these two approaches to be used in tandem.

This pull request includes the following:

 - Dynamic ban score type & functions, with tests for core functionality
 - Ban score of connected peers can be queried via rpc (getpeerinfo)
 - Example policy with decaying score increments on mempool and getdata
 - Logging of misbehavior once half of the ban threshold is reached
 - Banning logic can be disabled via configuration (enabled by default)
 - User defined ban threshold can be set via configuration
2016-02-16 10:10:29 +01:00
David Hill
907152cef9 mempool: reduce lock contention 2016-02-14 22:03:37 -05:00
Dave Collins
9abc2c0e19 txscript: Comment improvements and fixes
This commit improves and corrects a few comments in txscript to ensure
they match reality.
2016-02-11 21:43:32 -06:00
Kefkius
d272bfebb7 Fix documentation for opcodeInvalid
Change 'opcodeReserved' to 'opcodeInvalid'
2016-02-11 20:42:41 -06:00
Dave Collins
d127ad4083 server: Make consistent use of svr peer stringer.
This updates a couple of logging statements to use the serverPeer
instance instead of the embedded peer.Peer so they are consistent with
all of the other log statements.
2016-02-10 22:29:30 -06:00
Kefkius
d759d1d3df Remove duplicate stack tests. 2016-02-09 11:17:04 -06:00
John C. Vernaleo
b7f030192e Do not use unkeyed fields. 2016-02-08 17:50:38 -05:00
Jonathan Gillham
73d353247c peer: Consolidate Connect, Disconnect, Start, Shutdown public methods.
This commit does not change functionality. It makes the creation of inbound and outbound peers more homogeneous. As a result the Start method of peer was removed as it was found not to be necessary. This is the first of several pull requests/commits designed to make the peer public API and internals less complex.
2016-02-06 11:11:15 +00:00
David Hill
ae00fff14a wire: Implement sendheaders command (BIP0130)
This implements the wire protocol encoding portion of a new
sendheaders message as described by BIP0130. It purpose is to request
that a peer sends header commands instead of inv commands when
announcing new blocks. This includes a protocol version bump to 70012
and a wire version bump to 0.4.0.

Note that this does not implement logic to handle the command in btcd,
rather it only makes the command available at the wire protocol level.
A future commit which honors the command and therefore provides full
BIP0130 support is still required.
2016-02-05 12:41:39 -05:00
David Hill
383ed041ec Use atomic operations instead of mutexes. 2016-02-04 15:20:04 -05:00
Dave Collins
16582789c3 rpcserver: Optimize filteraddr code.
This optimizes the filter address handling code in the RPC server
handleSearchRawTransasctions path in a few ways:

- Only normalize the filter addresses provided in the command once
  rather than for every transaction being returned
- Reset the passes filter flag just before it's used
- Use a local for the encoded address to avoid the bounds checking
  overhead of accessing it from the slice
- Avoiding subsequent map lookups once the filter has already passed

Also, while here, modify a few of the related comments to match code
style and consistency.
2016-02-03 20:38:45 -06:00
Dave Collins
c7e6c1e88f txscript: Correct JSON float conversions in tests.
This modifies the conversion of the output index from the JSON-based
test data for valid and invalid transactions as well as the signature
hash type for signature hash tests to first convert to a signed int and
then to an unsigned int.  This is necessary because the result of a
direct conversion of a float to an unsigned int is implementation
dependent and doesn't result in the expected value on all platforms.

Also, while here, change the function names in the error prints to match
the actual names.

Fixes #600.
2016-02-03 13:38:35 -06:00
Dave Collins
3c2c858888 btcjson: Specify constant size in tests.
This modifies the test for createrawtransaction to specify the constant
size passed as an int64.  This is necessary because the NewCmd function
accepts the parameters as interfaces in order to support varargs and a
raw numeric constant is treated as an integer.  Since the constant value
is larger than an int32, this causes certain platforms like ARM which
treat a raw integer as a 32-bit integer to fail to compile.

This is work towards #600.
2016-02-03 13:13:11 -06:00
Dave Collins
0b32febe5c database: Implement cache layer.
This commit adds a database cache layer to the ffldb database backend so
that callers can commit multiple transactions without having to incur
the overhead of a disk sync on every new block.
2016-02-03 11:42:14 -06:00
Dave Collins
af3ed803f5 database: Major redesign of database package.
This commit contains a complete redesign and rewrite of the database
package that approaches things in a vastly different manner than the
previous version.  This is the first part of several stages that will be
needed to ultimately make use of this new package.

Some of the reason for this were discussed in #255, however a quick
summary is as follows:

- The previous database could only contain blocks on the main chain and
  reorgs required deleting the blocks from the database.  This made it
  impossible to store orphans and could make external RPC calls for
  information about blocks during the middle of a reorg fail.
- The previous database interface forced a high level of bitcoin-specific
  intelligence such as spend tracking into each backend driver.
- The aforementioned point led to making it difficult to implement new
  backend drivers due to the need to repeat a lot of non-trivial logic
  which is better handled at a higher layer, such as the blockchain
  package.
- The old database stored all blocks in leveldb.  This made it extremely
  inefficient to do things such as lookup headers and individual
  transactions since the entire block had to be loaded from leveldb (which
  entails it doing data copies) to get access.

In order to address all of these concerns, and others not mentioned, the
database interface has been redesigned as follows:

- Two main categories of functionality are provided: block storage and
  metadata storage
- All block storage and metadata storage are done via read-only and
  read-write MVCC transactions with both manual and managed modes
  - Support for multiple concurrent readers and a single writer
  - Readers use a snapshot and therefore are not blocked by the writer
- Some key properties of the block storage and retrieval API:
  - It is generic and does NOT contain additional bitcoin logic such spend
    tracking and block linking
  - Provides access to the raw serialized bytes so deserialization is not
    forced for callers that don't need it
  - Support for fetching headers via independent functions which allows
    implementations to provide significant optimizations
  - Ability to efficiently retrieve arbitrary regions of blocks
    (transactions, scripts, etc)
- A rich metadata storage API is provided:
  - Key/value with arbitrary data
  - Support for buckets and nested buckets
  - Bucket iteration through a couple of different mechanisms
  - Cursors for efficient and direct key seeking
- Supports registration of backend database implementations
- Comprehensive test coverage
- Provides strong documentation with example usage

This commit also contains an implementation of the previously discussed
interface named ffldb (flat file plus leveldb metadata backend).  Here
is a quick overview:

- Highly optimized for read performance with consistent write performance
  regardless of database size
- All blocks are stored in flat files on the file system
- Bulk block region fetching is optimized to perform linear reads which
  improves performance on spindle disks
- Anti-corruption mechanisms:
  - Flat files contain full block checksums to quickly an easily detect
    database corruption without needing to do expensive merkle root
    calculations
  - Metadata checksums
  - Open reconciliation
- Extensive test coverage:
  - Comprehensive blackbox interface testing
  - Whitebox testing which uses intimate knowledge to exercise uncommon
    failure paths such as deleting files out from under the database
  - Corruption tests (replacing random data in the files)

In addition, this commit also contains a new tool under the new database
directory named dbtool which provides a few basic commands for testing the
database.  It is designed around commands, so it could be useful to expand
on in the future.

Finally, this commit addresses the following issues:

- Adds support for and therefore closes #255
- Fixes #199
- Fixes #201
- Implements and closes #256
- Obsoletes and closes #257
- Closes #247 once the required chain and btcd modifications are in place
  to make use of this new code
2016-02-03 11:42:04 -06:00
Chris Shepherd
528ddaf23e txscript: Fix typo in README 2016-01-29 12:39:11 -08:00
Jonathan Gillham
95361a2afc wire: Minor code clean up. 2016-01-26 23:09:18 +00:00
Jonathan Gillham
e03fa30e89 peer: Simplify PushAddrMsg method loop.
This implementation ensures that all addresses have an equal chance of
being included in the addr message. It also moves the pseudorandom number
generator seeding to package level so that it can be overridden for
testing if required.
2016-01-24 10:52:59 +00:00
Jonathan Zeppettini
407fcc2aaf Update LICENSE 2016-01-23 16:06:10 -05:00
Mawuli Adzoe
f5ded65636 Change copyright date for code that was updated this year(2016). 2016-01-07 09:01:51 -07:00
Mawuli Adzoe
1944637333 Bump copyright date to reflect fixes since the beginning of this year. 2016-01-06 15:29:58 -07:00
Javed Khan
7996eb1f9d peer: drain chans before exiting peerHandler
Also disconnect the failed peer to allow the peerDoneHandler goroutine
to exit, instead of hanging around. Fixes #583.
2016-01-06 20:59:43 +05:30
David Hill
e24fe94f58 chaincfg: Remove testnet-seed.alexykot.me, it is defunct. 2016-01-05 16:16:11 -05:00
Daniel Martí
829e87a733 Replace *KoblitzCurve by elliptic.Curve
Found via github.com/mvdan/interfacer.
2016-01-03 13:40:26 +01:00
Mawuli Adzoe
14ccab80e7 Review and fix typos in SigCache code. 2015-12-30 11:57:15 -07:00
David Hill
34a94b7d0b txscript: sync Bitcoin Core script tests 2015-12-30 09:38:16 -05:00