Skip to content

Commit

Permalink
Add subscription endpoint for tainted transaction events.
Browse files Browse the repository at this point in the history
  • Loading branch information
MxmUrw committed Oct 29, 2024
1 parent e81028a commit 9ea7e6f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

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
20 changes: 19 additions & 1 deletion state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ use state_chain_runtime::{
runtime_apis::{
AuctionState, BoostPoolDepth, BoostPoolDetails, BrokerInfo, CustomRuntimeApi,
DispatchErrorWithMessage, ElectoralRuntimeApi, FailingWitnessValidators,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty, ValidatorInfo,
LiquidityProviderBoostPoolInfo, LiquidityProviderInfo, RuntimeApiPenalty,
TaintedBtcTransactionEvent, ValidatorInfo,
},
safe_mode::RuntimeSafeMode,
Hash, NetworkFee, SolanaInstance,
Expand Down Expand Up @@ -954,6 +955,9 @@ pub trait CustomApi {
broker: state_chain_runtime::AccountId,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Vec<<cf_chains::Bitcoin as Chain>::ChainAccount>>;

#[subscription(name = "subscribe_tainted_btc_transaction_events", item = BlockUpdate<Vec<TaintedBtcTransactionEvent>>)]
fn cf_subscribe_tainted_btc_transaction_events(&self);
}

/// An RPC extension for the state chain node.
Expand Down Expand Up @@ -1738,6 +1742,20 @@ where
) -> RpcResult<Vec<u8>> {
self.with_runtime_api(at, |api, hash| api.cf_filter_votes(hash, validator, proposed_votes))
}

fn cf_subscribe_tainted_btc_transaction_events(
&self,
sink: jsonrpsee::PendingSubscriptionSink,
) {
self.new_subscription(
false, /* only_on_changes */
true, /* end_on_error */
sink,
|client, hash| {
Ok(client.runtime_api().cf_tainted_btc_transaction_events(hash)?)
},
)
}
}

impl<C, B> CustomRpc<C, B>
Expand Down
22 changes: 21 additions & 1 deletion state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use pallet_cf_pools::{
PoolPriceV2, UnidirectionalPoolDepth,
};

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

use pallet_cf_reputation::{ExclusionList, HeartbeatQualification, ReputationPointsQualification};
use pallet_cf_swapping::SwapLegInfo;
Expand Down Expand Up @@ -2085,8 +2085,28 @@ impl_runtime_apis! {
.map(|channel_details| channel_details.deposit_channel.address)
.collect())
}

fn cf_tainted_btc_transaction_events() -> Vec<crate::runtime_apis::TaintedBtcTransactionEvent> {

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(TaintedBtcTransactionEvent::TaintedTransactionReportExpired{ account_id, tx_id }),
pallet_cf_ingress_egress::Event::TaintedTransactionReportReceived{ account_id, tx_id, expires_at: _ } =>
Some(TaintedBtcTransactionEvent::TaintedTransactionReportReceived{account_id, tx_id }),
pallet_cf_ingress_egress::Event::TaintedTransactionRejected{ broadcast_id, tx_id } =>
Some(TaintedBtcTransactionEvent::TaintedTransactionRejected{ broadcast_id, tx_id: tx_id.utxo_id.tx_id }),
_ => None,
}
} else {
None
}
}).collect()
}
}


impl monitoring_apis::MonitoringRuntimeApi<Block> for Runtime {

fn cf_authorities() -> AuthoritiesInfo {
Expand Down
19 changes: 19 additions & 0 deletions state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ pub struct FailingWitnessValidators {
pub validators: Vec<(cf_primitives::AccountId, String, bool)>,
}

#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)]
pub enum TaintedBtcTransactionEvent {
TaintedTransactionReportReceived {
account_id: <Runtime as frame_system::Config>::AccountId,
tx_id: <<cf_chains::Bitcoin as cf_chains::Chain>::ChainCrypto as cf_chains::ChainCrypto>::TransactionInId,
},

TaintedTransactionReportExpired {
account_id: <Runtime as frame_system::Config>::AccountId,
tx_id: <<cf_chains::Bitcoin as cf_chains::Chain>::ChainCrypto as cf_chains::ChainCrypto>::TransactionInId,
},

TaintedTransactionRejected {
broadcast_id: BroadcastId,
tx_id: <<cf_chains::Bitcoin as cf_chains::Chain>::ChainCrypto as cf_chains::ChainCrypto>::TransactionInId,
},
}

decl_runtime_apis!(
/// Definition for all runtime API interfaces.
pub trait CustomRuntimeApi {
Expand Down Expand Up @@ -312,6 +330,7 @@ decl_runtime_apis!(
Vec<<cf_chains::Bitcoin as cf_chains::Chain>::ChainAccount>,
DispatchErrorWithMessage,
>;
fn cf_tainted_btc_transaction_events() -> Vec<TaintedBtcTransactionEvent>;
}
);

Expand Down

0 comments on commit 9ea7e6f

Please sign in to comment.