Commit graph

2973 commits

Author SHA1 Message Date
Dave Collins
2b6a9a56e5 mempool/mining: Introduce TxSource interface.
This introduces the concept of a new interface named TxSource which aims
to generically provide a concurrent safe source of transactions to be
considered for inclusion in a new block.  This is a step towards
decoupling the mining code from the internals of btcd.  Ultimately the
intent is to create a separate mining package.

The new TxSource interface relies on a new struct named miningTxDesc,
which describes each entry in the transaction source.  Once this code is
refactored into a separate mining package, the mining prefix can simply
be dropped leaving the type exported as mining.TxDesc.

To go along with this, the existing TxDesc type in the mempool has been
renamed to mempoolTxDesc and changed to embed the new miningTxDesc type.
This allows the mempool to efficiently implement the MiningTxDescs
method needed to satisfy the TxSource interface.

This approach effectively separates the direct reliance of the mining
code on the mempool and its data structures.  Even though the memory
pool will still be the default concrete implementation of the interface,
making it an interface offers much more flexibility in terms of testing
and even provides the potential to allow more than one source (perhaps
multiple independent relay networks, for example).

Finally, the memory pool and all of the mining code has been updated to
implement and use the new interface.
2015-11-25 13:30:44 -06:00
Dave Collins
8ab565ce21 mempool/mining: Decouple and optimize priority calcs.
This does three things:

- Splits the priority calculation logic from the TxDesc type
- Modifies the calcPriority function to perform the value age
  calculation instead of accepting it as a parameter
- Changes the starting priority to be calculated when the transaction is
  added to the pool

The first is useful as it is a step towards decoupling the mining code
from the internals of the memory pool.  Also, the concept of priority is
related to mining policy, so it makes more sense to have the
calculations separate than being defined on the memory pool tx
descriptor.

The second change has been made because everywhere that uses the
calcPriority function first has to calculate the value age anyways and
by making it part of the function it can be avoided altogether in
certain circumstances thereby provided a bit of optimization.

The third change ensure the starting priority is safe for reentrancy
which will be important once the mempool is split into a separate
package.
2015-11-25 12:39:47 -06:00
Dave Collins
f41ff545be server: Improve the persistent peer retry logic.
This fixes an issue introduced during the peer refactor where persistent
peers that failed the initial connection are not retried as intended.

It also improves the retry logic as follows:

- Make the retry handler goroutine simply use a for loop instead of
  launching a new goroutine for each backoff attempt.  Even though
  goroutines are fairly cheap to create, it is much more efficient to
  simply loop
- Change the retry handler to accept a flag if it is the initial attempt
- Rather than dividing the const interval by 2 everywhere and passing
  the retry duration in, just half the constant and set the initial
  duration to it in the retry handler

Finally, include the address of the peer in the error message when a new
outbound peer can't be created.
2015-11-24 16:25:32 -06:00
Dave Collins
1217e00d39 peer: Unexport the mru inventory map.
This unexports the mruInventoryMap type since it is only needed within
the peer package.
2015-11-24 09:41:35 -06:00
Dave Collins
a4aa131dd5 mining: Refactor policy into its own struct.
This introduces the concept of a mining policy struct which is used to
control block template generation instead of directly accessing the
config struct.  This is a step toward decoupling the mining code from
the internals of btcd.  Ultimately the intent is to create a separate
mining package.
2015-11-23 22:02:14 -06:00
Dave Collins
2799ddf538 mining: Remove comment and change fee calc to int.
Now that the memory pool minimum fee calculation code is also
calculating a more precise value instead of rounding up to the nearest
kilobyte boundary, the comment in NewBlockTemplate regarding this
behavior is no longer accurate.  Thus, this removes the comment.

Also, while here, change the calculation to use an int64 instead of
float since it matches the precision of the new calculation code used by
the memory pool and can also avoid the need for the slower floating
point math.
2015-11-23 21:52:39 -06:00
Michail Kargakis
09874f1e91 Fix dropaddrindex flag usage message 2015-11-23 22:20:02 +01:00
Dave Collins
d0f0a2ac02 server: Improve handling of disconnected peers.
When the peer code was refactored, the lists of peers were converted to
maps however the code which runs when a peer disconnects still iterates
them like a slice.  This is no longer necessary since they are maps
which means the peer can simply be looked up by its ID.

