Skip to content

Commit

Permalink
feat: update custom_rpc, runtime_api and broker api for broker level …
Browse files Browse the repository at this point in the history
…screening (#5341)

* Squashed commit of the following:

commit 3166701
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Tue Oct 29 15:27:51 2024 +0100

    Add `subscribe_tainted_btc_transaction_events` method to Broker API.

    It which forwards the node subscription of the same name.

commit 010772d
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Tue Oct 29 13:56:07 2024 +0100

    Add `open_btc_deposit_channels` method to broker api.

commit 9ea7e6f
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Tue Oct 29 11:19:07 2024 +0100

    Add subscription endpoint for tainted transaction events.

commit e81028a
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Mon Oct 28 15:12:47 2024 +0100

    Implement `open_btc_deposit_channels`.

commit 439ddd1
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Mon Oct 28 14:51:22 2024 +0100

    Add boilerplate for `open_btc_deposit_channels`.

commit a3835f1
Author: Maxim Urschumzew <maxu@chainflip.io>
Date:   Mon Oct 28 10:25:51 2024 +0100

    Add endpoint for marking btc tx as tainted.

* Apply suggestions from review.

* Return all channels for *all* channel actions.

We do not filter out `LiquidityProvision` channels anymore.

* Create tainted event subscription in broker API instead of forwarding from the node.

This is now the same as in the LP API binary. The previous version didn't
use finalized blocks and thus might not have been stable.

Theoretically it would be nice to be able to forward the subscription from the node,
see PRO-1768.

* Apply clippy suggestion.

* Remove now unused `tainted_transaction_events` subscription.

* Apply suggestions from @kylezs's review.
  • Loading branch information
MxmUrw authored and dandanlen committed Nov 6, 2024
1 parent 6e1fb1a commit 0844d1a
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions api/bin/chainflip-broker-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ workspace = true

[dependencies]
chainflip-api = { workspace = true }
cf-chains = { workspace = true, default-features = true }
cf-utilities = { workspace = true, default-features = true }
custom-rpc = { workspace = true }

Expand All @@ -32,8 +33,9 @@ futures = { workspace = true }
hex = { workspace = true, default-features = true }
jsonrpsee = { workspace = true, features = ["full"] }
serde = { workspace = true, default-features = true, features = ["derive"] }
sp-core = { workspace = true }
sp-rpc = { workspace = true }
sp-core = { workspace = true, default-features = true }
sp-rpc = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
18 changes: 15 additions & 3 deletions api/bin/chainflip-broker-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ use cf_utilities::{
};
use chainflip_api::{
self,
primitives::{AccountRole, Affiliates, Asset, BasisPoints, CcmChannelMetadata, DcaParameters},
primitives::{
state_chain_runtime::runtime_apis::{ChainAccounts, TaintedTransactionEvents},
AccountRole, Affiliates, Asset, BasisPoints, CcmChannelMetadata, DcaParameters,
},
settings::StateChain,
AccountId32, AddressString, BrokerApi, OperatorApi, RefundParameters, StateChainApi,
SwapDepositAddress, WithdrawFeesDetail,
};
use clap::Parser;
use futures::FutureExt;
use custom_rpc::CustomApiClient;
use futures::{FutureExt, StreamExt};
use jsonrpsee::{
core::{async_trait, ClientError},
core::{async_trait, ClientError, SubscriptionResult},
proc_macros::rpc,
server::ServerBuilder,
types::{ErrorCode, ErrorObject, ErrorObjectOwned},
PendingSubscriptionSink,
};
use serde::{Deserialize, Serialize};
use std::{
path::PathBuf,
sync::{atomic::AtomicBool, Arc},
Expand Down Expand Up @@ -59,6 +65,12 @@ impl From<BrokerApiError> for ErrorObjectOwned {
}
}

#[derive(Serialize, Deserialize)]
pub enum GetOpenDepositChannelsQuery {
All,
Mine,
}

#[rpc(server, client, namespace = "broker")]
pub trait Rpc {
#[method(name = "register_account", aliases = ["broker_registerAccount"])]
Expand Down
35 changes: 33 additions & 2 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cf_chains::{
dot::PolkadotAccountId,
evm::to_evm_address,
sol::SolAddress,
CcmChannelMetadata, ChannelRefundParametersGeneric, ForeignChain, ForeignChainAddress,
CcmChannelMetadata, Chain, ChainCrypto, ChannelRefundParametersEncoded,
};
pub use cf_primitives::{AccountRole, Affiliates, Asset, BasisPoints, ChannelId, SemVer};
use cf_primitives::{BlockNumber, DcaParameters, NetworkEnvironment, Price};
Expand All @@ -28,7 +28,7 @@ pub mod primitives {
pub type RedemptionAmount = pallet_cf_funding::RedemptionAmount<FlipBalance>;
pub use cf_chains::{
address::{EncodedAddress, ForeignChainAddress},
CcmChannelMetadata, CcmDepositMetadata,
CcmChannelMetadata, CcmDepositMetadata, Chain, ChainCrypto,
};
}
pub use cf_chains::eth::Address as EthereumAddress;
Expand Down Expand Up @@ -150,6 +150,10 @@ impl StateChainApi {
self.state_chain_client.clone()
}

pub fn deposit_monitor_api(&self) -> Arc<impl DepositMonitorApi> {
self.state_chain_client.clone()
}

pub fn query_api(&self) -> queries::QueryApi {
queries::QueryApi { state_chain_client: self.state_chain_client.clone() }
}
Expand All @@ -167,6 +171,8 @@ impl BrokerApi for StateChainClient {
impl OperatorApi for StateChainClient {}
#[async_trait]
impl ValidatorApi for StateChainClient {}
#[async_trait]
impl DepositMonitorApi for StateChainClient {}

#[async_trait]
pub trait ValidatorApi: SimpleSubmissionApi {
Expand Down Expand Up @@ -548,6 +554,31 @@ pub fn clean_foreign_chain_address(chain: ForeignChain, address: &str) -> Result
})
}

pub type TransactionInIdFor<C> = <<C as Chain>::ChainCrypto as ChainCrypto>::TransactionInId;

#[derive(Serialize, Deserialize)]
pub enum TransactionInId {
Bitcoin(TransactionInIdFor<cf_chains::Bitcoin>),
// other variants reserved for other chains.
}

#[async_trait]
pub trait DepositMonitorApi:
SignedExtrinsicApi + StorageApi + Sized + Send + Sync + 'static
{
async fn mark_transaction_as_tainted(&self, tx_id: TransactionInId) -> Result<H256> {
match tx_id {
TransactionInId::Bitcoin(tx_id) =>
self.simple_submission_with_dry_run(
state_chain_runtime::RuntimeCall::BitcoinIngressEgress(
pallet_cf_ingress_egress::Call::mark_transaction_as_tainted { tx_id },
),
)
.await,
}
}
}

#[derive(Debug, Zeroize, PartialEq, Eq)]
/// Public and Secret keys as bytes
pub struct KeyPair {
Expand Down
1 change: 1 addition & 0 deletions state-chain/custom-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pallet-cf-governance = { workspace = true, default-features = true }
pallet-cf-pools = { workspace = true, default-features = true }
pallet-cf-witnesser = { workspace = true, default-features = true }
pallet-cf-swapping = { workspace = true, default-features = true }
pallet-cf-ingress-egress = { workspace = true, default-features = true }

sp-api = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
Expand Down
28 changes: 25 additions & 3 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ use state_chain_runtime::{
PendingBroadcasts, PendingTssCeremonies, RedemptionsInfo, SolanaNonces,
},
runtime_apis::{
AuctionState, BoostPoolDepth, BoostPoolDetails, BrokerInfo, CustomRuntimeApi,
DispatchErrorWithMessage, ElectoralRuntimeApi, FailingWitnessValidators,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty, ValidatorInfo,
AuctionState, BoostPoolDepth, BoostPoolDetails, BrokerInfo, ChainAccounts,
CustomRuntimeApi, DispatchErrorWithMessage, ElectoralRuntimeApi, FailingWitnessValidators,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty,
TaintedTransactionEvents, ValidatorInfo,
},
safe_mode::RuntimeSafeMode,
Hash, NetworkFee, SolanaInstance,
Expand Down Expand Up @@ -938,6 +939,19 @@ pub trait CustomApi {
proposed_votes: Vec<u8>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Vec<u8>>;

#[method(name = "get_open_deposit_channels")]
fn cf_get_open_deposit_channels(
&self,
broker: Option<state_chain_runtime::AccountId>,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<ChainAccounts>;

#[method(name = "get_tainted_transaction_events")]
fn cf_get_tainted_transaction_events(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<TaintedTransactionEvents>;
}

/// An RPC extension for the state chain node.
Expand Down Expand Up @@ -1190,6 +1204,7 @@ where
cf_failed_call_arbitrum(broadcast_id: BroadcastId) -> Option<<cf_chains::Arbitrum as Chain>::Transaction>,
cf_boost_pools_depth() -> Vec<BoostPoolDepth>,
cf_pool_price(from_asset: Asset, to_asset: Asset) -> Option<PoolPriceV1>,
cf_get_open_deposit_channels(account_id: Option<state_chain_runtime::AccountId>) -> ChainAccounts,
}

pass_through_and_flatten! {
Expand Down Expand Up @@ -1725,6 +1740,13 @@ where
) -> RpcResult<Vec<u8>> {
self.with_runtime_api(at, |api, hash| api.cf_filter_votes(hash, validator, proposed_votes))
}

fn cf_get_tainted_transaction_events(
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<TaintedTransactionEvents> {
self.with_runtime_api(at, |api, hash| api.cf_tainted_transaction_events(hash))
}
}

impl<C, B> CustomRpc<C, B>
Expand Down
40 changes: 38 additions & 2 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use crate::{
runtime_decl_for_custom_runtime_api::CustomRuntimeApiV1, AuctionState, BoostPoolDepth,
BoostPoolDetails, BrokerInfo, DispatchErrorWithMessage, FailingWitnessValidators,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty,
SimulateSwapAdditionalOrder, SimulatedSwapInformation, ValidatorInfo,
SimulateSwapAdditionalOrder, SimulatedSwapInformation, TaintedTransactionEvents,
ValidatorInfo,
},
};
use cf_amm::{
Expand Down Expand Up @@ -71,8 +72,9 @@ use pallet_cf_pools::{
AskBidMap, AssetPair, HistoricalEarnedFees, OrderId, PoolLiquidity, PoolOrderbook, PoolPriceV1,
PoolPriceV2, UnidirectionalPoolDepth,
};
use runtime_apis::ChainAccounts;

use crate::chainflip::EvmLimit;
use crate::{chainflip::EvmLimit, runtime_apis::TaintedTransactionEvent};

use pallet_cf_reputation::{ExclusionList, HeartbeatQualification, ReputationPointsQualification};
use pallet_cf_swapping::SwapLegInfo;
Expand Down Expand Up @@ -2065,8 +2067,42 @@ impl_runtime_apis! {
fn cf_pools() -> Vec<PoolPairsMap<Asset>> {
LiquidityPools::pools()
}

fn cf_get_open_deposit_channels(account_id: Option<AccountId>) -> ChainAccounts {
let btc_chain_accounts = pallet_cf_ingress_egress::DepositChannelLookup::<Runtime,BitcoinInstance>::iter_values()
.filter(|channel_details| account_id.is_none() || Some(&channel_details.owner) == account_id.as_ref())
.map(|channel_details| channel_details.deposit_channel.address)
.collect::<Vec<_>>();

ChainAccounts {
btc_chain_accounts
}
}

fn cf_tainted_transaction_events() -> crate::runtime_apis::TaintedTransactionEvents {
let btc_events = System::read_events_no_consensus().filter_map(|event_record| {
if let RuntimeEvent::BitcoinIngressEgress(btc_ie_event) = event_record.event {
match btc_ie_event {
pallet_cf_ingress_egress::Event::TaintedTransactionReportExpired{ account_id, tx_id } =>
Some(TaintedTransactionEvent::TaintedTransactionReportExpired{ account_id, tx_id }),
pallet_cf_ingress_egress::Event::TaintedTransactionReportReceived{ account_id, tx_id, expires_at: _ } =>
Some(TaintedTransactionEvent::TaintedTransactionReportReceived{account_id, tx_id }),
pallet_cf_ingress_egress::Event::TaintedTransactionRejected{ broadcast_id, tx_id } =>
Some(TaintedTransactionEvent::TaintedTransactionRejected{ refund_broadcast_id: broadcast_id, tx_id: tx_id.id.tx_id }),
_ => None,
}
} else {
None
}
}).collect();

TaintedTransactionEvents {
btc_events
}
}
}


impl monitoring_apis::MonitoringRuntimeApi<Block> for Runtime {

fn cf_authorities() -> AuthoritiesInfo {
Expand Down
38 changes: 37 additions & 1 deletion state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use cf_amm::{
range_orders::Liquidity,
};
use cf_chains::{
assets::any::AssetMap, eth::Address as EthereumAddress, Chain, ForeignChainAddress,
self, assets::any::AssetMap, eth::Address as EthereumAddress, Chain, ChainCrypto,
ForeignChainAddress,
};
use cf_primitives::{
AccountRole, Asset, AssetAmount, BlockNumber, BroadcastId, EpochIndex, FlipBalance,
Expand Down Expand Up @@ -183,6 +184,39 @@ pub struct FailingWitnessValidators {
pub validators: Vec<(cf_primitives::AccountId, String, bool)>,
}

type ChainAccountFor<C> = <C as Chain>::ChainAccount;

#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)]
pub struct ChainAccounts {
pub btc_chain_accounts: Vec<ChainAccountFor<cf_chains::Bitcoin>>,
}

#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)]
pub enum TaintedTransactionEvent<TxId> {
TaintedTransactionReportReceived {
account_id: <Runtime as frame_system::Config>::AccountId,
tx_id: TxId,
},

TaintedTransactionReportExpired {
account_id: <Runtime as frame_system::Config>::AccountId,
tx_id: TxId,
},

TaintedTransactionRejected {
refund_broadcast_id: BroadcastId,
tx_id: TxId,
},
}

type TaintedTransactionEventFor<C> =
TaintedTransactionEvent<<<C as Chain>::ChainCrypto as ChainCrypto>::TransactionInId>;

#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)]
pub struct TaintedTransactionEvents {
pub btc_events: Vec<TaintedTransactionEventFor<cf_chains::Bitcoin>>,
}

decl_runtime_apis!(
/// Definition for all runtime API interfaces.
pub trait CustomRuntimeApi {
Expand Down Expand Up @@ -301,6 +335,8 @@ decl_runtime_apis!(
fn cf_swap_limits() -> SwapLimits;
fn cf_lp_events() -> Vec<pallet_cf_pools::Event<Runtime>>;
fn cf_minimum_chunk_size(asset: Asset) -> AssetAmount;
fn cf_get_open_deposit_channels(account_id: Option<AccountId32>) -> ChainAccounts;
fn cf_tainted_transaction_events() -> TaintedTransactionEvents;
}
);

Expand Down

0 comments on commit 0844d1a

Please sign in to comment.