Commit Graph

88 Commits

Author SHA1 Message Date
dependabot[bot]
6badf09394
Bump bdk from 0.6.0 to 0.7.0
Bumps [bdk](https://github.com/bitcoindevkit/bdk) from 0.6.0 to 0.7.0.
- [Release notes](https://github.com/bitcoindevkit/bdk/releases)
- [Changelog](https://github.com/bitcoindevkit/bdk/blob/v0.7.0/CHANGELOG.md)
- [Commits](https://github.com/bitcoindevkit/bdk/compare/v0.6.0...v0.7.0)

Signed-off-by: dependabot[bot] <support@github.com>
2021-05-28 18:42:25 +10:00
Thomas Eizinger
e79ac4563b
Delete unused code 2021-05-25 15:32:03 +10:00
Daniel Karzel
6694e4f4e0
Ensure that output of lock script is at tx-output index 0
We subscribe to transactions upon broadcast, where we use output index `0` for the subscription.
In order to ensure that this subscription is guaranteed to be for the locking script (and not a change output) we now ensure that the locking script output is always at index `0` of the outputs of the transaction.

We chose this solution because otherwise we would have to add more information to broadcasting a transaction.
This solution is less intrusive, because the order of transaction outputs should not have any side effects and ensuring index `0` makes the whole behaviour more deterministic.
2021-05-20 13:05:52 +10:00
Daniel Karzel
bae38a712f
Sync on interval instead of ping
Since we don't rely on long running subscriptions anymore we can remove the ping that was used to ensure a connection refresh.
2021-05-20 13:05:51 +10:00
Daniel Karzel
efb51820b1
Poll block headers for latest block on each iteration
The Electrum block-header subscription did not provide us with block headers, because upon the connection being closed by a node the subscription would end.
Re-newing the the subscription upon re-connect is not easily achievable, that's why we opted for a polling mode for now, where we start a block header subscription on every update iteration, that is only used once (when the subscription is made).
2021-05-20 13:05:51 +10:00
Philipp Hoenisch
7fc5a47619
Add checks to max_giveable amount.
We need to check two things:
- balance to be higher than dust amount (546).
- balance to be higher than min-relay fee.

Additionally, the tx_builder might fail if not enough funds are in the wallet to pay for the overall transaction fees.
2021-05-12 16:31:32 +10:00
Philipp Hoenisch
26e0383e17
Apply suggestions from code review
Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
2021-05-11 16:06:44 +10:00
Philipp Hoenisch
316f95c65b
Always log whole causality chain for errors. 2021-05-11 15:27:53 +10:00
Philipp Hoenisch
c011e95062
Improve logging by adding details as fields instead of into the msg. 2021-05-11 15:27:53 +10:00
Philipp Hoenisch
b5073e3052
Use rust_decimal in estimate_fee function. 2021-05-10 10:53:43 +10:00
Philipp Hoenisch
e93eeb81a2
Added some proptests. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
040cce1eb6
Format all the files 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
aebdb063f1
Remove TODO 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
9fb1a43f29
Rename variable. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
e69a2c9111
Apply suggestions from code review
Co-authored-by: Daniel Karzel <daniel.karzel@coblox.tech>
2021-05-07 10:24:41 +10:00
Philipp Hoenisch
dc8dd5af28
Add relative and absolute max transaction fee. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
dc6ab0fa52
Ensure that constant weights do not go out of sync with code. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
14c5a4f025
Add upper bound for bitcoin fees of 100,000 satoshi.
Fees are hard to compute and it is too easy to get wrong and lose a lot of money. Hence, a hardcoded maximum of 100,000 satoshi for a single transaction is in place.
2021-05-07 10:24:41 +10:00
Philipp Hoenisch
ee90c228b4
Dynamically calculate fees using electrum's estimate_fee.
Electrum has an estimate-fee feature which takes as input the block you want a tx to be included.
The result is a recommendation of BTC/vbyte.
Using this recommendation and the knowledge about the size of our transactions we compute an appropriate fee.
The size of the transactions were taken from real transactions as published on bitcoin testnet.
Note: in reality these sizes might fluctuate a bit but not for much.
2021-05-07 10:24:41 +10:00
Philipp Hoenisch
38540b4de5
Dynamically chose fee for TxCancel.
Bob chooses the fee for TxCancel because he is the one that cares.
2021-05-07 10:24:41 +10:00
Philipp Hoenisch
1012e39527
Dynamically chose fee for TxRefund and TxPunish.
Alice chooses the fee for TxPunish because she is the one that cares.
Bob chooses the fee for TxRefund because he is the one that cares.

Note must be taken here because if the fee is too low (e.g. < min tx fee) then she might not be able to publish TxRedeem at all.
2021-05-07 10:24:41 +10:00
Philipp Hoenisch
d5c1b6693e
Dynamically chose fee for TxRedeem.
Alice chooses the fee for TxRedeem because she is the one that cares. Note must be taken here because if the fee is too low (e.g. < min tx fee) then she might not be able to publish TxRedeem at all.
2021-05-07 10:24:41 +10:00
Thomas Eizinger
39eea61538
Upgrade to bdk 0.6 2021-04-19 10:14:14 +10:00
Daniel Karzel
0341e7c9fc
Point BDK to commit that fixes overflow error
Edge cases of UTXOs where value < fee cause the BDK's `coin_select` calculation to panic.
This issue was fixed upstream thus we point the BDK dependency against the commit of the merged fix.
2021-04-06 14:50:27 +10:00
Thomas Eizinger
52b9a78de2
Alice to validate Bob's PSBT for correctness
In order for the re-construction of TxLock to be meaningful, we limit
`Message2` to the PSBT instead of the full struct. This is a breaking
change in the network layer.

The PSBT is valid if:

- It has at most two outputs (we allow a change output)
- One of the outputs pays the agreed upon amount to a shared output script

Resolves #260.
2021-03-30 13:02:56 +11:00
Thomas Eizinger
8576894c10
Split bitcoin::Wallet functions into various impl blocks
This allows us to construct instances of bitcoin::Wallet for test
purposes that use a different blockchain and database implementation.

We also parameterize the electrum-client to make it possible to
construct a bitcoin::Wallet for tests that doesn't have one. This
is necessary because the client validates the connection as it is
constructed and we don't want to provide an Electrum backend for
unit tests.
2021-03-30 13:02:55 +11:00
Thomas Eizinger
01739eddb1
Introduce a more flexible transaction subscription system
Instead of watching for status changes directly on bitcoin::Wallet,
we return a Subscription object back to the caller. This subscription
object can be re-used multiple times.

Among other things, this now allows callers of `broadcast` to decide
on what to wait for given the returned Subscription object.

The new API is also more concise which allows us to remove some of
the functions on the actor states in favor of simple inline calls.

Co-authored-by: rishflab <rishflab@hotmail.com>
2021-03-30 10:29:19 +11:00
Thomas Eizinger
96b2a76971
Take advantage of upgraded electrum-client dependency
The new version implements std::error::Error and fixes a bug that
allows us to use the default config again.
2021-03-23 14:57:27 +11:00
Thomas Eizinger
c92f2dbc77
Move more domain knowledge onto the TxCancel type 2021-03-18 15:44:37 +11:00
Thomas Eizinger
e77f1729b4
Move extract_monero_private_key onto TxRefund
This functionality is domain-specific to the refund transaction.
Move it onto there.
2021-03-18 15:44:36 +11:00
Thomas Eizinger
ce78075932
Make Monero and Bitcoin wallet use a generalized sync interval
We define the sync interval as 1/10th of the blocktime. For the
special case of our tests, we however check at max once per second.
The tests have a super fast blocktime. As such we shouldn't hammer
the nodes with a request every 100ms.
2021-03-17 16:31:17 +11:00
Thomas Eizinger
09c41f89c4
Rename ExecutionParams to EnvironmentConfig 2021-03-17 16:31:16 +11:00
Thomas Eizinger
bc43ed6ebd
Pass execution params directly into wallet for initialization
This reduces the amount of parameters that we need to pass in.
2021-03-17 16:30:58 +11:00
Thomas Eizinger
273cf15631
Introduce Watchable abstraction for Bitcoin wallet
We have a repeated pattern where we construct one of our
Tx{Cancel,Redeem,Punish,Refund,Lock} transactions and wait until
the status of this transaction changes. We can make this more
ergonomic by creating and implementing a `Watchable` trait that
gives access to the TxId and relevant script for this transaction.
This allows us to remove a parameter from the `watch_until_status`
function.

Additionally, there is a 2nd pattern: "Completing" one of these
transaction and waiting until they are confirmed with the configured
number of blocks for finality. We can make this more ergonomic by
returning a future from `broadcast` that callers can await in case
they want to wait for the broadcasted transaction to reach finality.
2021-03-16 19:24:32 +11:00
Thomas Eizinger
a0830f099f
Pass relevant execution params into wallet instead of via functions
The execution params don't change throughout the lifetime of the
program. They can be set in the wallet at the very beginning.
This simplifies the interface of the wallet functions.
2021-03-16 19:24:31 +11:00
rishflab
e5c0158597
Greatly reduce load onto the Electrum backend
We achieve our optimizations in three ways:

1. Batching calls instead of making them individually.

To get access to the batch calls, we replace all our
calls to the HTTP interface with RPC calls.

2. Never directly make network calls based on function
calls on the wallet.

Instead, inquiring about the status of a script always
just returns information based on local data. With every
call, we check when we last refreshed the local data and
do so if the data is considered to be too old. This
interval is configurable.

3. Use electrum's notification feature to get updated
with the latest blockheight.

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Rishab Sharma <rishflab@hotmail.com>
2021-03-16 19:24:31 +11:00
Thomas Eizinger
6beb732e35
Eliminate build_bitcoin_punish_transaction
We reduce indirection by constructing TxPunish directly based off
`State3` and make the type itself more powerful by moving the logic
of completing it with a signature onto it.
2021-03-16 19:21:15 +11:00
Thomas Eizinger
dd6c66a594
Move completing of Bitcoin redeem tx onto RedeemTx
This allows us to have access to RedeemTx from within the scope
of the state transition which we are going to need for more
efficient watching of what happens to this TX on the blockchain.
2021-03-16 17:11:31 +11:00
Daniel Karzel
0091b6cdaf Remove CLI config file in favour of parameters
The CLI has sensible default values for all parameters,
thus a config file is not really an advantage but just
keeps getting in our way, so re remove it.
2021-03-15 15:41:46 +11:00
Thomas Eizinger
08923a14f3
Simplify GET request for block tip height 2021-03-05 17:06:17 +11:00
Thomas Eizinger
e9d7d9299c
Simplify the GET request to the tx status URL 2021-03-05 16:56:48 +11:00
Thomas Eizinger
418ad7089d
Make tests more readable by following arrange-act-assert 2021-03-05 16:56:48 +11:00
Thomas Eizinger
4883e23dd8
Tell the user for how many confirmations we are waiting
Without this, the user has no idea for how long the program is
waiting.
2021-03-05 16:56:47 +11:00
Thomas Eizinger
1aa6d177bf
Improve error messages when determining BTC amount to be swapped 2021-03-05 15:49:16 +11:00
Thomas Eizinger
4138039ea0
Make sure all error messages start with an uppercase letter
These might potentially be shown to a user, let's make them all
consistent.
2021-03-05 15:49:15 +11:00
Thomas Eizinger
816e8b9b96
Add more context to fallible functions inside bitcoin::Wallet 2021-03-05 15:49:15 +11:00
Thomas Eizinger
37f97ac471
Shorten function name
The variable will always be at least called `wallet`, hence we can
omit the `_wallet` postfix from the function name.
2021-03-05 15:49:14 +11:00
Thomas Eizinger
4f66269887
Move error message on sync _into_ the function
The bitcoin::Wallet::sync_wallet function doesn't do anything else
other than delegating. As such, we have just as much information
about what went wrong inside this function as we have outside.

By moving the .context call into the function, we can avoid repeating
us on every call-site.
2021-03-05 15:49:14 +11:00
Thomas Eizinger
6d9b21cb47
Change imports_granularity to module
This reduces the overall amount of LoC that imports take up in our
codebase by almost 100.
It also makes merge-conflicts less likely because there is less
grouping together of imports that may lead to layout changes which
in turn can cause merge conflicts.
2021-03-04 14:48:13 +11:00
rishflab
a41b255dab Upgrade bitcoin wallet to use BIP84 derivation scheme
Explicitly specify the change descriptor because the behaviour when it
is not specified is unclear.
2021-03-03 12:12:10 +11:00