7
7
8
8
use crate :: chain:: ChainSource ;
9
9
use crate :: config:: {
10
- default_user_config, may_announce_channel, AnnounceError , BitcoindRestClientConfig , Config ,
11
- ElectrumSyncConfig , EsploraSyncConfig , DEFAULT_ESPLORA_SERVER_URL , DEFAULT_LOG_FILENAME ,
12
- DEFAULT_LOG_LEVEL , WALLET_KEYS_SEED_LEN ,
10
+ default_user_config, may_announce_channel, AnnounceError , AsyncPaymentsRole ,
11
+ BitcoindRestClientConfig , Config , ElectrumSyncConfig , EsploraSyncConfig ,
12
+ DEFAULT_ESPLORA_SERVER_URL , DEFAULT_LOG_FILENAME , DEFAULT_LOG_LEVEL , WALLET_KEYS_SEED_LEN ,
13
13
} ;
14
14
15
15
use crate :: connection:: ConnectionManager ;
@@ -27,6 +27,7 @@ use crate::liquidity::{
27
27
} ;
28
28
use crate :: logger:: { log_error, LdkLogger , LogLevel , LogWriter , Logger } ;
29
29
use crate :: message_handler:: NodeCustomMessageHandler ;
30
+ use crate :: payment:: asynchronous:: om_mailbox:: OnionMessageMailbox ;
30
31
use crate :: peer_store:: PeerStore ;
31
32
use crate :: runtime:: Runtime ;
32
33
use crate :: tx_broadcaster:: TransactionBroadcaster ;
@@ -191,6 +192,8 @@ pub enum BuildError {
191
192
LoggerSetupFailed ,
192
193
/// The given network does not match the node's previously configured network.
193
194
NetworkMismatch ,
195
+ /// The role of the node in an asynchronous payments context is not compatible with the current configuration.
196
+ AsyncPaymentsConfigMismatch ,
194
197
}
195
198
196
199
impl fmt:: Display for BuildError {
@@ -219,6 +222,12 @@ impl fmt::Display for BuildError {
219
222
Self :: NetworkMismatch => {
220
223
write ! ( f, "Given network does not match the node's previously configured network." )
221
224
} ,
225
+ Self :: AsyncPaymentsConfigMismatch => {
226
+ write ! (
227
+ f,
228
+ "The async payments role is not compatible with the current configuration."
229
+ )
230
+ } ,
222
231
}
223
232
}
224
233
}
@@ -240,6 +249,7 @@ pub struct NodeBuilder {
240
249
gossip_source_config : Option < GossipSourceConfig > ,
241
250
liquidity_source_config : Option < LiquiditySourceConfig > ,
242
251
log_writer_config : Option < LogWriterConfig > ,
252
+ async_payments_role : Option < AsyncPaymentsRole > ,
243
253
runtime_handle : Option < tokio:: runtime:: Handle > ,
244
254
}
245
255
@@ -266,6 +276,7 @@ impl NodeBuilder {
266
276
liquidity_source_config,
267
277
log_writer_config,
268
278
runtime_handle,
279
+ async_payments_role : None ,
269
280
}
270
281
}
271
282
@@ -544,6 +555,21 @@ impl NodeBuilder {
544
555
Ok ( self )
545
556
}
546
557
558
+ /// Sets the role of the node in an asynchronous payments context.
559
+ ///
560
+ /// See <https://github.com/lightning/bolts/pull/1149> for more information about the async payments protocol.
561
+ pub fn set_async_payments_role (
562
+ & mut self , role : Option < AsyncPaymentsRole > ,
563
+ ) -> Result < & mut Self , BuildError > {
564
+ if let Some ( AsyncPaymentsRole :: Server ) = role {
565
+ may_announce_channel ( & self . config )
566
+ . map_err ( |_| BuildError :: AsyncPaymentsConfigMismatch ) ?;
567
+ }
568
+
569
+ self . async_payments_role = role;
570
+ Ok ( self )
571
+ }
572
+
547
573
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
548
574
/// previously configured.
549
575
pub fn build ( & self ) -> Result < Node , BuildError > {
@@ -700,6 +726,7 @@ impl NodeBuilder {
700
726
self . chain_data_source_config . as_ref ( ) ,
701
727
self . gossip_source_config . as_ref ( ) ,
702
728
self . liquidity_source_config . as_ref ( ) ,
729
+ self . async_payments_role ,
703
730
seed_bytes,
704
731
runtime,
705
732
logger,
@@ -732,6 +759,7 @@ impl NodeBuilder {
732
759
self . chain_data_source_config . as_ref ( ) ,
733
760
self . gossip_source_config . as_ref ( ) ,
734
761
self . liquidity_source_config . as_ref ( ) ,
762
+ self . async_payments_role ,
735
763
seed_bytes,
736
764
runtime,
737
765
logger,
@@ -989,6 +1017,13 @@ impl ArcedNodeBuilder {
989
1017
self . inner . write ( ) . unwrap ( ) . set_node_alias ( node_alias) . map ( |_| ( ) )
990
1018
}
991
1019
1020
+ /// Sets the role of the node in an asynchronous payments context.
1021
+ pub fn set_async_payments_role (
1022
+ & self , role : Option < AsyncPaymentsRole > ,
1023
+ ) -> Result < ( ) , BuildError > {
1024
+ self . inner . write ( ) . unwrap ( ) . set_async_payments_role ( role) . map ( |_| ( ) )
1025
+ }
1026
+
992
1027
/// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
993
1028
/// previously configured.
994
1029
pub fn build ( & self ) -> Result < Arc < Node > , BuildError > {
@@ -1082,8 +1117,9 @@ impl ArcedNodeBuilder {
1082
1117
fn build_with_store_internal (
1083
1118
config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
1084
1119
gossip_source_config : Option < & GossipSourceConfig > ,
1085
- liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
1086
- runtime : Arc < Runtime > , logger : Arc < Logger > , kv_store : Arc < DynStore > ,
1120
+ liquidity_source_config : Option < & LiquiditySourceConfig > ,
1121
+ async_payments_role : Option < AsyncPaymentsRole > , seed_bytes : [ u8 ; 64 ] , runtime : Arc < Runtime > ,
1122
+ logger : Arc < Logger > , kv_store : Arc < DynStore > ,
1087
1123
) -> Result < Node , BuildError > {
1088
1124
optionally_install_rustls_cryptoprovider ( ) ;
1089
1125
@@ -1378,8 +1414,14 @@ fn build_with_store_internal(
1378
1414
100 ;
1379
1415
}
1380
1416
1381
- if config. async_payment_services_enabled {
1382
- user_config. accept_forwards_to_priv_channels = true ;
1417
+ if let Some ( role) = async_payments_role {
1418
+ match role {
1419
+ AsyncPaymentsRole :: Server => {
1420
+ user_config. accept_forwards_to_priv_channels = true ;
1421
+ user_config. enable_htlc_hold = true ;
1422
+ } ,
1423
+ AsyncPaymentsRole :: Client => user_config. hold_outbound_htlcs_at_next_hop = true ,
1424
+ }
1383
1425
}
1384
1426
1385
1427
let message_router =
@@ -1452,17 +1494,32 @@ fn build_with_store_internal(
1452
1494
}
1453
1495
1454
1496
// Initialize the PeerManager
1455
- let onion_messenger: Arc < OnionMessenger > = Arc :: new ( OnionMessenger :: new (
1456
- Arc :: clone ( & keys_manager) ,
1457
- Arc :: clone ( & keys_manager) ,
1458
- Arc :: clone ( & logger) ,
1459
- Arc :: clone ( & channel_manager) ,
1460
- message_router,
1461
- Arc :: clone ( & channel_manager) ,
1462
- Arc :: clone ( & channel_manager) ,
1463
- IgnoringMessageHandler { } ,
1464
- IgnoringMessageHandler { } ,
1465
- ) ) ;
1497
+ let onion_messenger: Arc < OnionMessenger > =
1498
+ if let Some ( AsyncPaymentsRole :: Server ) = async_payments_role {
1499
+ Arc :: new ( OnionMessenger :: new_with_offline_peer_interception (
1500
+ Arc :: clone ( & keys_manager) ,
1501
+ Arc :: clone ( & keys_manager) ,
1502
+ Arc :: clone ( & logger) ,
1503
+ Arc :: clone ( & channel_manager) ,
1504
+ message_router,
1505
+ Arc :: clone ( & channel_manager) ,
1506
+ Arc :: clone ( & channel_manager) ,
1507
+ IgnoringMessageHandler { } ,
1508
+ IgnoringMessageHandler { } ,
1509
+ ) )
1510
+ } else {
1511
+ Arc :: new ( OnionMessenger :: new (
1512
+ Arc :: clone ( & keys_manager) ,
1513
+ Arc :: clone ( & keys_manager) ,
1514
+ Arc :: clone ( & logger) ,
1515
+ Arc :: clone ( & channel_manager) ,
1516
+ message_router,
1517
+ Arc :: clone ( & channel_manager) ,
1518
+ Arc :: clone ( & channel_manager) ,
1519
+ IgnoringMessageHandler { } ,
1520
+ IgnoringMessageHandler { } ,
1521
+ ) )
1522
+ } ;
1466
1523
let ephemeral_bytes: [ u8 ; 32 ] = keys_manager. get_secure_random_bytes ( ) ;
1467
1524
1468
1525
// Initialize the GossipSource
@@ -1649,6 +1706,12 @@ fn build_with_store_internal(
1649
1706
} ,
1650
1707
} ;
1651
1708
1709
+ let om_mailbox = if let Some ( AsyncPaymentsRole :: Server ) = async_payments_role {
1710
+ Some ( Arc :: new ( OnionMessageMailbox :: new ( ) ) )
1711
+ } else {
1712
+ None
1713
+ } ;
1714
+
1652
1715
let ( stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
1653
1716
let ( background_processor_stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
1654
1717
let is_running = Arc :: new ( RwLock :: new ( false ) ) ;
@@ -1681,6 +1744,8 @@ fn build_with_store_internal(
1681
1744
is_running,
1682
1745
is_listening,
1683
1746
node_metrics,
1747
+ om_mailbox,
1748
+ async_payments_role,
1684
1749
} )
1685
1750
}
1686
1751
0 commit comments