Commit graph

357 commits

Author SHA1 Message Date
Dave Collins
5cd4c8265c Add subsystem to RPC cert generation log messages. 2013-11-19 11:41:31 -06:00
David Hill
6fcc1c9d1b Add localhost to DNSNames in generated certificate. 2013-11-19 12:02:40 -05:00
Owain G. Ainsworth
75e577c82e RPC TLS Support.
All rpc sockets now listen using TLS by default, and this can not be
turned off. The keys (defauling to the datadirectory) may be provided by
--rpccert and --rpckey. If the keys do not exist we will generate a new
self-signed keypair with some sane defaults (hostname and all current
interface addresses).

Additionally add tls capability to btcctl so that it can still be used.
The certificate to use for verify can be provided on the commandline or
verification can be turned off (this leaves you susceptible to MITM
attacks)

Initial code from dhill (rpc tls support) and jrick (key generation),
cleanup, debugging and polishing from me.
2013-11-19 14:50:31 +00:00
Owain G. Ainsworth
5da5dfe1c4 Add --rpclisten that behaves frighteningly similar to --listen.
Except it works for the rpcserver instead of the main server.

Closes #34
2013-11-19 14:48:58 +00:00
David Hill
7b7d4e8555 fix typo - now addresses show up in getrawtransaction 2013-11-15 16:12:08 -05:00
Owain G. Ainsworth
6116a6cb02 Support --listen.
This allows the provision of address/port pairs to be listened on instead
of just providing the port. e.g.:
btcd --listen 1.2.3.4:4321 --listen 127.0.0.01 --listen [::1]:5432

When --proxy and --connect are used, we disable listening *unless* any --listen
arguments have been provided, when we will listen on those addresses as
requested.

Initial code by davec, integration by myself.

Closes #33

allow listens to fail, but warn. error if all failed

fmt
2013-11-14 01:15:47 +00:00
Owain G. Ainsworth
31a97d5c09 look up tx in mempool first for getrawtransaction
Closes #26
2013-11-12 22:57:27 +00:00
Owain G. Ainsworth
bb276b53aa Add support for the verifychain command.
So far we only do level 0 and level 1 checks (precense and basic
sanity). The checks done at higher levels in bitcoind are closely
coupled with their database layout.

arguably Closes #13
2013-11-12 22:57:26 +00:00
Josh Rickmar
31f27cffd5 Safely remove elements from list.Lists. 2013-11-12 16:24:32 -05:00
Josh Rickmar
77e1af792b Fix mutex handling for removing minedtx requests.
Previously, RemoveMinedTxRequest was being run from a caller which
held a reader lock for the websocket request contexts.  When
RemoveMinedTxRequest tried to grab a writer lock, it would block.
This change creates a new function, removeMinedTxRequest, that does
not grab any locks, and the caller (NotifyBlockConnected) grabs a
writer lock instead of a reader lock.
2013-11-11 14:23:11 -05:00
Josh Rickmar
8b5413a4ac Always release ws context reader lock.
Previously, on a blockconnected notification, the websocket context
reader lock was not always being given up properly.  This change
defers the unlock so it will always happen.

This fixes an issue where wallet will stop responding (due to not
being able to complete its handshake) on reconnect.
2013-11-11 12:54:49 -05:00
Josh Rickmar
41838b83b0 Use btcws package for wallet notifications. 2013-11-08 12:44:00 -05:00
Owain G. Ainsworth
d81a2c9067 Check for 0 shas before dereferencing list 2013-11-07 22:26:43 +00:00
Josh Rickmar
ced24946a4 Rework JSON handlers to take a btcjson.Cmd.
This change reworks where the command parsing occurs to be done before
handlers are checked.  Before, the websocket extension handler called
the standard handler with the same message, and if it was unhandled,
would unmarshal it a second time for checking extension handlers.
2013-11-07 13:53:41 -05:00
Josh Rickmar
d403863e2b Don't marshal a function for getbestblockhash replies. 2013-11-07 13:53:22 -05:00
Josh Rickmar
b97a2145d8 Try to match bitcoind sendrawtransaction RPC errors. 2013-11-07 10:34:55 -05:00
Owain G. Ainsworth
d8c5222474 Rework the way we send notifications over the websocket
Redo the datastructures we search so that we only do one lookup per txin and
txout instead of doing a loop per wallet connection.

Don't send spent data on tx notifications, this can be worked out in wallet and
it is expensiveish to calculate. However we DO check upon getting a notification
request if the output is already spent, and in which case we send an immediate
notification to force a rescan.

MinedTxNotfications are handled separately to the connected block messages
largely to enable this to scale rather better.

Tested by jrick (who found one bug i had introduced, thanks!)

