Commit graph

60 commits

Author SHA1 Message Date
Owain G. Ainsworth
5a9cc91e62 Add an idle timer for peers.
If we don't hear from a peer for 5 minutes, we disconnect them. To keep
traffic flowing we send a ping every 2 minutes if we have not send any
other message that should get a reply.
2013-10-17 17:06:47 +01:00
Owain G. Ainsworth
a41c874837 Remove stale comment. 2013-10-17 17:06:47 +01:00
Owain G. Ainsworth
163b32887b Add blockmanager.current with extra checks for whether we are up to date.
This uses peer state to confirm if we are up to date, as well as what
chain thinks.
2013-10-17 17:05:18 +01:00
Josh Rickmar
a80f9da6c3 Add websocket handlers to the RPC server for wallet connections.
This change adds additional http listeners for websocket connections
on "/wallet".  Websockets are used to provide asynchronous messaging
between wallet daemons (i.e. btcwallet) and btcd as they allow an easy
way for btcd to provide instant notifications (instead of a wallet
polling for updates) and multiple replies to a single request.
Standard RPC commands sent over a websocket connection are handled
just like RPC, returning the same results, the only difference being
that the connection is async.  In cases where the standard RPC
commands fall short of wallet daemons requests, and to request
notifications for addresses and events, extension JSON methods are
used.

Multiple wallets can be connected to the same btcd, and replies to
websocket requests and notifications are properly routed back to the
original requesting wallet.

Due to the nature of turning a synchronous protocol asynchronous, it
is highly recommended to use the JSON id field as a type of sequence
number, so replies from btcd can be routed back to the proper handler
in a wallet daemon.
2013-10-14 13:37:48 -04:00
Dave Collins
6368d5b170 Respond to getdata requests for transactions.
This commit adds code to properly respond to getdata requests for
transactions by fetching them from the transaction pool.  Previously, we
advertised newly available transactions, but the code to respond with the
actual transaction was not written yet.

Also, fix a couple of comments and make the pushTxMsg and pushBlockMsg
functions consistent.
2013-10-11 14:14:55 -05:00
Dave Collins
3d62b24fe1 Reformat periodic block height ouput and add time.
Closes #9.
2013-10-10 18:24:23 -05:00
Dave Collins
9772626dd8 Improve logging.
This commit is a first pass at improving the logging.  It changes a number
of things to improve the readability of the output.  The biggest addition
is message summaries for each message type when using the debug logging
level.

There is sitll more to do here such as allowing the level of each
subsystem to be independently specified, syslog support, and allowing the
logging level to be changed run-time.
2013-10-10 17:22:19 -05:00
Dave Collins
e04986528c Add --nocheckpoints option.
This commit provides a new flag, --nocheckpoints, to disable built-in
checkpoints.

Checkpoints are used for a number of things such a ensuring
the block chain being downloaded matches various known good blocks,
allowing quicker verification on old blocks since scripts don't have to be
executed, and preventing forks from old blocks, etc.
2013-10-09 19:34:02 -05:00
Dave Collins
a0119b056e Move transaction handling into block manager.
The block manager handles inventory messges to know which inventory should
be requested based on what is already known and what is already in flight.
So, this commit adds logic to ask the transaction memory pool if the
transaction is already known before requesting it and tracks pending
requests into an in-flight transaction map owned by the block manager.

It also moves the transaction processing into the block manager so the
in-flight map can be properly cleaned.
2013-10-08 20:46:59 -05:00
Dave Collins
f4dac3abf0 Update to use latest btcwire invtype constants. 2013-10-08 15:55:07 -05:00
Dave Collins
c8160a57aa Switch to leveldb by default.
Also, add a warning if multiple block databases are detected.
2013-10-08 15:00:34 -05:00
Dave Collins
1d2c48555b Misc cleanup. 2013-10-07 19:24:44 -05:00
Dave Collins
4b728df738 Update some comments and code consistency. 2013-10-07 17:27:59 -05:00
Dave Collins
ca5c734e05 Add warning if there are no sync peers.
This is to help track down stalls that are being seen.
2013-10-07 17:09:33 -05:00
Dave Collins
995d8da491 Handle another removal case missed in prev commit.
Also, the loops which only remove a single element and break or return
don't need the extra logic for iteration since they don't continue
iteration after removal.
2013-10-07 10:10:22 -05:00
Dave Collins
219a6131f4 Correct all list/container loops that remove items.
It is not safe to remove an item from a container/list while iterating the
list without first saving the next pointer since removing the item nils
the internal list element's next pointer.
2013-10-07 09:06:29 -05:00
Dave Collins
efb54784c8 Log rejected blocks and transactions as info.
Rather than logging these as warnings which implies something is wrong
that might require user action, log them as info.
2013-10-04 13:35:28 -05:00
Dave Collins
78b555fcf9 Improve logging of rejected blocks.
Rather than showing all errors from ProcessBlock as a failure, check if
the error is a RuleError meaning the block was rejected as opposed to
something actually going wrong and log it accordingly.
2013-10-04 13:13:16 -05:00
Dave Collins
47c07ee5e1 Update a few comments. 2013-10-04 00:35:26 -05:00
Dave Collins
78e9b94d93 Implement transaction pool and relay.
This commit is a rather large one which implements transaction pool and
relay according to the protocol rules of the reference implementation.
It makes use of btcchain to ensure the transactions are valid for the
block chain and includes several stricter checks which determine if they
are "standard" or not before admitting them into the pool and relaying
them.

