Skip to content

Commit 0147a6f

Browse files
authored
add: bridge data source mock + wiring (#985)
1 parent 88f8548 commit 0147a6f

File tree

11 files changed

+104
-8
lines changed

11 files changed

+104
-8
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/node/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ derive-new = { workspace = true }
7171
partner-chains-cli = { workspace = true }
7272
partner-chains-node-commands = { workspace = true }
7373
envy = { workspace = true }
74+
sp-partner-chains-bridge = { workspace = true }
7475

7576
# These dependencies are used for the node template's RPCs
7677
jsonrpsee = { workspace = true }
@@ -109,6 +110,7 @@ partner-chains-mock-data-sources = { workspace = true, features = [
109110
"mc-hash",
110111
"sidechain-rpc",
111112
"block-participation",
113+
"bridge",
112114
] }
113115
tokio = { workspace = true }
114116
sp-block-participation = { workspace = true, features = ["std"] }

demo/node/src/data_sources.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ use partner_chains_db_sync_data_sources::{
55
McFollowerMetrics, McHashDataSourceImpl, NativeTokenManagementDataSourceImpl,
66
SidechainRpcDataSourceImpl, StakeDistributionDataSourceImpl,
77
};
8+
use partner_chains_demo_runtime::AccountId;
89
use partner_chains_mock_data_sources::{
910
AuthoritySelectionDataSourceMock, BlockDataSourceMock, GovernedMapDataSourceMock,
1011
McHashDataSourceMock, NativeTokenDataSourceMock, SidechainRpcDataSourceMock,
11-
StakeDistributionDataSourceMock,
12+
StakeDistributionDataSourceMock, TokenBridgeDataSourceMock,
1213
};
1314
use sc_service::error::Error as ServiceError;
1415
use sidechain_mc_hash::McHashDataSource;
1516
use sp_block_participation::inherent_data::BlockParticipationDataSource;
1617
use sp_governed_map::GovernedMapDataSource;
1718
use sp_native_token_management::NativeTokenManagementDataSource;
19+
use sp_partner_chains_bridge::TokenBridgeDataSource;
1820
use std::{error::Error, sync::Arc};
1921

2022
pub const DATA_SOURCE_VAR: &str = "CARDANO_DATA_SOURCE";
@@ -68,6 +70,7 @@ pub struct DataSources {
6870
pub sidechain_rpc: Arc<dyn SidechainRpcDataSource + Send + Sync>,
6971
pub block_participation: Arc<dyn BlockParticipationDataSource + Send + Sync>,
7072
pub governed_map: Arc<dyn GovernedMapDataSource + Send + Sync>,
73+
pub bridge: Arc<dyn TokenBridgeDataSource<AccountId> + Send + Sync>,
7174
}
7275

7376
pub(crate) async fn create_cached_data_sources(
@@ -105,6 +108,7 @@ pub fn create_mock_data_sources()
105108
native_token: Arc::new(NativeTokenDataSourceMock::new()),
106109
block_participation: Arc::new(StakeDistributionDataSourceMock::new()),
107110
governed_map: Arc::new(GovernedMapDataSourceMock::default()),
111+
bridge: Arc::new(TokenBridgeDataSourceMock::new()),
108112
})
109113
}
110114

@@ -140,12 +144,13 @@ pub async fn create_cached_db_sync_data_sources(
140144
)),
141145
governed_map: Arc::new(
142146
GovernedMapDataSourceCachedImpl::new(
143-
pool,
147+
pool.clone(),
144148
metrics_opt.clone(),
145149
GOVERNED_MAP_CACHE_SIZE,
146150
block,
147151
)
148152
.await?,
149153
),
154+
bridge: Arc::new(TokenBridgeDataSourceMock::new()),
150155
})
151156
}

