Commit graph

366 commits

Author SHA1 Message Date
Wilmer Paulino
e754478496
wallet: modify recovery logic to not start from genesis
This commit serves as another building point to allow the wallet to not
store blocks all the way from genesis to the tip of chain. We modify the
wallet's recovery logic to now start from either its birthday block, or
the current reorg safe height if it's before the birthday, to ensure the
wallet properly only stores MaxReorgDepth blocks.

We also refactor things a bit in hopes of making the logic a bit more
readable.
2019-06-13 18:09:02 -07:00
Wilmer Paulino
2a6f24c61b
wallet: locate birthday block without scanning chain from genesis
We do this as the wallet will no longer store blocks all the way from
genesis to the tip of the chain. Instead, in order to find a reasonable
birthday block, we resort to performing a binary search for a block
timestamp that's within +/-2 hours of the birthday timestamp.
2019-06-13 18:09:01 -07:00
Wilmer Paulino
17efcdba2f
wallet: wait until chain backend is current to begin wallet sync
This serves as groundwork for only storing up to MaxReorgDepth blocks
upon initial sync. To do so, we want to make sure the chain backend
considers itself current so that we can only fetch the latest
MaxReorgDepth blocks from it.
2019-06-13 18:09:00 -07:00
Wilmer Paulino
39f81c630b
chain: add IsCurrent method to chain.Interface
IsCurrent allows us to determine if the chain backend considers itself
"current" with the chain.
2019-06-13 18:08:59 -07:00
Wilmer Paulino
1ee2a239de
wallet: make wallet initial sync synchronous
This ensures the wallet can properly do an initial sync, a recovery, or
detect if it's on a stale branch before attempting to process new blocks
at tip.

Since the rescan will be triggered synchronously as well, we'll need to
catch the wallet's quit chan when handling rescan batches in order to
allow for clean shutdowns.
2019-06-13 18:08:58 -07:00
Wilmer Paulino
bf9cc20045
wallet: check RPC error code for rejected confirmed transactions
This unifies the logic of receiving an error when broadcasting a
confirmed transaction through btcd's/bitcoind's RPC interface. The btcd
dependency update is required in order for it to match bitcoind's
behavior. For older nodes that have yet to update, the confirmed
transaction will still be caught by the "transaction already exists"
case. This is not needed for bitcoind however, because its been sending
the same RPC error code for several major releases now.
2019-05-22 17:06:37 -07:00
Olaoluwa Osuntokun
acf3b04b02
Merge pull request #604 from wpaulino/scan-chain-bug-fix
wallet: ensure bestHeight is updated before isCurrent check
2019-03-12 20:26:08 -07:00
Olaoluwa Osuntokun
ecfdd888dd
Merge pull request #602 from wpaulino/wallet-sync-dev-env
wallet/wallet: accept 0 bestHeight in developer environments
2019-03-12 20:17:22 -07:00
Wilmer Paulino
06bf42c746
wallet: use last synced block as birthday in development environments
In this commit, we address an issue with chains that are not current,
like in the often case of regtest and simnet chains. Syncing the wallet
would fail due to the chain not being current and not finding a suitable
birthday block. We fix this by just using the last synced block as the
birthday block to ensure we can properly sync to the chain.
2019-03-12 18:33:45 -07:00
Wilmer Paulino
ea7c6c3ed9
wallet: accept 0 bestHeight in developer environments
In this commit, we fix a regression in the wallet when attempting to
sync new developer test chains such as regtest and simnet. The wallet
would block syncing until a block was mined, but in order to mine a
block, an address must be generated by the wallet first. This address
generation would block as the syncing logic was already holding the
database's mutex.
2019-03-12 18:33:31 -07:00
Olaoluwa Osuntokun
25804bf90f
Merge pull request #597 from wpaulino/tx-broadcast-err-handling
wallet/wallet: remove invalid transactions when broadcast fails
2019-03-08 21:10:47 -08:00
Wilmer Paulino
5f1ab915be
wallet: ensure bestHeight is updated before isCurrent check
In this commit, we fix an issue with the wallet's initial sync logic
where we'd miss processing all of the blocks in the chain. This can
happen if the backend is considered current while we're still catching
up. To address this, we make sure we update our best height to process
those missed blocks.

Co-authored-by: Roei Erez <roeierez@gmail.com>
2019-03-08 18:31:23 -08:00
Olaoluwa Osuntokun
8c64a08971
Merge pull request #601 from Roasbeef/recovery-bug-fix-node
wallet: only log block batch if non-empty
2019-03-01 13:43:46 -03:00
Olaoluwa Osuntokun
b9da1fbd8d
wallet: only log block batch if non-empty
This fixes an issue reputed by a user that would cause btcwallet to
panic if the full node was stopped while btcwallet was still restoring
the wallet.
2019-02-27 18:38:17 -03:00
Johan T. Halseth
650f859fdb
wallet: add dryrun arg to tx create, rolling back db if set 2019-02-27 13:08:18 -03:00
Wilmer Paulino
c7ab11f310
wallet/wallet: reuse publishTransaction within resendUnminedTxs
By doing this, we defer all error string-matching to happen within
publishTransaction, which allows us to simplify some of the existing
logic and maintain consistency.
2019-02-20 14:50:27 -08:00
Wilmer Paulino
7a2b5cef76
wallet/wallet: remove invalid transactions when broadcast fails
In this commit, we rework how publishTransaction works in order to
correctly handle removing invalid transactions from the wallet's
unconfirmed transaction store. This is crucial as otherwise, invalid
transactions can remain within the wallet and be used for further
transactions, causing a chain of inaccurate transactions.

