Commit graph

360 commits

Author SHA1 Message Date
Dave Collins 92ca0e92c0 Implement getwork RPC.
This commit adds full support for the getwork RPC which can be used to
support external mining through applications such as cgminer.

Closes #125.
2014-05-01 10:52:28 -05:00
Dave Collins bdaa5f7f8d Set RPC content type to applicaton/json.
Closes #121.
2014-04-24 23:20:13 -05:00
David Hill 8a322e4792 Bypass rate limiter for sendrawtransaction.
ok @davecgh
2014-04-23 07:45:56 -04:00
Dave Collins de51409185 Add debug print for chain verify.
Since a chain verification can take a long time depending on the
parameters, this commit adds a debug print to the RPC server at the info
level for how many blocks are being verified and at what level.

The logic was also slightly modified so the number of blocks being checked
can easily be calculated and shown.
2014-04-14 10:29:51 -05:00
David Hill 3937c1a67c This RPCS debug statement should be a trace. 2014-04-13 15:50:28 -04:00
Dave Collins 444d05eafc Run go fmt. 2014-03-28 14:49:48 -05:00
Owain G. Ainsworth 613f61ec59 simplify comment and fit into 80 cols. 2014-03-28 00:02:57 +00:00
mydesktop ab002c90cc Implement a rebroadcast handler.
This commit implements a rebroadcast handler which deals with
rebroadcasting inventory at a random time interval between 0 and 30
minutes.  It then uses the new rebroadcast logic to ensure transactions
which were submitted via the sendrawtransaction RPC are rebroadcast until
they make it into a block.

Closes #99.
2014-03-27 14:09:33 -05:00
Dave Collins 2ae7cb8ee2 Update handleGetBlock to not use deprecated func.
Rather than using the deprecated TxShas function on a btcutil.Block,
convert handleGetBlock to use the newer preferred method of ranging over
the Transactions to obtain the cached hash of each transaction.

This is a little more efficient since it can avoid creating and caching an
extra slice to keep the hashes in addition to having the hash cached with
each transaction.
2014-03-24 13:19:32 -05:00
Dave Collins 049a545427 Switch RPC server to use fastsha256 package. 2014-03-23 00:49:37 -05:00
Dave Collins 9d6dd6fa6b Don't fail RPC server create due to single iface.
Rather than returning an error when creating the RPC server an it can't
listen on any of the specified interfaces, only error when it can't listen
on all of the specified interfaces.
2014-03-20 21:54:50 -05:00
Dave Collins 3b9b11cb41 Update for recent btcutil.Address API change. 2014-03-18 20:44:03 -05:00
Josh Rickmar 239e13c0a5 Add createencryptedwallet to wallet request set. 2014-03-18 10:52:43 -05:00
Dave Collins e5afb5e984 Improve error return from sendrawtransaction.
Rather than simply returning "TX rejected" to the RPC client which
submitted a sendrawtransaction that fails, include the reason the it
failed.
2014-03-17 17:34:25 -05:00
Dave Collins 7a885b3cf6 Reject orphan transactions from sendrawtransaction.
This commit modifies the sendrawtransaction RPC to reject transactions
which are orphans.  This mirrors the behavior of the reference
implementation.
2014-03-17 17:32:30 -05:00
Francis Lam 24028ad37f Updated getinfo to return new RelayFee and use btcjson.InfoResult 2014-03-16 13:31:17 -05:00
Dave Collins 510a44bdd9 Fix some comment typos. 2014-03-13 08:45:41 -05:00
Josh Rickmar a7d5b365b1 Retain order when processing client notifications.
This change modifies the RPC server's notifiation manager from a
struct with requests, protected by a mutux, to two goroutines.  The
first maintains a queue of all notifications and control requests
(registering/unregistering notifications), while the second reads from
the queue and processes notifications and requests one at a time.

Previously, to prevent slowing down block and mempool processing, each
notification would be handled by spawning a new goroutine.  This lead
to cases where notifications would end up being sent to clients in a
different order than they were created.  Adding a queue keeps the
order of notifications originating from the same goroutine, while also
not slowing down processing while waiting for notifications to be
processed and sent.

