Commit Graph

349 Commits

Author SHA1 Message Date
Daniel Karzel
18faa786d6 Fail if something goes wrong when checking tx lock status
Probably a failure when interacting with the electrum node to get script
 status updates
2021-08-31 18:26:00 +10:00
Thomas Eizinger
8f50eb2f34
Utilize tracing's fields more 2021-07-07 16:02:02 +10:00
Thomas Eizinger
9119ce5cc4
Tidy up log messages across the codebase
1. Clearly separate the log messages from any fields that are
captured. The log message itself should be meaningful because it
depends on the underlying formatter, how/if the fields are displayed.
2. Some log messages had very little context, expand that.
3. Wording of errors was inconsistent, hopefully all errors should
now start with `Failed to ...`.
4. Some log messages were duplicated across multiple layers (like opening
the database).
5. Some log messages were split into two where one part is now an `error!`
and the 2nd part is an `info!` on what is happening next.
6. Where appropriate, punctuation has been removed to not interrupt
the reader's flow.
2021-07-07 16:02:00 +10:00
Thomas Eizinger
78480547d5
Don't import tracing's macros
Log statements end up getting changed constantly and having to clean
up imports after that is annoying, for example, if the last `info!`
in a file disappears, you end up with an unused import warning.

Fully qualifying tracing's macros prevents that and also communicates
clearly that we are using tracing and not log.
2021-07-07 16:01:33 +10:00
Thomas Eizinger
5463bde4f8
Add a mandatory --change-address parameter to buy-xmr
Fixes #513.
2021-07-06 20:07:53 +10:00
Thomas Eizinger
683d565679
Make variable naming consistent 2021-07-06 20:06:41 +10:00
Daniel Karzel
c0070f8fa7
Move files from protocol to appropriate module
Some network and application specific code does not belong in the protocol module and was moved.
Eventloop, recovery and the outside behaviour were moved to the respective application module because they are application specific.

The `swap_setup` was moved into the network module because upon change both sides will have to be changed and should thus stay close together.
2021-06-28 10:04:08 +10:00
Daniel Karzel
818147a629
swap_setup instead of spot_price and execution_setup
Having `spot_price` and `execution_setup` as separate protocols did not bring any advantages, but was problematic because we had to ensure that `execution_setup` would be triggered after `spot_price`. Because of this dependency it is better to combine the protocols into one.

Combining the protocols also allows a refactoring to get rid of the `libp2p-async-await` dependency.