There are still a few TODOs around the more strict rules which determine
which transactions are willing to be mined, but the core checks which
are imperative (everything except the all of the "standard" checks really)
to operate as a good citizen on the bitcoin network are in place.
2013-10-03 22:31:54 -05:00
Dave Collins
3678153ae4 Allow regtest tool to be treated as a sync peer. 2013-10-03 22:31:00 -05:00
Owain G. Ainsworth
182c4d9ee6 Remove peers from duplicate map *before* we restart sync.
Just in case this ordering causes problems.
2013-10-03 20:13:56 +01:00
Owain G. Ainsworth
6bb0b80bbb Ignore non-sync invs and don't relay invs if we are not yet caught up.
We originally wanted to also not fetch orphan parents in this commit, however,
I have discovered that if you are doing a main sync from a peer, if it
sends you an orphan you must fetch it, else you ahven't fetched
everything it told you about and thus it will nto send you end more invs
from the main sync.

So we always fetch orphan parents, but we still don't fetch from
non-sync peers (all invs from them will be unsolicited). Seems to fix some hangs
with multiple peers.
2013-10-03 20:13:56 +01:00
Dave Collins
6dbc61e960 Remove the noverify option.
This was a development only option.
2013-10-03 13:01:18 -05:00
Dave Collins
0c6b79afb0 Don't disconnect on unrequested blocks for regtest.
The "official" regression test tool intentionally sends some unrequested
duplicate blocks to ensure the chain handling code does not fail when
trying to insert them.  This commit adds an exception to the block manager
which typically disconnects peers that send unrequested blocks (they are
misbehaving if they do this) for regression test mode.
2013-10-03 11:21:41 -05:00
Dave Collins
bd3a39ad4a Fix comment typo. 2013-10-03 11:10:57 -05:00
Owain G. Ainsworth
2b56e7a397 Don't need the blockpeermutex anymore.
Really, it would be nice to pass an interface{} into chain to be given
to us when the callback calls, it would avoid the awkward sidchanneling
through the map and should actually be more efffieint (pointer passing >
hashtable insert, lookup, then remove).
2013-10-03 16:13:47 +01:00
Dave Collins
fd0679acdd Update notification for recent btcchain API change. 2013-10-02 21:21:52 -05:00
Dave Collins
29dfa22086 Introduce a Stringer on peers.
Rather than having all of the various places that print peer figure out
the direction and form the string, centralize it by implementing the
Stringer interface on the peer.
2013-10-02 20:05:10 -05:00
Dave Collins
f1e2de4f0c Go fmt. 2013-10-02 19:44:07 -05:00
Dave Collins
52701d826d Add directionality back to remove peer log too. 2013-10-02 19:43:38 -05:00
Owain G. Ainsworth
13a8a8e7e3 use directionString() instead of handrollong. 2013-10-03 01:39:31 +01:00
Owain G. Ainsworth
2dcd003722 Print if a peer is inbound or outbound.
This got lost in the peer logging rework. Pointed out by davec@.
2013-10-03 01:21:30 +01:00
Owain G. Ainsworth
eb667fdf83 Remove races on chain.
Chain is not concurrency safe, so we move the chainNotifySink handling
into the main blockmanager goroutine. Due to a possible deadlock if the
buffer is filled this still has to be a single channel that isn't linked
to the other ones. There is a possible starvation issue where the main
msgChan gets selected more often than the  notification sink, but until
chain is concurrency safe this is rather unavoidable.
2013-10-03 00:50:36 +01:00
Owain G. Ainsworth
8974e789f7 Convert the rest of the subsystems to use atomics for shutdown vars. 2013-10-03 00:33:42 +01:00
Owain G. Ainsworth
f333cb4220 Remove a (largely harmless) race on p.conn
Add a Connected() member function that checks atomic variables to see if
the peer is connected.
2013-10-02 23:06:29 +01:00
Owain G. Ainsworth
5c1340be8f make logging about peers rather quieter.
Only log errors for most cases if the peer is persisent (and thus requested).
Only log by default after version exchange, and after losing a peer that had
completed version exchange. Make most other messages debug.
2013-10-02 22:50:24 +01:00
Owain G. Ainsworth
7eb1a5ce4e Disconnect misbehaving peers who send unsolicited blocks. 2013-10-02 14:36:04 +01:00
Owain G. Ainsworth
65725189db Keep track of currently requested blocks per peer.
Use this information so that we do not request a block per peer we got
an inv for it, makes multi peer much quieter and rather more bandwidth
efficient.