publishTransaction will now only return an error if the transaction
fails to be broadcast and it has not been previously seen in the
mempool/chain. This is intended in order to provide an easier API to
callers. Any other errors when broadcasting the transaction will cause
it to be removed from the wallet's unconfirmed transaction store to
ensure it maintains an accurate view of the chain.
2019-02-20 14:50:27 -08:00
Wilmer Paulino
7e00d1843e
wallet/wallet: refactor sending transaction to backend into own method
We do this in order to be able to reuse the new publishTransaction
method within other parts of the wallet in order to consolidate all
error string-matching within one place.
2019-02-20 13:13:44 -08:00
Johan T. Halseth
eef81a4124
wallet/wallet: assume not current if best height is 0
This is done to avoid the birthday rescan to fail if the chain backend
reports a bestheight of 0.

Earlier it could happen that we attempted to sync to the birthday, but
since only the genesis block was available, which would be rejected as
birthday block because of the timestamp, it would fail to find a block
and the sync would fail.
2019-02-06 11:26:24 +01:00
Wilmer Paulino
c853ceaa60
wallet/wallet: consolidate rollback logic
In this commit, we consolidate the existing rollback logic to carry out
its duties under one database transaction.

Co-authored-by: Roei Erez <roeierez@gmail.com>
2019-01-14 22:42:45 -08:00
Wilmer Paulino
db837f1ba3
wallet/wallet: use new syncToBirthday and recovery methods
In this commit, we refactor the wallet's syncing logic with
syncWithChain to use the newer, simpler methods: syncToBirthday and
recovery. Along the way, we also fix a bug within the wallet where it
was possible to sync past the birthday, but not sync to tip completely
and restart, which would lead to us starting a rescan from the latest
synced height, rather than from the birthday stamp.

This commit slightly changes the wallet's syncing behavior to the
following:

  1. Ensure the wallet is synced to its birthday.
  2. Perform a recovery if requested.
  3. Check for chain reorgs.
  4. Dispatch a rescan from the current synced height.

Co-authored-by: Roei Erez <roeierez@gmail.com>
2019-01-14 22:42:45 -08:00
Wilmer Paulino
29e1f0c4fb
wallet/wallet: add new recovery method
In this commit, we add a new recovery method to the wallet. This method
attempts to recover any unspent outputs which pay to any of the wallet's
addresses. Most of the logic found within it is heavily borrowed from
the existing syncWithChain method. This method is currently unused, but
it will end up replacing some of the existing sync logic in a later
commit.
2019-01-14 22:42:45 -08:00
Wilmer Paulino
089cc747db
wallet/wallet: add new syncToBirthday method
In this commit, we add a new syncToBirthday method to the wallet. This
method intends to sync the wallet's point of the view of the chain until
finding its birthday. Most of the logic found within it is heavily
borrowed from the existing syncWithChain method. This method is
currently unused, but it will end up replacing some of the existing sync
logic in a later commit.

