Skip to content

Commit 5588eeb

Browse files
authored
Merge pull request #1867 from wpaulino/remove-signer-persistence
Re-derive signers instead of persisting them
2 parents f4ab077 + 444fce7 commit 5588eeb

16 files changed

+262
-208
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,14 @@ impl KeysInterface for KeyProvider {
193193
ShutdownScript::new_p2wpkh(&pubkey_hash)
194194
}
195195

196-
fn get_channel_signer(&self, _inbound: bool, channel_value_satoshis: u64) -> EnforcingSigner {
196+
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
197+
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed) as u8;
198+
[id; 32]
199+
}
200+
201+
fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> Self::Signer {
197202
let secp_ctx = Secp256k1::signing_only();
198-
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
203+
let id = channel_keys_id[0];
199204
let keys = InMemorySigner::new(
200205
&secp_ctx,
201206
self.get_node_secret(Recipient::Node).unwrap(),
@@ -204,9 +209,9 @@ impl KeysInterface for KeyProvider {
204209
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
205210
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
206211
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
207-
[id as u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
212+
[id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
208213
channel_value_satoshis,
209-
[0; 32],
214+
channel_keys_id,
210215
);
211216
let revoked_commitment = self.make_enforcement_state_cell(keys.commitment_seed);
212217
EnforcingSigner::new_with_revoked(keys, revoked_commitment, false)

fuzz/src/full_stack.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ struct KeyProvider {
263263
node_secret: SecretKey,
264264
inbound_payment_key: KeyMaterial,
265265
counter: AtomicU64,
266+
signer_state: RefCell<HashMap<u8, (bool, Arc<Mutex<EnforcementState>>)>>
266267
}
267268
impl KeysInterface for KeyProvider {
268269
type Signer = EnforcingSigner;
@@ -297,10 +298,17 @@ impl KeysInterface for KeyProvider {
297298
ShutdownScript::new_p2wpkh(&pubkey_hash)
298299
}
299300

300-
fn get_channel_signer(&self, inbound: bool, channel_value_satoshis: u64) -> EnforcingSigner {
301+
fn generate_channel_keys_id(&self, inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] {
301302
let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
303+
self.signer_state.borrow_mut().insert(ctr, (inbound, Arc::new(Mutex::new(EnforcementState::new()))));
304+
[ctr; 32]
305+
}
306+
307+
fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> Self::Signer {
302308
let secp_ctx = Secp256k1::signing_only();
303-
EnforcingSigner::new(if inbound {
309+
let ctr = channel_keys_id[0];
310+
let (inbound, state) = self.signer_state.borrow().get(&ctr).unwrap().clone();
311+
EnforcingSigner::new_with_revoked(if inbound {
304312
InMemorySigner::new(
305313
&secp_ctx,
306314
self.node_secret.clone(),
@@ -311,7 +319,7 @@ impl KeysInterface for KeyProvider {
311319
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
312320
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
313321
channel_value_satoshis,
314-
[0; 32],
322+
channel_keys_id,
315323
)
316324
} else {
317325
InMemorySigner::new(
@@ -324,9 +332,9 @@ impl KeysInterface for KeyProvider {
324332
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
325333
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
326334
channel_value_satoshis,
327-
[0; 32],
335+
channel_keys_id,
328336
)
329-
})
337+
}, state, false)
330338
}
331339

332340
fn get_secure_random_bytes(&self) -> [u8; 32] {
@@ -390,7 +398,12 @@ pub fn do_test(data: &[u8], logger: &Arc<dyn Logger>) {
390398
let monitor = Arc::new(chainmonitor::ChainMonitor::new(None, broadcast.clone(), Arc::clone(&logger), fee_est.clone(),
391399
Arc::new(TestPersister { update_ret: Mutex::new(ChannelMonitorUpdateStatus::Completed) })));
392400

393-
let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone(), inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()), counter: AtomicU64::new(0) });
401+
let keys_manager = Arc::new(KeyProvider {
402+
node_secret: our_network_key.clone(),
403+
inbound_payment_key: KeyMaterial(inbound_payment_key.try_into().unwrap()),
404+
counter: AtomicU64::new(0),
405+
signer_state: RefCell::new(HashMap::new())
406+
});
394407
let mut config = UserConfig::default();
395408
config.channel_config.forwarding_fee_proportional_millionths = slice_to_be32(get_slice!(4));
396409
config.channel_handshake_config.announced_channel = get_slice!(1)[0] != 0;

fuzz/src/onion_message.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ impl KeysInterface for KeyProvider {
111111

112112
fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!() }
113113

114-
fn get_channel_signer(&self, _inbound: bool, _channel_value_satoshis: u64) -> EnforcingSigner {
114+
fn generate_channel_keys_id(&self, _inbound: bool, _channel_value_satoshis: u64, _user_channel_id: u128) -> [u8; 32] { unreachable!() }
115+
116+
fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer {
115117
unreachable!()
116118
}
117119

lightning/src/chain/channelmonitor.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ struct CounterpartyCommitmentParameters {
291291

292292
impl Writeable for CounterpartyCommitmentParameters {
293293
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
294-
w.write_all(&byte_utils::be64_to_array(0))?;
294+
w.write_all(&(0 as u64).to_be_bytes())?;
295295
write_tlv_fields!(w, {
296296
(0, self.counterparty_delayed_payment_base_key, required),
297297
(2, self.counterparty_htlc_base_key, required),
@@ -945,7 +945,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
945945
self.channel_keys_id.write(writer)?;
946946
self.holder_revocation_basepoint.write(writer)?;
947947
writer.write_all(&self.funding_info.0.txid[..])?;
948-
writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
948+
writer.write_all(&self.funding_info.0.index.to_be_bytes())?;
949949
self.funding_info.1.write(writer)?;
950950
self.current_counterparty_commitment_txid.write(writer)?;
951951
self.prev_counterparty_commitment_txid.write(writer)?;
@@ -972,24 +972,24 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
972972
},
973973
}
974974

975-
writer.write_all(&byte_utils::be16_to_array(self.on_holder_tx_csv))?;
975+
writer.write_all(&self.on_holder_tx_csv.to_be_bytes())?;
976976

977977
self.commitment_secrets.write(writer)?;
978978

979979
macro_rules! serialize_htlc_in_commitment {
980980
($htlc_output: expr) => {
981981
writer.write_all(&[$htlc_output.offered as u8; 1])?;
982-
writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
983-
writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
982+
writer.write_all(&$htlc_output.amount_msat.to_be_bytes())?;
983+
writer.write_all(&$htlc_output.cltv_expiry.to_be_bytes())?;
984984
writer.write_all(&$htlc_output.payment_hash.0[..])?;
985985
$htlc_output.transaction_output_index.write(writer)?;
986986
}
987987
}
988988

989-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_claimable_outpoints.len() as u64))?;
989+
writer.write_all(&(self.counterparty_claimable_outpoints.len() as u64).to_be_bytes())?;
990990
for (ref txid, ref htlc_infos) in self.counterparty_claimable_outpoints.iter() {
991991
writer.write_all(&txid[..])?;
992-
writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
992+
writer.write_all(&(htlc_infos.len() as u64).to_be_bytes())?;
993993
for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
994994
debug_assert!(htlc_source.is_none() || Some(**txid) == self.current_counterparty_commitment_txid
995995
|| Some(**txid) == self.prev_counterparty_commitment_txid,
@@ -999,13 +999,13 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
999999
}
10001000
}
10011001

1002-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_commitment_txn_on_chain.len() as u64))?;
1002+
writer.write_all(&(self.counterparty_commitment_txn_on_chain.len() as u64).to_be_bytes())?;
10031003
for (ref txid, commitment_number) in self.counterparty_commitment_txn_on_chain.iter() {
10041004
writer.write_all(&txid[..])?;
10051005
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
10061006
}
10071007

1008-
writer.write_all(&byte_utils::be64_to_array(self.counterparty_hash_commitment_number.len() as u64))?;
1008+
writer.write_all(&(self.counterparty_hash_commitment_number.len() as u64).to_be_bytes())?;
10091009
for (ref payment_hash, commitment_number) in self.counterparty_hash_commitment_number.iter() {
10101010
writer.write_all(&payment_hash.0[..])?;
10111011
writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
@@ -1023,7 +1023,7 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
10231023
writer.write_all(&byte_utils::be48_to_array(self.current_counterparty_commitment_number))?;
10241024
writer.write_all(&byte_utils::be48_to_array(self.current_holder_commitment_number))?;
10251025

1026-
writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
1026+
writer.write_all(&(self.payment_preimages.len() as u64).to_be_bytes())?;
10271027
for payment_preimage in self.payment_preimages.values() {
10281028
writer.write_all(&payment_preimage.0[..])?;
10291029
}
@@ -1044,15 +1044,15 @@ impl<Signer: Sign> Writeable for ChannelMonitorImpl<Signer> {
10441044
}
10451045
}
10461046

1047-
writer.write_all(&byte_utils::be64_to_array(self.pending_events.len() as u64))?;
1047+
writer.write_all(&(self.pending_events.len() as u64).to_be_bytes())?;
10481048
for event in self.pending_events.iter() {
10491049
event.write(writer)?;
10501050
}
10511051

10521052
self.best_block.block_hash().write(writer)?;
1053-
writer.write_all(&byte_utils::be32_to_array(self.best_block.height()))?;
1053+
writer.write_all(&self.best_block.height().to_be_bytes())?;
10541054

1055-
writer.write_all(&byte_utils::be64_to_array(self.onchain_events_awaiting_threshold_conf.len() as u64))?;
1055+
writer.write_all(&(self.onchain_events_awaiting_threshold_conf.len() as u64).to_be_bytes())?;
10561056
for ref entry in self.onchain_events_awaiting_threshold_conf.iter() {
10571057
entry.write(writer)?;
10581058
}
@@ -3792,7 +3792,9 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K>
37923792
return Err(DecodeError::InvalidValue);
37933793
}
37943794
}
3795-
let onchain_tx_handler: OnchainTxHandler<K::Signer> = ReadableArgs::read(reader, keys_manager)?;
3795+
let onchain_tx_handler: OnchainTxHandler<K::Signer> = ReadableArgs::read(
3796+
reader, (keys_manager, channel_value_satoshis, channel_keys_id)
3797+
)?;
37963798

37973799
let lockdown_from_offchain = Readable::read(reader)?;
37983800
let holder_tx_signed = Readable::read(reader)?;

0 commit comments

Comments
 (0)