Also, the old code was comparing the map entry and the peer being
removed by their pointers which could lead to potentially not properly
finding the peer.  This issue is also resolved by this change since it
looks up the peer by its ID.
2015-11-23 11:03:42 -06:00
Dave Collins
6c00d07910 peer: Combine stats struct into peer struct.
This defines the peer stat fields directly in the Peer struct instead of
defining them in a separate struct and embedding them.  It is slightly
more efficient this way and there is really no reason to have them
defined separately anyways since they are not passed around or otherwise
used independently.
2015-11-23 09:25:53 -06:00
Rune T. Aune
b81555beea Fix "Established connection" log message.
Don't log "Established connection" message on new when
DisableConnectOnNew is set and no connection was actually established.
Do log that same message when Connect() successfully connects.
2015-11-21 14:41:06 -06:00
Dave Collins
cea5d3c1cc Prepare for release 0.12.0. 2015-11-20 19:33:17 -06:00
Dave Collins
23df5ed2fc Add 0.12.0 deps to deps.txt. 2015-11-20 17:35:07 -06:00
Dave Collins
ee8601ecac Add checkpoint at block height 382320. 2015-11-20 13:06:42 -06:00
danda
975536f20f add filteraddrs param to searchrawtransactions 2015-11-16 22:17:19 -08:00
Javed Khan
0c3cf63773 Use new connections for peers in TestOutboundPeer
Fixes a rare hang during peer tests due to the same connection being
used for different outbound peers. Also fixed passing a chan to
QueueMessage(..) which was not being waited on, so was not being used.
2015-11-16 23:01:51 +05:30
danda
c7eaee6020 adds filteraddrs param to searchrawtransactions API 2015-11-15 15:30:13 -08:00
Rune T. Aune
b691a222d5 Add signature hash calculation tests from Bitcoin Core.
500 tests with various transactions and scripts, verifying that
calcSignatureHash generates the expected hash in each case.

This requires changing SigHashType to uint32; that won't affect the
standard use-cases, but will make calcSignatureHash behave more like the
Core counterpart for non-standard SigHashType settings, like those in
some of these tests.
2015-11-15 16:39:00 -05:00
David Hill
365b1bd156 mempool: convert orphansByPrev from a list to a map.
This is more efficient and prevents duplicate entries which can
lead to no-longer-orphans being attempted be added to the mempool
multiple times.

Bug found by me, debugged with @davecgh, and patch from @davecgh
2015-11-13 08:38:17 -05:00
Dave Collins
58e2762158 mempool: Move checkTransactionStandard to policy.
This refactors the checkTransactionStandard function, along with related
constants, from the mempool to the policy.go file since it is strictly
related to policy.

In addition, it adds tests for the function which cover all code paths.
2015-11-11 13:34:32 -06:00
David Hill
d765c73a41 mempool: Remove returned error from ProcessOrphans
ProcessTransaction could have accepted a new transaction into mempool
but could have returned a reject message if a no-longer-orphan
transaction failed to be accepted.  This would also skip any
additional no-longer-orphans, keeping them in the orphan pool.

Instead of returning an error incorrectly, log the error and skip
the no-longer-orphan transaction.  This allows the rest of the
no-longer-orphans to be processed as well.
2015-11-10 18:34:51 -05:00
David Hill
5016675d40 Move comment to where it belongs. 2015-11-10 13:20:29 -05:00
Javed Khan
21d11e2809 Fix failing test case because of wrong TimeOffset 2015-11-10 05:15:55 +05:30
David Hill
cb71f278ec chaincfg: Move DNS Seeds to chaincfg.
This allows API users access to the DNS Seeds for use with SPV
clients, seeders, etc.
2015-11-09 17:21:16 -05:00
Dave Collins
aa0efa1f3e server: Allow IPv6 addresses with zone id.
This modifies the IP parsing code to work with IPv6 zone ids.  This is
needed since the net.ParseIP function does not allow zone ids even
though net.Listen does.
2015-11-09 10:39:30 -06:00
Josh Rickmar
23bcb367b2 blockchain: Fix bogus error message.
In the presence of overflow, the actual output sum is still not
negative and should not be reported as so.
2015-11-09 11:33:37 -05:00
Justus Ranvier
756f58b581
add installation instructions for Gentoo 2015-11-08 14:55:08 -06:00
Dario Nieuwenhuis
912a8d8301 Fix mempool transactions disappearing if a parent tx is confirmed. 2015-11-06 11:42:42 +01:00
David Hill
c56072017d Add optional locktime parameter to CreateRawTransaction APIs. 2015-10-30 19:34:34 -04:00
David Hill
4b7206b54f btcjson: Add optional locktime to createrawtransaction
rpcserver:
If the locktime is given, the transaction inputs will be set to a
non-max value, activating the locktime.  The locktime for the
new transaction will be set to the given value.

