Skip to content

Commit a3a06f6

Browse files
committed
Add a new method read_chan_signer to KeysInterface
This adds a new method to the general cross-channel `KeysInterface` which requires it to handle the deserialization of per-channel signer objects. This allows the deserialization of per-channel signers to have more context available, which, in the case of the C bindings, includes the actual KeysInterface information itself.
1 parent 34cc4fd commit a3a06f6

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget,
3737
use lightning::chain::keysinterface::{KeysInterface, InMemoryChannelKeys};
3838
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret, PaymentSendFailure, ChannelManagerReadArgs};
3939
use lightning::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
40-
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, UpdateAddHTLC, Init};
40+
use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, DecodeError, ErrorAction, UpdateAddHTLC, Init};
4141
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
4242
use lightning::util::errors::APIError;
4343
use lightning::util::events;
@@ -184,6 +184,10 @@ impl KeysInterface for KeyProvider {
184184
let id = self.rand_bytes_id.fetch_add(1, atomic::Ordering::Relaxed);
185185
[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, id, 11, self.node_id]
186186
}
187+
188+
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingChannelKeys, DecodeError> {
189+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(data))
190+
}
187191
}
188192

189193
#[inline]

fuzz/src/full_stack.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ use lightning::chain::transaction::OutPoint;
3232
use lightning::chain::keysinterface::{InMemoryChannelKeys, KeysInterface};
3333
use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, PaymentSecret};
3434
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor};
35+
use lightning::ln::msgs::DecodeError;
3536
use lightning::routing::router::get_route;
3637
use lightning::routing::network_graph::NetGraphMsgHandler;
38+
use lightning::util::config::UserConfig;
3739
use lightning::util::events::{EventsProvider,Event};
3840
use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
3941
use lightning::util::logger::Logger;
40-
use lightning::util::config::UserConfig;
42+
use lightning::util::ser::Readable;
4143

4244
use utils::test_logger;
4345
use utils::test_persister::TestPersister;
@@ -298,6 +300,10 @@ impl KeysInterface for KeyProvider {
298300
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
299301
(ctr >> 8*7) as u8, (ctr >> 8*6) as u8, (ctr >> 8*5) as u8, (ctr >> 8*4) as u8, (ctr >> 8*3) as u8, (ctr >> 8*2) as u8, (ctr >> 8*1) as u8, 14, (ctr >> 8*0) as u8]
300302
}
303+
304+
fn read_chan_signer(&self, data: &[u8]) -> Result<EnforcingChannelKeys, DecodeError> {
305+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(data))
306+
}
301307
}
302308

303309
#[inline]

lightning/src/chain/keysinterface.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ pub trait KeysInterface: Send + Sync {
346346
/// onion packets and for temporary channel IDs. There is no requirement that these be
347347
/// persisted anywhere, though they must be unique across restarts.
348348
fn get_secure_random_bytes(&self) -> [u8; 32];
349+
350+
/// Reads a `ChanKeySigner` for this `KeysInterface` from the given input stream.
351+
/// This is only called during deserialization of other objects which contain
352+
/// `ChannelKeys`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s) and must
353+
/// read exactly the bytes that `<Self::ChanKeySigner as Writeable>::write()` writes, or return
354+
/// an error.
355+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError>;
349356
}
350357

351358
#[derive(Clone)]
@@ -825,4 +832,8 @@ impl KeysInterface for KeysManager {
825832
sha.input(b"Unique Secure Random Bytes Salt");
826833
Sha256::from_engine(sha).into_inner()
827834
}
835+
836+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError> {
837+
InMemoryChannelKeys::read(&mut std::io::Cursor::new(reader))
838+
}
828839
}

lightning/src/ln/channel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4503,7 +4503,7 @@ mod tests {
45034503
use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
45044504
use ln::channel::MAX_FUNDING_SATOSHIS;
45054505
use ln::features::InitFeatures;
4506-
use ln::msgs::{OptionalField, DataLossProtect};
4506+
use ln::msgs::{OptionalField, DataLossProtect, DecodeError};
45074507
use ln::chan_utils;
45084508
use ln::chan_utils::{HolderCommitmentTransaction, ChannelPublicKeys};
45094509
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -4559,6 +4559,7 @@ mod tests {
45594559
self.chan_keys.clone()
45604560
}
45614561
fn get_secure_random_bytes(&self) -> [u8; 32] { [0; 32] }
4562+
fn read_chan_signer(&self, data: &[u8]) -> Result<Self::ChanKeySigner, DecodeError> { panic!(); }
45624563
}
45634564

45644565
fn public_from_secret_hex(secp_ctx: &Secp256k1<All>, hex: &str) -> PublicKey {

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7362,7 +7362,7 @@ fn test_user_configurable_csv_delay() {
73627362
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
73637363

73647364
// We test config.our_to_self > BREAKDOWN_TIMEOUT is enforced in Channel::new_outbound()
7365-
let keys_manager: Arc<KeysInterface<ChanKeySigner = EnforcingChannelKeys>> = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet));
7365+
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&nodes[0].node_seed, Network::Testnet));
73667366
if let Err(error) = Channel::new_outbound(&&test_utils::TestFeeEstimator { sat_per_kw: 253 }, &keys_manager, nodes[1].node.get_our_node_id(), 1000000, 1000000, 0, &low_our_to_self_config) {
73677367
match error {
73687368
APIError::APIMisuseError { err } => { assert!(regex::Regex::new(r"Configured with an unreasonable our_to_self_delay \(\d+\) putting user funds at risks").unwrap().is_match(err.as_str())); },

lightning/src/util/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ impl keysinterface::KeysInterface for TestKeysInterface {
410410
}
411411
self.backing.get_secure_random_bytes()
412412
}
413+
414+
fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, msgs::DecodeError> {
415+
EnforcingChannelKeys::read(&mut std::io::Cursor::new(reader))
416+
}
413417
}
414418

415419
impl TestKeysInterface {

0 commit comments

Comments
 (0)