Skip to content

Commit 46d2ccd

Browse files
Aditya SharmaAditya Sharma
authored andcommitted
channelmonitor: Create new_stub so that channels can be stubbed to recover from peer storage.
1 parent d754e7d commit 46d2ccd

File tree

1 file changed

+102
-3
lines changed

1 file changed

+102
-3
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use crate::ln::channel::INITIAL_COMMITMENT_NUMBER;
3737
use crate::ln::types::{PaymentHash, PaymentPreimage, ChannelId};
3838
use crate::ln::msgs::DecodeError;
3939
use crate::ln::channel_keys::{DelayedPaymentKey, DelayedPaymentBasepoint, HtlcBasepoint, HtlcKey, RevocationKey, RevocationBasepoint};
40-
use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys};
41-
use crate::ln::channelmanager::{HTLCSource, SentHTLCId};
40+
use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys, CounterpartyChannelTransactionParameters};
41+
use crate::ln::channelmanager::{HTLCSource, SentHTLCId, StubChannel};
42+
use crate::ln::features::ChannelTypeFeatures;
4243
use crate::chain;
4344
use crate::chain::{BestBlock, WatchedOutput};
4445
use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoundedFeeEstimator};
@@ -1346,6 +1347,103 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
13461347
})
13471348
}
13481349

