Skip to content

Commit b28bf0b

Browse files
committed
Add test for coinbase funding transactions
1 parent 9a37135 commit b28bf0b

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::chain::transaction::OutPoint;
2020
use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource, SignerProvider};
2121
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
2222
use crate::ln::{ChannelId, PaymentPreimage, PaymentSecret, PaymentHash};
23-
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel};
23+
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY};
2424
use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
2525
use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError};
2626
use crate::ln::{chan_utils, onion_utils};
@@ -9133,6 +9133,66 @@ fn test_invalid_funding_tx() {
91339133
mine_transaction(&nodes[1], &spend_tx);
91349134
}
91359135

9136+
#[test]
9137+
fn test_coinbase_funding_tx() {
9138+
// Miners are able to fund channels directly from coinbase transactions, however
9139+
// by consensus rules, outputs of a coinbase transaction are encumbered by a 100
9140+
// block maturity timelock. To ensure that a (non-0conf) channel like this is enforceable
9141+
// on-chain, the minimum depth is updated to 100 blocks for coinbase funding transactions.
9142+
//
9143+
// Note that 0conf channels with coinbase funding transactions are unaffected and are
9144+
// immediately operational after opening.
9145+
let chanmon_cfgs = create_chanmon_cfgs(2);
9146+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9147+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
9148+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9149+
9150+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
9151+
let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9152+
9153+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
9154+
let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9155+
9156+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
9157+
9158+
// Create the coinbase funding transaction.
9159+
let (temporary_channel_id, tx, _) = create_coinbase_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
9160+
9161+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
9162+
check_added_monitors!(nodes[0], 0);
9163+
let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
9164+
9165+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
9166+
check_added_monitors!(nodes[1], 1);
9167+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
9168+
9169+
let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
9170+
9171+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
9172+
check_added_monitors!(nodes[0], 1);
9173+
9174+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
9175+
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
9176+
9177+
// Starting at height 0, we "confirm" the coinbase at height 1.
9178+
confirm_transaction_at(&nodes[0], &tx, 1);
9179+
// We connect 98 more blocks to have 99 confirmations for the coinbase transaction.
9180+
connect_blocks(&nodes[0], COINBASE_MATURITY - 2);
9181+
// Check that we have no pending message events (we have not queued a `channel_ready` yet).
9182+
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
9183+
// Now connect one more block which results in 100 confirmations of the coinbase transaction.
9184+
connect_blocks(&nodes[0], 1);
9185+
// There should now be a `channel_ready` which can be handled.
9186+
let _ = &nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &get_event_msg!(&nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id()));
9187+
9188+
confirm_transaction_at(&nodes[1], &tx, 1);
9189+
connect_blocks(&nodes[1], COINBASE_MATURITY - 2);
9190+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
9191+
connect_blocks(&nodes[1], 1);
9192+
expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
9193+
create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]);
9194+
}
9195+
91369196
fn do_test_tx_confirmed_skipping_blocks_immediate_broadcast(test_height_before_timelock: bool) {
91379197
// In the first version of the chain::Confirm interface, after a refactor was made to not
91389198
// broadcast CSV-locked transactions until their CSV lock is up, we wouldn't reliably broadcast

0 commit comments

Comments
 (0)