Commit Graph

143 Commits

Author SHA1 Message Date
Lucas Soriano del Pino
24104d5d35 Test that Alice punishes if Bob is inactive after locking bitcoin 2020-10-26 16:05:30 +11:00
Tobin C. Harding
cbccc282ef Send Message2 from Bob to Alice
In order for Alice to complete the handshake she needs to transition to state 3,
for this she needs message 2 from Bob.

Send `bob::Message2` to Alice and transition to `State3` - completing the
handshake.
2020-10-26 14:30:41 +11:00
Lucas Soriano del Pino
0dd01c3a3b Do not re-export curve25519_dalek
Anything that needs to be re-exported by this crate from
`curve25519_dalek` can be re-exported from the `monero` module. In
fact, the `Scalar` type was already being re-exported.
2020-10-26 13:06:20 +11:00
Lucas Soriano del Pino
cea1af1e1a Take bitcoin_tx_lock_timeout as argument to action generators 2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
1d21ae7e7a Use pin_mut! instead of futures::pin_mut! 2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
aa2a20916e Include Bob being inactive as a reason for failure 2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
41e8c7283c Verify Bob's redeem encsig as Alice
Not doing so means that receiving an invalid encrypted signature from
Bob would make the generator produce a `RedeemBtc` action that should
not be accepted by the node (since Bob's signature would be invalid
after decrypting his encrypted signature).

It's better to fail early and let the user know what went wrong,
rather than let them hit an incomprehensible error when using their
wallet.
2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
def3399d1c Use while instead of loop 2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
c86a82b315 Rename action_generator_alice to action_generator 2020-10-26 12:30:15 +11:00
Lucas Soriano del Pino
e84c56378c Test that both parties refund if Alice does not redeem
Also:

- Move generator functions to `alice` and `bob` modules. This makes
using `tracing` a lot easier, since the context of the file name let's
us differentiate between Alice's and Bob's generator logs more
clearly.
- Accept 0 confirmations when watching for the Monero lock
transaction. This should eventually be configured by the application,
but in the tests it's making things unexpectedly slower.
2020-10-26 12:30:15 +11:00
rishflab
ec71f98252 Revert re-imports to qualified paths 2020-10-26 11:45:27 +11:00
rishflab
fea6b29da9 Test monero amount serialisation 2020-10-26 11:39:54 +11:00
rishflab
28ffe063b7 Remove stale comment 2020-10-26 11:36:46 +11:00
Franck Royer
82b092d86e Use consensus encoding for monero private key
Using consensus encoding future proof the library by using a commonly
accepted de/serialization format.
2020-10-26 11:36:46 +11:00
Franck Royer
9ea708b2a5 Remove custom implementation of bitcoin amount serde
This is unnecessary as rust-bitcoin provides it.
2020-10-26 11:36:40 +11:00
rishflab
c781ee949d Move storage into application crate 2020-10-26 10:32:54 +11:00
Philipp Hoenisch
11a4a7563b
Merge pull request #19 from comit-network/move-tor
Moving tor into swap
2020-10-23 13:32:53 +11:00
Philipp Hoenisch
17b32fed90
Moving tor into swap 2020-10-23 12:47:52 +11:00
Philipp Hoenisch
c0109d12cd
Use lib to ensure ports are free 2020-10-23 11:28:58 +11:00
rishflab
a29786e175 Use external serialisation implementation 2020-10-22 19:41:52 +11:00
rishflab
03dee7a3ea Generate temp dir for db 2020-10-22 19:34:53 +11:00
rishflab
3e82211a82 Add database todos 2020-10-22 19:27:28 +11:00
rishflab
62bc3c0432 Fix rebase conflicts 2020-10-22 19:25:54 +11:00
rishflab
e3b68a3864 Improve database type safety
The database is now bound to a type eg. alice::State or bob::State.
The caller cannot expect to retrieve a type that is different to
the type that was stored.
2020-10-22 18:09:16 +11:00
rishflab
8eda051087 Remove redundant serialisation implementation
ECDSAfun's serialisation implementation was already imported
making the custom implementation redundant

Remove remnants of stale comment

Remove redundant conversion