ok @davecgh
2014-03-04 11:15:25 -05:00
Dave Collins f01459c30e Update for recent btcutil Addr interface changes. 2014-02-26 14:06:48 -06:00
Josh Rickmar a3ccc25e5a Return errors for any sendrawtransaction rejection.
This changes the implementation of the sendrawtransaction RPC handler
to match bitcoind behavior by always returning a rejection error for
any error processing or accepting the tx by the mempool.  Previously,
if the tx was rejected for a rule error rather than an actual failure,
a client would still receive the tx sha as a result with no error.
2014-02-21 09:03:04 -05:00
Dave Collins 54203d7db0 Rework and improve websocket notification system.
This commit refactors the entire websocket client code to resolve several
issues with the previous implementation.  Note that this commit does not
change the public API for websockets.  It only consists of internal
improvements.

The following is the major issues which have been addressed:
- A slow websocket client could impede notifications to all clients
- Long-running operations such as rescans would block all other requests
  until it had completed
- The above two points taken together could lead to apparant hangs since
  the client doing the rescan would eventually run out of channel buffer
  and block the entire group of clients until the rescan completed
- Disconnecting a websocket during certain operations could lead to a hang
- Stopping the rpc server with operations under way could lead to a hang
- There were no limits to the number of websocket clients that could
  connect

The following is a summary of the major changes:

- The websocket code has been split into two entities: a
  connection/notification manager and a websocket client
- The new connection/notification manager acts as the entry point from
  the rest of the subsystems to feed data which potentially needs to
  notify clients
- Each websocket client now has its own instance of the new websocket
  client type which controls its own lifecycle
- The data flow has been completely redesigned to closely resemble the
  peer data flow
- Each websocket now has its own long-lived goroutines for input, output,
  and queuing of notifications
- Notifications use the new notification queue goroutine along with
  queueing to ensure they dont't block on stalled or slow peers
- There is a new infrastructure for asynchronously executing long-running
  commands such as a rescan while still allowing the faster operations to
  continue to be serviced by the same client
- Since long-running operations now run asynchronously, they have been
  limited to one at a time
- Added a limit of 10 websocket clients.  This is hard coded for now, but
  will be made configurable in the future

Taken together these changes make the code far easier to reason about and
update as well solve the aforementioned issues.

Further optimizations to improve performance are possible in regards to
the way the connection/notification manager works, however this commit
already contains a ton of changes, so they are being left for another
time.
2014-02-19 00:53:05 -06:00
Dave Collins 97e0149dc3 Include IP address in RPC auth failure log message. 2014-02-19 00:30:18 -06:00
Dave Collins a293212581 Add --rpcmaxclients option with default of 10.
This commit adds a new configuration option, --rpcmaxclients, to limit the
number of max standard RPC clients that are served concurrently.  Note
that this value does not apply to websocket connections.  A future commit
will add support for limiting those separately.

Closes #68.
2014-02-18 20:46:41 -06:00
Dave Collins 66e93f5163 Switch over to new btcjson.GetRawMempoolResult.
Rather than using a type specifically in btcd for the getrawmempool, this
commit, along with a recent commit to btcjson, changes the code over to
use the type from btcjson.  This is more consistent with other RPC results
and provides a few extra benefits such as the ability for btcjson to
automatically unmarshal the results into a concrete type with proper field
types as opposed to a generic interface.
2014-02-16 14:00:40 -06:00
Dave Collins 44e3a44a9c Remove help addenda for getpeerinfo.
This was no longer accurate since btcd now implements all getpeerinfo
fields.
2014-02-11 20:45:15 -06:00
David Hill 642c834ada move Connection header so it is applied to failed authentication
attempts as well.

ok @davecgh
2014-02-07 20:59:20 -05:00
Dave Collins 476000193f Implement getnetworkhashps RPC.
Closes #87.
2014-02-07 16:32:11 -06:00
Dave Collins 1716136f62 Return milliseconds in getnettotals RPC.
This was returning microsecond instead of milliseconds.
2014-02-05 11:26:03 -06:00
Dave Collins a39f4a0698 Correct total byte counters for server.
Previously the getnettotals was just looping through all of the currently
connected peers to sum the byte counts and returning that.  However, the
intention of the getnettotals RPC is to get all bytes since the server was
started, so this logic was not correct.

This commit modifies the code to keep an atomic counter on the server for
bytes read/written and has each peer update the server counters as well as
the per-peer counters.
2014-02-05 11:15:41 -06:00
Dave Collins 5d70935b04 Convert getnettotals TimeMillis field to UTC.
This matches the reference implementation.
2014-02-05 09:54:16 -06:00
Dave Collins 6f5f582c42 Implement getnettotals RPC.
Also, change the display handler for getnettotals in btcctl to the JSON
display handler for better display.

