|
| 1 | +//! Db-Sync data source used by the Partner Chain token bridge observability |
| 2 | +//! |
| 3 | +//! # Assumptions |
| 4 | +//! |
| 5 | +//! The data source implementation assumes that the utxos found at the illiquid circulating |
| 6 | +//! supply address conform to rules that are enforced by the Partner Chains smart contracts. |
| 7 | +//! |
| 8 | +//! Most importantly, transactions that spend any UTXOs from the ICS can only create at most |
| 9 | +//! one new UTXO at the ICS address. Conversely, transactions that create more than one UTXO |
| 10 | +//! at the illiquid supply address can only spend UTXOs from outside of it. This guarantees |
| 11 | +//! that the observability layer can always correctly identify the number of tokens transfered |
| 12 | +//! by calculating the delta of `tokens in the new UTXO` - `tokens in the old ICS UTXOs`. |
| 13 | +
|
| 14 | +use crate::McFollowerMetrics; |
| 15 | +use crate::db_model::*; |
| 16 | +use crate::observed_async_trait; |
| 17 | +use partner_chains_plutus_data::bridge::TokenTransferDatum; |
| 18 | +use partner_chains_plutus_data::bridge::TokenTransferDatumV1; |
| 19 | +use sidechain_domain::McBlockHash; |
| 20 | +use sp_partner_chains_bridge::*; |
| 21 | +use sqlx::PgPool; |
| 22 | +use std::fmt::Debug; |
| 23 | + |
| 24 | +#[cfg(test)] |
| 25 | +mod tests; |
| 26 | + |
| 27 | +/// Db-Sync data source serving data for Partner Chains token bridge |
| 28 | +pub struct TokenBridgeDataSourceImpl { |
| 29 | + /// Postgres connection pool |
| 30 | + pool: PgPool, |
| 31 | + /// Prometheus metrics client |
| 32 | + metrics_opt: Option<McFollowerMetrics>, |
| 33 | + /// Configuration used by Db-Sync |
| 34 | + db_sync_config: DbSyncConfigurationProvider, |
| 35 | +} |
| 36 | + |
| 37 | +impl TokenBridgeDataSourceImpl { |
| 38 | + /// Crates a new token bridge data source |
| 39 | + pub fn new(pool: PgPool, metrics_opt: Option<McFollowerMetrics>) -> Self { |
| 40 | + Self { db_sync_config: DbSyncConfigurationProvider::new(pool.clone()), pool, metrics_opt } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +observed_async_trait!( |
| 45 | + impl<RecipientAddress> TokenBridgeDataSource<RecipientAddress> for TokenBridgeDataSourceImpl |
| 46 | + where |
| 47 | + RecipientAddress: Debug, |
| 48 | + RecipientAddress: (for<'a> TryFrom<&'a [u8]>), |
| 49 | + { |
| 50 | + async fn get_transfers( |
| 51 | + &self, |
| 52 | + main_chain_scripts: MainChainScripts, |
| 53 | + data_checkpoint: BridgeDataCheckpoint, |
| 54 | + max_transfers: u32, |
| 55 | + current_mc_block_hash: McBlockHash, |
| 56 | + ) -> Result< |
| 57 | + (Vec<BridgeTransferV1<RecipientAddress>>, BridgeDataCheckpoint), |
| 58 | + Box<dyn std::error::Error + Send + Sync>, |
| 59 | + > { |
| 60 | + let asset = Asset { |
| 61 | + policy_id: main_chain_scripts.token_policy_id.into(), |
| 62 | + asset_name: main_chain_scripts.token_asset_name.into(), |
| 63 | + }; |
| 64 | + |
| 65 | + let current_mc_block = get_block_by_hash(&self.pool, current_mc_block_hash.clone()) |
| 66 | + .await? |
| 67 | + .ok_or(format!("Could not find block for hash {current_mc_block_hash:?}"))?; |
| 68 | + |
| 69 | + let data_checkpoint = match data_checkpoint { |
| 70 | + BridgeDataCheckpoint::Utxo(utxo) => { |
| 71 | + let TxBlockInfo { block_number, tx_ix } = |
| 72 | + get_block_info_for_utxo(&self.pool, utxo.tx_hash.into()).await?.ok_or( |
| 73 | + format!( |
| 74 | + "Could not find block info for data checkpoint: {data_checkpoint:?}" |
| 75 | + ), |
| 76 | + )?; |
| 77 | + BridgeCheckpoint::Utxo { |
| 78 | + block_number: block_number.0, |
| 79 | + tx_ix: tx_ix.0, |
| 80 | + tx_out_ix: utxo.index.0, |
| 81 | + } |
| 82 | + }, |
| 83 | + BridgeDataCheckpoint::Block(checkpoint_block_number) => { |
| 84 | + BridgeCheckpoint::Block { number: checkpoint_block_number.0 } |
| 85 | + }, |
| 86 | + }; |
| 87 | + |
| 88 | + let utxos = get_bridge_utxos_tx( |
| 89 | + self.db_sync_config.get_tx_in_config().await?, |
| 90 | + &self.pool, |
| 91 | + &main_chain_scripts.illiquid_circulation_supply_validator_address.into(), |
| 92 | + asset, |
| 93 | + data_checkpoint, |
| 94 | + current_mc_block.block_no, |
| 95 | + max_transfers, |
| 96 | + ) |
| 97 | + .await?; |
| 98 | + |
| 99 | + let new_checkpoint = match utxos.last() { |
| 100 | + None => BridgeDataCheckpoint::Block(current_mc_block.block_no.into()), |
| 101 | + Some(_) if (utxos.len() as u32) < max_transfers => { |
| 102 | + BridgeDataCheckpoint::Block(current_mc_block.block_no.into()) |
| 103 | + }, |
| 104 | + Some(utxo) => BridgeDataCheckpoint::Utxo(utxo.utxo_id()), |
| 105 | + }; |
| 106 | + |
| 107 | + let transfers = utxos.into_iter().flat_map(utxo_to_transfer).collect(); |
| 108 | + |
| 109 | + Ok((transfers, new_checkpoint)) |
| 110 | + } |
| 111 | + } |
| 112 | +); |
| 113 | + |
| 114 | +fn utxo_to_transfer<RecipientAddress>( |
| 115 | + utxo: BridgeUtxo, |
| 116 | +) -> Option<BridgeTransferV1<RecipientAddress>> |
| 117 | +where |
| 118 | + RecipientAddress: for<'a> TryFrom<&'a [u8]>, |
| 119 | +{ |
| 120 | + let token_delta = (utxo.tokens_out.0 as i128) - (utxo.tokens_in.0 as i128); |
| 121 | + |
| 122 | + if token_delta <= 0 { |
| 123 | + return None; |
| 124 | + } |
| 125 | + |
| 126 | + let token_amount = token_delta as u64; |
| 127 | + |
| 128 | + let transfer = match TokenTransferDatum::try_from(utxo.datum.0.clone()) { |
| 129 | + Ok(TokenTransferDatum::V1(TokenTransferDatumV1::UserTransfer { receiver })) => { |
| 130 | + match RecipientAddress::try_from(receiver.0.as_ref()) { |
| 131 | + Ok(recipient) => BridgeTransferV1::UserTransfer { token_amount, recipient }, |
| 132 | + Err(_) => { |
| 133 | + BridgeTransferV1::InvalidTransfer { token_amount, utxo_id: utxo.utxo_id() } |
| 134 | + }, |
| 135 | + } |
| 136 | + }, |
| 137 | + Ok(TokenTransferDatum::V1(TokenTransferDatumV1::ReserveTransfer)) => { |
| 138 | + BridgeTransferV1::ReserveTransfer { token_amount } |
| 139 | + }, |
| 140 | + Err(_) => BridgeTransferV1::InvalidTransfer { token_amount, utxo_id: utxo.utxo_id() }, |
| 141 | + }; |
| 142 | + |
| 143 | + Some(transfer) |
| 144 | +} |
0 commit comments