Commit graph

1501 commits

Author SHA1 Message Date
Owain G. Ainsworth
35bd7ef6d9 rework the account manager somewhat.
- Move the MarkAddresForAccount and LookupAccountByAddress functionality
into account maanger.

- Move the wallet opeing logic into account manager (the only place that calls
it) and unexport.

- Move accountHandler to using a single channel for commands. Many of
the commands have ordering restraints (add account, list all accounts,
remove account, access account, mark account for address) which are very
much undefined with the multi-channel model.

- Rework all callers of LookupAccountByAddress to get the account structure
directly.
2014-04-03 17:52:16 +01:00
Owain G. Ainsworth
391b269d06 Implement addmultisigaddress and createmultisig. 2014-04-03 17:52:15 +01:00
Josh Rickmar
3f40e256c2 Return correct JSON object for listunspent.
This change fixes the reply for listunspent to return a JSON object in
the same format as done by the reference implementation.  Previously,
listunspent would return an array of the same objects as returned for
listtransactions.
2014-03-31 10:11:37 -05:00
Josh Rickmar
189df5c535 Mark partially synced addresses.
This change adds a notification handler for the new rescanprogress
notification and takes advantage of the recent rescan manager and
partial syncing support to mark addresses as partially synced.  If the
network connection to btcd is lost or wallet is restarted during a
rescan, a new rescan will start at the earliest block height for any
wallet address, taking partial syncs into consideration.
2014-03-28 11:28:59 -05:00
Josh Rickmar
aa0980bfa7 Simplify logic in WalletRequestProcessor. 2014-03-27 22:18:23 -05:00
Josh Rickmar
53e4070a5a Support partial syncing of addresses in wallet format.
This change reappropriates the unused `last block` field from Armory's
wallet format to hold the block chain height for a partially synced
address, that is, an address that has been partially synced to
somewhere between its first seen block and the most recently seen
block.  The wallet's SyncHeight method has been updated to return
partial heights as well.

The actual marking of partially unsynced address from a rescan
progress update is not implemented yet.
2014-03-27 13:48:40 -05:00
Josh Rickmar
9444fdb985 Make a comment understandable. 2014-03-26 21:15:56 -05:00
Josh Rickmar
d4bee3656c Avoid unnecessarily calculating a rescan job twice. 2014-03-26 21:13:10 -05:00
Josh Rickmar
59845d9c21 Implement a batching rescan manager.
Recent btcd versions only allow one rescan to run at any given time
per websocket client.  To better handle this, a new set of goroutines
are started by the account manager which batch and serialize rescan
jobs.

If no rescans are currently running, a new rescan starts.  If a rescan
is already being processed, the request is queued and runs after the
current rescan finishes.  For any additional incoming requests before
the current rescan finishes, the requests are merged with the
currently-waiting request so both can be handled with a single rescan.

This change also prepares for rescan progress notifications from btcd,
but are still unhandled until the necessary details for
partially-synced addresses are added to the wallet file format.
2014-03-26 17:27:30 -05:00
Jimmy Song
e22d221ea8 Issue #65: Give the correct error when wallet is locked
When sending coins to an address with a wallet that's both
locked and has insufficient funds, the correct ErrWalletLocked
error will be returned.
2014-03-25 16:38:31 -05:00
Josh Rickmar
c9ff0531f9 Bootstrap rescan requests with utxo set. 2014-03-21 15:36:42 -05:00
Josh Rickmar
6024e0ecb6 Pad byte slice passed to btcutil.EncodePrivateKey.
Calling the Bytes method for a big.Int does not pad the result to
required size for EncodePrivateKey.  This change adds the leading
padding, preventing seemingly-random "malformed private key" errors
from being returned to users of dumpprivkey.
2014-03-21 12:47:10 -05:00
Josh Rickmar
19fd6406e8 Prevent a send on closed chan panic.
The select statement does not guarantee selecting a better case if one
might panic for sending to a closed channel.  This case was hit during
client disconnect due to having multiple senders on a single channel
with one of the senders closing the chan to notify the next goroutine
to finish.  This change gives each writes its own unique channel to
prevent this error.
2014-03-20 16:10:42 -05:00
Josh Rickmar
4f1d2e7121 Add support for the authenticate extension request.
Just like btcd, this commit adds support for the authenticate request
allowing clients unable to set the HTTP Authorization header to
authenticate to use the RPC server.  The rules for the authenticate
request are as follows:

1) Authentication make clients stateful.  Clients may either be flagged
   as authenticated or unauthenticated.

2) Clients may authenticate by exactly one of two possible ways,
   either by setting the Authorization header or by sending a JSON-RPC
   authenticate request as follows:

   {
     "jsonrpc":"1.0",
     "id":0,
     "method":"authenticate",
     "params":["rpcuser", "rpcpass"]
   }

