Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit f82d6c9

Browse files
authored
Merge pull request #427 from coblox/instantiate-state-406
Refactor State Machine and Events have correct types for Alice AND Bob
2 parents 3dde256 + bd22a72 commit f82d6c9

File tree

27 files changed

+1055
-1349
lines changed

27 files changed

+1055
-1349
lines changed

application/comit_node/src/comit_client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{io, net::SocketAddr, sync::Arc};
77
use std::{fmt::Debug, panic::RefUnwindSafe};
88
use swap_protocols::{bam_types, rfc003};
99

10-
pub trait Client: Send + Sync {
10+
pub trait Client: Send + Sync + 'static {
1111
fn send_swap_request<
1212
SL: rfc003::Ledger,
1313
TL: rfc003::Ledger,

application/comit_node/src/http_api/rfc003/swap.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use swap_protocols::{
1313
ledger::{Bitcoin, Ethereum},
1414
rfc003::{
1515
self, bitcoin,
16+
roles::{Alice, Bob},
1617
state_store::{self, StateStore},
17-
Ledger, Secret, SecretHash,
18+
Ledger, Secret,
1819
},
1920
Assets, Ledgers, Metadata, MetadataStore, Roles,
2021
};
@@ -290,14 +291,16 @@ fn handle_state_for_get_swap<T: MetadataStore<SwapId>, S: state_store::StateStor
290291
target_asset: Assets::Ether,
291292
role,
292293
}) => match role {
293-
Roles::Alice => match state_store
294-
.get::<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity, Secret>(id)
295-
{
296-
Err(e) => error!("Could not retrieve state: {:?}", e),
297-
Ok(state) => info!("Here is the state we have retrieved: {:?}", state),
298-
},
294+
Roles::Alice => {
295+
match state_store
296+
.get::<Alice<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity>>(id)
297+
{
298+
Err(e) => error!("Could not retrieve state: {:?}", e),
299+
Ok(state) => info!("Here is the state we have retrieved: {:?}", state),
300+
}
301+
}
299302
Roles::Bob => match state_store
300-
.get::<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity, SecretHash>(id)
303+
.get::<Bob<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity>>(id)
301304
{
302305
Err(e) => error!("Could not retrieve state: {:?}", e),
303306
Ok(state) => info!("Here is the state we have retrieved: {:?}", state),

application/comit_node/src/swap_protocols/rfc003/actions/alice/btc_eth.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ use swap_protocols::{
88
ethereum::EtherRedeem,
99
Action, StateActions,
1010
},
11-
bitcoin::{bitcoin_htlc, bitcoin_htlc_address},
11+
roles::Alice,
1212
state_machine::*,
13-
Secret,
1413
},
1514
};
1615

17-
impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity, Secret> {
16+
impl StateActions for SwapStates<Alice<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity>> {
1817
type Accept = ();
1918
type Decline = ();
2019
type Fund = BitcoinFund;
@@ -26,7 +25,7 @@ impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuanti
2625
match *self {
2726
SS::Start { .. } => vec![],
2827
SS::Accepted(Accepted { ref swap, .. }) => vec![Action::Fund(BitcoinFund {
29-
address: bitcoin_htlc_address(swap),
28+
address: swap.source_htlc_params().compute_address(),
3029
value: swap.source_asset,
3130
})],
3231
SS::SourceFunded { .. } => vec![],
@@ -44,7 +43,7 @@ impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuanti
4443
}),
4544
Action::Refund(BitcoinRefund {
4645
outpoint: *source_htlc_location,
47-
htlc: bitcoin_htlc(swap),
46+
htlc: swap.source_htlc_params().into(),
4847
value: swap.source_asset,
4948
transient_keypair: swap.source_ledger_refund_identity,
5049
}),
@@ -60,7 +59,7 @@ impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuanti
6059
..
6160
}) => vec![Action::Refund(BitcoinRefund {
6261
outpoint: *source_htlc_location,
63-
htlc: bitcoin_htlc(swap),
62+
htlc: swap.source_htlc_params().into(),
6463
value: swap.source_asset,
6564
transient_keypair: swap.source_ledger_refund_identity,
6665
})],