Rename params to be consistent
2020-10-22 18:09:16 +11:00
Tobin C. Harding
79c6c8bbef Sort Cargo.toml lines 2020-10-22 15:02:32 +11:00
Tobin C. Harding
ade0e08db6 Merge branch 'master' into on-chain-protocol 2020-10-22 14:49:55 +11:00
Tobin C. Harding
97363cb05c Fix build after merge 2020-10-22 14:43:58 +11:00
Tobin C. Harding
f8adf6d7e0 Merge branch 'on-chain-protocol' of github.com:comit-network/xmr-btc-swap into on-chain-protocol 2020-10-22 14:33:16 +11:00
Tobin C. Harding
eed5e8e9a4 Move monero serde code to lib 2020-10-22 13:54:13 +11:00
Tobin C. Harding
0f17ec076c Add message1 2020-10-22 13:23:12 +11:00
Lucas Soriano del Pino
5395303a99 Test on-chain protocol happy path 2020-10-22 11:50:06 +11:00
Tobin C. Harding
a0987ee2b8 Use upstream serder for dleq Proof 2020-10-22 11:19:43 +11:00
Tobin C. Harding
ad0d8d5713 Emit message0 out of NB (both parties) 2020-10-22 11:04:49 +11:00
Tobin C. Harding
ad0d563d93 Use Normal secp256k1::Point 2020-10-22 10:37:06 +11:00
Tobin C. Harding
7dffe4cf5f Implement Clone on states 2020-10-22 09:18:57 +11:00
Tobin C. Harding
2a07113074 Fix buld after merge of db branch 2020-10-22 08:28:51 +11:00
Tobin C. Harding
3e0b40c248 Merge branch 'db' into on-chain-protocol 2020-10-22 08:17:54 +11:00
Philipp Hoenisch
ae1ea08bb2
Make it work on CI 2020-10-22 08:03:06 +11:00
Philipp Hoenisch
e1d8a1c39b
PR feedback integrated 2020-10-22 08:03:06 +11:00
Philipp Hoenisch
e67e940768
Split authenticated struct into unauthenticated struct to make it usage more obvious 2020-10-22 08:01:59 +11:00
Philipp Hoenisch
a73f1fcc6f
Start tor from test 2020-10-22 08:01:59 +11:00
rishflab
39afb4196b Save and recover protocol state from disk
NOTE: This implementation saves secrets to disk! It is not
secure.

The storage API allows the caller to atomically record the state
of the protocol. The user can retrieve this recorded state and
re-commence the protocol from that point. The state is recorded
using a hard coded key, causing it to overwrite the previously
recorded state. This limitation means that this recovery
mechanism should not be used in a program that simultaneously
manages the execution of multiple swaps.

An e2e test was added to show how to save, recover and resume
protocol execution. This logic could also be integrated into the
run_until functions to automate saving but was not included at
this stage as protocol execution is currently under development.

Serialisation and deserialisation was implemented on the states
to allow the to be stored using the database. Currently the
secret's are also being stored to disk but should be recovered
from a seed or wallets.
2020-10-21 18:39:45 +11:00
Philipp Hoenisch
295216a8ee
Add Tor feature flag and only run test if enabled 2020-10-21 18:18:25 +11:00
Philipp Hoenisch
5e19949d71
Add library to create a Tor service via Tor control port 2020-10-21 18:18:25 +11:00
Lucas Soriano del Pino
d3a7689059 Update comment about minimum stack size 2020-10-21 16:10:20 +11:00
Lucas Soriano del Pino
e5ddec76f7 Increase Rust minimum stack size in CI 2020-10-21 16:09:24 +11:00
Lucas Soriano del Pino
8b48191e1c Increase Rust minimum stack size in CI 2020-10-21 15:55:20 +11:00
Lucas Soriano del Pino
5250e16b64 Update dependency to cross-curve-dleq and ecdsa_fun 2020-10-21 15:28:50 +11:00
Lucas Soriano del Pino
04e1dca16a Fix dependency to cross-curve-dleq and ecdsa_fun 2020-10-21 15:14:31 +11:00
Tobin C. Harding
f22729c5da Derive Clone/ on initial states and messages 2020-10-21 15:02:37 +11:00
Lucas Soriano del Pino
50ed74319f Simplify xmr-btc/tests Monero wallet
- Make it the same for Alice and Bob.
- Make it contain a wallet client instead of the `Monero` struct.

Also:

Remove `Container` from inside `Monero` struct. The caller of `new`
can simply ensure that `Container` is not dropped to keep the
container alive.

