Skip to content

Commit

Permalink
correctly ensure we don't use recycled addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed Sep 20, 2023
1 parent 93d3b49 commit 14a8da1
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ pub mod pallet {
destination_address: ForeignChainAddress,
channel_metadata: CcmChannelMetadata,
},
NoAction,
}

#[derive(
Expand Down Expand Up @@ -402,8 +401,7 @@ pub mod pallet {

println!("Current target chain height: {:?}", current_target_chain_height);

DepositChannelLookup::<T, I>::iter().for_each(|(_, details)| {
println!("Itering...");
for (address, details) in DepositChannelLookup::<T, I>::iter() {
// We add an extra lifetime of safety.
// The CFEs will stop witnessing the address of this deposit channel at the
// expires_at block number. However, because the CFE uses a safety margin, and here
Expand All @@ -414,10 +412,15 @@ pub mod pallet {
if details.expires_at + DepositChannelLifetime::<T, I>::get() <=
current_target_chain_height
{
println!("Recyling!");
Self::recycle_channel(details.deposit_channel);
println!("Recyling! Adding back to pool");
if let Some(state) = details.deposit_channel.state.maybe_recycle() {
DepositChannelPool::<T, I>::insert(
details.deposit_channel.channel_id,
DepositChannel { state, ..details.deposit_channel },
);
}
}
});
}

// TODO: return the actual weight
remaining_weight
Expand Down Expand Up @@ -737,6 +740,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let deposit_channel_details = DepositChannelLookup::<T, I>::get(&deposit_address)
.ok_or(Error::<T, I>::InvalidDepositAddress)?;

// The address should not be used if it's being recycled
ensure!(
DepositChannelPool::<T, I>::get(&deposit_channel_details.deposit_channel.channel_id)
.is_none(),
Error::<T, I>::InvalidDepositAddress
);

// Check can_fetch too?

ensure!(
deposit_channel_details.deposit_channel.asset == asset,
Error::<T, I>::AssetMismatch
Expand Down Expand Up @@ -868,15 +880,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

Ok((channel_id, deposit_address))
}

fn recycle_channel(channel: DepositChannel<T::TargetChain>) {
if let Some(state) = channel.state.maybe_recycle() {
DepositChannelPool::<T, I>::insert(
channel.channel_id,
DepositChannel { state, ..channel },
);
}
}
}

impl<T: Config<I>, I: 'static> EgressApi<T::TargetChain> for Pallet<T, I> {
Expand Down

0 comments on commit 14a8da1

Please sign in to comment.