application/comit_node/src/swap_protocols/rfc003/actions/bob/btc_eth.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use swap_protocols::{
88
ethereum::{EtherDeploy, EtherRefund},
99
Accept, Action, Decline, StateActions,
1010
},
11-
bitcoin::bitcoin_htlc,
12-
ethereum::ethereum_htlc,
11+
ethereum::{EtherHtlc, Htlc},
12+
roles::Bob,
1313
state_machine::*,
14-
SecretHash,
1514
},
1615
};
1716

18-
impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity, SecretHash> {
17+
impl StateActions for SwapStates<Bob<Bitcoin, Ethereum, BitcoinQuantity, EtherQuantity>> {
1918
type Accept = Accept;
2019
type Decline = Decline;
2120
type Fund = EtherDeploy;
@@ -28,7 +27,7 @@ impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuanti
2827
SS::Start { .. } => vec![Action::Accept(Accept), Action::Decline(Decline)],
2928
SS::Accepted { .. } => vec![],
3029
SS::SourceFunded(SourceFunded { ref swap, .. }) => {
31-
let htlc = ethereum_htlc(swap);
30+
let htlc: EtherHtlc = swap.target_htlc_params().into();
3231
vec![Action::Fund(EtherDeploy {
3332
data: htlc.compile_to_hex().into(),
3433
value: swap.target_asset,
@@ -68,9 +67,9 @@ impl StateActions for SwapStates<Bitcoin, Ethereum, BitcoinQuantity, EtherQuanti
6867
..
6968
}) => vec![Action::Redeem(BitcoinRedeem {
7069
outpoint: *source_htlc_location,
71-
htlc: bitcoin_htlc(swap),
70+
htlc: swap.source_htlc_params().into(),
7271
value: swap.source_asset,
73-
transient_keypair: swap.source_ledger_refund_identity,
72+
transient_keypair: swap.source_ledger_success_identity,
7473
secret: *secret,
7574
})],
7675
SS::Error(_) => vec![],
@@ -84,16 +83,14 @@ mod tests {
8483

8584
use super::*;
8685
use bitcoin_support;
87-
use hex;
88-
use secp256k1_support;
89-
use swap_protocols::rfc003::Secret;
86+
use hex::FromHex;
87+
use swap_protocols::rfc003::{roles::test::Bobisha, Secret};
9088

9189
#[test]
9290
fn given_state_instance_when_calling_actions_should_not_need_to_specify_type_arguments() {
93-
let swap_state = SwapStates::from(Start {
94-
source_ledger_refund_identity: secp256k1_support::KeyPair::from_secret_key_slice(
95-
&hex::decode("18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725")
96-
.unwrap(),
91+
let swap_state = SwapStates::from(Start::<Bobisha> {
92+
source_ledger_refund_identity: bitcoin_support::PubkeyHash::from_hex(
93+
"875638cac0b0ae9f826575e190f2788918c354c2",
9794
)
9895
.unwrap(),
9996
target_ledger_success_identity: "8457037fcd80a8650c4692d7fcfc1d0a96b92867"

application/comit_node/src/swap_protocols/rfc003/alice/handler.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ use swap_protocols::{
1515
self,
1616
alice::SwapRequests,
1717
messages::Request,
18+
roles::Alice,
1819
state_machine::{Start, SwapStates},
1920
state_store::StateStore,
20-
ExtractSecret, Ledger, Secret,
21+
Ledger, Secret,
2122
},
2223
};
2324
use swaps::{alice_events, common::SwapId};
@@ -123,11 +124,9 @@ impl<
123124

124125
fn spawn_state_machine<SL: Ledger, TL: Ledger, SA: Asset, TA: Asset, S: StateStore<SwapId>>(
125126
id: SwapId,
126-
start_state: Start<SL, TL, SA, TA, Secret>,
127+
start_state: Start<Alice<SL, TL, SA, TA>>,
127128
state_store: &S,
128-
) where
129-
TL::Transaction: ExtractSecret,
130-
{
129+
) {
131130
let state = SwapStates::Start(start_state);
132131

133132
// TODO: spawn state machine from state here

application/comit_node/src/swap_protocols/rfc003/bitcoin/extract_secret.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use bitcoin_support::Transaction;
2-
use swap_protocols::rfc003::secret::{ExtractSecret, Secret, SecretHash};
2+
use swap_protocols::{
3+
ledger::Bitcoin,
4+
rfc003::{
5+
secret::{Secret, SecretHash},
6+
ExtractSecret,
7+
},
8+
};
39

4-
impl ExtractSecret for Transaction {
5-
fn extract_secret(&self, secret_hash: &SecretHash) -> Option<Secret> {
6-
self.input.iter().find_map(|txin| {
10+
impl ExtractSecret for Bitcoin {
11+
fn extract_secret(transaction: &Transaction, secret_hash: &SecretHash) -> Option<Secret> {
12+
transaction.input.iter().find_map(|txin| {
713
txin.witness
814
.iter()
915
.find_map(|script_item| match Secret::from_vec(&script_item) {
@@ -50,7 +56,7 @@ mod test {
5056
let secret = Secret::from(*b"This is our favourite passphrase");
5157
let transaction = setup(&secret);
5258

53-
assert_that!(transaction.extract_secret(&secret.hash()))
59+
assert_that!(Bitcoin::extract_secret(&transaction, &secret.hash()))
5460
.is_some()
5561
.is_equal_to(&secret);
5662
}
@@ -65,7 +71,7 @@ mod test {
6571
bfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbf",
6672
)
6773
.unwrap();
68-
assert_that!(transaction.extract_secret(&secret_hash)).is_none();
74+
assert_that!(Bitcoin::extract_secret(&transaction, &secret_hash)).is_none();
6975
}
7076

7177
#[test]
@@ -77,7 +83,7 @@ mod test {
7783
.unwrap();
7884
let secret = Secret::from_vec(&hex_secret).unwrap();
7985

80-
assert_that!(transaction.extract_secret(&secret.hash()))
86+
assert_that!(Bitcoin::extract_secret(&transaction, &secret.hash()))
8187
.is_some()
8288
.is_equal_to(&secret);
8389
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
use bitcoin_support::{Address, BitcoinQuantity, Blocks, OutPoint};
22
use secp256k1_support::KeyPair;
3-
use swap_protocols::{ledger::Bitcoin, rfc003::Ledger};
3+
use swap_protocols::{
4+
ledger::Bitcoin,
5+
rfc003::{state_machine::HtlcParams, Ledger},
6+
};
47

58
mod extract_secret;
69
mod htlc;
710
mod queries;
11+
mod validation;
812

913
pub use self::{
1014
htlc::{Htlc, UnlockingError},
1115
queries::*,
1216
};
13-
use swap_protocols::{
14-
asset::Asset,
15-
rfc003::{state_machine::OngoingSwap, IntoSecretHash},
16-
};
1717

1818
impl Ledger for Bitcoin {
1919
type LockDuration = Blocks;
2020
type HtlcLocation = OutPoint;
2121
type HtlcIdentity = KeyPair;
2222
}
2323

24-
pub fn bitcoin_htlc<TL: Ledger, TA: Asset, S: IntoSecretHash>(
25-
swap: &OngoingSwap<Bitcoin, TL, BitcoinQuantity, TA, S>,
26-
) -> Htlc {
27-
Htlc::new(
28-
swap.source_ledger_success_identity,
29-
swap.source_ledger_refund_identity,
30-
swap.secret.clone().into(),
31-
swap.source_ledger_lock_duration.into(),
32-
)
24+
impl From<HtlcParams<Bitcoin, BitcoinQuantity>> for Htlc {
25+
fn from(htlc_params: HtlcParams<Bitcoin, BitcoinQuantity>) -> Self {
26+
Htlc::new(
27+
htlc_params.success_identity,
28+
htlc_params.refund_identity,
29+
htlc_params.secret_hash,
30+
htlc_params.lock_duration.into(),
31+
)
32+
}
3333
}
3434

35-
pub fn bitcoin_htlc_address<TL: Ledger, TA: Asset, S: IntoSecretHash>(
36-
swap: &OngoingSwap<Bitcoin, TL, BitcoinQuantity, TA, S>,
37-
) -> Address {
38-
bitcoin_htlc(swap).compute_address(swap.source_ledger.network)
35+
impl HtlcParams<Bitcoin, BitcoinQuantity> {
36+
pub fn compute_address(&self) -> Address {
37+
Htlc::from(self.clone()).compute_address(self.ledger.network)
38+
}
3939
}

application/comit_node/src/swap_protocols/rfc003/bitcoin/queries.rs

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,44 @@
11
use bitcoin_support::{BitcoinQuantity, OutPoint};
22
use ledger_query_service::BitcoinQuery;
33
use swap_protocols::{
4-
asset::Asset,
54
ledger::Bitcoin,
65
rfc003::{
7-
bitcoin::bitcoin_htlc_address,
8-
events::{
9-
NewSourceHtlcFundedQuery, NewSourceHtlcRedeemedQuery, NewSourceHtlcRefundedQuery,
10-
},
11-
state_machine::OngoingSwap,
12-
IntoSecretHash, Ledger,
6+
events::{NewHtlcFundedQuery, NewHtlcRedeemedQuery, NewHtlcRefundedQuery},
7+
state_machine::HtlcParams,
138
},
149
};
1510

16-
impl<TL, TA, S> NewSourceHtlcFundedQuery<Bitcoin, TL, BitcoinQuantity, TA, S> for BitcoinQuery
17-
where
18-
TL: Ledger,
19-
TA: Asset,
20-
S: IntoSecretHash,
21-
{
22-
fn new_source_htlc_funded_query(
23-
swap: &OngoingSwap<Bitcoin, TL, BitcoinQuantity, TA, S>,
24-
) -> Self {
11+
impl NewHtlcFundedQuery<Bitcoin, BitcoinQuantity> for BitcoinQuery {
12+
fn new_htlc_funded_query(htlc_params: &HtlcParams<Bitcoin, BitcoinQuantity>) -> Self {
2513
BitcoinQuery::Transaction {
26-
to_address: Some(bitcoin_htlc_address(swap)),
14+
to_address: Some(htlc_params.compute_address()),
2715
from_outpoint: None,
2816
unlock_script: None,
2917
}
3018
}
3119
}
3220

33-
impl<TL, TA, S> NewSourceHtlcRefundedQuery<Bitcoin, TL, BitcoinQuantity, TA, S> for BitcoinQuery
34-
where
35-
TL: Ledger,
36-
TA: Asset,
37-
S: IntoSecretHash,
38-
{
39-
fn new_source_htlc_refunded_query(
40-
swap: &OngoingSwap<Bitcoin, TL, BitcoinQuantity, TA, S>,
41-
source_htlc_location: &OutPoint,
21+
impl NewHtlcRefundedQuery<Bitcoin, BitcoinQuantity> for BitcoinQuery {
22+
fn new_htlc_refunded_query(
23+
_htlc_params: &HtlcParams<Bitcoin, BitcoinQuantity>,
24+
htlc_location: &OutPoint,
4225
) -> Self {
4326
BitcoinQuery::Transaction {
4427
to_address: None,
45-
from_outpoint: Some(source_htlc_location.clone()),
46-
unlock_script: Some(vec![
47-
swap.source_ledger_refund_identity
48-
.public_key()
49-
.inner()
50-
.serialize()
51-
.to_vec(),
52-
vec![0u8],
53-
]),
28+
from_outpoint: Some(*htlc_location),
29+
unlock_script: Some(vec![vec![0u8]]),
5430
}
5531
}
5632
}
5733

58-
impl<TL, TA, S> NewSourceHtlcRedeemedQuery<Bitcoin, TL, BitcoinQuantity, TA, S> for BitcoinQuery
59-
where
60-
TL: Ledger,
61-
TA: Asset,
62-
S: IntoSecretHash,
63-
{
64-
fn new_source_htlc_redeemed_query(
65-
_swap: &OngoingSwap<Bitcoin, TL, BitcoinQuantity, TA, S>,
66-
source_htlc_location: &OutPoint,
34+
impl NewHtlcRedeemedQuery<Bitcoin, BitcoinQuantity> for BitcoinQuery {
35+
fn new_htlc_redeemed_query(
36+
_htlc_params: &HtlcParams<Bitcoin, BitcoinQuantity>,
37+
htlc_location: &OutPoint,
6738
) -> Self {
6839
BitcoinQuery::Transaction {
6940
to_address: None,
70-
from_outpoint: Some(source_htlc_location.clone()),
41+
from_outpoint: Some(*htlc_location),
7142
unlock_script: Some(vec![vec![1u8]]),
7243
}
7344
}

0 commit comments

Comments
 (0)