@@ -1291,4 +1291,67 @@ mod tests {
1291
1291
Err ( ForwardingError :: InsufficientBalance ( _, _) )
1292
1292
) ) ;
1293
1293
}
1294
+
1295
+ /// Tests basic functionality of a `SimulatedChannel` but does no endeavor to test the underlying
1296
+ /// `ChannelState`, as this is covered elsewhere in our tests.
1297
+ #[ test]
1298
+ fn test_simulated_channel ( ) {
1299
+ // Create a test channel with all balance available to node 1 as local liquidity, and none for node_2 to begin
1300
+ // with.
1301
+ let capacity_msat = 500_000_000 ;
1302
+ let node_1 = ChannelState :: new ( create_test_policy ( capacity_msat / 2 ) , capacity_msat) ;
1303
+ let node_2 = ChannelState :: new ( create_test_policy ( capacity_msat / 2 ) , 0 ) ;
1304
+
1305
+ let mut simulated_channel = SimulatedChannel {
1306
+ capacity_msat,
1307
+ short_channel_id : ShortChannelID :: from ( 123 ) ,
1308
+ node_1 : node_1. clone ( ) ,
1309
+ node_2 : node_2. clone ( ) ,
1310
+ } ;
1311
+
1312
+ // Assert that we're not able to send a htlc over node_2 -> node_1 (no liquidity).
1313
+ let hash_1 = PaymentHash { 0 : [ 1 ; 32 ] } ;
1314
+ let htlc_1 = Htlc {
1315
+ amount_msat : node_2. policy . min_htlc_size_msat ,
1316
+ cltv_expiry : node_1. policy . cltv_expiry_delta ,
1317
+ } ;
1318
+
1319
+ assert ! ( matches!(
1320
+ simulated_channel. add_htlc( & node_2. policy. pubkey, hash_1, htlc_1) ,
1321
+ Err ( ForwardingError :: InsufficientBalance ( _, _) )
1322
+ ) ) ;
1323
+
1324
+ // Assert that we can send a htlc over node_1 -> node_2.
1325
+ let hash_2 = PaymentHash { 0 : [ 1 ; 32 ] } ;
1326
+ let htlc_2 = Htlc {
1327
+ amount_msat : node_1. policy . max_htlc_size_msat ,
1328
+ cltv_expiry : node_2. policy . cltv_expiry_delta ,
1329
+ } ;
1330
+ assert ! ( simulated_channel
1331
+ . add_htlc( & node_1. policy. pubkey, hash_2, htlc_2)
1332
+ . is_ok( ) ) ;
1333
+
1334
+ // Settle the htlc and then assert that we can send from node_2 -> node_2 because the balance has been shifted
1335
+ // across channels.
1336
+ assert ! ( simulated_channel
1337
+ . remove_htlc( & node_1. policy. pubkey, & hash_2, true )
1338
+ . is_ok( ) ) ;
1339
+
1340
+ assert ! ( simulated_channel
1341
+ . add_htlc( & node_2. policy. pubkey, hash_2, htlc_2)
1342
+ . is_ok( ) ) ;
1343
+
1344
+ // Finally, try to add/remove htlcs for a pubkey that is not participating in the channel and assert that we
1345
+ // fail.
1346
+ let ( _, pk) = get_random_keypair ( ) ;
1347
+ assert ! ( matches!(
1348
+ simulated_channel. add_htlc( & pk, hash_2, htlc_2) ,
1349
+ Err ( ForwardingError :: NodeNotFound ( _) )
1350
+ ) ) ;
1351
+
1352
+ assert ! ( matches!(
1353
+ simulated_channel. remove_htlc( & pk, & hash_2, true ) ,
1354
+ Err ( ForwardingError :: NodeNotFound ( _) )
1355
+ ) ) ;
1356
+ }
1294
1357
}
0 commit comments