Closes #84.
2014-02-05 09:39:03 -06:00
David Hill 9f044fb946 use %d for int32 2014-02-04 13:55:23 -05:00
Dave Collins 2a7d725a09 Move websocket endpoint to /ws.
This commit moves the connection endpoint for websockets to /ws instead of
/wallet. First, the former is more standard, and second the latter
presumes how the websocket is to be used.

Closes #80.
2014-02-03 10:46:03 -06:00
David Hill 2b9f5b8932 in getrawmempool verbose mode, initialize depends to an empty array.
this matches bitcoind.

from jrick
ok davec
2014-01-29 22:22:07 -05:00
David Hill b532860477 sync with btcjson changes to getrawtransaction.
ok oga@
2014-01-29 21:21:58 -05:00
Owain G. Ainsworth 82fca37eae implement the getinfo rpc command.
This contains some wallet inforamtion, but bitcoind if wallet is
disabled returns just the non wallet information. we do the same.
2014-01-29 18:31:33 +00:00
Owain G. Ainsworth 8d930ceed1 convert getdifficulty to getBlockHeaderBySha to save some cycles. 2014-01-29 18:31:33 +00:00
Dave Collins 72afc787e6 Move getinfo RPC method to askwallet list.
The getinfo RPC method requires access to information only available in
the wallet.  Therefore, it has been moved to the list of methods which
return an error information the caller to send the request to the wallet
instead.
2014-01-27 13:52:59 -06:00
Dave Collins b9a641ab79 Move getnewaddress RPC method to askwallet list.
The getnewaddress RPC method deals with wallet-related functionality and
therefore has been moved to the list of methods which return an error
information the caller to send the request to the wallet instead.
2014-01-27 10:58:24 -06:00
Dave Collins 0bf4e0e097 Move getblocktemplate/getwork to askwallet list.
Both of these RPC methods require access to information ony available in
the wallet.  Therefore they have been moved to the list of methods which
return an error information the caller to send the request to the wallet
instead.
2014-01-25 22:58:38 -06:00
Dave Collins dcef4128b8 Add support for getaddednodeinfo RPC command.
This commit adds full support for the getaddednodeinfo RPC command
including DNS lookups which abide by proxy/onion/tor rules when the DNS
flag is specified.  Note that it returns an array of strings when the DNS
flag is not set which is different than the current version of bitcoind
which is bugged and scheduled to be fixed per issue 3581 on the bitcoind
issue tracker.
2014-01-25 22:50:32 -06:00
Dave Collins 5859deea7e Improve RPC authentication failure responses.
This commit improves how the legacy RPC server responds to authentication
failures so things like web browsers can react better.  The following
changes have been made:

First, authentication failures were only printing the 401 error response
in the body instead of setting the http status code.  This means the
response had a 200 OK header with a body of 401 Unauthorized.  Therefore
the client would think everything was ok, but see the response as
malformed JSON.

Second, the spec for 401 Unauthorized responses state they must include a
WWW-Authenticate header to instruct the client how to authenticate.
Without this, browsers won't prompt the user for credentials.
2014-01-23 11:27:48 -06:00
Dave Collins 0d40bf901d Implement alternative auth for websockets.
The previous websocket code required HTTP auth headers to be sent in order
to use the websocket.  While this makes sense for most applications, some
use cases such as javascript-based websockets from browsers do no have the
ability to send headers.

This commit modifies the authentication logic to allow an alternative
authentication mechanism when HTTP auth headers can't be sent.  In
particular, it introduces a new JSON-RPC command named authenticate which
accepts the username and passphrase for the RPC server.  The command is
only required if the connetion has not already been authenticated via HTTP
auth headers and it must be the first command to be received.  Sending any
other command will immediately disconnect the websocket.

ok from @owainga and @jrick.

This closes #77.
2014-01-22 17:40:14 -06:00
Owain G. Ainsworth 9cb5190ac2 add support for the ping rpc command.
And the pingtime and pingwait fields of getpeerinfo.
2014-01-22 16:21:08 +00:00
Owain G. Ainsworth 1487a352da add basic support for the help rpc command. 2014-01-22 16:20:38 +00:00
Dave Collins 8c7d44c8dc Add authentication deadline to RPC server cnxns.
Previously it was possible to open a connection to the RPC server, never
authenticate, and idle forever.

