Skip to content

Commit

Permalink
refactor: deposit address expiry preparation (#4033)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs authored and dandanlen committed Sep 28, 2023
1 parent a3b9a3e commit 1ac5e78
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 52 deletions.
18 changes: 0 additions & 18 deletions state-chain/chains/src/deposit_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ pub trait ChannelLifecycleHooks: Sized {

impl ChannelLifecycleHooks for () {}

impl<C: Chain> ChannelLifecycleHooks for DepositChannel<C> {
fn can_fetch(&self) -> bool {
self.state.can_fetch()
}

fn on_fetch_scheduled(&mut self) -> bool {
self.state.on_fetch_scheduled()
}

fn on_fetch_completed(&mut self) -> bool {
self.state.on_fetch_completed()
}

fn maybe_recycle(self) -> Option<Self> {
self.state.maybe_recycle().map(|state| Self { state, ..self })
}
}

impl<C: Chain> DepositChannel<C> {
pub fn generate_new<A: AddressDerivationApi<C>>(
channel_id: ChannelId,
Expand Down
65 changes: 31 additions & 34 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,11 @@ pub mod pallet {
) -> DispatchResult {
T::EnsureWitnessedAtCurrentEpoch::ensure_origin(origin)?;
for deposit_address in addresses {
if let Some(mut deposit_details) =
DepositChannelLookup::<T, I>::get(&deposit_address)
{
if deposit_details.deposit_channel.on_fetch_completed() {
DepositChannelLookup::<T, I>::insert(&deposit_address, deposit_details);
}
} else {
log::error!(
"Deposit address {:?} not found in DepositChannelLookup",
deposit_address
);
}
DepositChannelLookup::<T, I>::mutate(deposit_address, |deposit_channel_details| {
deposit_channel_details
.as_mut()
.map(|details| details.deposit_channel.state.on_fetch_completed());
});
}
Ok(())
}
Expand Down Expand Up @@ -551,27 +544,28 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
deposit_address,
deposit_fetch_id,
..
} =>
if let Some(mut details) =
DepositChannelLookup::<T, I>::get(&*deposit_address)
{
if details.deposit_channel.can_fetch() {
deposit_fetch_id
.replace(details.deposit_channel.fetch_id());
if details.deposit_channel.on_fetch_scheduled() {
DepositChannelLookup::<T, I>::insert(
deposit_address,
details,
);
}
true
} else {
false
}
} else {
log::error!("Deposit address {:?} not found in DepositChannelLookup", deposit_address);
false
} => DepositChannelLookup::<T, I>::mutate(
deposit_address,
|details| {
details
.as_mut()
.map(|details| {
let can_fetch =
details.deposit_channel.state.can_fetch();
if can_fetch {
deposit_fetch_id.replace(
details.deposit_channel.fetch_id(),
);
details
.deposit_channel
.state
.on_fetch_scheduled();
}
can_fetch
})
.unwrap_or(false)
},
),
FetchOrTransfer::Transfer { .. } => true,
}
})
Expand Down Expand Up @@ -935,8 +929,11 @@ impl<T: Config<I>, I: 'static> DepositApi<T::TargetChain> for Pallet<T, I> {
fn expire_channel(address: TargetChainAccount<T, I>) {
ChannelActions::<T, I>::remove(&address);
if let Some(deposit_channel_details) = DepositChannelLookup::<T, I>::get(&address) {
if let Some(channel) = deposit_channel_details.deposit_channel.maybe_recycle() {
DepositChannelPool::<T, I>::insert(channel.channel_id, channel);
if let Some(state) = deposit_channel_details.deposit_channel.state.maybe_recycle() {
DepositChannelPool::<T, I>::insert(
deposit_channel_details.deposit_channel.channel_id,
DepositChannel { state, ..deposit_channel_details.deposit_channel },
);
}
} else {
log_or_panic!("Tried to close an unknown channel.");
Expand Down

0 comments on commit 1ac5e78

Please sign in to comment.