This makes the `Monero` struct easier to work with, as it just holds
the data necessary to create the different clients created during
`init`, and does not have any lifetime restrictions.
2020-10-20 12:18:27 +11:00
Lucas Soriano del Pino
55629838f4 Implement test function that calls action_generator_alice 2020-10-19 10:44:25 +11:00
Lucas Soriano del Pino
7c99415cff Write action_generator_alice 2020-10-16 17:05:11 +11:00
Lucas Soriano del Pino
55ea8f23e2 s/{monero,bitcoin}_ledger/{monero,bitcoin}_client/g 2020-10-16 14:17:10 +11:00
Lucas Soriano del Pino
9d64f3fd29 Watch the blockchain during Bitcoin refund 2020-10-16 14:16:06 +11:00
Lucas Soriano del Pino
969ca63081 Split RefundBitcoin into CancelBitcoin and RefundBitcoin 2020-10-16 14:07:31 +11:00
Lucas Soriano del Pino
d813957c5a Rename InsufficientXMR to InsufficientXmr 2020-10-16 14:02:47 +11:00
Lucas Soriano del Pino
bae391ff9c Bubble up unrecoverable errors instead of expecting
This does introduce the ability of expressing incorrect combinations
of the enums `SwapFailed` and `Reason`, but these are just internal
to this function and it's terser that way.
2020-10-16 14:01:57 +11:00
Lucas Soriano del Pino
e457130b6c Bubble up unrecoverable errors instead of expecting
This does introduce the ability of expressing incorrect combinations
of the enums `SwapFailed` and `Reason`, but these are just internal
to this function and it's terser that way.
2020-10-16 11:43:24 +11:00
Lucas Soriano del Pino
369770488f Document Reason enum 2020-10-16 11:31:16 +11:00
Lucas Soriano del Pino
7d307e581a Fail the swap early if Bitcoin TxLock is never published
This helps distinguish between the case where the refund timelock is
reached before the bitcoin is locked and the case where the refund
timelock is reached after the bitcoin is locked and before Alice sends
over the transfer proof for locking up the monero. In the first case
we can abort without doing anything, but in the second case we must
instruct the caller to refund the bitcoin.
2020-10-16 11:26:17 +11:00
Lucas Soriano del Pino
9f1bf72c7b Only generate refund action after Bitcoin lock 2020-10-16 11:19:38 +11:00
Lucas Soriano del Pino
c1f6adc8ed Refund if Alice takes too long to prove that Monero has been locked 2020-10-16 10:55:13 +11:00
Lucas Soriano del Pino
176b2195e3 Make ReceiveTransferProof async and take &mut self 2020-10-16 10:55:13 +11:00
Lucas Soriano del Pino
4723626fc0 Suppress compiler noise in tests folder
By having two modules that declare and use the `harness` module we ran
into a situation where not all parts of the `harness` module were
being used by both of these other test modules. This was producing
annoying warnings.

For now this can be solved by marking the `harness` module declaration
as public. This has no real effect since modules under `/tests` cannot
be depended on elsewhere, but it satisfies the compiler. It does mean
that we will not hear about unused functions, but this change should
be temporary and it's a very minor problem given that it's just test
code.
2020-10-15 21:53:55 +11:00
Lucas Soriano del Pino
f0ecc62748 Check expiry before emitting first action 2020-10-15 21:48:08 +11:00
Lucas Soriano del Pino
df4ffb65c9 Make bitcoin::WatchForRawTransaction infallible
And trigger refund if Alice's redeem transaction takes too long.
2020-10-15 21:22:52 +11:00
Lucas Soriano del Pino
ba3011a9c9 Trigger refund if the publication of Monero TxLock takes too long 2020-10-15 21:22:31 +11:00
Lucas Soriano del Pino
08be87747f Configure exponential backoff so that it never stops retrying 2020-10-15 18:34:13 +11:00
Lucas Soriano del Pino
15f7932f7f Replace monero::CheckTransfer with monero::WatchForTransfer
Instead of checking once to see if Monero's `TxLock` has been
published, the new trait should keep looking until the transaction has
been found.

The new trait also allows the caller to set an expected number of
confirmations on the transaction.

The implementation of the trait is currently part of test code, but it
should be similar to what we will eventually do for an application.
2020-10-15 13:10:31 +11:00
Lucas Soriano del Pino
5daa3ea9a8 [WIP] Generate actions for Bob's on-chain protocol
Mimics what @thomaseizinger did here [1] and here [2].

This has the advantage that the consumer has more freedom to execute
`Action`s without having to implement particular traits. The error
handling required inside this protocol-executing function is also
reduced.