3) When not authenticated by the Authorization header, the first request
   must be an authenticate request.

4) Sending an authenticate request after a client has already
   successfully authenticated (either by the Authorization header or a
   previous authentication request) is invalid.

5) The result used in the response to a successful authenticate request
   is a JSON null.  For any unsuccessful or invalid authenticate
   requests, the connection is terminated.

This change also orders all incoming requests for a client.  This was
required to ensure that any authentication requests are processed
first.
2014-03-20 14:27:52 -05:00
Jimmy Song
c51cbb3332 Refactor len(w.secret) != 32
Now using w.IsLocked() for all instances of above.
Also changed one other place where the logic had to be reversed
in nextChainedAddress (len(w.secret) == 32 was the condition).
2014-03-20 11:37:54 -05:00
Owain G. Ainsworth
6dea3789cb update for btcutil.DecodeAddress api change. 2014-03-19 01:47:12 +00:00
Josh Rickmar
ec0d84882a Make tests compile after API change. 2014-03-18 17:52:08 -05:00
Josh Rickmar
29fef9ffd2 Fix nil pointer dereference.
Ran into by @toddfries and brought up on IRC.
2014-03-18 16:41:01 -05:00
Josh Rickmar
d179af8ecf Use min of now and blocktime for tx recv time. 2014-03-18 15:31:16 -05:00
Josh Rickmar
998a29b0e6 Do not warn for io.EOF when receiving ws msgs. 2014-03-17 22:36:44 -05:00
Josh Rickmar
e7cf48aa26 Warn when inconsistant TxStore is detected. 2014-03-17 20:42:47 -05:00
Josh Rickmar
25b7b7ea84 Improve btcd connection management.
This change slightly improves the goroutines managing sends and
receives for a btcd connection by improving the logging (logging the
exact errors that caused the connection to be lost) as well as
cleaning up the shutdown handling by closing the websocket connection
for any fail and closing the response channel when no more responses
can be read.
2014-03-17 16:00:04 -05:00
Josh Rickmar
5027acc348 Another Info -> Infof. 2014-03-17 15:50:45 -05:00
Josh Rickmar
6be1d86df2 Log reasons for disconnecting a websocket client. 2014-03-17 15:32:26 -05:00
Josh Rickmar
a0ffa6bdab Info -> Infof 2014-03-17 15:06:50 -05:00
Josh Rickmar
089fa9de18 Rescan and track sync status of imported addresses.
The private key import codepath (called when handling the
importprivkey RPC method) was not triggering rescans for the imported
address.  This change begins a new rescan for each import and adds
additional logic to the wallet file to keep track of unsynced imported
addresses.  After a rescan on an imported address completes, the
address is marked as in sync with the rest of wallet and future
handshake rescans will start from the last seen block, rather than the
import height of the unsynced address.

While here, improve the logging for not just import rescans, but
rescanning on btcd connect (part of the handshake) as well.

Fixes #74.
2014-03-17 13:46:42 -05:00
Owain G. Ainsworth
e358da905a Fix build. 2014-03-17 15:24:23 +00:00
Josh Rickmar
03185be3cf Perform all serialization tests on a bytes.Buffer. 2014-03-17 14:09:02 +00:00
Josh Rickmar
2fb710a02b fix up old comment 2014-03-17 14:09:02 +00:00
Owain G. Ainsworth
6a02b61b61 Move walletAddress interface around a bit.
Move the stuff that scripts can't possibly support out of the interface
and move about two type assertions so that everything still works. They
key-using interfaces can be made into a KeyedAddress itnerface if we add
any more.
2014-03-17 14:09:02 +00:00
Owain G. Ainsworth
2ef11ae7f5 Add support for pay-to-script-hash addresses to wallet. 2014-03-17 14:09:02 +00:00
Owain G. Ainsworth
3831ba7abc add very basic tests for privkey import. 2014-03-13 19:14:27 +00:00
Owain G. Ainsworth
59fb904dc7 Prepare for adding other types of wallet addresses other than pkhash
Add a walletAddress interface to handle the differences betweent he
different types. Stop using btcutil.AddressPubKeyHash everywhere and just use
the standard address.
2014-03-13 19:14:27 +00:00
Owain G. Ainsworth
df31e30839 Make AddressInfo an interface.
Shortly we will add new types of address, so make AddressInfo an
interface, with concrete types providing address-specific information.
Adapt existing code to this new status quo.
2014-03-13 19:14:27 +00:00
Owain G. Ainsworth
34e4c0be35 call addr.lock() instead of handrolling. 2014-03-13 19:14:27 +00:00
Owain G. Ainsworth
a75ec902e8 AddressUsed: try all addresses in a transaction 2014-03-13 19:14:26 +00:00
Owain G. Ainsworth
d9a3f4324c tyop. 2014-03-13 19:14:26 +00:00
Josh Rickmar
715aae1e8d Always return an initialized map from OpenAccounts. 2014-03-13 13:37:09 -05:00
Josh Rickmar
44c2e8a3cb Fix grammar. 2014-03-03 19:31:57 -05:00
Josh Rickmar
c27f52e068 Remove devil comment. 2014-03-03 18:15:05 -05:00
Josh Rickmar
2e76bcd159 Handle out-of-order notifications from btcd.
Notifications ariving from btcd were being reordered (each handled by
its own goroutine, rather then being always sent in the order they
originated).  This was breaking the new transaction store by inserting
transaction records in an 'impossible' manner, that is, inserting txs
without block info after the store already held records of the same tx
with block info, without first performing a rollback.