This is work toward #68.
2014-01-18 00:08:39 -06:00
Dave Collins 0fbd962f8a Use our own websocket.Server instance.
The websocket.Server used the by websocket.Handler type automatically adds
a handshake function which prevents connections when the Origin header is
not set.  Not all clients send this information and we already require
authentication headers as the auth mechanism anyways.
2014-01-17 16:30:29 -06:00
Dave Collins f089853d4d Move RPC websocket init code to rpcwebsocket.go. 2014-01-17 15:44:36 -06:00
Josh Rickmar 20e56d6eda Ask for block(dis)connected updates.
This removes the last notification that was being sent unsolicited.
Since it is no longer needed, the code to duplicate notifications to
all clients has been removed.
2014-01-17 16:38:16 -05:00
David Hill 871481ce1b Implement submitblock.
Closes #61.
2014-01-15 15:07:43 -06:00
Josh Rickmar bd98836a2b Fix several bugs in the RPC server shutdown.
The RPC server was performing some of the shutdown logic in the wrong
order, that is, logging the the server has shut down, waiting for all
server goroutines to finish, and then closing a channel to notify
server goroutines to stop.  These three items have been reversed to
fix a hang where goroutines currently being waited on had not shut
down because they did not receive the notification.

While here, the server waitgroup was incremented for a goroutine that
was running without it, another select statement was added to stop a
duplicate close (which never occured last commit when I added the
select statements), and the "stopping rescan" logging was moved to
debug to make the ^C shutdown logging nicer.
2014-01-14 22:53:07 -05:00
Josh Rickmar 6abad1d8ac Change websocket handlers to return results.
This is the first step to fixing #66.
2014-01-14 13:15:22 -05:00
Josh Rickmar 035f8f82b7 Switch to btcutil for certificate generation. 2014-01-10 15:41:57 -05:00
David Hill 3a59e4d064 Set the Connection header to close 2014-01-10 10:57:05 -05:00
Josh Rickmar cd3084afcd Rework the btcwallet connection.
This changes the protocol between btcd and btcwallet to follow
JSON-RPC specifications sending notifications as requests with an
empty ID.

The notification request context handling has been greatly cleaned up
now that IDs no longer need to be saved when sending notifications.
2014-01-09 14:03:00 -05:00
Dave Collins 462bc5a031 Update for recent btcscript API changes.
This commit changes all code which deals with extracting addresses from
scripts to use the btcscript API ExtractPkScriptAddrs which in turn makes
use of the new btcutil.Address interface.

This provides much cleaner code for dealing with arbitrary script
destinations which is extensible without having to churn the APIs if new
destination types are added.
2014-01-08 11:22:29 -06:00
Dave Collins 8a73f9b245 Correct p2sh field of decodescript RPC result.
The p2sh field in the result should be the encoded address which includes
the network (human-readable address) instead of just the raw hash.
2014-01-04 12:44:14 -06:00
Dave Collins 92eee5cb96 Populate p2sh field in decodescript RPC result. 2014-01-04 11:55:09 -06:00
Dave Collins 47a78ea5c2 Add support for decodescript RPC command. 2014-01-03 23:29:24 -06:00
Dave Collins b6b2fd15b3 Add support for createrawtransaction RPC command. 2014-01-03 12:43:13 -06:00
Josh Rickmar 405eca4a44 Remove usage of deprecated address encode/decode API. 2014-01-03 13:22:28 -05:00
Francis Lam 082ad7caf2 Updated createVoutList to support scripthash/multisig outputs
Used updates in btcscript/btcutil to support decoding scripthash and
multisig outputs for display in getrawtransactions/decoderawtransaction
2014-01-02 13:48:50 -06:00
Dave Collins 30802fdd52 Rename RPC handlers map to rpcHandlers.
The name handlers for a package level is a bit too generic and could
easily cause a name collision.  Even though the compiler would catch it,
use something a bit more descriptive.
2013-12-31 15:53:19 -06:00
Dave Collins 835cee229a Move RPC handler maps near the top of file.
Since the command to handler mappings are the most often modified and
referenced code in rpcserver.go and rpcwebsocket.go, move them near the
top of their respective files.
2013-12-31 14:48:50 -06:00
Dave Collins 9b166b3876 Remove wallet notifications chan from std commands.
This commit cleans up the standard RPC command hanlding a bit by removing
the websocket specific notification channel from the handlers.  This was
previously required because the sendrawtransaction, when called from a
websocket enabled connection, needs to add a notification for when the
transaction is mined.