demo/node/src/inherent_data.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use authority_selection_inherents::{
55
use derive_new::new;
66
use jsonrpsee::core::async_trait;
77
use partner_chains_demo_runtime::{
8-
BlockAuthor, CrossChainPublic,
8+
AccountId, BlockAuthor, CrossChainPublic,
99
opaque::{Block, SessionKeys},
1010
};
1111
use sc_consensus_aura::{SlotDuration, find_pre_digest};
@@ -32,6 +32,9 @@ use sp_native_token_management::{
3232
NativeTokenManagementApi, NativeTokenManagementDataSource,
3333
NativeTokenManagementInherentDataProvider as NativeTokenIDP,
3434
};
35+
use sp_partner_chains_bridge::{
36+
TokenBridgeDataSource, TokenBridgeIDPRuntimeApi, TokenBridgeInherentDataProvider,
37+
};
3538
use sp_partner_chains_consensus_aura::CurrentSlotProvider;
3639
use sp_runtime::traits::{Block as BlockT, Header, Zero};
3740
use sp_session_validator_management::SessionValidatorManagementApi;
@@ -48,6 +51,7 @@ pub struct ProposalCIDP<T> {
4851
native_token_data_source: Arc<dyn NativeTokenManagementDataSource + Send + Sync>,
4952
block_participation_data_source: Arc<dyn BlockParticipationDataSource + Send + Sync>,
5053
governed_map_data_source: Arc<dyn GovernedMapDataSource + Send + Sync>,
54+
bridge_data_source: Arc<dyn TokenBridgeDataSource<AccountId> + Send + Sync>,
5155
}
5256

5357
#[async_trait]
@@ -65,6 +69,7 @@ where
6569
T::Api: BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>>,
6670
T::Api: BlockParticipationApi<Block, BlockAuthor>,
6771
T::Api: GovernedMapIDPApi<Block>,
72+
T::Api: TokenBridgeIDPRuntimeApi<Block>,
6873
{
6974
type InherentDataProviders = (
7075
AuraIDP,
@@ -75,6 +80,7 @@ where
7580
NativeTokenIDP,
7681
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
7782
GovernedMapInherentDataProvider,
83+
TokenBridgeInherentDataProvider<AccountId>,
7884
);
7985

8086
async fn create_inherent_data_providers(
@@ -90,6 +96,7 @@ where
9096
native_token_data_source,
9197
block_participation_data_source,
9298
governed_map_data_source,
99+
bridge_data_source,
93100
} = self;
94101
let CreateInherentDataConfig { mc_epoch_config, sc_slot_config, time_source } = config;
95102

@@ -145,6 +152,14 @@ where
145152
)
146153
.await?;
147154

155+
let bridge = TokenBridgeInherentDataProvider::new(
156+
client.as_ref(),
157+
parent_hash,
158+
mc_hash.mc_hash(),
159+
bridge_data_source.as_ref(),
160+
)
161+
.await?;
162+
148163
Ok((
149164
slot,
150165
timestamp,
@@ -154,6 +169,7 @@ where
154169
native_token,
155170
payouts,
156171
governed_map,
172+
bridge,
157173
))
158174
}
159175
}
@@ -167,6 +183,7 @@ pub struct VerifierCIDP<T> {
167183
native_token_data_source: Arc<dyn NativeTokenManagementDataSource + Send + Sync>,
168184
block_participation_data_source: Arc<dyn BlockParticipationDataSource + Send + Sync>,
169185
governed_map_data_source: Arc<dyn GovernedMapDataSource + Send + Sync>,
186+
bridge_data_source: Arc<dyn TokenBridgeDataSource<AccountId> + Send + Sync>,
170187
}
171188