This is handled by the transaction store insert methods by checking
for identical transactions (double spends with the same tx sha), but
where the block heights mismatch and the new record does not have a
block set.  The error is returned all the way up to the goroutine
running each rpc request/notification handler, and if hit, the btcd
connection is closed and all accounts are reopened from disk.  This is
not optimal, but it allows us to use the connect logic to correctly
catch us up to the best chain with the last good state of all accounts
while only rescanning a few blocks.

Fixes #72.
2014-02-28 15:43:50 -05:00
Josh Rickmar
76c6379a54 Update for btcutil.Address API change. 2014-02-26 15:22:48 -05:00
Josh Rickmar
acb4819ee1 Allocate and init transaction store for newly created accounts. 2014-02-26 13:56:44 -05:00
Josh Rickmar
6805d7a7a1 Kick off full rescan if tx file is missing. 2014-02-24 16:01:03 -05:00
Josh Rickmar
fc2e313a39 Introduce new transaction store.
This change replaces the old transaction store file format and
implementation.  The most important change is how the full backing
transactions for any received or sent transaction are now saved,
rather than simply saving parsed-out details of the tx (tx shas, block
height/hash, pkScripts, etc.).

To support the change, notifications for received transaction outputs
and txs spending watched outpoints have been updated to use the new
redeemingtx and recvtx notifications as these contain the full tx,
which is deserializead and inserted into the store.

The old transaction store serialization code is completely removed, as
updating to the new format automatically cannot be done.  Old wallets
first running past this change will error reading the file and start a
full rescan to rebuild the data.  Unlike previous rescan code,
transactions spending outpoint managed by wallet are also included.
This results in recovering not just received history, but history for
sent transactions as well.
2014-02-24 14:35:30 -05:00
Josh Rickmar
438f55a0a4 Set deadline on client websocket sends.
It appears that the websocket package will occasionally enter a
Codec's Send function and block forever, never erroring (presumably
due to a closed connection).  This change adds a deadline for the send
of two seconds.  If the send cannot complete before the deadline is
reached, the send is aborted and the connection is assumed to be lost.

A buffer should be added here as well, so even waiting max two seconds
for the send to error out won't cause wallet code to block.
2014-02-19 13:57:52 -05:00
Josh Rickmar
3a23fdaf64 Move duplicater chan send out of default case.
At any instant when a duplicated notification must be sent, either one
of two channel sends/recvs must occur.  The first possibility is that
the client is disconnected, in which case the disconnected channel
will be read, and then the context removed from the goroutine-managed
map.  The second possibility is that the disconnect channel has not
yet been closed, in which case it must block on an actual message
send.  This change moves the second case out of the default case of
the select statement to avoid a race where:

1) The client has not yet disconnected, and the disconnected chan is
   not ready for reads.
2) Control switches to the default case.
3) The client disconnects, the goroutine reading the send channel
   returns, closes the disconnected channel, and no more reads occur.
4) The notification duplicator blocks forever trying to send the
   message even when the disconnected notification channel has
   already been closed.
2014-02-18 21:11:33 -05:00
Josh Rickmar
2ff7f85b0c Notify connected ws clients of btcd connect state.
This shouldn't be nececssary (a TODO was added to remind me to make
clients explicitly ask for this info) but in the meantime this fixes
clients such as btcgui which otherwise wouldn't think btcwallet is
properly connected to btcd and will desensitise some widgets.
2014-02-18 15:51:06 -05:00
Josh Rickmar
e837ca5b64 Cleanly remove disconnected clients.
Also fixes a bug where responses for a single client would be sent to
every connected client.
2014-02-18 12:39:53 -05:00
Owain G. Ainsworth
3a6ae93a4b Implement validateaddress rpc command.
Closes #60
2014-02-14 14:35:59 +00:00