Skip to content

Commit 8b74141

Browse files
committed
change: move data epoch offset calculation out of data sources
1 parent 8a7a660 commit 8b74141

File tree

9 files changed

+47
-117
lines changed

9 files changed

+47
-117
lines changed

demo/node/src/tests/inherent_data_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async fn block_proposal_cidp_should_be_created_correctly() {
4343

4444
let inherent_data_providers = ProposalCIDP::new(
4545
test_create_inherent_data_config(),
46-
TestApi::new(ScEpochNumber(2))
46+
TestApi::new(ScEpochNumber(20))
4747
.with_headers([(mock_header().hash(), mock_header())])
4848
.with_pariticipation_data(vec![(past_block_slot(), past_block_author())])
4949
.into(),
@@ -124,7 +124,7 @@ async fn block_verification_cidp_should_be_created_correctly() {
124124

125125
let verifier_cidp = VerifierCIDP::new(
126126
create_inherent_data_config.clone(),
127-
TestApi::new(ScEpochNumber(2))
127+
TestApi::new(ScEpochNumber(20))
128128
.with_pariticipation_data(vec![(past_block_slot(), past_block_author())])
129129
.into(),
130130
Arc::new(mc_hash_data_source),

toolkit/committee-selection/authority-selection-inherents/src/ariadne_inherent_data_provider.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,31 @@ impl AriadneInherentDataProvider {
6262
timestamp_millis,
6363
)?;
6464

65-
let data_epoch = data_source.data_epoch(for_mc_epoch).await?;
65+
let data_epoch = offset_data_epoch(&for_mc_epoch).expect(&format!(
66+
"Could not apply data offset to MC epoch {} for authority selection; this is caused either by misconfiguration or a bug.",
67+
for_mc_epoch.0,
68+
));
69+
6670
// We could accept mc_reference at last slot of data_epoch, but calculations are much easier like that.
6771
// Additionally, in current implementation, the inequality below is always true, thus there is no need to make it more accurate.
6872
let scripts = client.runtime_api().get_main_chain_scripts(parent_hash)?;
6973
if data_epoch < mc_reference_epoch {
70-
Ok(AriadneInherentDataProvider::from_mc_data(data_source, for_mc_epoch, scripts)
71-
.await?)
74+
Ok(AriadneInherentDataProvider::from_mc_data(data_source, data_epoch, scripts).await?)
7275
} else {
7376
Ok(AriadneInherentDataProvider { data: None })
7477
}
7578
}
7679

7780
async fn from_mc_data(
7881
candidate_data_source: &(dyn AuthoritySelectionDataSource + Send + Sync),
79-
for_epoch: McEpochNumber,
82+
data_epoch: McEpochNumber,
8083
scripts: MainChainScripts,
8184
) -> Result<Self, InherentProviderCreationError> {
8285
use crate::authority_selection_inputs::authority_selection_inputs_from_mc_data;
8386

8487
Ok(Self {
8588
data: Some(
86-
authority_selection_inputs_from_mc_data(candidate_data_source, for_epoch, scripts)
89+
authority_selection_inputs_from_mc_data(candidate_data_source, data_epoch, scripts)
8790
.await?,
8891
),
8992
})

toolkit/committee-selection/authority-selection-inherents/src/authority_selection_inputs.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ pub struct AriadneParameters {
5050
#[async_trait::async_trait]
5151
pub trait AuthoritySelectionDataSource {
5252
/// Returns D-parameter and list of permissioned candidates that is effective for the given epoch.
53-
/// The data from the latest block of `data_epoch(epoch)` will be used if available, otherwise returns data at the latest block of the chain.
5453
async fn get_ariadne_parameters(
5554
&self,
5655
epoch_number: McEpochNumber,
@@ -59,7 +58,6 @@ pub trait AuthoritySelectionDataSource {
5958
) -> Result<AriadneParameters, Box<dyn std::error::Error + Send + Sync>>;
6059

6160
/// Returns the list of registrations that is effective for the given epoch.
62-
/// The data from the latest block of `data_epoch(epoch)` will be used if available, otherwise returns data at the latest block of the chain.
6361
/// Each item is a list of one candidate registrations.
6462
async fn get_candidates(
6563
&self,
@@ -72,37 +70,24 @@ pub trait AuthoritySelectionDataSource {
7270
&self,
7371
epoch: McEpochNumber,
7472
) -> Result<Option<EpochNonce>, Box<dyn std::error::Error + Send + Sync>>;
75-
76-
///
77-
/// # Arguments
78-
///
79-
/// * `for_epoch`: main chain epoch number during which candidate data is meant to be used
80-
///
81-
/// returns: Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>> - data source methods called with `for_epoch` will query only for data which was stored on main chain in the returned epoch or earlier
82-
///
83-
///
84-
async fn data_epoch(
85-
&self,
86-
for_epoch: McEpochNumber,
87-
) -> Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>>;
8873
}
8974

9075
#[cfg(feature = "std")]
9176
pub(crate) async fn authority_selection_inputs_from_mc_data(
9277
candidate_data_source: &(dyn AuthoritySelectionDataSource + Send + Sync),
93-
for_epoch: McEpochNumber,
78+
data_epoch: McEpochNumber,
9479
scripts: sp_session_validator_management::MainChainScripts,
9580
) -> Result<AuthoritySelectionInputs, AuthoritySelectionInputsCreationError> {
9681
let ariadne_parameters_response = candidate_data_source
9782
.get_ariadne_parameters(
98-
for_epoch,
83+
data_epoch,
9984
scripts.d_parameter_policy_id.clone(),
10085
scripts.permissioned_candidates_policy_id.clone(),
10186
)
10287
.await
10388
.map_err(|err| {
10489
AuthoritySelectionInputsCreationError::AriadneParametersQuery(
105-
for_epoch,
90+
data_epoch,
10691
scripts.d_parameter_policy_id.clone(),
10792
scripts.permissioned_candidates_policy_id.clone(),
10893
err,
@@ -115,7 +100,7 @@ pub(crate) async fn authority_selection_inputs_from_mc_data(
115100
None if no_permissioned_candidates_expected => Vec::new(),
116101
None => {
117102
return Err(AuthoritySelectionInputsCreationError::AriadneParametersQuery(
118-
for_epoch,
103+
data_epoch,
119104
scripts.d_parameter_policy_id,
120105
scripts.permissioned_candidates_policy_id,
121106
("Expected Data Not Found: Permissioned Candidates List".to_string()).into(),
@@ -125,19 +110,19 @@ pub(crate) async fn authority_selection_inputs_from_mc_data(
125110
};
126111

127112
let registered_candidates: Vec<CandidateRegistrations> = candidate_data_source
128-
.get_candidates(for_epoch, scripts.committee_candidate_address.clone())
113+
.get_candidates(data_epoch, scripts.committee_candidate_address.clone())
129114
.await
130115
.map_err(|err| {
131116
AuthoritySelectionInputsCreationError::GetCandidatesQuery(
132-
for_epoch,
117+
data_epoch,
133118
scripts.committee_candidate_address.to_string(),
134119
err,
135120
)
136121
})?;
137-
let epoch_nonce_response = candidate_data_source
138-
.get_epoch_nonce(for_epoch)
139-
.await
140-
.map_err(|err| AuthoritySelectionInputsCreationError::GetEpochNonceQuery(for_epoch, err))?;
122+
let epoch_nonce_response =
123+
candidate_data_source.get_epoch_nonce(data_epoch).await.map_err(|err| {
124+
AuthoritySelectionInputsCreationError::GetEpochNonceQuery(data_epoch, err)
125+
})?;
141126
let epoch_nonce = epoch_nonce_response.unwrap_or(EpochNonce(vec![]));
142127

143128
Ok(AuthoritySelectionInputs {

toolkit/committee-selection/authority-selection-inherents/src/mock.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Default for MockAuthoritySelectionDataSource {
2020
fn default() -> Self {
2121
Self {
2222
candidates: vec![vec![], vec![]],
23-
permissioned_candidates: vec![Some(vec![]), Some(vec![])],
23+
permissioned_candidates: vec![Some(vec![]), Some(vec![]), Some(vec![])],
2424
num_permissioned_candidates: 3,
2525
}
2626
}
@@ -80,11 +80,4 @@ impl AuthoritySelectionDataSource for MockAuthoritySelectionDataSource {
8080
) -> Result<Option<EpochNonce>, Box<dyn std::error::Error + Send + Sync>> {
8181
Ok(Some(EpochNonce(vec![42u8])))
8282
}
83-
84-
async fn data_epoch(
85-
&self,
86-
for_epoch: McEpochNumber,
87-
) -> Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>> {
88-
Ok(for_epoch)
89-
}
9083
}

toolkit/data-sources/db-sync/src/candidates/cached.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ impl AuthoritySelectionDataSource for CandidateDataSourceCached {
7171
) -> Result<Option<EpochNonce>, Box<dyn std::error::Error + Send + Sync>> {
7272
self.inner.get_epoch_nonce(epoch).await
7373
}
74-
75-
async fn data_epoch(
76-
&self,
77-
for_epoch: McEpochNumber,
78-
) -> Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>> {
79-
self.inner.data_epoch(for_epoch).await
80-
}
8174
}
8275

8376
#[derive(Debug, Clone, Deserialize)]
@@ -176,9 +169,8 @@ impl CandidateDataSourceCached {
176169

177170
async fn can_use_caching_for_request(
178171
&self,
179-
request_epoch: McEpochNumber,
172+
data_epoch: McEpochNumber,
180173
) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
181-
let data_epoch = self.inner.data_epoch(request_epoch).await?;
182174
if let Ok(stable_epoch) = self.highest_seen_stable_epoch.lock() {
183175
if stable_epoch.map_or(false, |stable_epoch| stable_epoch >= data_epoch) {
184176
return Ok(true);

toolkit/data-sources/db-sync/src/candidates/mod.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl AuthoritySelectionDataSource for CandidatesDataSourceImpl {
6161
d_parameter_policy: PolicyId,
6262
permissioned_candidate_policy: PolicyId
6363
) -> Result<AriadneParameters, Box<dyn std::error::Error + Send + Sync>> {
64-
let epoch = EpochNumber::from(self.get_epoch_of_data_storage(epoch)?);
64+
let epoch = EpochNumber::from(epoch);
6565
let d_parameter_asset = Asset::new(d_parameter_policy);
6666
let permissioned_candidate_asset = Asset::new(permissioned_candidate_policy);
6767

@@ -97,7 +97,7 @@ impl AuthoritySelectionDataSource for CandidatesDataSourceImpl {
9797
epoch: McEpochNumber,
9898
committee_candidate_address: MainchainAddress
9999
)-> Result<Vec<CandidateRegistrations>, Box<dyn std::error::Error + Send + Sync>> {
100-
let epoch = EpochNumber::from(self.get_epoch_of_data_storage(epoch)?);
100+
let epoch = EpochNumber::from(epoch);
101101
let candidates = self.get_registered_candidates(epoch, committee_candidate_address).await?;
102102
let stake_map = Self::make_stake_map(db_model::get_stake_distribution(&self.pool, epoch).await?);
103103
Ok(Self::group_candidates_by_mc_pub_key(candidates).into_iter().map(|(mainchain_pub_key, candidate_registrations)| {
@@ -110,14 +110,9 @@ impl AuthoritySelectionDataSource for CandidatesDataSourceImpl {
110110
}
111111

112112
async fn get_epoch_nonce(&self, epoch: McEpochNumber) -> Result<Option<EpochNonce>, Box<dyn std::error::Error + Send + Sync>> {
113-
let epoch = self.get_epoch_of_data_storage(epoch)?;
114113
let nonce = db_model::get_epoch_nonce(&self.pool, EpochNumber(epoch.0)).await?;
115114
Ok(nonce.map(|n| EpochNonce(n.0)))
116115
}
117-
118-
async fn data_epoch(&self, for_epoch: McEpochNumber) -> Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>> {
119-
self.get_epoch_of_data_storage(for_epoch)
120-
}
121116
});
122117

123118
impl CandidatesDataSourceImpl {
@@ -310,17 +305,4 @@ impl CandidatesDataSourceImpl {
310305
})
311306
.collect()
312307
}
313-
314-
fn get_epoch_of_data_storage(
315-
&self,
316-
epoch_of_data_usage: McEpochNumber,
317-
) -> Result<McEpochNumber, Box<dyn std::error::Error + Send + Sync>> {
318-
offset_data_epoch(&epoch_of_data_usage).map_err(|offset| {
319-
BadRequest(format!(
320-
"Minimum supported epoch of data usage is {offset}, but {} was provided",
321-
epoch_of_data_usage.0
322-
))
323-
.into()
324-
})
325-
}
326308
}

0 commit comments

Comments
 (0)