Co-authored-by: Roei Erez <roeierez@gmail.com>
2019-01-14 22:42:45 -08:00
Wilmer Paulino
80450c9033
wallet/chainntfns: make birthdaySanityCheck return ErrBirthdayBlockNotSet 2018-11-20 13:00:45 -08:00
Wilmer Paulino
cc77e41198
wallet/chainntfns: set height for new birthday block candidate
In this commit, we address an issue that would cause users to be stuck
in an infinite loop by fetching the same candidate birthday block due to
its height not being updated if the sanity check was attempting to fix
an estimate in the future. We fix this by setting the new candidate
height so that new candidate blocks can be fetched and tested.
2018-11-19 20:48:14 -08:00
Wilmer Paulino
f92cc4db42
wallet/chainntfns_test: add birthdaySanityCheck tests 2018-11-19 20:48:14 -08:00
Wilmer Paulino
03a818efaa
wallet/chainntfns: remove wallet dependency from birthdaySanityCheck
In this commit, we remove the wallet dependency from the
birthdaySanityCheck function. Every interaction with the wallet is now
backed by two interfaces, birthdayStore and chainConn. These interfaces
will allow us to increase the test coverage of the birthdaySanityCheck
as now we'll only need to mock out only the necessary functionality.
2018-11-19 20:48:14 -08:00
Wilmer Paulino
bd95bfa6fb
wallet/wallet: prompt sanity check upon setting new birthday block within ImportPrivateKey
In this commit, we set the verified bit to false upon setting the new
birthday block to ensure its correctness as it was provided by the caller.
2018-11-15 18:17:56 -08:00
Wilmer Paulino
16ea72e95b
wallet/wallet: update to latest SetBirthdayBlock changes 2018-11-15 18:17:56 -08:00
Wilmer Paulino
f9df4908b3
wallet/chainntfns: prevent sanity check if correctness of birthday block has been verified
In this commit, we prevent any further sanity check attempts by the
wallet if its correctness has previously been verified. We do this to
ensure we don't unnecessarily attempt to find a new candidate.
2018-11-15 18:17:56 -08:00
Wilmer Paulino
4f5baed780
wallet/wallet: prevent always rescanning from birthday block
In this commit, we address an issue with the wallet where it would
always request a rescan from the birthday block. This is very crucial
for older wallets, as it'll potentially go through thousands of blocks.
To address this, we'll now only request a rescan from our birthday if
we're recovering our wallet from our seed, the birthday block was rolled
back, or if we're performing our initial sync. Otherwise, we'll request
a rescan from tip.
2018-11-15 16:34:42 -08:00
Wilmer Paulino
31011de227
wtxmgr+wallet: add wtxmgr sublogger 2018-11-14 18:09:10 -08:00
Wilmer Paulino
ae31984630
wallet: request notification of tx confirmation that pays to relevant addr
In this commit, we address a slight regression within the wallet
that was introduced in a previous commit. When attempting to send coins
on-chain, we would never ask the chain backend to notify us of the
transaction upon confirmation. This, along with the rebroadcast of
unconfirmed transactions logic, would result in the wallet becoming out
of sync with the chain.

Below is an example of how this could have happened:

  1. Send funds on-chain.
  2. Wallet doesn't ask to be notified of the confirmation.
  3. Since the wallet is not notified of the confirmation, the
  transaction remains in the unconfirmed bucket, even though it might
  have already confirmed on-chain.
  4. Restart and trigger the rebroadcast of unconfirmed transactions.
  5. The unconfirmed transaction is removed from the unconfirmed bucket
  due to it already existing on-chain, without it being moved to the
  confirmed bucket. Moving to the confirmed bucket would require the
  block at which it confirmed, which we don't have at this point.
2018-11-14 18:06:26 -08:00
Wilmer Paulino
4976c84f8f
wallet/wallet: prevent logging resend tx error if already accepted into
mempool
2018-11-14 18:06:25 -08:00
Wilmer Paulino
71ead8e3b4
wallet/wallet: commit birthday blockstamp upon initial sync/recovery 2018-11-14 17:24:00 -08:00
Wilmer Paulino
603e03de32
wallet/chainntnfs: add sanity check for birthday block before syncing
In this commit, we add a sanity check for the wallet's birthday block
before syncing as a result of the migration that populated it for
existing wallets. This is done as the second part to the migration to
ensure we do not miss any relevant events throughout rescans.

The sanity check performs two main checks: whether the birthday block
timestamp reflects a time before the birthday timestamp and whether the
delta between these two timestamps is a reasonable amount. The birthday
block is then found as the first candidate that satisfies both of these
conditions.
2018-11-14 17:24:00 -08:00
Wilmer Paulino
709fa17540
waddrmgr+wallet: add waddrmgr log to the wallet 2018-11-12 08:56:31 -08:00
Wilmer Paulino
33629dcfc2
wallet/wallet: set birthday block when importing private key 2018-11-12 08:56:31 -08:00
Olaoluwa Osuntokun
6d43b2e29b
Merge pull request #568 from joostjager/output-indices
wallet: return full tx from SendOutputs
2018-11-07 16:38:07 +11:00
Olaoluwa Osuntokun
ea4b832693
Merge pull request #567 from wpaulino/wtxmgr-migrations
wallet: add atomic migration logic for sub-buckets
2018-11-07 16:36:42 +11:00
Joost Jager
b718296188
wallet: return full tx from SendOutputs 2018-11-06 10:40:37 +01:00
Wilmer Paulino
df36d100e5
wallet/wallet: only set new birthday if before current within
ImportPrivateKey

In this commit, we ensure that when an external private key is imported
into the wallet, that we do not overwrite our existing birthday with the
one provided. If this were to happen and we forced a wallet rescan using
the birthday as our starting point, then we'd miss detecting relevant
on-chain events that occurred between them.
2018-11-05 18:23:15 -08:00
Wilmer Paulino
105faf52cb
wallet/log: use migration logger 2018-11-05 17:58:16 -08:00
Wilmer Paulino
69cb45e3e7
wallet/wallet: use new migration logic for waddrmgr and wtxmgr
In this commit, we modify the wallet to use the new migration logic
provided by the recently introduced migration package. Additionally,
we'll also perform all of our upgrades within the same database
transaction to guarantee fault-tolerance of the wallet.
2018-11-05 17:58:16 -08:00
Johan T. Halseth
d791cb7c30
wallet: always wait for Neutrino IsCurrent during initial sync 2018-10-16 18:18:46 -07:00
Olaoluwa Osuntokun
fd0bac8e18
wallet: add godoc to publishTransaction 2018-09-25 19:19:20 -07:00
Olaoluwa Osuntokun
f7085cf1bf
wallet: relax initial sync detection logic to speed up sync in case of mid restart
In this commit, we relax the initial sync detection logic a bit. We do
this as right now, if a user creates an address during the sync point,
if they restart, then we'll fall back to performing a rescan from that
height as we'll detect that we aren't performing the initial sync, so
won't pick up the birthday timestamp.

