Skip to content

Commit 617e4ab

Browse files
joostjagerclaude
andcommitted
Extract second stage of ChannelManager::read into from_channel_manager_data
Move the validation and reconstruction logic (stage 2) from the ReadableArgs::read implementation into a new pub(super) constructor `from_channel_manager_data`. This separates the pure deserialization from the complex reconstruction logic, making the code more modular and easier to test. The read function now: 1. Deserializes into ChannelManagerData (stage 1) 2. Calls from_channel_manager_data for validation/reconstruction (stage 2) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e7e23c9 commit 617e4ab

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

lightning/src/ln/channelmanager.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17840,7 +17840,7 @@ where
1784017840
L::Target: Logger,
1784117841
{
1784217842
fn read<Reader: io::Read>(
17843-
reader: &mut Reader, mut args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>,
17843+
reader: &mut Reader, args: ChannelManagerReadArgs<'a, M, T, ES, NS, SP, F, R, MR, L>,
1784417844
) -> Result<Self, DecodeError> {
1784517845
// Stage 1: Pure deserialization into DTO
1784617846
let data: ChannelManagerData<SP> = ChannelManagerData::read(
@@ -17854,6 +17854,44 @@ where
1785417854
)?;
1785517855

1785617856
// Stage 2: Validation and reconstruction
17857+
ChannelManager::from_channel_manager_data(data, args)
17858+
}
17859+
}
17860+
17861+
impl<
17862+
M: Deref,
17863+
T: Deref,
17864+
ES: Deref,
17865+
NS: Deref,
17866+
SP: Deref,
17867+
F: Deref,
17868+
R: Deref,
17869+
MR: Deref,
17870+
L: Deref + Clone,
17871+
> ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
17872+
where
17873+
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
17874+
T::Target: BroadcasterInterface,
17875+
ES::Target: EntropySource,
17876+
NS::Target: NodeSigner,
17877+
SP::Target: SignerProvider,
17878+
F::Target: FeeEstimator,
17879+
R::Target: Router,
17880+
MR::Target: MessageRouter,
17881+
L::Target: Logger,
17882+
{
17883+
/// Constructs a `ChannelManager` from deserialized data and runtime dependencies.
17884+
///
17885+
/// This is the second stage of deserialization, taking the raw [`ChannelManagerData`] and combining it with the
17886+
/// provided [`ChannelManagerReadArgs`] to produce a fully functional `ChannelManager`.
17887+
///
17888+
/// This method performs validation, reconciliation with [`ChannelMonitor`]s, and reconstruction of internal state.
17889+
/// It may close channels if monitors are ahead of the serialized state, and will replay any pending
17890+
/// [`ChannelMonitorUpdate`]s.
17891+
pub(super) fn from_channel_manager_data(
17892+
data: ChannelManagerData<SP>,
17893+
mut args: ChannelManagerReadArgs<'_, M, T, ES, NS, SP, F, R, MR, L>,
17894+
) -> Result<(BlockHash, Self), DecodeError> {
1785717895
// Extract mutable/complex fields into local variables; simple fields accessed via data.*
1785817896
let mut forward_htlcs_legacy = data.forward_htlcs;
1785917897
let mut claimable_htlcs_list = data.claimable_htlcs;

0 commit comments

Comments
 (0)