@@ -925,6 +925,148 @@ async fn concurrent_connections_succeed() {
925925 }
926926}
927927
928+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
929+ async fn splice_channel ( ) {
930+ macro_rules! expect_splice_pending_event {
931+ ( $node: expr, $counterparty_node_id: expr) => { {
932+ match $node. next_event_async( ) . await {
933+ ref e @ Event :: SplicePending { new_funding_txo, counterparty_node_id, .. } => {
934+ println!( "{} got event {:?}" , $node. node_id( ) , e) ;
935+ assert_eq!( counterparty_node_id, $counterparty_node_id) ;
936+ $node. event_handled( ) . unwrap( ) ;
937+ new_funding_txo
938+ } ,
939+ ref e => {
940+ panic!( "{} got unexpected event!: {:?}" , std:: stringify!( $node) , e) ;
941+ } ,
942+ }
943+ } } ;
944+ }
945+
946+ let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
947+ let chain_source = TestChainSource :: Esplora ( & electrsd) ;
948+ let ( node_a, node_b) = setup_two_nodes ( & chain_source, false , true , false ) ;
949+
950+ let address_a = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
951+ let address_b = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
952+ let premine_amount_sat = 5_000_000 ;
953+ premine_and_distribute_funds (
954+ & bitcoind. client ,
955+ & electrsd. client ,
956+ vec ! [ address_a, address_b] ,
957+ Amount :: from_sat ( premine_amount_sat) ,
958+ )
959+ . await ;
960+
961+ node_a. sync_wallets ( ) . unwrap ( ) ;
962+ node_b. sync_wallets ( ) . unwrap ( ) ;
963+
964+ assert_eq ! ( node_a. list_balances( ) . total_onchain_balance_sats, premine_amount_sat) ;
965+ assert_eq ! ( node_b. list_balances( ) . total_onchain_balance_sats, premine_amount_sat) ;
966+
967+ open_channel ( & node_a, & node_b, 4_000_000 , false , & electrsd) . await ;
968+
969+ // Open a channel with Node A contributing the funding
970+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) . await ;
971+
972+ node_a. sync_wallets ( ) . unwrap ( ) ;
973+ node_b. sync_wallets ( ) . unwrap ( ) ;
974+
975+ let user_channel_id_a = expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
976+ let user_channel_id_b = expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
977+
978+ let opening_transaction_fee_sat = 156 ;
979+ let closing_transaction_fee_sat = 614 ;
980+ let anchor_output_sat = 330 ;
981+
982+ assert_eq ! (
983+ node_a. list_balances( ) . total_onchain_balance_sats,
984+ premine_amount_sat - 4_000_000 - opening_transaction_fee_sat
985+ ) ;
986+ assert_eq ! (
987+ node_a. list_balances( ) . total_lightning_balance_sats,
988+ 4_000_000 - closing_transaction_fee_sat - anchor_output_sat
989+ ) ;
990+ assert_eq ! ( node_b. list_balances( ) . total_lightning_balance_sats, 0 ) ;
991+
992+ // Test that splicing and payments fail when there are insufficient funds
993+ let address = node_b. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
994+ let amount_msat = 400_000_000 ;
995+
996+ assert_eq ! (
997+ node_b. splice_in( & user_channel_id_a, node_b. node_id( ) , 5_000_000 ) ,
998+ Err ( NodeError :: ChannelSplicingFailed ) ,
999+ ) ;
1000+ assert_eq ! (
1001+ node_b. splice_out( & user_channel_id_a, node_b. node_id( ) , & address, amount_msat / 1000 ) ,
1002+ Err ( NodeError :: ChannelSplicingFailed ) ,
1003+ ) ;
1004+ assert_eq ! (
1005+ node_b. spontaneous_payment( ) . send( amount_msat, node_a. node_id( ) , None ) ,
1006+ Err ( NodeError :: PaymentSendingFailed )
1007+ ) ;
1008+
1009+ // Splice-in funds for Node B so that it has outbound liquidity to make a payment
1010+ node_b. splice_in ( & user_channel_id_b, node_a. node_id ( ) , 4_000_000 ) . unwrap ( ) ;
1011+
1012+ expect_splice_pending_event ! ( node_a, node_b. node_id( ) ) ;
1013+ expect_splice_pending_event ! ( node_b, node_a. node_id( ) ) ;
1014+
1015+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) . await ;
1016+
1017+ node_a. sync_wallets ( ) . unwrap ( ) ;
1018+ node_b. sync_wallets ( ) . unwrap ( ) ;
1019+
1020+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
1021+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
1022+
1023+ let splice_in_fee_sat = 253 ;
1024+
1025+ assert_eq ! (
1026+ node_b. list_balances( ) . total_onchain_balance_sats,
1027+ premine_amount_sat - 4_000_000 - splice_in_fee_sat
1028+ ) ;
1029+ assert_eq ! ( node_b. list_balances( ) . total_lightning_balance_sats, 4_000_000 ) ;
1030+
1031+ let payment_id =
1032+ node_b. spontaneous_payment ( ) . send ( amount_msat, node_a. node_id ( ) , None ) . unwrap ( ) ;
1033+
1034+ expect_payment_successful_event ! ( node_b, Some ( payment_id) , None ) ;
1035+ expect_payment_received_event ! ( node_a, amount_msat) ;
1036+
1037+ assert_eq ! (
1038+ node_a. list_balances( ) . total_lightning_balance_sats,
1039+ 4_000_000 - closing_transaction_fee_sat - anchor_output_sat + amount_msat / 1000
1040+ ) ;
1041+ assert_eq ! ( node_b. list_balances( ) . total_lightning_balance_sats, 4_000_000 - amount_msat / 1000 ) ;
1042+
1043+ // Splice-out funds for Node A from the payment sent by Node B
1044+ let address = node_a. onchain_payment ( ) . new_address ( ) . unwrap ( ) ;
1045+ node_a. splice_out ( & user_channel_id_a, node_b. node_id ( ) , & address, amount_msat / 1000 ) . unwrap ( ) ;
1046+
1047+ expect_splice_pending_event ! ( node_a, node_b. node_id( ) ) ;
1048+ expect_splice_pending_event ! ( node_b, node_a. node_id( ) ) ;
1049+
1050+ generate_blocks_and_wait ( & bitcoind. client , & electrsd. client , 6 ) . await ;
1051+
1052+ node_a. sync_wallets ( ) . unwrap ( ) ;
1053+ node_b. sync_wallets ( ) . unwrap ( ) ;
1054+
1055+ expect_channel_ready_event ! ( node_a, node_b. node_id( ) ) ;
1056+ expect_channel_ready_event ! ( node_b, node_a. node_id( ) ) ;
1057+
1058+ let splice_out_fee_sat = 184 ;
1059+
1060+ assert_eq ! (
1061+ node_a. list_balances( ) . total_onchain_balance_sats,
1062+ premine_amount_sat - 4_000_000 - opening_transaction_fee_sat + amount_msat / 1000
1063+ ) ;
1064+ assert_eq ! (
1065+ node_a. list_balances( ) . total_lightning_balance_sats,
1066+ 4_000_000 - closing_transaction_fee_sat - anchor_output_sat - splice_out_fee_sat
1067+ ) ;
1068+ }
1069+
9281070#[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
9291071async fn simple_bolt12_send_receive ( ) {
9301072 let ( bitcoind, electrsd) = setup_bitcoind_and_electrsd ( ) ;
0 commit comments