To fix this, we now declare that if we have no UTXO's, then we're still
performing the initial sync. This solves this issue as when the user
restarts, we'll continue to wait for the backend to sync, and pick up
the proper birthday height before we attempt to scan forward for the
rescan. However, the one tradeoff is that we'll now always start the
rescan from the birthday height until the wallet has gained it's first
UTXO. I don't think this is too bad, as after all, the point of a wallet
is to manage utxos.
2018-09-25 19:19:04 -07:00
Wilmer Paulino
db51e8b8de
wallet/wallet: use publishTransaction within SendOutputs 2018-09-20 19:04:30 -07:00
Wilmer Paulino
c125b59df4
wallet/wallet: refactor PublishTransaction to use unexported method
In this commit, we refactor the logic outside of PublishTransaction into
another unexported method. This will pave the road for unifying the
logic between SendOutputs and PublishTransaction.
2018-09-20 18:59:02 -07:00
Wilmer Paulino
06e70c0f08
wallet/wallet: use addRelevantTx when publishing transactions
In this commit, we simplify the logic when broadcasting transactions to
the greater network. Rather than special casing when running with a
Neutrino backend, we'll always add the transaction to the store as
relevant when attempting to broadcast it. This will properly insert it
into the store and update unconfirmed balances. In the event that the
transaction failed to broadcast, it can be removed from the store with
no side-effects, essentially acting as if the transaction was never
added to the store in the first place.
2018-09-20 18:58:34 -07:00
Olaoluwa Osuntokun
421298df22
Merge pull request #542 from wpaulino/bitcoind-client-birthday
chain+wallet: set bitcoind client birthday after wallet init
2018-09-12 20:41:30 -07:00
Olaoluwa Osuntokun
54a82426c8
Merge pull request #543 from Roasbeef/neutrino-ntfn
wallet: allow SendOutputs to notify new outgoing transactions for neu…
2018-09-12 20:21:40 -07:00
Olaoluwa Osuntokun
0cfe15c0c7
wallet: allow SendOutputs to notify new outgoing transactions for neutrino
In this commit, we modify the SendOutputs method to also notify new
outgoing transctions for neutriino. For the full node backends, they'll
get this notification when the transactino hits the mempool. However,
for neutrino it will only be notified once the transaction has been
confirmed. This commit ensures that we'll notify on send as well.
2018-09-12 19:25:06 -07:00
Wilmer Paulino
ed47296c88
wallet: set chain client birthday after the wallet has been initialized 2018-09-12 14:25:11 -07:00
Wilmer Paulino
aca9bebfab
wallet/chainntfns: avoid notifying txs if not found within the wallet
In this commit, we avoid notifying clients of transactions that we've
received chain.RelevantTx notifications for, but are not found within
the wallet. This can happen as now we'll prevent adding an unconfirmed
transaction to the wallet that already exists as confirmed. Due to this,
UniqueTxDetails will be unable to find the transaction and return nil,
casuing a panic for potential callers.
2018-09-06 19:42:13 -07:00
Olaoluwa Osuntokun
8ae4afc701
Merge pull request #536 from cfromknecht/birthday-block-rescan
wallet: start initial rescan from birthday block
2018-09-03 18:05:40 -07:00
Conner Fromknecht
0c5c8d7f9e
wallet/wallet: start initial rescan from birthday block 2018-08-31 16:49:11 -07:00
Conner Fromknecht
e508a127b6
wallet/wallet: notify addrs+props after db commit
This PR moves any address notifications outside of the
db transaction that creates them. This is known to have
resulted in deadlocks, since chainClient.NotifyReceived
could block the db transaction from committing.