Additionally (accidentally squashed in):

Add handlers for all known commands.

We have handlers for all wallet-requiring commands that will return a suitable
error.

Unimplemented commands temporarily return an error stating so.
2013-11-06 18:47:06 +00:00
Josh Rickmar
8c2b9ae06e Begin using btcws.
This change adds support for the unmarshaling custom commands sent by
btcwallet and supported in the btcws package.
2013-11-06 11:20:36 -05:00
Josh Rickmar
1f087adf15 Separate ws command handlers into separate funcs. 2013-11-04 14:15:05 -05:00
Josh Rickmar
53e1c2d6bd Stringify the address hash for txRequests map.
This change allows map lookups using address hashes (which are
returned as []byte) instead of either copying the hash into an array,
or doing a bytes.Equal().

A stupid range over a map until the right key is found was also just
changed to a single map lookup.
2013-11-04 14:11:39 -05:00
Josh Rickmar
2511fee152 Complete rescan extension.
This adds to the initial rescan implementation, but switches it to
rescan based on a group of addresses, rather than just one.  Due to
how expensive database lookups are during a rescan, wallets should
take advantage of this to rescan once for all needed addresses for all
accounts.
2013-11-01 09:35:52 -04:00
Dave Collins
a4794798d4 Make use of new btcwire SerializeSize API.
This commit changes the various cases that were serializing transactions
into a buffer and taking the length to use the new faster SerializeSize
API.  It also completes a TODO since the serialized size of a transaction
output is now available.
2013-10-31 00:28:37 -05:00
Dave Collins
470ef8c58e Use correct calls to tx.Sha() in RPC server. 2013-10-30 18:40:55 -05:00
Dave Collins
b866e9f14c Improve RPC server log of rejected transactions.
Rather than showing all errors from ProcessTransaction as an error, check
if the error is a TxRuleError meaning the transaction was rejected as
opposed to something actually going wrong and log it accordingly.
2013-10-30 14:11:45 -05:00
Dave Collins
e76fada2d2 Optimize NotifyNewTxListener.
Looking up transactions from the database is an expensive operation.
This commit modifies the NotifyNewTxListener code to simply iterate the
transactions in the block instead of looking them up from the db.

Currently the wallet code needs a spent flag which ultimately shouldn't be
required.  For now, the spent data is simply created on the fly which is
still significantly faster than doing database transaction lookups.

Closes #24.
2013-10-30 10:54:03 -05:00
Josh Rickmar
d7d2385fb0 Return JSON id with errors when possible.
This change unbreaks the case where an unknown command is sent to the
RPC server.  Instead of replying back with a nil JSON id, if the
initial unmarshal was successful (and thus, the message was valid
JSON-RPC), the unmarshaled id will be used in the error reply.
2013-10-29 23:23:22 -04:00
Dave Collins
9442d96929 Convert errors in RPC server to new btcjson consts. 2013-10-29 19:42:21 -05:00
Owain G. Ainsworth
da2901a4fd Add support for the addnode command
Currently this acts on the list of all peers, and not just the list of
permanent peers. this will be changed shortly.
2013-10-29 20:30:07 +00:00
Owain G. Ainsworth
b1f14732b1 Implement getpeerinfo and getconnectedcount
We have a channel for queries and commands in server, where we pass in
args and the channel to reply from, let rpcserver use these interfaces
to provide the requistie information.

So far not all of the informaation is 100% correct, the syncpeer
information needs to be fetched from blockmanager, the subversion isn't
recorded and the number of bytes sent and recieved needs to be obtained
from btcwire. The rest should be correct.
2013-10-29 17:05:12 +00:00
Owain G. Ainsworth
c242d75866 Split the massive switch statement in rpcserver into a map of handlers.
Cleans up a the code a lot and removes the need to be careful about
shadowing err.

Much rejoicing from jcv, jrick and davec.
2013-10-29 17:00:25 +00:00
Owain G. Ainsworth
b96cb4317a Alphabetise command handlers. 2013-10-29 17:00:25 +00:00
Owain G. Ainsworth
975640c6b3 Switch rpcserver.go over to using the jsoncmd apis. 2013-10-29 16:59:19 +00:00
Owain G. Ainsworth
3c405563bd Add missing return; avoids panic. 2013-10-29 16:42:26 +00:00
Dave Collins
08fc3050a3 Convert to use new btcutil.Tx and btcchain APIs.
This commit updates btcd to work with the new btcchain APIs which now
accept btcutil.Tx instead of raw btcwire.MsgTx.  It also modifies the
transaction memory pool to store btcutil.Tx.

