Skip to content

Commit 4ed56dc

Browse files
committed
wip
1 parent eca42ef commit 4ed56dc

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

toolkit/bridge/primitives/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use alloc::vec::*;
88
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
99
use scale_info::TypeInfo;
1010
use serde::{Deserialize, Serialize};
11-
use sidechain_domain::{AssetName, MainchainAddress, McBlockHash, McBlockNumber, PolicyId, UtxoId};
11+
use sidechain_domain::{
12+
AssetId, AssetName, MainchainAddress, McBlockHash, McBlockNumber, PolicyId, UtxoId,
13+
};
1214
use sp_inherents::*;
1315

1416
#[cfg(feature = "std")]
@@ -40,6 +42,16 @@ pub struct MainChainScripts {
4042
pub illiquid_circulation_supply_validator_address: MainchainAddress,
4143
}
4244

45+
impl MainChainScripts {
46+
/// Return full asset ID fo the bridged token (minting policy ID and asset name)
47+
pub fn asset_id(&self) -> AssetId {
48+
AssetId {
49+
policy_id: self.token_policy_id.clone(),
50+
asset_name: self.token_asset_name.clone(),
51+
}
52+
}
53+
}
54+
4355
#[cfg(feature = "std")]
4456
impl MainChainScripts {
4557
/// Reads the main chain script values from environment

toolkit/data-sources/db-sync/src/bridge/cache.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ observed_async_trait!(
125125

126126
let to_block = self.get_block_by_hash(&current_mc_block_hash).await?.number.into();
127127

128-
let data_checkpoint =
129-
self.domain_data_checkpoint_to_db_checkpoint(&data_checkpoint).await?;
128+
let data_checkpoint = self.resolve_data_checkpoint(&data_checkpoint).await?;
130129

131130
let utxos =
132131
match self.try_serve_from_cache(&data_checkpoint, to_block, max_transfers).await {
@@ -170,6 +169,7 @@ impl CachedTokenBridgeDataSourceImpl {
170169
cache_lookahead,
171170
}
172171
}
172+
173173
async fn set_cache_mc_scripts(&self, main_chain_scripts: MainChainScripts) {
174174
let mut cache = self.cache.lock().await;
175175
cache.set_mc_scripts(main_chain_scripts.clone());
@@ -191,16 +191,11 @@ impl CachedTokenBridgeDataSourceImpl {
191191
data_checkpoint: &BridgeCheckpoint,
192192
to_block: BlockNumber,
193193
) -> Result<(), Box<dyn Error + Send + Sync>> {
194-
let from_block = match data_checkpoint {
195-
BridgeCheckpoint::Block { number } => number.clone(),
196-
BridgeCheckpoint::Utxo { block_number, .. } => block_number.clone(),
197-
};
198-
let data_checkpoint = BridgeCheckpoint::Block { number: from_block.saturating_sub(1u32) };
194+
let from_block = data_checkpoint.get_block_number();
199195

200-
let asset = Asset {
201-
policy_id: main_chain_scripts.token_policy_id.into(),
202-
asset_name: main_chain_scripts.token_asset_name.into(),
203-
};
196+
// We want to load all data in the block of `data_checkpoint`, so we go one block back.
197+
let effective_data_checkpoint =
198+
BridgeCheckpoint::Block { number: from_block.saturating_sub(1u32) };
204199

205200
let latest_block = self.get_latest_stable_block().await?.unwrap_or(to_block);
206201

@@ -210,9 +205,9 @@ impl CachedTokenBridgeDataSourceImpl {
210205
let utxos = get_bridge_utxos_tx(
211206
self.db_sync_config.get_tx_in_config().await?,
212207
&self.pool,
213-
&main_chain_scripts.illiquid_circulation_supply_validator_address.into(),
214-
asset,
215-
data_checkpoint,
208+
&main_chain_scripts.illiquid_circulation_supply_validator_address.clone().into(),
209+
main_chain_scripts.asset_id().into(),
210+
effective_data_checkpoint,
216211
to_block.into(),
217212
None,
218213
)
@@ -253,7 +248,7 @@ impl CachedTokenBridgeDataSourceImpl {
253248
.ok_or(format!("Could not find block info for utxo: {utxo_id:?}").into())
254249
}
255250

256-
async fn domain_data_checkpoint_to_db_checkpoint(
251+
async fn resolve_data_checkpoint(
257252
&self,
258253
data_checkpoint: &BridgeDataCheckpoint,
259254
) -> Result<BridgeCheckpoint, Box<dyn Error + Send + Sync>> {

toolkit/data-sources/db-sync/src/db_model.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,16 @@ pub(crate) enum BridgeCheckpoint {
949949
Block { number: BlockNumber },
950950
}
951951

952+
#[cfg(feature = "bridge")]
953+
impl BridgeCheckpoint {
954+
pub(crate) fn get_block_number(&self) -> BlockNumber {
955+
match self {
956+
BridgeCheckpoint::Block { number } => number.clone(),
957+
BridgeCheckpoint::Utxo { block_number, .. } => block_number.clone(),
958+
}
959+
}
960+
}
961+
952962
#[cfg(feature = "bridge")]
953963
pub(crate) async fn get_bridge_utxos_tx(
954964
tx_in_configuration: TxInConfiguration,

0 commit comments

Comments
 (0)