Doing so also prevents the situation where we send
notifications about the new addresses, but the db txn
fails to commit and the addresses are in fact never
created.
2018-08-31 16:30:39 -07:00
Conner Fromknecht
dfa3a88529
wallet/rescan: add rescanWithTarget helper
This commit adds rescanWithTarget, in order to facilitate
rescans beginning a certain height. This is done as a
precursor to fixing a bug in the initial sync, that would
cause us to miss relevant txns if they are confirmed before
starting the initial rescan.
2018-08-30 19:40:06 -07:00
Olaoluwa Osuntokun
7d0d499cd0
wallet: remove txns from store if they're in the chain, or have a negative output 2018-08-22 20:20:54 -07:00
Olaoluwa Osuntokun
1fbdc8c44b
wallet: catch and return error from rollback failure 2018-08-14 18:17:47 -07:00
Wilmer Paulino
dec9978ca2 wallet: stop handling chain notifications once wallet has stopped
In this commit, we alter the behavior for handling chain notifications
within the wallet. The previous code would assume that the channel would
close, but due to now using a ConcurrentQueue to handle notifications,
this assumption no longer stands. Now, we'll stop handling notifications
either once the wallet has or stopped or once the notifications channel
has been closed.
2018-07-30 17:54:08 -07:00
Wilmer Paulino
bbb5a6c058 wallet: remove the need to set the birthday for bitcoind chain clients
Due to the previous commit allowing us to specify the birthday of the
wallet at the time of the BitcoindClient's creation, this is now
unnecessary.
2018-07-30 17:54:08 -07:00
Olaoluwa Osuntokun
8b2629a935
Merge pull request #514 from vapopov/zerobalance
wallet: fix lost balance of unmined transaction for neutrino
2018-07-23 17:58:56 -07:00
Olaoluwa Osuntokun
dbb7ae6668 wallet: remove extraneous err check 2018-07-16 19:47:17 -07:00
Olaoluwa Osuntokun
fb511876c5 wallet: update to use new recovery manager API 2018-07-16 19:47:17 -07:00
Olaoluwa Osuntokun
49e46f3232 wallet: update rescan to take addrs along with their outpoints 2018-07-16 19:47:17 -07:00
Olaoluwa Osuntokun
d6ee9fd0e4 wallet: update HD recovery logic to map outpoints to addresses 2018-07-16 19:47:17 -07:00
Vadym Popov
40ba75dd9d
wallet: fix lost balance of unmined transaction for neutrino 2018-07-13 12:41:02 +03:00
Conner Fromknecht
78ea2cdc9c wallet/chainntfns: ensure safe shutdown during sync 2018-07-12 18:00:07 -07:00
Olaoluwa Osuntokun
b0b64d3bbd multi: update due to latest API changes 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
7770cac383 multi: switch import paths back to upstream 2018-05-23 19:38:56 -07:00
Conner Fromknecht
4c6b9053b8 wallet/wallet: adds recovery for default scopes 2018-05-23 19:38:56 -07:00
Conner Fromknecht
71ce1d5474 wallet/recovery_test: adds test for 1-in-1-out spend 2018-05-23 19:38:56 -07:00
Conner Fromknecht
e4124d8e8b wallet/recovery: adds wallet recovery manager 2018-05-23 19:38:56 -07:00
Conner Fromknecht
d26cf062fe wallet/loader: feed birthday+recovery window to wallet 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
620be5821f wallet: ensure PublishTransaction handles nil error case 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
fbfca5f4ae wallet: in PublishTransaction if tx is rejected, remove it from the txstore 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
921dae5d5e wallet: catch remaining bitcoind errors, and generic RPC errors resendUnminedTxs
In this commit, ensure that upon restart, if any of the full-node based
backends we support reject the transaction, then we'll properly remove
the now invalid transaction from the tx store. Before this commit, we
could miss a few errors from bitcoind. To remedy this, we explicitly
catch those errors, but then also attempt to precisely catch the set of
generic json RPC errors that can be returned.
2018-05-23 19:38:56 -07:00
Wilmer Paulino
8b2aebe89e wallet: add call to modify both public and private passphrases atomically 2018-05-23 19:38:56 -07:00
Kenneth Perry (thothonegan)
0547e5a313 When determining whether to rollback, ask the chain using the chainhash instead of the possibly invalid local hash 2018-05-23 19:38:56 -07:00
Alex
6d16463627 wallet: demote chainntfns error about out-of-sync blocks to debug 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
5eaecee2c9 wallet: ensure timestamp is always set when calling waddrmgr.SetSyncedTo 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
0dcd36bf59 wallet: only remove conflicting unmined transactions
In this commit, we fix a bug introduced in an earlier commit. Before
this commit, we would *always* remove an unmined transaction if it
failed to be accepted by the network upon restart. Instead, we should
only remove transaction that are actually due to us trying to spend an
output that’s already spent, or an orphan transaction.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
3b66af9154 wallet: properly set timestamp during block connect/disconnect 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
b75bf1426e wallet: update wallet to be aware of new KeyScopes 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
ab4ccacbb9 wallet: extend PublishTransaction to be a general reliable broadcaster
In this commit, we extend the PublishTransction method to be a more
general semi reliable transaction broadcast mechanism. We do this by
removing the special casing for neutrino. With this change, we’ll
_always_ write any transactions to be broadcast to disk. A side effect
of this, is that if the transaction doesn’t *directly* involve any
outputs we control, then it’ll linger around until a restart, when we
try to rebroadcast, and observe that it has bene rejected.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
54b31c1a35 wallet: remove conflicting double spend transactions on start up 2018-05-23 19:38:56 -07:00
Johan T. Halseth
8bcd56fc27 wallet/size test: add vsize test for non-segwit tx 2018-05-23 19:38:56 -07:00
Johan T. Halseth
d626036401 wallet/author: use vsize when estimating fees
This commit makes use of the recently added EstimateVirtualSize
method to estimated the size of a transaction when calculating
fees. This makes fee estimation more accurate when we are spending
segwit outputs, as before we wouldn't account for the witness
descount, resulting in overshooting fee estimates.
2018-05-23 19:38:56 -07:00
Johan T. Halseth
2caa8878b4 wallet/size test: add TestEstimateVirtualSize
This commit adds a test for EstimateVirtualSize. Currently three
test transactions are added:
    - 1 P2WPKH inputs + 2 P2PKH outputs
    - 1 P2SH-P2WPKH inputs + 2 P2PKH outputs
    - 1 P2WPKH inputs + 1 P2PKH output + 1 P2WPKH change output