This mimics Bitcoin Core commit 212bcca92089f406d9313dbe6d0e1d25143d61ff
2015-10-30 17:16:50 -04:00
Ben Echols
489ba8d31d Update minimum fee calculations to match Core.
Calculate rate*fee first before dividing by 1000.
Add more unit tests around the fee calculations.
2015-10-30 13:41:25 -06:00
David Hill
7811770d31 Implement BIP0065 changeover logic for v4 blocks.
This commit implements the changeover logic for version 4 blocks as
described by BIP0065.
2015-10-28 13:28:50 -04:00
David Hill
3d6afcffe7 Move non-mempool specific functions to new file.
No functional change. Add tests.
2015-10-27 10:05:06 -04:00
Dave Collins
5c50db5357 peer: Don't stall on pongs. 2015-10-26 08:59:01 -05:00
Dave Collins
4a397d51d4 peer: Don't log write errs due to disconnect.
This commit modifies the peer logging so that it will not log an error
when it's due to the remote peer disconnecting or the peer being
forcibly disconnected or shutdown.
2015-10-23 14:55:43 -05:00
Dave Collins
3942a116e4 docs: Make various README.md files consistent.
First, it removes the documentation section from all the README.md files
and instead puts a web-based godoc badge and link at the top with the
other badges.  This is being done since the local godoc tool no longer
ships with Go by default, so the instructions no longer work without
first installing godoc. Due to this, pretty much everyone uses the
web-based godoc these days anyways.  Anyone who has manually installed
godoc won't need instructions.

Second, it makes sure the ISC license badge is at the top with the other
badges and removes the textual reference in the overview section.

Finally, it's modifies the Installation section to Installation and
Updating and adds a '-u' to the 'go get' command since it works for both
and thus is simpler.
2015-10-23 14:51:36 -05:00
Dave Collins
aa03d68e1e peer: Correct a few typos in documentation. 2015-10-23 14:47:18 -05:00
Dave Collins
cbbe3a8bbe peer: Implement stall detection.
This commit implements stall detection logic at the peer level to detect
and disconnect peers that are either not following the protocol in
regards to expected response messages or have otherwise stalled.  This
is accomplished by setting deadlines for each message type which expects
a response and periodically checking them while properly taking into
account processing time.

There are an increasing number of nodes on the network which claim to be
full nodes, but don't actually properly implement the entire p2p
protocol even though they implement it enough to cause properly
implemented nodes to make data requests to which they never respond.

Since btcd currently only syncs new blocks via single sync peer and,
prior to this commit only had very basic stall detection, this could
lead to a situation where the block download became stalled indefinitely
due to one of these misbehaving peers.  This commit fixes that issue
since the stalled peer will now be detected and disconnected which leads
to a new sync peer being selected.

This logic will also fit nicely with the future multi-peer sync model
which is on the roadmap and for which infrastructure work is underway.

Fixes #486 and fixes #229.
2015-10-23 10:27:00 -05:00
Dave Collins
f1bd2f8d6e peer: Ping on interval instead of delayed timer.
This commit modifies the ping logic in the peer to ping on an interval
regardless of what other messages are being sent versus the previous
method of delaying the ping each time a message that is expected to
receive data is sent.

This helps improve the ping statistics and simplifies its logic.
2015-10-23 10:22:22 -05:00
Dave Collins
250228c32f peer: Improve documentation.
This fleshes out the doc.go documentation which is shown on godoc, the
README.md shown on github, and improves a couple of comments for the
fields in the Config struct.
2015-10-23 01:17:12 -05:00
Javed Khan
00bddf7540 peer: Refactor peer code into its own package.
This commit introduces package peer which contains peer related features
refactored from peer.go.

The following is an overview of the features the package provides:

- Provides a basic concurrent safe bitcoin peer for handling bitcoin
  communications via the peer-to-peer protocol
- Full duplex reading and writing of bitcoin protocol messages
- Automatic handling of the initial handshake process including protocol
  version negotiation
- Automatic periodic keep-alive pinging and pong responses
- Asynchronous message queueing of outbound messages with optional
  channel for notification when the message is actually sent
- Inventory message batching and send trickling with known inventory
  detection and avoidance
- Ability to wait for shutdown/disconnect
- Flexible peer configuration
  - Caller is responsible for creating outgoing connections and listening
    for incoming connections so they have flexibility to establish
    connections as they see fit (proxies, etc.)
  - User agent name and version
  - Bitcoin network
  - Service support signalling (full nodes, bloom filters, etc.)
  - Maximum supported protocol version
  - Ability to register callbacks for handling bitcoin protocol messages
- Proper handling of bloom filter related commands when the caller does
  not specify the related flag to signal support
  - Disconnects the peer when the protocol version is high enough
  - Does not invoke the related callbacks for older protocol versions
- Snapshottable peer statistics such as the total number of bytes read
  and written, the remote address, user agent, and negotiated protocol
  version
- Helper functions for pushing addresses, getblocks, getheaders, and
  reject messages
  - These could all be sent manually via the standard message output
    function, but the helpers provide additional nice functionality such
    as duplicate filtering and address randomization