172189
impl<T: Send + Sync> CurrentSlotProvider for VerifierCIDP<T> {
@@ -189,6 +206,7 @@ where
189206
T::Api: BlockProductionLogApi<Block, CommitteeMember<CrossChainPublic, SessionKeys>>,
190207
T::Api: BlockParticipationApi<Block, BlockAuthor>,
191208
T::Api: GovernedMapIDPApi<Block>,
209+
T::Api: TokenBridgeIDPRuntimeApi<Block>,
192210
{
193211
type InherentDataProviders = (
194212
TimestampIDP,
@@ -197,6 +215,7 @@ where
197215
NativeTokenIDP,
198216
BlockParticipationInherentDataProvider<BlockAuthor, DelegatorKey>,
199217
GovernedMapInherentDataProvider,
218+
TokenBridgeInherentDataProvider<AccountId>,
200219
);
201220

202221
async fn create_inherent_data_providers(
@@ -212,6 +231,7 @@ where
212231
native_token_data_source,
213232
block_participation_data_source,
214233
governed_map_data_source,
234+
bridge_data_source,
215235
} = self;
216236
let CreateInherentDataConfig { mc_epoch_config, sc_slot_config, time_source, .. } = config;
217237

@@ -264,19 +284,28 @@ where
264284
let governed_map = GovernedMapInherentDataProvider::new(
265285
client.as_ref(),
266286
parent_hash,
267-
mc_hash,
287+
mc_hash.clone(),
268288
mc_state_reference.previous_mc_hash(),
269289
governed_map_data_source.as_ref(),
270290
)
271291
.await?;
272292

293+
let bridge = TokenBridgeInherentDataProvider::new(
294+
client.as_ref(),
295+
parent_hash,
296+
mc_hash,
297+
bridge_data_source.as_ref(),
298+
)
299+
.await?;
300+
273301
Ok((
274302
timestamp,
275303
ariadne_data_provider,
276304
block_producer_id_provider,
277305
native_token,
278306
payouts,
279307
governed_map,
308+
bridge,
280309
))
281310
}
282311
}

demo/node/src/service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub fn new_partial(
163163
data_sources.native_token.clone(),
164164
data_sources.block_participation.clone(),
165165
data_sources.governed_map.clone(),
166+
data_sources.bridge.clone(),
166167
),
167168
spawner: &task_manager.spawn_essential_handle(),
168169
registry: config.prometheus_registry(),
@@ -352,6 +353,7 @@ pub async fn new_full_base<Network: sc_network::NetworkBackend<Block, <Block as
352353
data_sources.native_token.clone(),
353354
data_sources.block_participation,
354355
data_sources.governed_map,
356+
data_sources.bridge.clone(),
355357
),
356358
force_authoring,
357359
backoff_authoring_blocks,

demo/node/src/tests/inherent_data_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use authority_selection_inherents::{
66
AuthoritySelectionInputs, mock::MockAuthoritySelectionDataSource,
77
};
88
use hex_literal::hex;
9-
use partner_chains_demo_runtime::BlockAuthor;
10-
use partner_chains_mock_data_sources::GovernedMapDataSourceMock;
9+
use partner_chains_demo_runtime::{AccountId, BlockAuthor};
1110
use partner_chains_mock_data_sources::StakeDistributionDataSourceMock;
11+
use partner_chains_mock_data_sources::{GovernedMapDataSourceMock, TokenBridgeDataSourceMock};
1212
use sidechain_domain::{
1313
DelegatorKey, MainchainBlock, McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber,
1414
NativeTokenAmount, ScEpochNumber,
@@ -55,6 +55,7 @@ async fn block_proposal_cidp_should_be_created_correctly() {
5555
Arc::new(native_token_data_source),
5656
Arc::new(StakeDistributionDataSourceMock::new()),
5757
Arc::new(GovernedMapDataSourceMock::default()),
58+
Arc::new(TokenBridgeDataSourceMock::<AccountId>::new()),
5859
)
5960
.create_inherent_data_providers(mock_header().hash(), ())
6061
.await
@@ -153,6 +154,7 @@ async fn block_verification_cidp_should_be_created_correctly() {
153154
Arc::new(native_token_data_source),
154155
Arc::new(StakeDistributionDataSourceMock::new()),
155156
Arc::new(GovernedMapDataSourceMock::default()),
157+
Arc::new(TokenBridgeDataSourceMock::new()),
156158
);
157159

158160
let inherent_data_providers = verifier_cidp

demo/node/src/tests/runtime_api_mock.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ sp_api::mock_impl_runtime_apis! {
146146
1
147147
}
148148
}
149+
150+
impl sp_partner_chains_bridge::TokenBridgeIDPRuntimeApi<Block> for TestApi {
151+
fn get_pallet_version() -> u32 {
152+
1
153+
}
154+
fn get_main_chain_scripts() -> Option<sp_partner_chains_bridge::MainChainScripts> {
155+
None
156+
}
157+
fn get_max_transfers_per_block() -> u32 {
158+
32
159+
}
160+
fn get_last_data_checkpoint() -> Option<sp_partner_chains_bridge::BridgeDataCheckpoint> {
161+
None
162+
}
163+
}
149164
}
150165