This is part of the ongoing transaction hash optimization effort noted in
conformal/btcd#25.
2013-10-28 15:47:24 -05:00
Josh Rickmar
618b885e9e Notify wallets of mined transactions.
This change allows btcwallet to keep a pool of transactions that have
not yet been mined into a block, notifying wallet when transactions
are mined, as well as introducing a new way to send the
btcd:blockconnected notification with wallet-specific information as
part of the same notification.  When a transaction is sent using the
RPC call 'sendrawtransaction', a notification request will be
automatically registered with the connected wallet (if using
websockets) to notify the wallet when the transaction first appears in
a block.

To perform this notification, and to avoid requiring wallets from
waiting for seperate mined tx notifications (and resend after a
timeout) or from sending an additional tx mined request for every tx
in the pool after each new block, the blockconnected notification is
now created seperately for each wallet.  If the notified wallet has
sent a transaction, an additional JSON field "minedtxs" will include
an array of transaction IDs that the wallet has created and which are
included in the new block.

This new unique blockconnected notification can also be used for
additional notifications that may happen each new block in the future,
and to cut down on existing notification handlers in btcwallet, such
as for transactions to a watched address.
2013-10-23 18:13:03 -04:00
Dave Collins
5a4772bdc6 Change the RPC server to use it's own Muxer.
Rather than relying on the http package's DefaultServeMux for the RPC
server, create a unique mux specifically for the RPC server.  This ensures
things, such as the http profiling handlers, do not commingle.
2013-10-23 10:45:43 -05:00
John C. Vernaleo
1420eccf73 Use rpc errors defined in btcjson. 2013-10-22 12:28:00 -04:00
David Hill
a975b60a0a add getconnectioncount skeleton to rpc 2013-10-21 13:00:13 -04:00
Josh Rickmar
5bfc9c7eed Remove possible nil pointer dereferences.
Results from FetchTxByShaList must each be checked for a nil Err and a
non-nil Tx.  Fix this issue in two places where these conditions were
not being checked.
2013-10-20 13:02:34 -04:00
Dave Collins
d44d253dff Make quit channel with RPC server.
The latest websockets code added a quit channel to the RPC server, but did
not initialize it.  This commit corrects that so shutdown works properly
again.
2013-10-20 11:06:36 -05:00
Dave Collins
e5620d6387 Correct reply for getrawmempool RPC command.
The RPC command needs to return a slice of hash strings, not the
underlying *btcwire.ShaHash bytes.
2013-10-17 12:11:04 -05:00
David Hill
4c50ff541f fix getbestblockhash 2013-10-17 12:33:46 -04:00
David Hill
ffed6599ce add getrawmempool to rpcserver 2013-10-17 12:23:53 -04:00
Josh Rickmar
bbcfdcf5aa Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored.  Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value.  This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).

Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.

The RPC code is also refactored with this change as well, to separate
it more from websocket code.  Websocket JSON extensions are no longer
available to RPC clients.

While here, unbreak RPC.  Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened.  A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-17 09:11:55 -04:00
Josh Rickmar
340e8b999e Switch a log.Error to Debug 2013-10-14 14:04:36 -04: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
John C. Vernaleo
6212a7daff Improve rpcserver.
Add getdifficulty command to rpcserver.

Add missing error check in an old bit of rpcserver.

Add getbestblockhash command.
2013-10-11 17:04:18 -04: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
Josh Rickmar
21a2158eca Fixes for btcutil and btcscript api changes 2013-10-08 12:37:06 -04:00
Dale Rahn
a10da1ed6c Update btcd for btcdb API change.
Rearrange code to pull out last tx as was previously expected.
2013-10-04 09:23:48 -04:00
John C. Vernaleo
35c95f0a36 Handle case where no auth is sent to rpcserver. 2013-10-03 08:12:18 -04: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
John C. Vernaleo
73f08e72a2 Add basic http auth to rpc server. 2013-10-01 17:10:21 -04:00
Dave Collins
a5cc716940 Address some minor things reported by golint. 2013-09-18 00:36:40 -05:00
Dave Collins
33b65f943f Increment waitgroups before goroutines are started.
This commit changes the code so that all calls to .Add on waitgroups
happen before the associated goroutines are launched.   Doing this after
the goroutine could technically cause a race where the goroutine started
and finished before the main goroutine has a chance to increment the
counter.  In our particular case none of the goroutines exit quickly
enough for this to be an issue, but nevertheless the correct way should be
used.
2013-09-12 17:24:37 -05:00
John C. Vernaleo
00cd730c3f Add stop command to rpcserver. 2013-08-09 16:20:06 -04:00
Dave Collins
8574846e87 Move btcd to root directory.
This allows easier go get paths.
2013-08-07 12:47:51 -05:00
Renamed from btcd/rpcserver.go (Browse further)