In order to remove a number of possible races we combine blockhandling
an synchandler and use one channel for all messages. This ensures that
all messages from a single peer will be recieved in order.  It also
removes the need for a lot of locking between the peer removal code and
the block/inv handlers.
2013-10-02 14:36:04 +01:00
Owain G. Ainsworth
ca7cb8c875 move the intiial chain cache generation into bm.
Blockmanager is otherwise the only current consumer of chain and it is
messy to do it outside of it.
2013-10-02 14:36:04 +01:00
Dave Collins
ea256aeb5a Update a few comments. 2013-09-29 15:26:25 -05:00
Dave Collins
9880cf4646 Improve chain notification handling.
Previously a new goroutine was launched for each notification in order to
avoid blocking chain from continuing while the notification is being
processed.  This approach had a couple of issues.

First, since goroutines are not guaranteed to execute in any given order,
the notifications were no longer handled in the same order as they were
sent.  For the current code, this is not a problem, but upcoming code that
handles a transaction memory pool, the order needs to be correct.

Second, goroutines are relatively cheap, but it's still quite a bit of
overhead to launch 3-4 goroutines per block.

This commit modifies the handling code to have a single sink executing in
a separate goroutine.  The main handler then adds the notifications to a
queue which is processed by the sink.  This approach retains the
non-blocking behavior of the previous approach, but also keeps the order
correct and, as an additional benefit, is also more efficient.
2013-09-28 22:13:22 -05:00
Dave Collins
f1cd96ceb5 Update a few comments. 2013-09-26 21:06:01 -05:00
Owain G. Ainsworth
b97db056c1 Move the inventory handling from peer into blockmanager.
This removes a horrible case of reach-around from per into the guts of
the blockmaanger to frob the chain. Soon, when we try to deduplicate the
fetching of blocks from multiple peers this will need decisions made in
a central point.

Discussed at length with davec.
2013-09-27 01:51:05 +01:00
Dave Collins
a69647b94d Handle notifications from chain in own goroutine.
This change is in response to the recent change in btcchain such that
notifications are no longer sent in their own goroutine.
2013-09-21 09:57:47 -05:00
Dave Collins
92a8605b24 Continue work on addrmgr and multi-peer.
- Remove leftover debug log prints
- Increment waitgroup outside of goroutine
- Various comment and log message consistency
- Combine peer setup and newPeer -> newInboundPeer
- Save and load peers.json to/from cfg.DataDir
- Only claim addrmgr needs more addresses when it has less than 1000
- Add warning if unkown peer on orphan block.
2013-09-17 10:48:47 -05:00
Dave Collins
79bb334b9a Remove old regression test db in regtest mode.
The regression test mode is special in that the 'official' block test
suite requires an empty database to work properly.  Rather than having to
manual go delete it before each test, add code to automatically delete the
old regression test database when in regression test mode.
2013-09-15 16:58:02 -05:00
Dave Collins
629a1c9d06 Rework the data path and db type handling.
This commit modifies the way the data paths are handled.  Since there will
ultimately be more data associated with each network than just the block
database, the data path has been modified to be "namespaced" based on the
network.  This allows all data associated with a specific network to
simply use the data path without having to worry about conflicts with data
from other networks.

In addition, this commit renames the block database to "blocks" plus a
suffix which denotes the database type.  This prevents issues that would
otherwise arise if the user decides to use a different database type and
a file/folder with the same name already eixsts but is of the old database
type.  For most users this won't matter, but it does provide nice
properties for testing and development as well since it makes it easy to
go back and forth between database types.

This commit also includes code to upgrade the old database paths to the
new ones so the change is seamless for the user.

Finally, bump the version to 0.2.0.
2013-09-15 14:25:32 -05:00
Dave Collins
1c9a7095b3 Change --dbdir flag to --datadir.
This change paves the way for saving more than just the block database to
the filesystem (such as address manager data, index data, etc) where the
name "dbdir" no longer makes sense.
2013-09-13 18:02:10 -05:00
Dale Rahn
af7aa39624 Allow config file and command line to select between sqlite and levdldb 2013-09-13 10:08:19 -04:00