- Full documentation with example usage
- Test coverage

In addition to the addition of the new package, btcd has been refactored
to make use of the new package by extending the basic peer it provides to
work with the blockmanager and server to act as a full node.  The
following is a broad overview of the changes to integrate the package:

- The server is responsible for all connection management including
  persistent peers and banning
- Callbacks for all messages that are required to implement a full node
  are registered
- Logic necessary to serve data and behave as a full node is now in the
  callback registered with the peer

Finally, the following peer-related things have been improved as a part
of this refactor:

- Don't log or send reject message due to peer disconnects
- Remove trace logs that aren't particularly helpful
- Finish an old TODO to switch the queue WaitGroup over to a channel
- Improve various comments and fix some code consistency cases
- Improve a few logging bits
- Implement a most-recently-used nonce tracking for detecting self
  connections and generate a unique nonce for each peer
2015-10-23 06:17:29 +05:30
David Hill
2e6e896aa6 txscript: Sync Bitcoin Core tests. 2015-10-22 16:10:29 -04:00
David Hill
3fa416a7ef txscript: fix isMultiSig bug.
isMultiSig was not verifying the number of pubkeys specified matched
the number of pubkeys provided.  This caused certain non-standard
scripts to be considered multisig scripts.

However, the script still would have failed during execution.

NOTE: This only affects whether or not the script is considered
standard and does NOT affect consensus.

Also, add a test for this check.
2015-10-22 15:55:34 -04:00
David Hill
a56db22e9b config: New option --minrelaytxfee
--minrelaytxfee allows the user to specify the minimum transaction
fee in BTC/kB in which the fee is considered a non-zero fee.
2015-10-20 12:41:12 -04:00
Dave Collins
5a9bac9668 Correct a few style related issues found by golint.
Also, update TravisCI goclean script to remove the special casing which
ignored 'Id' from the lint output since that exception is no longer
needed.  It was previously required due to the old version of btcjson,
but that is no longer in the repo.
2015-10-20 10:34:14 -05:00
Josh Rickmar
07406791c9 rpcserver: Copy btcwallet fix for verifymessage. 2015-10-16 14:25:07 -04:00
Dave Collins
80fa803875 wire: Export var length string serialization funcs.
This commit exports the ReadVarString and WriteVarString functions so
they are available for callers to use.

A variable length string is encoded as a variable length integer
containing the length of the string followed by the bytes that represent
the string itself.
2015-10-16 11:24:45 -05:00
David Hill
4c3ad4987b txscript: Implement CheckLockTimeVerify (BIP0065)
See https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki for
more information.

This commit mimics Bitcoin Core commit bc60b2b4b401f0adff5b8b9678903ff8feb5867b
and includes additional tests from Bitcoin Core commit
cb54d17355864fa08826d6511a0d7692b21ef2c9
2015-10-14 13:19:49 -04:00
David Hill
0f57a41ed8 txscript: Add ScriptVerifyLowS to the standard flags
We've already been generating lowS sigs for quite a while.  This removes
the malleability vector.

This mimics Bitcoin Core commit 49dd5c629df0a08cf3b1ea8085c03312d1a81696
2015-10-09 15:30:12 -04:00
David Hill
f862536929 btcjson: Add errors to InfoChainResult
The getinfo RPC will now include the errors attribute in the
result.
2015-10-09 14:32:53 -04:00
Olaoluwa Osuntokun
0029905d43 Integrate a valid ECDSA signature cache into btcd
Introduce an ECDSA signature verification into btcd in order to
mitigate a certain DoS attack and as a performance optimization.

The benefits of SigCache are two fold. Firstly, usage of SigCache
mitigates a DoS attack wherein an attacker causes a victim's client to
hang due to worst-case behavior triggered while processing attacker
crafted invalid transactions. A detailed description of the mitigated
DoS attack can be found here: https://bitslog.wordpress.com/2013/01/23/fixed-bitcoin-vulnerability-explanation-why-the-signature-cache-is-a-dos-protection/
Secondly, usage of the SigCache introduces a signature verification
optimization which speeds up the validation of transactions within a
block, if they've already been seen and verified within the mempool.

The server itself manages the sigCache instance. The blockManager and
txMempool respectively now receive pointers to the created sigCache
instance. All read (sig triplet existence) operations on the sigCache
will not block unless a separate goroutine is adding an entry (writing)
to the sigCache. GetBlockTemplate generation now also utilizes the
sigCache in order to avoid unnecessarily double checking signatures
when generating a template after previously accepting a txn to the
mempool. Consequently, the CPU miner now also employs the same
optimization.

The maximum number of entries for the sigCache has been introduced as a
config parameter in order to allow users to configure the amount of
memory consumed by this new additional caching.
2015-10-08 17:31:42 -07:00