Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add block height and deposit details to PrewitnessedDeposit #4606

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions state-chain/pallets/cf-ingress-egress/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ mod benchmarks {
asset: source_asset,
amount: deposit_amount,
deposit_address: deposit_address.clone(),
deposit_details: BenchmarkValue::benchmark_value(),
block_height: BenchmarkValue::benchmark_value(),
},
);

Expand Down Expand Up @@ -169,6 +171,8 @@ mod benchmarks {
asset: BenchmarkValue::benchmark_value(),
amount: BenchmarkValue::benchmark_value(),
deposit_address: BenchmarkValue::benchmark_value(),
deposit_details: BenchmarkValue::benchmark_value(),
block_height: BenchmarkValue::benchmark_value(),
},
);
}
Expand Down
16 changes: 13 additions & 3 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct PrewitnessedDeposit<C: Chain> {
pub asset: C::ChainAsset,
pub amount: C::ChainAmount,
pub deposit_address: C::ChainAccount,
pub block_height: C::ChainBlockNumber,
pub deposit_details: C::DepositDetails,
}

/// Enum wrapper for fetch and egress requests.
Expand Down Expand Up @@ -787,7 +789,7 @@ pub mod pallet {
block_height: TargetChainBlockNumber<T, I>,
) -> DispatchResult {
if T::EnsurePrewitnessed::ensure_origin(origin.clone()).is_ok() {
Self::add_prewitnessed_deposits(deposit_witnesses)?;
Self::add_prewitnessed_deposits(deposit_witnesses, block_height)?;
} else {
T::EnsureWitnessed::ensure_origin(origin)?;
Self::process_deposit_witnesses(deposit_witnesses, block_height)?;
Expand Down Expand Up @@ -1105,8 +1107,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

fn add_prewitnessed_deposits(
deposit_witnesses: Vec<DepositWitness<T::TargetChain>>,
block_height: TargetChainBlockNumber<T, I>,
) -> DispatchResult {
for DepositWitness { deposit_address, asset, amount, .. } in deposit_witnesses {
for DepositWitness { deposit_address, asset, amount, deposit_details } in deposit_witnesses
{
let id = PrewitnessedDepositIdCounter::<T, I>::mutate(|id| -> u64 {
*id = id.saturating_add(1);
*id
Expand All @@ -1118,7 +1122,13 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
PrewitnessedDeposits::<T, I>::insert(
deposit_channel_details.deposit_channel.channel_id,
id,
PrewitnessedDeposit { asset, amount, deposit_address },
PrewitnessedDeposit {
asset,
amount,
deposit_address,
block_height,
deposit_details,
},
);
}
Ok(())
Expand Down
40 changes: 34 additions & 6 deletions state-chain/pallets/cf-ingress-egress/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1585,15 +1585,26 @@ fn should_cleanup_prewitnessed_deposits_when_channel_is_recycled() {
amount: DEPOSIT_AMOUNT,
deposit_details: (),
};

const TARGET_CHAIN_HEIGHT: u64 = 0;
let deposit_witnesses = vec![deposit_detail.clone()];
assert_ok!(IngressEgress::add_prewitnessed_deposits(deposit_witnesses.clone(),));
assert_ok!(IngressEgress::add_prewitnessed_deposits(
deposit_witnesses.clone(),
TARGET_CHAIN_HEIGHT
));

// Check that the deposit is stored in the storage
let prewitnessed_deposit_id = PrewitnessedDepositIdCounter::<Test>::get();
let channel_id = ChannelIdCounter::<Test>::get();
assert_eq!(
PrewitnessedDeposits::<Test>::get(channel_id, prewitnessed_deposit_id),
Some(PrewitnessedDeposit { asset: ASSET, amount: DEPOSIT_AMOUNT, deposit_address })
Some(PrewitnessedDeposit {
asset: ASSET,
amount: DEPOSIT_AMOUNT,
deposit_address,
block_height: TARGET_CHAIN_HEIGHT,
deposit_details: ()
})
);

// Fast forward the block height to the recycle block of the created deposit channel
Expand Down Expand Up @@ -1630,8 +1641,16 @@ fn should_remove_prewitnessed_deposit_when_witnessed() {
amount: DEPOSIT_AMOUNT_1,
deposit_details: (),
}];
assert_ok!(IngressEgress::add_prewitnessed_deposits(deposit_witnesses_1.clone()));
assert_ok!(IngressEgress::add_prewitnessed_deposits(deposit_witnesses_1.clone()));

const TARGET_CHAIN_HEIGHT: u64 = 0;
assert_ok!(IngressEgress::add_prewitnessed_deposits(
deposit_witnesses_1.clone(),
TARGET_CHAIN_HEIGHT
));
assert_ok!(IngressEgress::add_prewitnessed_deposits(
deposit_witnesses_1.clone(),
TARGET_CHAIN_HEIGHT
));

// Submit another prewitness for the same address but a different amount
let deposit_witnesses_2 = vec![DepositWitness::<Ethereum> {
Expand All @@ -1640,7 +1659,10 @@ fn should_remove_prewitnessed_deposit_when_witnessed() {
amount: DEPOSIT_AMOUNT_2,
deposit_details: (),
}];
assert_ok!(IngressEgress::add_prewitnessed_deposits(deposit_witnesses_2.clone()));
assert_ok!(IngressEgress::add_prewitnessed_deposits(
deposit_witnesses_2.clone(),
TARGET_CHAIN_HEIGHT
));

// Check that the deposits are in storage
let channel_id = ChannelIdCounter::<Test>::get();
Expand All @@ -1658,7 +1680,13 @@ fn should_remove_prewitnessed_deposit_when_witnessed() {
let prewitnessed_deposit_id = PrewitnessedDepositIdCounter::<Test>::get();
assert_eq!(
PrewitnessedDeposits::<Test>::get(channel_id, prewitnessed_deposit_id),
Some(PrewitnessedDeposit { asset: ASSET, amount: DEPOSIT_AMOUNT_2, deposit_address })
Some(PrewitnessedDeposit {
asset: ASSET,
amount: DEPOSIT_AMOUNT_2,
deposit_address,
block_height: TARGET_CHAIN_HEIGHT,
deposit_details: (),
})
);
});
}
Loading