This commit modifies that to instead implement a websocket extended
version of sendrawtransaction which invokes the standard handler and adds
the notification.  In addition, the main send was modified to first look
if the command has a websocket specific handler first, and then falls back
to standard commands, rather than the previous approach of first checking
for a standard command and falling through to websocket commands.  This
essentially allows websockets connections to extend commands with the same
name with additional functionality such as what was done in this commit.
2013-12-31 14:42:15 -06:00
Dave Collins 5ad6d543d6 Move RPC websocket code to its own file.
The rpcserver.go file is starting to get a bit unwieldy.  This commit
moves the separable websocket specific bits into a separate file named
rpcwebsocket.go.
2013-12-31 13:19:07 -06:00
Dave Collins f2a2744bec Correct error handling from the previous commit. 2013-12-30 20:27:36 -06:00
Francis Lam 28d08f8b16 Small changes to createVinList/createVoutList
Added error checking for script disassembley

Changed vout to handle errors in processing the way bitcoind does: the
type displayed is "nonstandard" when the calculated type is nonstandard
or nulltype and also when there is an error getting the address.

Still doesn't properly support multisig addresses, but now it should
return "nonstandard" since since address lookup fails for those cases.
2013-12-30 20:06:13 -05:00
Dave Collins eb624acfd4 Add support for decoderawtransaction RPC command.
Since the decoderawtransaction result makes use of the same vin and vout
lists, this commit also factors the logic for those out into separate
functions.
2013-12-30 18:33:13 -06:00
Dave Collins d17a97b485 Correct verifychain RPC call return.
The return value should be a boolean for compatibility.
2013-12-30 10:59:14 -06:00
Josh Rickmar bd29b12d31 Notify wallets when mempool txs pay to a wallet address. 2013-12-30 09:33:36 -05:00
Dave Collins 674ef590bb No need to cast something is already a uint32. 2013-12-27 13:20:13 -06:00
Dave Collins f0cc672d23 Update for btcjson sequence number type change. 2013-12-27 12:38:51 -06:00
Dave Collins ca0e38e58b Update for recent btcjson getrawtransaction change.
The ScriptSig field of the Vin type for TxRawResult is now a pointer in
btcjson so it can be properly omitted.  This commit updates the code to
create the new ScriptSig object as needed.
2013-12-27 12:24:03 -06:00
Dave Collins e0fab228a4 Correct getrawtransaction RPC handling and cleanup.
The getrawtransaction RPC call should return a hex-encoded string of the
transaction when verbose is false instead of a TxRawResult object with the
Hex field set to be compatible with the Sathoshi client.  This commit,
along with a recent commit to btcjson corrects this.

Also, while here, do a bit of cleanup, finish a TODO to check for an
invalid hash, and optimize the handling of non-verbose slightly.
2013-12-26 23:30:57 -06:00
Dave Collins af3609d861 Factor out common message to hex in RPC server. 2013-12-26 22:53:44 -06:00
Dave Collins 8477ef569a Update btcctl getblock for recent changes.
This commit allows btcctl to accept the optional verbosity parameters on
getblock.
2013-12-26 11:19:32 -06:00
Dave Collins 67b5c2fb7e Correct getblock RPC handling and optimize.
The getblock RPC call should return a hex-encoded string of the block when
verbose is false instead of a BlockResult object with a Hex field set to
be compatible with the Sathoshi client.  This commit, along with a recent
commit to btcjson corrects this.

Also, while here, move code which only applies to verbose mode after the
call which handles the non-verbose logic.  This saves a few cycles since
the non-verbose logic doesn't need the extra information.
2013-12-26 11:13:31 -06:00
Dave Collins dc200d002e Correct getrawmempool verbose fee field.
The fee field of the getrawmempool RPC JSON response should be in Bitcoins
instead of Satoshi.  This commit corrects that issue.

Also, add a couple of comments and fix a comment typo while here.
2013-12-25 12:33:12 -06:00
Owain G. Ainsworth d72255bce3 gofmt 2013-12-17 14:02:35 +00:00
Owain G. Ainsworth 8aaad1e97b Add support for verbose in getrawmempool.
Closes #55.
2013-12-16 18:14:50 +00:00
Josh Rickmar e4fa45ff08 Better logging for rescans. 2013-12-16 09:09:28 -05:00
Owain G. Ainsworth f93203b91e Initial basic support for selection of external ip address.
This implements only the bare bones of external ip address selection
using very similar algorithms and selection methods to bitcoind. Every
address we bind to, and if we bind to the wildcard, every listening
address is recorded, and one for the appropriate address type of the
peer is selected.

