Skip to content

Commit

Permalink
feat: tainted transaction reporting (#5310)
Browse files Browse the repository at this point in the history
* feat: Handling of tainted transactions

- Extended DepositChannelDetails with owner field
- Added extrinsic to mark transaction as tainted
- Handling deposit and save details for a refund if tx is tainted

* tests: Added tests

- Added tests to verify that tainted transactions can get detected for all possible swap types
- Added tests  to check that txs marked by other brokers are getting ignored

* chore: Extended LpRegistration trait + Added to Ingress/Egress config

* feat: Getting LP refund address

* feature: ensure by lp and broker + added more tests

* refactor: Don't error if tx is tainted

* refactor: Using DoubleMap instead of Map

* refactor: Using BadOrigin + Added unit test

* refactor: Inline code + Add Deposit Witness to struct

* refactor: Extended lp deposit with refund address

- Taking refund address if we open a deposit channel as lp
- Extended ChannelAction to take an optional refund address
- Removed all dependencies regarding the LpRegistration trait (added last commit)
- Refactored tests, benchmarks, etc

* feature: Added benchmark

* chore: Changes to benchmark

* chore: generated mock weights

* chore: only allow mark transactions for BTC

* feature: expire tainted transaction

* test: refactored tests

* chore: Removed unused events

* chore: Ensure only by broker

* chore: removed broker from tainted tx struct

* chore: Only clone owner

* chore: Moved tx tainted check

* feature: Added migration for DepositChannelLookup

* refactor: Changed data structure + fixed migrations

* chore: Handle LP refund address as requirement

* chore: Made clippy happy 🙂

* chore: don't manipulate storage in place in iteration 🙅‍♂️

* test: Added migration test 🧪

* chore: changed pallet storage version 📀

* chore: bumped pallet storage version (again)

* refactor: Changed accounting of expired transactions

* refactor: using translate for migration

* refactor: using append, refactored test

* feature: Added handling of boost channels

* feature: Marking txs when prewitness and reject when we process the depo

* feat: pre-witnessed rejection handling

* chore: Fixed logic + added tests

* tests: Refactor/Rearranged tests

* chore: Using SECONDS_PER_BLOCK instead of static block seconds

* chore: Addressed comments

* chore: Fixed clippy in CI

* chore: update comments

* fix: don't allow report overwrite

* chore: Renamed event

* feat: improvements:

- mark boosted transactions as boosted instead of using channel status
- allow pallet config instead of relying on chain
- add event for tx reports
- only allow reporting of unseen transactions
- add doc comments
- renaming of types/events
- remove unused error

* fix: migration

* fix: made clippy happy

---------

Co-authored-by: Daniel <daniel@chainflip.io>
Co-authored-by: Maxim Shishmarev <maxim@chainflip.io>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 734c419 commit dc8bf3e
Show file tree
Hide file tree
Showing 16 changed files with 857 additions and 75 deletions.
3 changes: 3 additions & 0 deletions engine/src/witness/btc/deposits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ pub mod tests {
fn fake_details(
deposit_address: DepositAddress,
) -> DepositChannelDetails<state_chain_runtime::Runtime, BitcoinInstance> {
use cf_chains::{btc::ScriptPubkey, ForeignChainAddress};
DepositChannelDetails::<_, BitcoinInstance> {
owner: AccountId32::new([0xab; 32]),
opened_at: 1,
expires_at: 10,
deposit_channel: DepositChannel {
Expand All @@ -204,6 +206,7 @@ pub mod tests {
},
action: ChannelAction::<AccountId32>::LiquidityProvision {
lp_account: AccountId32::new([0xab; 32]),
refund_address: Some(ForeignChainAddress::Btc(ScriptPubkey::P2PKH([0; 20]))),
},
boost_fee: 0,
boost_status: BoostStatus::NotBoosted,
Expand Down
38 changes: 36 additions & 2 deletions state-chain/pallets/cf-ingress-egress/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cf_chains::{
benchmarking_value::{BenchmarkValue, BenchmarkValueExtended},
DepositChannel,
};
use cf_primitives::AccountRole;
use cf_traits::AccountRoleRegistry;
use frame_benchmarking::v2::*;
use frame_support::{
Expand Down Expand Up @@ -61,6 +62,7 @@ mod benchmarks {
DepositChannelLookup::<T, I>::insert(
&deposit_address,
DepositChannelDetails {
owner: account("doogle", 0, 0),
opened_at: block_number,
expires_at: block_number,
deposit_channel:
Expand All @@ -71,6 +73,7 @@ mod benchmarks {
.unwrap(),
action: ChannelAction::<T::AccountId>::LiquidityProvision {
lp_account: account("doogle", 0, 0),
refund_address: None,
},
boost_fee: 0,
boost_status: BoostStatus::NotBoosted,
Expand Down Expand Up @@ -102,6 +105,7 @@ mod benchmarks {
let block_number = TargetChainBlockNumber::<T, I>::benchmark_value();
let mut channel =
DepositChannelDetails::<T, I> {
owner: account("doogle", 0, 0),
opened_at: block_number,
expires_at: block_number,
deposit_channel: DepositChannel::generate_new::<
Expand All @@ -110,6 +114,7 @@ mod benchmarks {
.unwrap(),
action: ChannelAction::<T::AccountId>::LiquidityProvision {
lp_account: account("doogle", 0, 0),
refund_address: None,
},
boost_fee: 0,
boost_status: BoostStatus::NotBoosted,
Expand Down Expand Up @@ -227,7 +232,10 @@ mod benchmarks {
let (_channel_id, deposit_address, ..) = Pallet::<T, I>::open_channel(
lp_account,
asset,
ChannelAction::LiquidityProvision { lp_account: lp_account.clone() },
ChannelAction::LiquidityProvision {
lp_account: lp_account.clone(),
refund_address: None,
},
fee_tier,
)
.unwrap();
Expand Down Expand Up @@ -401,7 +409,10 @@ mod benchmarks {
let (_channel_id, deposit_address, ..) = Pallet::<T, I>::open_channel(
&boosters[0],
asset,
ChannelAction::LiquidityProvision { lp_account: boosters[0].clone() },
ChannelAction::LiquidityProvision {
lp_account: boosters[0].clone(),
refund_address: None,
},
TIER_5_BPS,
)
.unwrap();
Expand Down Expand Up @@ -506,6 +517,26 @@ mod benchmarks {
assert_eq!(BoostPools::<T, I>::iter().count(), 1);
}

#[benchmark]
fn mark_transaction_as_tainted() {
let caller =
T::AccountRoleRegistry::whitelisted_caller_with_role(AccountRole::Broker).unwrap();
let tx_id = <<T as Config<I>>::TargetChain as Chain>::DepositDetails::benchmark_value();

#[block]
{
assert_ok!(Pallet::<T, I>::mark_transaction_as_tainted_inner(
caller.clone(),
tx_id.clone(),
));
}

assert!(
TaintedTransactions::<T, I>::get(caller, tx_id).is_some(),
"No tainted transactions found"
);
}

#[cfg(test)]
use crate::mock_eth::*;

Expand Down Expand Up @@ -550,5 +581,8 @@ mod benchmarks {
new_test_ext().execute_with(|| {
_contract_ccm_swap_request::<Test, ()>(true);
});
new_test_ext().execute_with(|| {
_mark_transaction_as_tainted::<Test, ()>(true);
});
}
}
Loading

0 comments on commit dc8bf3e

Please sign in to comment.