As discussed with Thomas, for this approach to work well, the
trait functions such as `receive_transfer_proof` should be infallible,
and the implementer should be forced to hide IO errors behind a retry
mechanism.

All of these asynchronous calls need to be "raced" against
the abort condition (determined by the `refund_timelock`), which is
missing in the current state of the implementation.

The initial handshake of the protocol has not been included here,
because it may not be easy to integrate this approach with libp2p, but
a couple of messages still need to exchanged. I need @tcharding to
tell me if it's feasible/good to do it like this.

[1]
https://github.com/comit-network/comit-rs/blob/move-nectar-swap-to-comit/nectar/src/swap/comit/herc20_hbit.rs#L57-L184.
[2] e584d2b14f/nectar/src/swap.rs (L716-L751).
2020-10-13 10:53:20 +11:00
Tobin C. Harding
fc12b0d9fd Re-order send/receive
There are no guarantees that send_message and receive_massage do not block
the flow of execution. Therefore they must be paired between Alice/Bob, one
send to one receive in the correct order.

Define Alice to call `receive_message` first, with Bob sending the message. Do
this because we are expecting Alice to be have a well known address, there is no
currently such assumption for Bob.
2020-10-12 08:53:21 +11:00
rishflab
c778f4e204 Remove incorrect todo 2020-10-09 11:46:33 +11:00
rishflab
2c6bb8fa19 Remove redundant attribute 2020-10-09 11:40:00 +11:00
rishflab
51760041f0 Remove duplicated macro definitions 2020-10-09 11:40:00 +11:00
rishflab
666c121bb3 Implement transport traits with generics 2020-10-09 11:40:00 +11:00
rishflab
bf576bf251 Move test functions out of lib 2020-10-09 11:40:00 +11:00
rishflab
61a8a3e428 Improve formatting of tests 2020-10-09 11:40:00 +11:00
rishflab
9f046944a5 Add context to log messages 2020-10-09 11:40:00 +11:00
rishflab
48e5eb8d6c Remove redundant type declarations 2020-10-09 11:40:00 +11:00
rishflab
154b3edcf9 Split SendReceive into seperate traits 2020-10-09 11:40:00 +11:00
rishflab
6b7193b974 Fix clippy warnings 2020-10-09 11:40:00 +11:00
rishflab
697e1195cd Fix Cargo.toml fmt 2020-10-09 11:40:00 +11:00
rishflab
a759f39b1d Fix module ambiguity in tests 2020-10-09 11:40:00 +11:00
rishflab
36ed3a0b8b Rename ImportOutput to reflect purpose 2020-10-09 11:40:00 +11:00
rishflab
213034dc1f Remove unused clone from states 2020-10-09 11:40:00 +11:00
rishflab
25edd90fb0 Remove empty lines between imports 2020-10-09 11:40:00 +11:00
rishflab
4e031ff9a2 Loop on blockchain call instead of delaying
Previously there was a delay making a get raw transaction call to
give some time for a transaction to be confirmed on the blockchain.
This has been replaced with a loop that waits until the call is
succesful.
2020-10-09 11:40:00 +11:00
rishflab
f6f4ec2bdb Fix dependencies 2020-10-09 11:40:00 +11:00
rishflab
148e49a959 Remove redundant lifetime parameter 2020-10-09 11:40:00 +11:00
rishflab
8754a9931b Execute Alice and Bob state machines concurrently
Previously we were testing the protocol by manually driving Alice and
Bob's state machines. This logic has now be moved to an async state
transition function that can take any possible state as input. The
state transition function is called in a loop until it returns the
desired state. This allows use to interrupt midway through the protocol
and perform refund and punish tests. This design was chosen over a
generator based implementation because the the generator based
implementation results in a impure state transition function that is
difficult to reason about and prone to bugs.

Test related code was extracted into the tests folder.

The 2b and 4b states were renamed to be consistent with the rest.

Macros were used to reduce code duplication when converting
child states to their parent states and vice versa.

Todos were added were neccessary.
2020-10-09 11:40:00 +11:00
Lucas Soriano del Pino
1f99cf001c Swap Monero for Bitcoin
Co-authored-by: rishflab <rishflab@hotmail.com>
Co-authored-by: Philipp Hoenisch <philipp@hoenisch.at>
Co-authored-by: Tobin C. Harding <tobin@coblox.tech>
2020-09-28 17:15:57 +10:00