Alice always listens for the `swap_setup` protocol. When Bob opens a substream on that protocol the spot price is communicated, and then all execution setup messages (swap-id and signature exchange).
2021-06-28 10:04:07 +10:00
Daniel Karzel
529de8d5fd
ASB aborts if CLI does not lock BTC
Includes a new state that is used to await BTC lock tx finality. Upon starting the swap we initially only wait for the BTC lock tx to be seen in the mempool.
This is guarded by a short timeout (3 mins), because it is assumed that in the current setup (sport_price + execution_setup only triggered upon funds being available already) the lock transaction should be picked up almost instanly after the execution setup succeeded.
2021-06-23 17:54:42 +10:00
Daniel Karzel
c4c798ea20
Timeout on execution_setup for ASB
Similar to the CLI the ASB has to ensure that the execution_setup is executed within a certain time.
Without a timeout the price (returned by `spot_price` would be guaranteed with the CLI indefinitely.
2021-06-23 17:54:42 +10:00
Daniel Karzel
1ae377ef24
Request timeout when swapping over Tor
It seems the current chosen channel timeouts are still not optimal.
I ran into issues with swapping over Tor and traced them down to the CLI timeout of the bmrng channel.
It appears that the ASB was not running as quick as the CLI, which caused a timeout on the CLI side (in addition to the delay when sending messages over Tor).
Only `execution_setup` caused the problem so far, but I would recommend changing all the channel timeouts to one minute to avoid this problem.
2021-06-23 17:53:50 +10:00
Thomas Eizinger
ac0b0150e6
Upgrade to Rust 1.53
Also fix all clippy lints that came up.
2021-06-21 11:56:04 +10:00
Thomas Eizinger
b7064578ca
Reduce level of indentation of match statements 2021-06-21 11:46:24 +10:00
Thomas Eizinger
f1182f97c0
Simplify match statement
This code is unnecessarily complicated.
2021-06-21 11:31:24 +10:00
Thomas Eizinger
1ba186aa63
Make log message a proper statement
"Listening on" is only half a sentence.
2021-06-21 09:01:21 +10:00
bors[bot]
baf5a0896e
Merge #536
536: Switch to using stable Rust instead of nightly r=thomaseizinger a=thomaseizinger



Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2021-05-28 04:47:26 +00:00
Daniel Karzel
c9064d5a37
Add ping protocol to ensure connection is alive
Adds the ping behaviour to both ASB and CLI behaviour that periodically pings a connected party to ensure that the underlying network connection is still alive.
This fixes problems with long-running connections that become dead without a connection closure being reported back to the swarm.
2021-05-27 11:19:27 +10:00
Thomas Eizinger
8f9d612af2
Change to stable Rust 1.52
Fix clippy warnings that are new in this version.
2021-05-25 16:11:49 +10:00
Daniel Karzel
01af9a5676
Bitcoin transaction published state
This improves the error handling on the ASB.
Once the Bitcoin redeem transaction is seen in mempool, the state machine cannot transition to a cancel scenario anymore because at that point the CLI will have redeemed the Monero.
The additional state then waits for transaction finality.
2021-05-24 10:53:14 +10:00
Daniel Karzel
fb9fb21c2b
CLI log statements to be more JSON friendly
Values to be logged as fields.
Upon starting a swap we print the swap-id as well.
2021-05-21 15:00:31 +10:00
Daniel Karzel
af60d3bb54
Network check upon spot price request 2021-05-20 13:03:29 +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
1706b36800
Log exchange rate at each state. 2021-05-11 15:27:49 +10:00
Daniel Karzel
652aae9590
Introduce a minimum buy amount
Introduces a minimum buy Bitcoin amount similar to the maximum amount already present.
For the CLI the minimum amount is enforced by waiting until at least the minimum is available as max-giveable amount.
2021-05-11 12:03:02 +10:00
Daniel Karzel
6d3cf0af91
Include too low balance into Alice's error 2021-05-11 12:03:02 +10:00
Daniel Karzel
23d9637a4b
Work in review comments 2021-05-07 16:21:02 +10:00
Daniel Karzel
daa572e5bf
Move recovery commands in dedicated module
Less clutter in the folder structure.
2021-05-07 16:21:02 +10:00
Daniel Karzel
4deb96a3c5
ASB manual recovery commands
Adds `cancel`, `refund`, `punish`, `redeem` and `safely-abort` commands to the ASB that can be used to trigger the specific scenario for the swap by ID.
2021-05-07 16:19:11 +10:00
Daniel Karzel
0c616c7437
Move loading the state into the function
In the production code it is a weird indirection that we load the state and then pass in the state and the database.
In the tests we have one additional load by doing it inside the command, but loading from the db is not expensive.
2021-05-07 16:12:49 +10:00
Daniel Karzel
89b3d07eba
Network protocol tests for spot_price behaviour
Each test spawns swarm for Alice and Bob that only contains the spot_price behaviours and uses a memory transport.
Tests cover happy path (i.e. expected price is returned) and error scenarios.
Implementation of `TestRate` on `LatestRate` allows testing rate fetch error and quote calculation error behaviour.

Thanks to @thomaseizinger for ramping up the test framework for comit-rs in the past!
2021-05-07 15:00:29 +10:00
Daniel Karzel
03a0dc73cd
Alice's spot_price Behaviour reports back Error
Instead of handling all errors on the inside spot_price errors are bubbled up (as `SwapRequestDeclined`).
This allows us to test both Alice's and Bob's behaviour for all scenarios.
2021-05-07 15:00:29 +10:00
Daniel Karzel
5aac76598d
Decouple ASB/CLI Errors from Error sent over wire
What goes over the wire should not be coupled to the errors being printed.
For the CLI and ASB we introduce a separate error enum that is used for logging.
When sending over the wire the errors are mapped to and from the `network::spot_price::Error`.

As part of Bob-specific spot_price code was moved from the network into bob.
Clearly separation of the network API from bob/alice.
2021-05-07 15:00:29 +10:00
Daniel Karzel
52f648e1de
Alice's spot price logic into dedicated behaviour
Move Alice's spot price logic into a dedicated network behaviour that handles all the logic.
The new behaviour encapsulates the complete state necessary for spot price request decision making.
The network behaviour cannot handle asynchronous calls, thus the balance is managed inside the spot price and has to updated regularly from the outside to ensure the spot price balance check has up to date data.
At the moment the balance is updated upon an incoming quote requests.

Code that is relevant for both ASB and CLI remains in the `network::spot_price` module (e.g. `network::spot_price::Error`).
2021-05-07 15:00:29 +10:00
Daniel Karzel
ea76ae5821
Return proper error to CLI for all expected scenarios
When a CLI requests a spot price have some errors that are expected, where we can provide a proper error message for the CLI:
- Balance of ASB too low
- Buy amount sent by CLI exceeds maximum buy amount accepted by ASB
- ASB is running in maintenance mode and does not accept incoming swap requests

All of these errors returns a proper error to the CLI and prints a warning in the ASB logs.
Any other unexpected error will result in closing the channel with the CLI and printing an error in the ASB logs.
2021-05-07 14:52:51 +10:00
Daniel Karzel
f6497778ed
Add resume-only mode for the ASB
Resume-only is a maintenance mode where no swaps are accepted but unfinished swaps are resumed.
This is achieve by ignoring incoming spot-price requests (that would lead to execution setup) in the event-loop.
2021-05-07 14:52:51 +10:00
Philipp Hoenisch
59a55dcbdd
Remove todo for cleanup note. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
68ae47edf4
Log error message. 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
7fe9087bc2
Pull trait-bounds up one level for easier testing. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
77f6f11a7f
Pass address/fees into State0 instead of passing in the wallet. 2021-05-07 10:24:41 +10:00
Philipp Hoenisch
9e8b788aa9
Rename weight constants. 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
Philipp Hoenisch
1db7586632
Increase timeout sending/receiving protocol messages. 2021-05-04 15:38:55 +10:00
Daniel Karzel
08fecb8fe3
Peer check for incoming transfer proofs
Bob validates that incoming transfer proof messages are coming from the peer-id of Alice.
Currently Bob will ignore any transfer proof message that is not coming from the counterparty peer-id associated to the current swap in execution.
Once we add support for trying to save received transfer proofs for swaps that are currently not in execution we can also adapy allowing this for different counterparty peer-ids. This requires access to the database in Bob's event loop.
2021-04-29 18:28:14 +10:00
Daniel Karzel
3c2dfa830a
Peer check for incoming encrypted signatures
Alice validates that incoming encsig messages are coming from the peer-id that is associated with the swap.
Encsig message from a peer-id different to the one associated with the swap are ignored.
2021-04-29 18:24:45 +10:00