Skip to content

Commit

Permalink
change: ETCM-3980 move epoch derivation to the domain crate (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea authored Sep 30, 2024
1 parent 08b5163 commit bf430fe
Show file tree
Hide file tree
Showing 29 changed files with 155 additions and 230 deletions.
24 changes: 2 additions & 22 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ members = [
"primitives/block-rewards",
"primitives/chain-params",
"primitives/domain",
"primitives/epoch-derivation",
"primitives/mock-types",
"primitives/selection",
"primitives/sidechain-block-search",
Expand Down Expand Up @@ -195,7 +194,6 @@ plutus = { path = "utils/plutus", default-features = false}
plutus-datum-derive = { default-features = false, path = "utils/plutus/plutus-datum-derive" }
byte-string-derive = { default-features = false, path = "utils/byte-string-derivation" }
sidechain-slots = { path = "primitives/sidechain-slots", default-features = false }
epoch-derivation = { path = "primitives/epoch-derivation", default-features = false }
mock-types = { path = "primitives/mock-types", default-features = false }
cli-commands = { path = "cli/commands" }
partner-chains-node-commands = { path = "cli/node-commands"}
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ allowing to selectively query main chain state based on runtime version
This change requires migration of the node, PartnerChainsProposerFactory has to be used.
See `service.rs` in `partner-chains-node` crate for an example.
* renamed sidechain-main-cli and relevant naming to pc-contracts-cli
* ETCM-6092 - removed the `epoch-derivation` crate, moving epoch derivation logic to the `sidechain-domain`
crate. Removed `EpochConfig` wrapper type; code using it should be changed to use `MainchainEpochConfig`
type directly, `EpochConfig::read()` uses should be replaced by `MainchainEpochConfig::read_from_env()`.

## Fixed
* ETCM-8267 - fixed `partner-chains-cli` missing the native token configuration
Expand Down
1 change: 0 additions & 1 deletion mainchain-follower/db-sync-follower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ log = { workspace = true }
thiserror = { workspace = true }
blake2b_simd = { workspace = true }
figment = { workspace = true }
epoch-derivation = { workspace = true, features = ["std"]}
substrate-prometheus-endpoint = { workspace = true }
derive-new = { workspace = true }
lru = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions mainchain-follower/db-sync-follower/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::observed_async_trait;
use async_trait::async_trait;
use chrono::{DateTime, NaiveDateTime, TimeDelta};
use derive_new::new;
use epoch_derivation::{MainchainEpochConfig, MainchainEpochDerivation};
use figment::providers::Env;
use figment::Figment;
use log::{debug, info};
use main_chain_follower_api::{
block::MainchainBlock, common::Timestamp, BlockDataSource, DataSourceError::*, Result,
};
use serde::Deserialize;
use sidechain_domain::mainchain_epoch::{MainchainEpochConfig, MainchainEpochDerivation};
use sidechain_domain::McBlockHash;
use sqlx::PgPool;
use std::error::Error;
Expand Down Expand Up @@ -238,7 +238,7 @@ impl BlockDataSourceImpl {
.timestamp_millis()
.try_into()
.map_err(|_| BadRequest(format!("Datetime out of range: {dt:?}")))?;
let ts = epoch_derivation::Timestamp::from_unix_millis(millis);
let ts = sidechain_domain::mainchain_epoch::Timestamp::from_unix_millis(millis);
let slot = self
.mainchain_epoch_config
.timestamp_to_mainchain_slot_number(ts)
Expand Down
2 changes: 1 addition & 1 deletion mainchain-follower/db-sync-follower/src/block/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::block::{BlockDataSource, BlockDataSourceImpl, BlocksCache};
use chrono::{NaiveDateTime, TimeDelta};
use epoch_derivation::{Duration, MainchainEpochConfig, Timestamp};
use hex_literal::hex;
use main_chain_follower_api::block::MainchainBlock;
use sidechain_domain::mainchain_epoch::{Duration, MainchainEpochConfig, Timestamp};
use sidechain_domain::{McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber};
use sqlx::PgPool;
use std::str::FromStr;
Expand Down
9 changes: 4 additions & 5 deletions mainchain-follower/db-sync-follower/src/data_sources.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Data sources implementations that read from db-sync postgres.

#[cfg(feature = "block-source")]
use epoch_derivation::{EpochConfig, MainchainEpochConfig};
use figment::providers::Env;
use figment::Figment;
use serde::Deserialize;
#[cfg(feature = "block-source")]
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
pub use sqlx::PgPool;
use std::error::Error;
Expand All @@ -14,9 +14,8 @@ use std::str::FromStr;

#[cfg(feature = "block-source")]
pub fn read_mc_epoch_config() -> Result<MainchainEpochConfig, Box<dyn Error + Send + Sync>> {
Ok(EpochConfig::read()
.map_err(|e| format!("Failed to read main chain config: {}", e))?
.mc)
Ok(MainchainEpochConfig::read_from_env()
.map_err(|e| format!("Failed to read main chain config: {}", e))?)
}

#[derive(Debug, Clone, Deserialize)]
Expand Down
2 changes: 0 additions & 2 deletions mainchain-follower/mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ publish = false

[dependencies]
async-trait = { workspace = true }
epoch-derivation = { workspace = true, default-features = true }
hex = { workspace = true }
hex-literal = { workspace = true }
log = { workspace = true }
Expand All @@ -22,7 +21,6 @@ std = [
"serde_json/std",
"sidechain-domain/std",
"main-chain-follower-api/std",
"epoch-derivation/std",
"rand/std"
]
block-source = ["main-chain-follower-api/block-source"]
Expand Down
8 changes: 4 additions & 4 deletions mainchain-follower/mock/src/candidate.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use async_trait::async_trait;
use epoch_derivation::MainchainEpochConfig;
use hex_literal::hex;
use log::{debug, info};
use main_chain_follower_api::{candidate::*, Result};
use serde::*;
use sidechain_domain::byte_string::*;
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sidechain_domain::*;
use std::error::Error;

Expand Down Expand Up @@ -167,7 +167,7 @@ impl MockRegistrationsConfig {

pub struct MockCandidateDataSource {
pub registrations_data: MockRegistrationsConfig,
pub epoch_config: MainchainEpochConfig,
pub mc_epoch_config: MainchainEpochConfig,
}

impl MockCandidateDataSource {
Expand All @@ -179,8 +179,8 @@ impl MockCandidateDataSource {

pub fn from_env() -> std::result::Result<Self, Box<dyn Error + Send + Sync + 'static>> {
let registrations_data = MockRegistrationsConfig::read()?;
let epoch_config = epoch_derivation::EpochConfig::read()?.mc;
Ok(MockCandidateDataSource { registrations_data, epoch_config })
let mc_epoch_config = MainchainEpochConfig::read_from_env()?;
Ok(MockCandidateDataSource { registrations_data, mc_epoch_config })
}
}

Expand Down
1 change: 0 additions & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ name = "partner-chains-node"
async-trait = { workspace = true }
clap = { workspace = true }
futures = { workspace = true }
epoch-derivation = { workspace = true }
log = { workspace = true }
sc-cli = { workspace = true, features = ["rocksdb"] }
sp-core = { workspace = true }
Expand Down
12 changes: 6 additions & 6 deletions node/src/inherent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::main_chain_follower::DataSources;
use authority_selection_inherents::ariadne_inherent_data_provider::AriadneInherentDataProvider as AriadneIDP;
use authority_selection_inherents::authority_selection_inputs::AuthoritySelectionInputs;
use derive_new::new;
use epoch_derivation::EpochConfig;
use jsonrpsee::core::async_trait;
use sc_consensus_aura::{find_pre_digest, SlotDuration};
use sc_service::Arc;
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sidechain_domain::{McBlockHash, ScEpochNumber};
use sidechain_mc_hash::McHashInherentDataProvider as McHashIDP;
use sidechain_runtime::{
Expand Down Expand Up @@ -67,7 +67,7 @@ where
_extra_args: (),
) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
let Self { config, client, data_sources } = self;
let CreateInherentDataConfig { epoch_config, sc_slot_config, time_source } = config;
let CreateInherentDataConfig { mc_epoch_config, sc_slot_config, time_source } = config;

let (slot, timestamp) =
timestamp_and_slot_cidp(sc_slot_config.slot_duration, time_source.clone());
Expand All @@ -81,7 +81,7 @@ where
let ariadne_data_provider = AriadneIDP::new(
client.as_ref(),
sc_slot_config,
epoch_config,
mc_epoch_config,
parent_hash,
*slot,
data_sources.candidate.as_ref(),
Expand Down Expand Up @@ -146,7 +146,7 @@ where
(verified_block_slot, mc_hash): (Slot, McBlockHash),
) -> Result<Self::InherentDataProviders, Box<dyn Error + Send + Sync>> {
let Self { config, client, data_sources } = self;
let CreateInherentDataConfig { epoch_config, sc_slot_config, time_source, .. } = config;
let CreateInherentDataConfig { mc_epoch_config, sc_slot_config, time_source, .. } = config;

let timestamp = TimestampIDP::new(Timestamp::new(time_source.get_current_time_millis()));
let parent_header = client.expect_header(parent_hash)?;
Expand All @@ -164,7 +164,7 @@ where
let ariadne_data_provider = AriadneIDP::new(
client.as_ref(),
sc_slot_config,
epoch_config,
mc_epoch_config,
parent_hash,
verified_block_slot,
data_sources.candidate.as_ref(),
Expand Down Expand Up @@ -197,7 +197,7 @@ pub fn slot_from_predigest(

#[derive(new, Clone)]
pub(crate) struct CreateInherentDataConfig {
pub epoch_config: EpochConfig,
pub mc_epoch_config: MainchainEpochConfig,
// TODO ETCM-4079 make sure that this struct can be instantiated only if sidechain epoch duration is divisible by slot_duration
pub sc_slot_config: ScSlotConfig,
pub time_source: Arc<dyn TimeSource + Send + Sync>,
Expand Down
4 changes: 2 additions & 2 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::main_chain_follower::DataSources;
use authority_selection_inherents::authority_selection_inputs::AuthoritySelectionInputs;
use authority_selection_inherents::filter_invalid_candidates::CandidateValidationApi;
use chain_params::SidechainParams;
use epoch_derivation::EpochConfig;
use jsonrpsee::RpcModule;
use pallet_session_validator_management_rpc::*;
use pallet_sidechain_rpc::*;
Expand All @@ -20,6 +19,7 @@ use sc_consensus_grandpa_rpc::{Grandpa, GrandpaApiServer};
use sc_rpc::SubscriptionTaskExecutor;
pub use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::TransactionPool;
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sidechain_domain::ScEpochNumber;
use sidechain_runtime::CrossChainPublic;
use sidechain_runtime::{
Expand Down Expand Up @@ -110,7 +110,7 @@ where
module.merge(
SidechainRpc::new(
client.clone(),
EpochConfig::read().unwrap(),
MainchainEpochConfig::read_from_env().unwrap(),
main_chain_follower_data_sources.block.clone(),
time_source.clone(),
)
Expand Down
11 changes: 6 additions & 5 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::main_chain_follower::DataSources;
use crate::rpc::GrandpaDeps;
use db_sync_follower::metrics::register_metrics_warn_errors;
use db_sync_follower::metrics::McFollowerMetrics;
use epoch_derivation::EpochConfig;
use futures::FutureExt;
use sc_client_api::{Backend, BlockBackend};
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
Expand All @@ -15,6 +14,7 @@ use sc_partner_chains_consensus_aura::import_queue as partner_chains_aura_import
use sc_service::{error::Error as ServiceError, Configuration, TaskManager, WarpSyncParams};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
use sidechain_mc_hash::McHashInherentDigest;
use sidechain_runtime::{self, opaque::Block, RuntimeApi};
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
Expand Down Expand Up @@ -123,7 +123,8 @@ pub fn new_partial(
.map_err(sp_blockchain::Error::from)?;

let time_source = Arc::new(SystemTimeSource);
let epoch_config = EpochConfig::read().map_err(|err| ServiceError::Application(err.into()))?;
let epoch_config = MainchainEpochConfig::read_from_env()
.map_err(|err| ServiceError::Application(err.into()))?;
let inherent_config = CreateInherentDataConfig::new(epoch_config, sc_slot_config, time_source);

let import_queue = partner_chains_aura_import_queue::import_queue::<
Expand Down Expand Up @@ -309,10 +310,10 @@ pub async fn new_full<Network: sc_network::NetworkBackend<Block, <Block as Block
let sc_slot_config = sidechain_slots::runtime_api_client::slot_config(&*client)
.map_err(sp_blockchain::Error::from)?;
let time_source = Arc::new(SystemTimeSource);
let epoch_config =
EpochConfig::read().map_err(|err| ServiceError::Application(err.into()))?;
let mc_epoch_config = MainchainEpochConfig::read_from_env()
.map_err(|err| ServiceError::Application(err.into()))?;
let inherent_config =
CreateInherentDataConfig::new(epoch_config, sc_slot_config.clone(), time_source);
CreateInherentDataConfig::new(mc_epoch_config, sc_slot_config.clone(), time_source);
let aura = sc_partner_chains_consensus_aura::start_aura::<
AuraPair,
_,
Expand Down
Loading

0 comments on commit bf430fe

Please sign in to comment.