1350+
pub(crate) fn new_stub(secp_ctx: Secp256k1<secp256k1::All>, stub_channel: &StubChannel, best_block: BestBlock, keys: Signer, funding_info_scriptbuf: ScriptBuf) -> ChannelMonitor<Signer> {
1351+
let mut outputs_to_watch = new_hash_map();
1352+
outputs_to_watch.insert(stub_channel.funding_outpoint.get_txid(), vec![(stub_channel.funding_outpoint.index as u32, funding_info_scriptbuf.clone())]);
1353+
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
1354+
let dummy_sig = crate::crypto::utils::sign(&secp_ctx, &secp256k1::Message::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap());
1355+
1356+
let holder_commitment_tx = HolderSignedTx {
1357+
txid: stub_channel.funding_outpoint.get_txid(),
1358+
revocation_key: RevocationKey(dummy_key),
1359+
a_htlc_key: HtlcKey(dummy_key),
1360+
b_htlc_key: HtlcKey(dummy_key),
1361+
delayed_payment_key: DelayedPaymentKey(dummy_key),
1362+
per_commitment_point: dummy_key,
1363+
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
1364+
to_self_value_sat: 0,
1365+
feerate_per_kw: 1,
1366+
};
1367+
1368+
let channel_parameters = ChannelTransactionParameters{
1369+
holder_pubkeys:keys.pubkeys().clone(),
1370+
is_outbound_from_holder: true,
1371+
holder_selected_contest_delay: 66,
1372+
counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: keys.pubkeys().clone(), selected_contest_delay: 0 }),
1373+
funding_outpoint: Some(stub_channel.funding_outpoint),
1374+
channel_type_features: ChannelTypeFeatures::only_static_remote_key(),
1375+
};
1376+
1377+
let dummy_tx_creation_keys = TxCreationKeys {
1378+
per_commitment_point: dummy_key.clone(),
1379+
revocation_key: RevocationKey::from_basepoint(&secp_ctx, &RevocationBasepoint::from(dummy_key), &dummy_key),
1380+
broadcaster_htlc_key: HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(dummy_key), &dummy_key),
1381+
countersignatory_htlc_key: HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(dummy_key), &dummy_key),
1382+
broadcaster_delayed_payment_key: DelayedPaymentKey::from_basepoint(&secp_ctx, &DelayedPaymentBasepoint::from(dummy_key), &dummy_key),
1383+
};
1384+
let counterparty_htlc_sigs = Vec::new();
1385+
let mut nondust_htlcs: Vec<(HTLCOutputInCommitment, Option<Box<HTLCSource>>)> = Vec::new();
1386+
let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, dummy_key.clone(), dummy_key.clone(), dummy_tx_creation_keys, 0, &mut nondust_htlcs, &channel_parameters.as_counterparty_broadcastable());
1387+
let holder_commitment = HolderCommitmentTransaction::new(inner, dummy_sig, counterparty_htlc_sigs, &dummy_key, &PublicKey::from_slice(&[2;33]).unwrap());
1388+
1389+
let onchain_tx_handler = OnchainTxHandler::new(
1390+
1000, stub_channel.channel_keys_id, ScriptBuf::new(), keys,
1391+
channel_parameters, holder_commitment, secp_ctx
1392+
);
1393+
1394+
Self::from_impl(ChannelMonitorImpl{
1395+
latest_update_id: CLOSED_CHANNEL_UPDATE_ID,
1396+
commitment_transaction_number_obscure_factor: 0,
1397+
destination_script: ScriptBuf::new(),
1398+
broadcasted_holder_revokable_script: None,
1399+
counterparty_payment_script: ScriptBuf::new(),
1400+
shutdown_script: None,
1401+
channel_keys_id: stub_channel.channel_keys_id,
1402+
holder_revocation_basepoint: RevocationBasepoint(PublicKey::from_slice(&[2;33]).unwrap()),
1403+
channel_id: stub_channel.channel_id,
1404+
funding_info: (stub_channel.funding_outpoint, ScriptBuf::new()),
1405+
current_counterparty_commitment_txid: None,
1406+
prev_counterparty_commitment_txid: None,
1407+
counterparty_commitment_params: CounterpartyCommitmentParameters {
1408+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint(PublicKey::from_slice(&[2;33]).unwrap()),
1409+
counterparty_htlc_base_key: HtlcBasepoint(PublicKey::from_slice(&[2;33]).unwrap()),
1410+
on_counterparty_tx_csv: 0,
1411+
},
1412+
funding_redeemscript: ScriptBuf::new(),
1413+
channel_value_satoshis: 0,
1414+
their_cur_per_commitment_points: None,
1415+
on_holder_tx_csv: 1,
1416+
commitment_secrets: stub_channel.commitment_secrets.clone(),
1417+
counterparty_claimable_outpoints: new_hash_map(),
1418+
counterparty_hash_commitment_number: new_hash_map(),
1419+
counterparty_commitment_txn_on_chain: new_hash_map(),
1420+
counterparty_fulfilled_htlcs: new_hash_map(),
1421+
prev_holder_signed_commitment_tx: None,
1422+
current_holder_commitment_tx: holder_commitment_tx,
1423+
current_counterparty_commitment_number: 1 << 48,
1424+
current_holder_commitment_number: 1,
1425+
payment_preimages: new_hash_map(),
1426+
peer_storage: Vec::new(),
1427+
pending_monitor_events: Vec::new(),
1428+
pending_events: Vec::new(),
1429+
is_processing_pending_events: false,
1430+
onchain_events_awaiting_threshold_conf: Vec::new(),
1431+
outputs_to_watch,
1432+
onchain_tx_handler,
1433+
lockdown_from_offchain: true,
1434+
holder_tx_signed: true,
1435+
funding_spend_seen: false,
1436+
funding_spend_confirmed: None,
1437+
confirmed_commitment_tx_counterparty_output: None,
1438+
htlcs_resolved_on_chain: Vec::new(),
1439+
spendable_txids_confirmed: Vec::new(),
1440+
best_block,
1441+
counterparty_node_id: Some(stub_channel.counterparty_node_id),
1442+
initial_counterparty_commitment_info: None,
1443+
balances_empty_height: None
1444+
})
1445+
}
1446+
13491447
#[cfg(test)]
13501448
fn provide_secret(&self, idx: u64, secret: [u8; 32]) -> Result<(), &'static str> {
13511449
self.inner.lock().unwrap().provide_secret(idx, secret)
@@ -2833,6 +2931,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
28332931

28342932
fn update_peer_storage(&mut self, new_data: Vec<u8>) {
28352933
self.peer_storage = new_data;
2934+
return;
28362935
}
28372936

28382937
fn generate_claimable_outpoints_and_watch_outputs(&mut self, reason: ClosureReason) -> (Vec<PackageTemplate>, Vec<TransactionOutputs>) {
@@ -3012,7 +3111,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
30123111
},
30133112
ChannelMonitorUpdateStep::LatestPeerStorage { data } => {
30143113
log_trace!(logger, "Updating ChannelMonitor with latest recieved PeerStorage");
3015-
self.update_peer_storage(data.clone());
3114+
self.update_peer_storage(data.clone())
30163115
}
30173116
}
30183117
}

0 commit comments

Comments
 (0)