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.
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.
This fixes two issues: first, the sendrawtransaction handler had an
extra character in the key in the websocket handler map, preventing
the handler from never running. Second, a nil pointer dereference was
removed from the handler.
This change fixes the minedtx notifications for btcwallet, since the
websocket-handler now runs instead of falling back to the legacy RPC
handler.
This commit adds the btcdb memdb backend as a supported database type.
Note that users will NOT want to run in this mode because, being memory
only, it obviously does not persist the database when shutdown.
It is being added for testing purposes to help prevent constant abuse to
developer's hard drive when churning the block database multiple times a
day.
The specific parameters required by a backend is better left up to the
backend itself. For example memdb has no need for a database path, while
ldb does. This commit modifies the OpenDB and CreateDB functions to take
a arbitrary arguments which are passed along to the driver. The driver is
expected to verify the correct type and number of arguments and error
accordingly.
The existing backends have been updated accordingly.
This commit prunes several unused functions from the Db interface and the
underlying implementations. For the most part these are holdovers from
the original sqlite implementation. It also removes the types associated
with those functions since they are no longer needed. The following
functions and types have been removed:
- InvalidateCache
- InvalidateBlockCache
- InvalidateTxCache
- SetDBInsertMode
- InsertMode type and associated constants
- NewIterateBlocks
- BlockIterator interface
The reasons for removing these are broken out below.
- Neither of two current implementations implement these functions nor
does any of the fully functional code using the interface invoke them.
- After contemplating and testing caching of blocks and transactions at
this layer, it doesn't seem to provide any real benefit unless very
specific assumptions about the use case are made. Making those
assumptions can make other use cases worse. For example, assuming a
large cache is harmful to memory-constrained use cases. Leaving it up
to the caller to choose when to cache block and transactions allows much
greater flexibility.
- The DB insert mode was an artifact of the original sqlite implementation
and probably should have only been exposed specifically on the
implementation as opposed through generic interface. If a specific
implementation wishes to provide functionality such as special modes,
that should be done through type assertions.
This commit switches the handleGetHeadersMsg function to make use of the
new FetchBlockHeightBySha and FetchBlockHeaderBySha functions in btcdb.
Also, while here, nuke the header copy which is no longer required due to
the recent btcwire changes.
This commit introduces two new functions to the btcdb.Db interface named
FetchBlockHeightBySha and FetchBlockHeaderBySha.
The FetchBlockHeightBySha function is useful since previously it was only
possible to get the height of block by fetching the entire block with
FetchBlockBySha and pulling the height out of the returned btcutil.Block.
The FetchBlockHeaderBySha function will ultimately make it much more
efficient to fetch block headers. Currently, due to the database design
in the ldb backend, the entire block has to be loaded anyways, so the only
current benefit is to avoid the deserialize on all of the transactions.
However, ultimately btcdb will gain a more efficient backend which can
also avoid reading all of the extra transaction data altogether.
The sqlite3 backend has been deprecated for quite some time. As a result,
it has not been updated with many of the more recent changes which means
the behavior no longer conforms to the interface contract.
This commit reduces the initial idle timeout before version negotiation
has happened on a new peer to 30 seconds. Previously it could take 5
minutes due to the general idle timeout.
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.
The websocket package assumes binary blobs if []byte is used. Since we're
using JSON-RPC for all the websocket communications, it should be text
based.
This commit changes the websocket Send/Receive and associated channels to
strings accordingly.
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.
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.
It was pointed out in #76 that if you arrived to the Update section
of the README without seeing the Installation section, the requirement for
Go 1.2 is easy to miss. This commit builds the requirement in the
Installation section and adds it to the Updating section as well to
hopefully make it more noticable.
The addblock utility was originally written as a quick debug tool during
initial development to populate blocks into the database. However, now
that it has been designated as the standard way to import bootstrap.dat
(and indeed block data files in general), it was lacking a few features
such as properly checking against the chain rules and known good
checkpoints.
This commit reworks and improves the utility in several ways:
- Imported blocks are now checked against the chain rules including
checkpoints to ensure they match the known good chain
- The utility now properly shuts down after processing all blocks
- Attempting to import orphan blocks (blocks which build off a block you
don't yet have in the database) returns an error
- Blocks that are already known are now skipped instead of causing an
error which means you can stop and restart the import mid-way through
without issues or start it after you've already downloaded a
portion of the chain
- The block height is no longer assumed to start at 0 which means input
files that start later in the chain will work properly so long as you
already have the chain at least up to the point of the block just before
the first one in the input file
- Improved error handling and reporting
- How often the progress display is shown is now configurable
- Statistics about how many blocks were processed, imported, and already
known are now displayed after the input file has been fully processed
This resolves comments made in #60.
The warning about a missing config file should only be shown after all
other configuration has succeeded so it's not shown when there are invalid
options specified. Also add a comment about it where its intended
placement is for the future.
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.