2018-05-23 19:38:56 -07:00
Johan T. Halseth
f782f9dc68 wallet/size: add EstimateVirtualSize
This commit adds a new method EstimateVirtualSize that calculates
the worst case estimate vsize for a transaction with a given set
of inputs and outputs. This method is aware of P2PKH, P2WPKH and
P2SH-P2WPKH inputs, and caulculates the transaction vsize with
the witness data included.
2018-05-23 19:38:56 -07:00
Alex
73dbcf3943 multi: add bitcoind back-end (only for API use for now) 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
b963eb3ba4 wallet: pass in fee-per-kb into CreateSimpleTx 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
7b9d880fee wallet: remove internal relayFee in favor of passing in fee rate when sending
In this commit, we do away with the internal relayFee all together.
Instead, we’ll pass in the fee rate when we’re crafting any
transactions. This allows the caller to manually dictate their desired
fee rate.
2018-05-23 19:38:56 -07:00
Johan T. Halseth
255f23679f wallet: close db on Open() failure.
This commit makes sure the wallet db is closed if the call to
open the wallet fails, as subsequent calls to OpenExistingWallet
would fail to open the already open database.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
ee841b07cf wallet: for ineutrino back-end insert all broadcast transactions into txstore 2018-05-23 19:38:56 -07:00
Alex
5c69110f47 wallet: ensure rescan is launched with all addresses
During the time of initial block hash catch-up, it is possible to
request an address be generated. This commit updates the active
addresses by calling `w.activeData` after the catch-up is complete.
2018-05-23 19:38:56 -07:00
Alex
5113a64617 wallet: fix nil dereference in rescanprogress ntfn handler 2018-05-23 19:38:56 -07:00
Alex
8e2c741f88 wallet: move DB access from rescan ntfns into correct goroutine 2018-05-23 19:38:56 -07:00
Alex
81a9bb67c1 wallet: set wallet birthday properly in rescans and manager 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
a0abd3632c multi: update to new upstream btcsuite/btcd API's 2018-05-23 19:38:56 -07:00
Alex
58f303b957 wallet: make initial sync play nicer with neutrino+bdb backends 2018-05-23 19:38:56 -07:00
Alex
fbde9a97c8 wallet: eliminate some unneeded database transaction opens/commits 2018-05-23 19:38:56 -07:00
Alex
4fb78bca18 wallet: add logging when catching up block hashes on startup/rescan 2018-05-23 19:38:56 -07:00
Alex
3eb28d2d37 wallet: batch initial block hash catch-up process every 10K blocks 2018-05-23 19:38:56 -07:00
Alex Akselrod
c85893de1a chain+waddrmgr+wallet: store all hashes for better reorg handling (#5) 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
3567ae87f4 wallet: avoid double-writing block stamps in onFilteredBlockConnected 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
3dc7a8529c multi: switch all imports to roasbeef's forks 2018-05-23 19:38:56 -07:00
Alex
3f12fa3c6c chain+wallet: rename SPVChain to NeutrinoClient 2018-05-23 19:38:56 -07:00
Alex
b5873a5b2c Finish integration of Neutrino; still untested. 2018-05-23 19:38:56 -07:00
Alex
9e5250e6d7 More neutrino integration into btcwallet. 2018-05-23 19:38:56 -07:00
Alex
32adc3c43f Make rescan a struct, add spending tx to GetUtxo, start integration. 2018-05-23 19:38:56 -07:00
Alex
e273e178dd Repoint repo to btcsuite on btcwalletln, and a few rebase fixes. 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
d27d1211c5 wallet: fix db deadlock when creating new tx 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
ef179b1e18 wallet: fix final compile error 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
4ce4a70968 wallet+votingpool: direct import paths to roasbeef 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
14e6fe36d0 wallet: remove sync.go, references decred 2018-05-23 19:38:56 -07:00
Josh Rickmar
bf86ccf5b4 calculate each account's balance individually 2018-05-23 19:38:56 -07:00
Josh Rickmar
4656a00705 Improve wallet atomicity.
This changes the database access APIs and each of the "manager"
packages (waddrmgr/wstakemgr) so that transactions are opened (only)
by the wallet package and the namespace buckets that each manager
expects to operate on are passed in as parameters.

This helps improve the atomicity situation as it means that many
calls to these APIs can be grouped together into a single
database transaction.

This change does not attempt to completely fix the "half-processed"
block problem.  Mined transactions are still added to the wallet
database under their own database transaction as this is how they are
notified by the consensus JSON-RPC server (as loose transactions,
without the rest of the block that contains them). It will make
updating to a fixed notification model significantly easier, as the
same "manager" APIs can still be used, but grouped into a single
atomic transaction.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
02b0f7d51c wallet: update to new btcrpcclient API 2018-05-23 19:38:56 -07:00
Andrey Samokhvalov
5785ca17cc txrules: add threshold function in order to reuse it in lnd 2018-05-23 19:38:56 -07:00
Josh Rickmar
504b045332 Fix credit slice indexing for transaction notifications.
Previously, this would always check a transaction output index against
the 0th credit's index.
2018-05-23 19:38:56 -07:00
Josh Rickmar
61e96a69e4 Cleanup TransactionDetails RPC messages.
Remove the addresses field from TransactionDetails.Output.  It is
assumed that the caller is able to deserialize the transaction and
encode the output scripts to addresses, so this is unnecessary server
overhead and conflicts with the current API philosophy of not
duplicating data already included in another field.

Since there is no additional data included for outputs not controlled
by the wallet, remove the `mine` specifier from the Output message and
replace it with an output index.  Only include messages for controlled
outputs, rather than creating messages for both controlled and
uncontrolled outputs.  Rename the repeated field from `outputs` to
`credits` to be consistent with the `debits` field.

Bump major API version as this is a breaking change.

Closes #408.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
f13a081e31 wallet: add public method to expose the database 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
77b756c651 multi: point all imports towards roasbeef's forks 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
6dfc793ad3 wallet: use intermediate hash cache during validateMsgTx 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
d88739df8f wallet: add address type param to NewChangeAddress+NewAddress 2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
beb3231a03 wallet/txauthor: add ability to sign for nested+normal p2wkh
This commit enabled the wallet to properly spend nested and normal
p2wkh outputs under its control.

For regular p2wkh outputs, spending simply involves presenting the
original pub key, and signature as the witness data.

For nested p2wkh outputs, in addition to the above, the version zero
witness p2wkh witness program is placed in the sigScript in order to
allow clients who are aware of BIP 16 to validate the witness spend.
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
3d464a0bbc wallet: include input values during utxo selection and tx validation
When spending a segwit output, the wallet also needs the input value of
the previous output script. Therefore when selecting outputs the input
value is now returned. Additionally when validating newly signed
outputs the input value as also passed into `txscript.Engine`
2018-05-23 19:38:56 -07:00
Olaoluwa Osuntokun
a2ff118b25 wallet: fix compile errors under WIP segwit branch 2018-05-23 19:38:56 -07:00
Josh Rickmar
3e598f0f7b Fix reported spendable balances from coinbase outputs. (#467)
Previously, this would not increment the spendable balance for matured
coinbase outputs and would only increment the immature balance if the
output was still immature.
2017-01-10 13:02:03 -05:00
Josh Rickmar
1cb3b8b29a Update project dependencies. (#456)
This updates both btcsuite and external dependencies to their latest
versions.  In particular, gRPC was updated to version 1.0.3 and bolt
to 1.3.0.

The walletrpc package needed to be regenerated for the gRPC update.

While here, update the Travis-CI script so this can be tested there.
2016-10-27 12:27:58 -04:00
DanielKrawisz
5e39e68778 Fix typo. (#453) 2016-08-15 11:27:32 -04:00
Dave Collins
d76627e6d5 Update for recent chaincfg API changes. (#451)
Since the coinbase maturity is now allowed to be defined per chain and
the old blockchain.CoinbaseMaturity constant has been removed, this
updates the code accordingly.

Also, update glide.lock to use the required version of btcd.
2016-08-12 19:27:51 -05:00
Dave Collins
e92f94dcd1 Update for recent chainhash-related API changes. (#450)
This updates all code to make use of the new chainhash package since the
old wire.ShaHash type and related functions have been removed in favor
of the abstracted package.

Also, while here, rename all variables that included sha in their name
to include hash instead.

Finally, update glide.lock to use the required version of btcd, btcutil,
and btcrpcclient.
2016-08-08 14:49:09 -05:00
Josh Rickmar
146e92edc1 Fix credit slice indexing for transaction notifications.
Previously, this would always check a transaction output index against
the 0th credit's index.
2016-05-04 15:13:28 -04:00
Josh Rickmar
fb06a6bd04 Cleanup TransactionDetails RPC messages.
Remove the addresses field from TransactionDetails.Output.  It is
assumed that the caller is able to deserialize the transaction and
encode the output scripts to addresses, so this is unnecessary server
overhead and conflicts with the current API philosophy of not
duplicating data already included in another field.

Since there is no additional data included for outputs not controlled
by the wallet, remove the `mine` specifier from the Output message and
replace it with an output index.  Only include messages for controlled
outputs, rather than creating messages for both controlled and
uncontrolled outputs.  Rename the repeated field from `outputs` to
`credits` to be consistent with the `debits` field.

Bump major API version as this is a breaking change.

Closes #408.
2016-04-28 11:53:03 -04:00
Josh Rickmar
7b2e1ac282 Do not include zero value change outputs.
Due to the way dust is calculated, if the transaction relay fee is
zero, then a zero output amount is not considered dust.  As the
transaction authoring code used this dust check to determine whether a
change output can be included or not, it could create unnecessary
change outputs which return no value back to the wallet.  Prevent this
by including an explicit check for zero values.
2016-04-20 10:46:12 -04:00
George Melika
eefc610904 Check for missing txs before dereferencing details 2016-04-06 16:24:46 -04:00
John C. Vernaleo
c2ed8ffc2b Add goclean.sh script from btcd.
This commit corrects various things found by the static checkers
(comments, unkeyed fields, return after some if/else).

Add generated files and legacy files to the whitelist to be ignored.

Catch .travis.yml up with btcd so goclean can be run.
2016-03-31 11:43:54 -04:00
Josh Rickmar
fcccae3d1a Manage wallet db namespaces from wallet package.
This changes the wallet.Open function signature to remove the database
namespace parameters.  This is done so that the wallet package itself
is responsible for the location and opening of these namespaces from
the database, rather than requiring the caller to open these ahead of
time.

A new wallet.Create function has also been added.  This function
initializes a new wallet in an empty database, using the same
namespaces as wallet.Open will eventually use.  This relieves the
caller from needing to manage wallet database namespaces explicitly.

Fixes #397.
2016-03-21 11:25:28 -04:00
Josh Rickmar
71649abefe improve language 2016-03-11 17:01:04 -05:00
Josh Rickmar
6e6cb307fa Log wallet locks and unlocks.
Fixes #392.
2016-03-11 16:22:43 -05:00
Josh Rickmar
6cf22b7944 Remove legacy JSON-RPC notifications.
These notifications were added to support real time updates for
btcgui.  As the btcgui project is no longer being developed, there are
no more consumers of this API, and it makes sense to remove them given
their various issues (the largest being that notifiations are sent
unsubscribed to clients that may never be interrested in them).

A new notification server has already been added to the wallet package
to handle notifications in a RPC-server agnostic way.  This server is
the means by which the wallet notifies changes for gRPC clients.  If
per-client registered notifications are to be re-added for the
JSON-RPC server, they should be integrated with the new notification
server rather than using this legacy code.
2016-03-11 14:14:33 -05:00
Josh Rickmar
2b79aad79c Stop wallet and close wallet DB on interrupt.
This corrects and simplifies the shutdown logic for interrupts, the
walletrpc.WalletLoaderService/CloseWallet RPC, and the legacy stop RPC
by both stopping all wallet processes and closing the wallet database.
It appears that this behavior broke as part of the wallet package
refactor, causing occasional nil pointer panics and memory faults when
closing the wallet database with active transactions.

Fixes #282.

Fixes #283.
2016-03-10 23:10:18 -05:00
Josh Rickmar
24fc8bb6c5 Read synchronized relay fee once when checking outputs. 2016-03-09 15:03:42 -05:00
Josh Rickmar
d09c2a84c1 Protect the relay fee field with a mutex.
This prevents races when setting a new relay fee through the legacy
RPC server (settxfee).

Fixes #379.
2016-03-09 14:54:09 -05:00
Josh Rickmar
f084802fec Refactor wallet transaction creation code.
This began as a change to improve the fee calculation code and evolved
into a much larger refactor which improves the readability and
modularity of all of the transaction creation code.

Transaction fee calculations have been switched from full increments
of the relay fee to a proportion based on the transaction size.  This
means that for a relay fee of 1e3 satoshis/kB, a 500 byte transaction
is only required to pay a 5e2 satoshi fee and a 1500 byte transaction
only need pay a 1.5e3 fee.  The previous code would end up estimating
these fees to be 1e3 and 2e3 respectively.

Because the previous code would add more fee than needed in almost
every case, the transaction size estimations were optimistic
(best/smallest case) and signing was done in a loop where the fee was
incremented by the relay fee again each time the actual size of the
signed transaction rendered the fee too low.  This has switched to
using worst case transaction size estimates rather than best case, and
signing is only performed once.

Transaction input signature creation has switched from using
txscript.SignatureScript to txscript.SignTxOutput.  The new API is
able to redeem outputs other than just P2PKH, so the previous
restrictions about P2SH outputs being unspendable (except through the
signrawtransaction RPC) no longer hold.

Several new public packages have been added:

wallet/txauthor - transaction authoring and signing
wallet/txfees - fee estimations and change output inclusion
wallet/txrules - simple consensus and mempool policy rule checks

Along with some internal packages:

wallet/internal/txsizes - transaction size estimation
internal/helpers - context free convenience functions

The txsizes package is internal as the estimations it provides are
specific for the algorithms used by these new packages.
2016-03-08 17:42:27 -05:00
Josh Rickmar
5140086f6e Use LICENSE file and short license headers. 2016-02-28 22:22:34 -05:00