151166
impl HeaderBackend<Block> for TestApi {

toolkit/bridge/primitives/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub struct BridgeDataCheckpoint(pub UtxoId);
133133
/// Interface for data sources that can be used by [TokenBridgeInherentDataProvider]
134134
#[cfg(feature = "std")]
135135
#[async_trait::async_trait]
136-
pub trait TokenBridgeDataSource<RecipientAddress> {
136+
pub trait TokenBridgeDataSource<RecipientAddress>: Send + Sync {
137137
/// Fetches at most `max_transfers` of token bridge transfers after `data_checkpoint` up to `current_mc_block`
138138
async fn get_transfers(
139139
&self,
@@ -219,7 +219,7 @@ impl<RecipientAddress: Encode + Send + Sync> TokenBridgeInherentDataProvider<Rec
219219
client: &T,
220220
parent_hash: Block::Hash,
221221
current_mc_hash: McBlockHash,
222-
data_source: &dyn TokenBridgeDataSource<RecipientAddress>,
222+
data_source: &(dyn TokenBridgeDataSource<RecipientAddress> + Send + Sync),
223223
) -> Result<Self, InherentDataCreationError>
224224
where
225225
Block: BlockT,

toolkit/data-sources/mock/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ sidechain-mc-hash = { workspace = true, optional = true }
2929
sp-block-participation = { workspace = true, optional = true }
3030
pallet-sidechain-rpc = { workspace = true, optional = true }
3131
authority-selection-inherents = { workspace = true, optional = true }
32+
sp-partner-chains-bridge = { workspace = true, optional = true }
3233

3334
[features]
3435
default = ["std", "block-source", "candidate-source", "governed-map", "native-token"]
@@ -48,3 +49,4 @@ native-token = ["sp-native-token-management"]
4849
mc-hash = ["sidechain-mc-hash"]
4950
sidechain-rpc = ["pallet-sidechain-rpc"]
5051
block-participation = ["sp-block-participation"]
52+
bridge = ["sp-partner-chains-bridge"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::Result;
2+
use sidechain_domain::*;
3+
use sp_partner_chains_bridge::{
4+
BridgeDataCheckpoint, BridgeTransferV1, MainChainScripts, TokenBridgeDataSource,
5+
};
6+
use std::marker::PhantomData;
7+
/// Mocked token bridge data source
8+
pub struct TokenBridgeDataSourceMock<RecipientAddress> {
9+
_phantom: PhantomData<RecipientAddress>,
10+
}
11+
12+
impl<RecipientAddress> TokenBridgeDataSourceMock<RecipientAddress> {
13+
/// Creates a new mocked token bridge data source
14+
pub fn new() -> Self {
15+
Self { _phantom: Default::default() }
16+
}
17+
}
18+
19+
#[async_trait::async_trait]
20+
impl<RecipientAddress: Send + Sync> TokenBridgeDataSource<RecipientAddress>
21+
for TokenBridgeDataSourceMock<RecipientAddress>
22+
{
23+
async fn get_transfers(
24+
&self,
25+
_main_chain_scripts: MainChainScripts,
26+
_data_checkpoint: BridgeDataCheckpoint,
27+
_max_transfers: u32,
28+
_current_mc_block: McBlockHash,
29+
) -> Result<(Vec<BridgeTransferV1<RecipientAddress>>, BridgeDataCheckpoint)> {
30+
Ok((vec![], Default::default()))
31+
}
32+
}

0 commit comments

Comments
 (0)