Commit Graph

295 Commits

Author SHA1 Message Date
Thomas Eizinger
325fcbdb8c
Introduce protocol::bob::behaviour to mimic structure from alice module 2021-04-26 12:45:37 +10:00
Thomas Eizinger
5b515d6fb2
Don't qualify with alice:: if we are within a module of Alice 2021-04-26 12:45:37 +10:00
Thomas Eizinger
6d06db3259
Use macro-based JSON-RPC client 2021-04-26 12:45:35 +10:00
dependabot[bot]
19483c5ead
Bump libp2p from 0.36.0 to 0.37.1
Bumps [libp2p](https://github.com/libp2p/rust-libp2p) from 0.36.0 to 0.37.1.
- [Release notes](https://github.com/libp2p/rust-libp2p/releases)
- [Changelog](https://github.com/libp2p/rust-libp2p/blob/master/CHANGELOG.md)
- [Commits](https://github.com/libp2p/rust-libp2p/compare/v0.36.0...v0.37.1)

Signed-off-by: dependabot[bot] <support@github.com>
2021-04-20 11:51:40 +10:00
Thomas Eizinger
b417950f99
Improve error reporting of failed protocols
Instead of forwarding every error, we deliberately ignore certain
variants that are not worth being printed to the log. In particular,
this concerns "UnsupportedProtocols" and "ResponseOmission".

To make this less verbose we introduce a macro for mapping a
`RequestResponseEvent` to `{alice,bob}::OutEvent`. We use a macro
because those `OutEvent`s are different types and the only other
way of abstracting over them would be to introduce traits that we
implement on both of them.

To make the macro easier to use, we move all the `From` implementations
that convert between the protocol and the more high-level behaviour
into the actual protocol module.
2021-04-14 10:34:06 +10:00
Thomas Eizinger
f0f7288bb6
Introduce a redial::Behaviour
This behaviour makes Bob re-dial Alice with an exponential backoff as
soon as the connection is lost.
2021-04-14 10:07:14 +10:00
bors[bot]
19766b9759
Merge #405
405: Concurrent swaps with same peer r=da-kami a=da-kami

Fixes #367 

- [x] Concurrent swaps with same peer

Not sure how much more time I should invest into this. We could just merge the current state and then do improvements on top...?

Improvements:

- [x] Think `// TODO: Remove unnecessary swap-id check` through and remove it
- [x] Add concurrent swap test, multiple swaps with same Bob
- [ ] Save swap messages without matching swap in execution in the database
- [ ] Assert the balances in the new concurrent swap tests
- [ ] ~~Add concurrent swap test, multiple swaps with different Bobs~~
- [ ] ~~Send swap-id in separate message, not on top of `Message0`~~

Co-authored-by: Daniel Karzel <daniel@comit.network>
2021-04-13 08:50:44 +00:00
Daniel Karzel
c976358c37
Multiple swaps with the same peer
- Swap-id is exchanged during execution setup. CLI (Bob) sends the swap-id to be used in his first message.
- Transfer poof and encryption signature messages include the swap-id so it can be properly associated with the correct swap.
- ASB: Encryption signatures are associated with swaps by swap-id, not peer-id.
- ASB: Transfer proofs are still associated to peer-ids (because they have to be sent to the respective peer), but the ASB can buffer multiple
- CLI: Incoming transfer proofs are checked for matching swap-id. If a transfer proof with a different swap-id than the current executing swap is received it will be ignored. We can change this to saving into the database.

Includes concurrent swap tests with the same Bob.

- One test that pauses and starts an additional swap after the transfer proof was received. Results in both swaps being redeemed after resuming the first swap.
- One test that pauses and starts an additional swap before the transfer proof is sent (just after BTC locked). Results in the second swap redeeming and the first swap being refunded (because the transfer proof on Bob's side is lost). Once we store transfer proofs that we receive during executing a different swap into the database both swaps should redeem.

Note that the monero harness was adapted to allow creating wallets with multiple outputs, which is needed for Alice.
2021-04-13 18:16:19 +10:00
Daniel Karzel
489696ee08
Swap-id as file name for generated Monero wallet
Instead of using the private view-key as wallet filename we use the swap-id, to be able to identify which wallet is associated with which swap.
2021-04-08 19:23:54 +10:00
Daniel Karzel
548f057726
Try to open wallet in case generate_from_keys fails 2021-04-08 19:15:30 +10:00
Daniel Karzel
b60790a32c
Allow buffering multiple transfer proofs per peer
Allowing multiple swaps with the same peer requires buffering multiple transfer proofs per peer.
2021-04-08 17:02:18 +10:00
bors[bot]
32912ebd4a
Merge #394
394: Add a configurable spread to the ASB r=thomaseizinger a=thomaseizinger

Fixes #381.

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2021-04-06 07:39:11 +00:00
Thomas Eizinger
a99d12b9df
Add a configurable spread to the ASB
Fixes #381.
2021-04-06 16:16:58 +10:00
Thomas Eizinger
3e0301a9d4
Move FixedRate into event_loop module
This is where these types are used, they can be defined in there.
2021-04-06 16:16:57 +10:00
Thomas Eizinger
654cfff2a8
Make kraken module emit PriceUpdates instead of Rates 2021-04-06 16:16:56 +10:00
Thomas Eizinger
cfc530e8ab
Make ask field of Rate private 2021-04-06 16:16:53 +10:00
Thomas Eizinger
1b2f476cae
Have --force flag only override the timelock check
It might very well be that the cancel transaction is already published.
If that is the case, there is no point in failing the command. We simply
transition to cancel and exit normally.

The reason this comes up now is because Alice now properly waits for
the cancel timelock as well and publishes the cancel transaction first.

Ultimately, she should not do that because there is no benefit to her
unless she can also publish the punish transaction.
2021-04-01 17:28:38 +11:00
Thomas Eizinger
24f444b9f7
Race sending transfer proof against cancel timelock
Sending the transfer proof might never resolve because Bob doesn't
come back online. In that case, we need to make sure we bail out
as soon as the timelock expires.
2021-04-01 17:09:18 +11:00
Thomas Eizinger
c0785ab05a
"Buffer" all requests to Alice until we are connected
We use the "precondition" feature of the `tokio::select!` macro to
avoid polling certain futures. In particular, we skip polling all
futures that - when resolved - require us to send a message to Alice.
2021-04-01 17:09:17 +11:00
Thomas Eizinger
1b0c29b424
Use bmrng to model communicaton of Alice's EventLoop with the handle
This allows us to delay the ACKing of the encrypted signature up until
the swap has actually requested it.

Similarly, it allows us to wait for the ACK of the transfer proof within
the swap before continuing.
2021-04-01 17:09:17 +11:00
Thomas Eizinger
1c47b32681
Use bmrng to model communicaton of Bob's EventLoop with the handle
bmrng is a library providing a request-response channel that allows
the receiving end of the channel to send a response back to the sender.
This allows us to more accurately implement the functions on the
`EventLoopHandle`. In particular, we now _wait_ for the ACK of specific
messages from the other party before resolving the future.

For example, when sending the encrypted signature, the async function
on the `EventLoopHandle` does not resolve until we received the ACK
from the other party.

We also delete the `Channels` abstraction in favor of directly creating
bmrng channels. This allows us to directly control the channel buffer
which we set to 1 because we don't need more than that on Bob's side.
2021-04-01 17:09:17 +11:00
Thomas Eizinger
958e5b12bc
Don't match on expired_timelocks and race it in a select in parallel
There is no point in first checking for the expired timelocks and
then constructing a `select!` that also watches for the timelock to
expiry.

We can simply only have the select! invocation to achieve the same
effect. In case the timelock is already expired, this future will
resolve immediately.

Normally, the polling order of `select!` is pseudo-random. We
configure it to be _biased_ here to make sure the futures are polled
in order.
2021-04-01 17:08:26 +11:00
Thomas Eizinger
dbe03ba1cf
Timeout Bob's execution-setup after 10 seconds
The execution setup is our only libp2p protocol that doesn't have
a timeout built-in. Hence, if anything fails on Alice's side, we
would wait here forever.

Wrapping the future in a timeout ensures that we fail eventually
if this protocol doesn't succeed.
2021-04-01 17:08:26 +11:00
Thomas Eizinger
5d75f1adba
Remove import line in favor of FQ macro usage 2021-04-01 17:08:26 +11:00
Thomas Eizinger
4c2e254543
Don't log subscription
This object is very verbose and not meant to be logged.
2021-04-01 17:08:25 +11:00
Thomas Eizinger
5b230bc75f
Don't import tracing macros
Typing them out is quicker than constantly adjusting imports.
2021-04-01 17:08:25 +11:00
Thomas Eizinger
90a7760124
Add some log statements to bob::cancel 2021-04-01 17:08:25 +11:00
Thomas Eizinger
3f54b39281
Make all error messages start with an uppercase letter 2021-04-01 16:12:14 +11:00
Thomas Eizinger
0ef9d97679
Remove delegation functions in favor of public fields
We don't need to hide the fields of this Behaviour as the only reason
for why this struct exists is because libp2p forces us to compose our
NetworkBehaviours into a new struct.
2021-04-01 16:12:13 +11:00
Thomas Eizinger
b1d0ae8db7
Remove dead code
No codepath constructs this event, we can delete the associated code.
2021-04-01 16:11:57 +11:00
Daniel Karzel
2135a6e53e
Alice resumes swaps 2021-04-01 16:09:13 +11:00
Daniel Karzel
d233e9914e
Avoid problems when re-ordering / changing Bob's states
Make it explicit in which states we are able NOT to cancel/refund.
2021-04-01 16:06:48 +11:00
Daniel Karzel
e6dd194f77
next_state loop always exits in final state
Otherwise we can run into scenarios where the loop never properly exits.
2021-04-01 16:06:48 +11:00
Daniel Karzel
bc442bcad3
Await 10 confirmations of lock tx in refund
Awaiting the confirmations in an earlier state can cause trouble with resuming
swaps with short cancel expiries (test scenarios).
Since it is the responsibility of the refund state to ensure that the XMR can
be sweeped, we now ensure that the lock transaction has 10 confirmations before
refunding the XMR using generate_from_keys.
2021-04-01 16:03:38 +11:00
Daniel Karzel
183e8f02de
Wait for lock tx and send transfer proof in separate state
Sending the transfer transaction in a distinct state helps ensuring
that we do not send the Monero lock transaction twice in a restart
scenario.
Waiting for the first transaction confirmation in a separate state
helps ensuring that we send the transfer proof in a restart scenario.
2021-04-01 16:03:19 +11:00
Daniel Karzel
dfd69c9c80
Alice aborts if any timelock expired before locking XMR
Once we resume unfinished swaps upon startup we have to ensure that
it is safe for Alice to act.
If Bob has locked BTC it is only make sense for Alice to lock up the
XMR as long as no timelock has expired. Hence we abort if the BTC is
locked, but any timelock expired already.
2021-04-01 16:02:42 +11: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
11b45cd8c0
Move messages into protocol module
This allows us to remove all visibility modifiers from the message
fields because child modules (in this case {alice,bob}::state) can
always access private fields of structs.

It also moves the messages into a more natural place. Previously,
they were defined within the network layer even though they are
independent of the libp2p implementation.
2021-03-30 12:59:34 +11:00
Thomas Eizinger
e130448200
Make as many fields of Alice's states private as possible
To achieve this, we need to add some pure helpers to the state structs.
This has the added benefit that we can reduce the amount of code within
the swap function.
2021-03-30 12:59:34 +11:00
Thomas Eizinger
7f5715e147
Remove unnecessary serde implementations 2021-03-30 12:59:33 +11:00
rishflab
86f312e143 Safely abort swap if TxLock is not confirmed in a reasonable time
If TxLock does not confirm in a reasonable amount of time, Alice should
give up on the swap rather than waiting forever. Watching for TxLock in
the mempool is not required and it causes unnecessary complexity. What
if Alice does not see the transaction in mempool but it is already
confirmed? She will abort the swap for no reason.
2021-03-30 11:13:09 +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
c5827f84ca
Refactor recursive function to loop
This should get rid of the ever-growing stack size issue.
2021-03-29 12:15:48 +11:00
Thomas Eizinger
fc175a3f53
De-couple state from Monero wallet 2021-03-29 12:15:12 +11:00
Thomas Eizinger
5616683d7d
Monero confirmations are a u64
Trying to deserialize the number as a u32 caused deserialization
errors.
2021-03-29 12:14:19 +11:00
Thomas Eizinger
1057d115d1
Re-order bob::event_loop based on importance 2021-03-24 15:17:54 +11:00
Thomas Eizinger
638a169a04
Buffer transfer proof if we are not connected to Bob
The request-response behaviour that is used for sending the transfer
proof actually has a functionality for buffering a message if we
are currently not connected. However, the request-response behaviour
also emits a dial attempt and **drops** all buffered messages if this
dial attempt fails. For us, the dial attempt will very likely always
fail because Bob is very likely behind NAT and we have to wait for
him to reconnect to us.

To mitigate this, we build our own buffer within the EventLoop and
send transfer proofs as soon as we are connected again.

Resolves #348.
2021-03-24 15:17:54 +11:00
Thomas Eizinger
cde3f0f74a
Remove connection handling from swap execution
The swap should not be concerned with connection handling. This is
the responsibility of the overall application.

All but the execution-setup NetworkBehaviour are `request-response`
behaviours. These have built-in functionality to automatically emit
a dial attempt in case we are not connected at the time we want to
send a message. We remove all of the manual dialling code from the
swap in favor of this behaviour.

Additionally, we make sure to establish a connection as soon as the
EventLoop gets started. In case we ever loose the connection to Alice,
we try to re-establish it.
2021-03-24 15:17:54 +11:00
Thomas Eizinger
804b34f6b0
Listen on all swarm events instead of just behaviour events 2021-03-24 11:39:41 +11:00
Thomas Eizinger
2200fce3f3
Pass Swarm into EventLoop
This reduces the amount of arguments we need to pass into the eventloop
at the expense of slightly more setup of the swarm.
2021-03-24 11:39:41 +11:00