Support for fetching addresses via upnp, external services, or via the
command line are not yet implemented.

Closes #35
2013-12-10 19:39:47 +00:00
Dave Collins 7654eb1eb5 Cast SatoshiPerBitcoin const for createTxRawResult. 2013-12-08 20:21:23 -06:00
Francis Lam 22b61f634a Updated createTxRawResult to use btcutil.SatoshiPerBitcoin 2013-12-08 18:47:39 -05:00
Francis Lam dd10de9e8b Fix handleGetBlock/handleGetRawTransaction to return btcjson.Error
Wrap *.BtcEncode errors into btcjson.Error when failing to encode wire
bytes to buffer
2013-12-08 18:43:47 -05:00
Francis Lam 762fc2c11c Fixed up GetRawTransaction and updated GetBlock to handle verbose
Updated handleGetRawTransaction to populate all the fields required to
match bitcoind.  It still doesn't handle MULTISIG addresses correctly.

Changed handleGetBlock to implement new optional verbose (default true)
flag and also added a verboseTx flag to return TxRawDefault instead of
Txid.  When verbose=false, GetBlock returns hex-encoded wire bytes for
the block.
2013-12-08 14:57:14 -05:00
Josh Rickmar 305be0c29f Correctly set vout for getrawtransaction.
The vout field (as part of the getrawtransaction JSON reply) should be
set to the input's previous outpoint's index, not the current input
index.

Found by flam and reported on IRC. Thanks!
2013-12-06 17:34:18 -05:00
Josh Rickmar bbb10dc387 Make authentication check time-constant. 2013-12-05 13:21:51 -05:00
Josh Rickmar f22164b261 Search each txout for payment to a wallet address.
This switches a break to a continue if a txout does not include a
pay-to-pubkey-hash script type.  btcwallet only supports
pay-to-pubkey-hash at the moment, and this fixes an issue where a tx
may have an different type of output, as well as pay-to-pubkey-hash,
which may be ignored by the wallet notification code.

Found by dhill.
2013-12-02 10:35:25 -05:00
Josh Rickmar aea23ddff3 Send nil when rescan is finished.
I previously fixed the duplicate send (before seeing GH issue #54),
but forgot that btcwallet expects a nil reply when rescan has
finished.  This adds the final reply back, but replies with nil.

Fixes #54.
2013-12-02 10:24:22 -05:00
Josh Rickmar 41ecc9f835 Do not duplicate sending final rescan tx. 2013-12-02 10:11:00 -05:00
Josh Rickmar d1570c5d87 Include more data in tx-to-wallet-address notifications.
This updates the replies for rescan and tx notifications with
additional information that is needed for wallet to properly support
the listtransactions command.

While here, drastically improve rescan performance by not looking up
every sha in rescan's block range.
2013-11-26 15:07:01 -05:00
Josh Rickmar 7e21226ca6 Update for new btcws.TxMinedNtfn information. 2013-11-25 12:54:23 -05:00
Dave Collins daa5310e2f Add support for debuglevel RPC command.
Also include the supported subsystems in the error message if an invalid
subsystem is specified.

Closes #15.
2013-11-22 10:47:29 -06:00
John C. Vernaleo e930dc55f0 Change a variable name to match btcjson. 2013-11-22 08:50:15 -05:00
Dave Collins eb8688df79 Convert btcd to use new btclog package.
Also, make every subsystem within btcd use its own logger instance so each
subsystem can have its own level specified independent of the others.

This is work towards #48.
2013-11-21 17:41:21 -06:00
Marco Peereboom 694fccefa8 typo 2013-11-20 15:55:36 -06:00
John C. Vernaleo 3f37e881dc Remove workaround for certs on go1.1.2.
btcd now requires go1.2.  A note to that effect is in README.md.
2013-11-20 15:34:37 -05:00
Josh Rickmar 5d13288174 Check auth header for websocket connections. 2013-11-19 18:28:08 -05:00
Dave Collins 9edf7d44fa Ensure generated cert contains 127.0.0.1. 2013-11-19 16:35:00 -06:00
David Hill deb19c2fa3 Bring MarshalECPrivateKey local so < go1.2 works.
Closes #45.
2013-11-19 15:53:31 -06:00
Dave Collins c3fab78e2c Use consistent case in TLS logging. 2013-11-19 11:42:56 -06:00
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)