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

Commit f526d4d

Browse files
Tobin C. HardingTobin Harding
authored andcommitted
Implement HtlcEvents directly on the Facade
Co-authored-by: Tobin Harding <tobin.harding@tenx.tech>
1 parent 2f2d153 commit f526d4d

File tree

11 files changed

+183
-144
lines changed

11 files changed

+183
-144
lines changed

cnd/src/http_api/route_factory.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use crate::{
22
config::settings::AllowedOrigins,
33
db::{DetermineTypes, Retrieve, Saver},
4+
ethereum::{Erc20Token, EtherQuantity},
45
http_api,
56
network::Network,
67
seed::SwapSeed,
7-
swap_protocols::{self, rfc003::state_store::StateStore, LedgerEventsCreator, SwapId},
8+
swap_protocols::{
9+
self,
10+
ledger::{Bitcoin, Ethereum},
11+
rfc003::{events::HtlcEvents, state_store::StateStore},
12+
SwapId,
13+
},
814
};
15+
use bitcoin::Amount;
916
use libp2p::PeerId;
1017
use tokio::executor::Executor;
1118
use warp::{self, filters::BoxedFilter, Filter, Reply};
@@ -28,7 +35,9 @@ pub fn create<
2835
+ SwapSeed
2936
+ DetermineTypes
3037
+ Retrieve
31-
+ LedgerEventsCreator
38+
+ HtlcEvents<Bitcoin, Amount>
39+
+ HtlcEvents<Ethereum, EtherQuantity>
40+
+ HtlcEvents<Ethereum, Erc20Token>
3241
+ Saver,
3342
>(
3443
peer_id: PeerId,

cnd/src/http_api/routes/rfc003/handlers/action.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
22
db::{DetermineTypes, Save, Saver},
3+
ethereum::{Erc20Token, EtherQuantity},
34
http_api::{
45
action::{
56
ActionExecutionParameters, ActionResponseBody, IntoResponsePayload, ListRequiredFields,
@@ -14,17 +15,20 @@ use crate::{
1415
swap_protocols::{
1516
self,
1617
actions::Actions,
18+
ledger::{Bitcoin, Ethereum},
1719
rfc003::{
1820
self,
1921
actions::{Action, ActionKind},
2022
bob::State,
23+
events::HtlcEvents,
2124
messages::{Decision, IntoAcceptMessage},
2225
state_store::StateStore,
2326
},
24-
LedgerEventsCreator, SwapId,
27+
SwapId,
2528
},
2629
};
2730
use anyhow::Context;
31+
use bitcoin::Amount;
2832
use libp2p_comit::frame::Response;
2933
use std::fmt::Debug;
3034
use tokio::executor::Executor;
@@ -37,7 +41,9 @@ pub async fn handle_action<
3741
+ SwapSeed
3842
+ Saver
3943
+ DetermineTypes
40-
+ LedgerEventsCreator
44+
+ HtlcEvents<Bitcoin, Amount>
45+
+ HtlcEvents<Ethereum, EtherQuantity>
46+
+ HtlcEvents<Ethereum, Erc20Token>
4147
+ Executor
4248
+ Clone,
4349
>(

cnd/src/http_api/routes/rfc003/handlers/post_swap.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
use crate::{
22
db::{Save, Saver, Swap},
3-
ethereum,
3+
ethereum::{self, Erc20Token, EtherQuantity},
44
http_api::{HttpAsset, HttpLedger},
55
network::{DialInformation, Network},
66
seed::SwapSeed,
77
swap_protocols::{
88
self,
99
asset::Asset,
10-
ledger,
10+
ledger::{self, Bitcoin, Ethereum},
1111
rfc003::{
12-
self, alice::State, state_store::StateStore, Accept, Decline, Ledger, Request,
13-
SecretHash, SecretSource,
12+
self, alice::State, events::HtlcEvents, state_store::StateStore, Accept, Decline,
13+
Ledger, Request, SecretHash, SecretSource,
1414
},
15-
HashFunction, LedgerEventsCreator, Role, SwapId,
15+
HashFunction, Role, SwapId,
1616
},
1717
timestamp::Timestamp,
18-
CreateLedgerEvents,
1918
};
2019
use anyhow::Context;
20+
use bitcoin::Amount;
2121
use futures::Future;
2222
use futures_core::{
2323
compat::Future01CompatExt,
@@ -36,7 +36,9 @@ pub async fn handle_post_swap<
3636
+ Saver
3737
+ Network
3838
+ Clone
39-
+ LedgerEventsCreator,
39+
+ HtlcEvents<Bitcoin, Amount>
40+
+ HtlcEvents<Ethereum, EtherQuantity>
41+
+ HtlcEvents<Ethereum, Erc20Token>,
4042
>(
4143
dependencies: D,
4244
body: serde_json::Value,
@@ -216,9 +218,8 @@ where
216218
+ Save<Swap>
217219
+ Save<Decline>
218220
+ Network
219-
+ LedgerEventsCreator
220-
+ CreateLedgerEvents<AL, AA>
221-
+ CreateLedgerEvents<BL, BA>
221+
+ HtlcEvents<AL, AA>
222+
+ HtlcEvents<BL, BA>
222223
+ Clone,
223224
AL: Ledger,
224225
BL: Ledger,

cnd/src/http_api/routes/rfc003/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod swap_state;
55

66
use crate::{
77
db::{DetermineTypes, Retrieve, Save, Swap},
8+
ethereum::{Erc20Token, EtherQuantity},
89
http_api::{
910
action::ActionExecutionParameters,
1011
route_factory::swap_path,
@@ -16,10 +17,12 @@ use crate::{
1617
network::Network,
1718
seed::SwapSeed,
1819
swap_protocols::{
19-
rfc003::{actions::ActionKind, state_store::StateStore},
20-
LedgerEventsCreator, SwapId,
20+
ledger::{Bitcoin, Ethereum},
21+
rfc003::{actions::ActionKind, events::HtlcEvents, state_store::StateStore},
22+
SwapId,
2123
},
2224
};
25+
use bitcoin::Amount;
2326
use futures::Future;
2427
use futures_core::future::{FutureExt, TryFutureExt};
2528
use warp::{
@@ -33,7 +36,16 @@ use tokio::executor::Executor;
3336

3437
#[allow(clippy::needless_pass_by_value)]
3538
pub fn post_swap<
36-
D: Clone + Network + StateStore + Executor + Save<Swap> + SwapSeed + Saver + LedgerEventsCreator,
39+
D: Clone
40+
+ Network
41+
+ StateStore
42+
+ Executor
43+
+ Save<Swap>
44+
+ SwapSeed
45+
+ Saver
46+
+ HtlcEvents<Bitcoin, Amount>
47+
+ HtlcEvents<Ethereum, EtherQuantity>
48+
+ HtlcEvents<Ethereum, Erc20Token>,
3749
>(
3850
dependencies: D,
3951
body: serde_json::Value,
@@ -74,7 +86,9 @@ pub fn action<
7486
+ Network
7587
+ SwapSeed
7688
+ Saver
77-
+ LedgerEventsCreator,
89+
+ HtlcEvents<Bitcoin, Amount>
90+
+ HtlcEvents<Ethereum, EtherQuantity>
91+
+ HtlcEvents<Ethereum, Erc20Token>,
7892
>(
7993
method: http::Method,
8094
id: SwapId,

cnd/src/load_swaps.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
swap_protocols::{
77
self,
88
ledger::{Bitcoin, Ethereum},
9-
rfc003::state_store::StateStore,
10-
LedgerEventsCreator,
9+
rfc003::{events::HtlcEvents, state_store::StateStore},
1110
},
1211
};
12+
use bitcoin::Amount;
1313
use tokio::executor::Executor;
1414

1515
#[allow(clippy::cognitive_complexity)]
@@ -19,9 +19,11 @@ where
1919
+ Executor
2020
+ Clone
2121
+ SwapSeed
22-
+ LedgerEventsCreator
2322
+ Retrieve
2423
+ DetermineTypes
24+
+ HtlcEvents<Bitcoin, Amount>
25+
+ HtlcEvents<Ethereum, EtherQuantity>
26+
+ HtlcEvents<Ethereum, Erc20Token>
2527
+ LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, EtherQuantity>
2628
+ LoadAcceptedSwap<Ethereum, Bitcoin, EtherQuantity, bitcoin::Amount>
2729
+ LoadAcceptedSwap<Bitcoin, Ethereum, bitcoin::Amount, Erc20Token>

cnd/src/main.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ use anyhow::Context;
55
use cnd::{
66
btsieve::{bitcoin::BitcoindConnector, ethereum::Web3Connector},
77
config::{self, Settings},
8-
db::{DetermineTypes, Retrieve, Saver, Sqlite},
8+
db::Sqlite,
99
http_api::route_factory,
1010
load_swaps,
1111
network::{self, transport, Network},
12-
seed::{Seed, SwapSeed},
13-
swap_protocols::{
14-
rfc003::state_store::{InMemoryStateStore, StateStore},
15-
Facade, LedgerEventsCreator,
16-
},
12+
seed::Seed,
13+
swap_protocols::{rfc003::state_store::InMemoryStateStore, Facade},
1714
};
1815
use futures::{stream, Future, Stream};
1916
use futures_core::{FutureExt, TryFutureExt};
@@ -28,7 +25,6 @@ use std::{
2825
sync::{Arc, Mutex},
2926
};
3027
use structopt::StructOpt;
31-
use tokio::executor::Executor;
3228

3329
mod cli;
3430
mod logging;
@@ -121,21 +117,11 @@ fn derive_key_pair(seed: &Seed) -> identity::Keypair {
121117
identity::Keypair::Ed25519(key.into())
122118
}
123119

124-
fn spawn_warp_instance<
125-
D: Clone
126-
+ StateStore
127-
+ Executor
128-
+ Network
129-
+ SwapSeed
130-
+ DetermineTypes
131-
+ Retrieve
132-
+ LedgerEventsCreator
133-
+ Saver,
134-
>(
120+
fn spawn_warp_instance<S: Network>(
135121
settings: &Settings,
136122
peer_id: PeerId,
137123
runtime: &mut tokio::runtime::Runtime,
138-
dependencies: D,
124+
dependencies: Facade<S>,
139125
) {
140126
let routes = route_factory::create(
141127
peer_id,

cnd/src/swap_protocols/facade.rs

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ use crate::{
44
AcceptedSwap, DetermineTypes, LoadAcceptedSwap, Retrieve, Save, Saver, Sqlite, Swap,
55
SwapTypes,
66
},
7-
ethereum::{Erc20Token, EtherQuantity},
87
network::{DialInformation, Network, RequestError},
98
seed::{Seed, SwapSeed},
109
swap_protocols::{
1110
asset::Asset,
1211
ledger::{Bitcoin, Ethereum},
1312
rfc003::{
1413
self,
15-
events::HtlcEvents,
16-
state_machine::SwapStates,
14+
events::{
15+
Deployed, DeployedFuture, Funded, FundedFuture, HtlcEvents,
16+
RedeemedOrRefundedFuture,
17+
},
18+
state_machine::{HtlcParams, SwapStates},
1719
state_store::{self, InMemoryStateStore, StateStore},
1820
ActorState, Ledger,
1921
},
2022
SwapId,
2123
},
22-
CreateLedgerEvents,
2324
};
2425
use async_trait::async_trait;
2526
use bitcoin::Amount;
@@ -168,32 +169,67 @@ where
168169
}
169170
}
170171

171-
pub trait LedgerEventsCreator:
172-
CreateLedgerEvents<Bitcoin, Amount>
173-
+ CreateLedgerEvents<Ethereum, EtherQuantity>
174-
+ CreateLedgerEvents<Ethereum, Erc20Token>
175-
{
176-
}
177-
178-
impl<S> LedgerEventsCreator for Facade<S> where S: Send + Sync + 'static {}
179-
180-
impl<S> CreateLedgerEvents<Bitcoin, Amount> for Facade<S>
172+
impl<S> HtlcEvents<Bitcoin, Amount> for Facade<S>
181173
where
182174
S: Send + Sync + 'static,
183175
{
184-
fn create_ledger_events(&self) -> Box<dyn HtlcEvents<Bitcoin, Amount>> {
185-
Box::new(self.bitcoin_connector.clone())
176+
fn htlc_deployed(
177+
&self,
178+
htlc_params: HtlcParams<Bitcoin, Amount>,
179+
) -> Box<DeployedFuture<Bitcoin>> {
180+
self.bitcoin_connector.htlc_deployed(htlc_params)
181+
}
182+
183+
fn htlc_funded(
184+
&self,
185+
htlc_params: HtlcParams<Bitcoin, Amount>,
186+
htlc_deployment: &Deployed<Bitcoin>,
187+
) -> Box<FundedFuture<Bitcoin, Amount>> {
188+
self.bitcoin_connector
189+
.htlc_funded(htlc_params, htlc_deployment)
190+
}
191+
192+
fn htlc_redeemed_or_refunded(
193+
&self,
194+
htlc_params: HtlcParams<Bitcoin, Amount>,
195+
htlc_deployment: &Deployed<Bitcoin>,
196+
htlc_funding: &Funded<Bitcoin, Amount>,
197+
) -> Box<RedeemedOrRefundedFuture<Bitcoin>> {
198+
self.bitcoin_connector
199+
.htlc_redeemed_or_refunded(htlc_params, htlc_deployment, htlc_funding)
186200
}
187201
}
188202

189-
impl<S, A> CreateLedgerEvents<Ethereum, A> for Facade<S>
203+
impl<A, S> HtlcEvents<Ethereum, A> for Facade<S>
190204
where
191205
S: Send + Sync + 'static,
192206
A: Asset + Send + Sync + 'static,
193207
Web3Connector: HtlcEvents<Ethereum, A>,
194208
{
195-
fn create_ledger_events(&self) -> Box<dyn HtlcEvents<Ethereum, A>> {
196-
Box::new(self.ethereum_connector.clone())
209+
fn htlc_deployed(&self, htlc_params: HtlcParams<Ethereum, A>) -> Box<DeployedFuture<Ethereum>> {
210+
self.ethereum_connector.htlc_deployed(htlc_params)
211+
}
212+
213+
fn htlc_funded(
214+
&self,
215+
htlc_params: HtlcParams<Ethereum, A>,
216+
htlc_deployment: &Deployed<Ethereum>,
217+
) -> Box<FundedFuture<Ethereum, A>> {
218+
self.ethereum_connector
219+
.htlc_funded(htlc_params, htlc_deployment)
220+
}
221+
222+
fn htlc_redeemed_or_refunded(
223+
&self,
224+
htlc_params: HtlcParams<Ethereum, A>,
225+
htlc_deployment: &Deployed<Ethereum>,
226+
htlc_funding: &Funded<Ethereum, A>,
227+
) -> Box<RedeemedOrRefundedFuture<Ethereum>> {
228+
self.ethereum_connector.htlc_redeemed_or_refunded(
229+
htlc_params,
230+
htlc_deployment,
231+
htlc_funding,
232+
)
197233
}
198234
}
199235

0 commit comments

Comments
 (0)