Skip to content

Commit

Permalink
make connect optional in genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Oct 30, 2024
1 parent e5bc4ce commit fd69353
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 85 deletions.
86 changes: 28 additions & 58 deletions crates/astria-core/src/protocol/genesis/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub struct GenesisAppState {
ibc_parameters: IBCParameters,
allowed_fee_assets: Vec<asset::Denom>,
fees: GenesisFees,
connect: ConnectGenesis,
connect: Option<ConnectGenesis>,
}

impl GenesisAppState {
Expand Down Expand Up @@ -232,7 +232,7 @@ impl GenesisAppState {
}

#[must_use]
pub fn connect(&self) -> &ConnectGenesis {
pub fn connect(&self) -> &Option<ConnectGenesis> {
&self.connect
}

Expand Down Expand Up @@ -267,23 +267,24 @@ impl GenesisAppState {
self.ensure_address_has_base_prefix(address, &format!(".ibc_relayer_addresses[{i}]"))?;
}

for (i, address) in self
.connect
.market_map
.params
.market_authorities
.iter()
.enumerate()
{
if let Some(connect) = &self.connect {
for (i, address) in connect
.market_map
.params
.market_authorities
.iter()
.enumerate()
{
self.ensure_address_has_base_prefix(
address,
&format!(".market_map.params.market_authorities[{i}]"),
)?;
}
self.ensure_address_has_base_prefix(
address,
&format!(".market_map.params.market_authorities[{i}]"),
&connect.market_map.params.admin,
".market_map.params.admin",
)?;
}
self.ensure_address_has_base_prefix(
&self.connect.market_map.params.admin,
".market_map.params.admin",
)?;

Ok(())
}
Expand All @@ -297,7 +298,6 @@ impl Protobuf for GenesisAppState {
#[expect(
clippy::allow_attributes,
clippy::allow_attributes_without_reason,
clippy::too_many_lines,
reason = "false positive on `allowed_fee_assets` due to \"allow\" in the name"
)]
fn try_from_raw_ref(raw: &Self::Raw) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -372,26 +372,11 @@ impl Protobuf for GenesisAppState {
.ok_or_else(|| Self::Error::field_not_set("fees"))
.and_then(|fees| GenesisFees::try_from_raw_ref(fees).map_err(Self::Error::fees))?;

let connect = connect
.as_ref()
.ok_or_else(|| Self::Error::field_not_set("connect"))?;

let market_map = connect
.market_map
.as_ref()
.ok_or_else(|| Self::Error::field_not_set("market_map"))
.and_then(|market_map| {
market_map::v2::GenesisState::try_from_raw(market_map.clone())
.map_err(Self::Error::market_map)
})?;

let oracle = connect
.oracle
.as_ref()
.ok_or_else(|| Self::Error::field_not_set("oracle"))
.and_then(|oracle| {
oracle::v2::GenesisState::try_from_raw(oracle.clone()).map_err(Self::Error::oracle)
})?;
let connect = if let Some(connect) = connect {
Some(ConnectGenesis::try_from_raw_ref(connect).map_err(Self::Error::connect)?)
} else {
None
};

let this = Self {
address_prefixes,
Expand All @@ -404,10 +389,7 @@ impl Protobuf for GenesisAppState {
ibc_parameters,
allowed_fee_assets,
fees,
connect: ConnectGenesis {
market_map,
oracle,
},
connect,
};
this.ensure_all_addresses_have_base_prefix()
.map_err(Self::Error::address_does_not_match_base)?;
Expand Down Expand Up @@ -441,7 +423,7 @@ impl Protobuf for GenesisAppState {
ibc_parameters: Some(ibc_parameters.to_raw()),
allowed_fee_assets: allowed_fee_assets.iter().map(ToString::to_string).collect(),
fees: Some(fees.to_raw()),
connect: Some(connect.to_raw()),
connect: connect.as_ref().map(ConnectGenesis::to_raw),
}
}
}
Expand Down Expand Up @@ -525,14 +507,8 @@ impl GenesisAppStateError {
})
}

fn market_map(source: market_map::v2::GenesisStateError) -> Self {
Self(GenesisAppStateErrorKind::MarketMap {
source,
})
}

fn oracle(source: oracle::v2::GenesisStateError) -> Self {
Self(GenesisAppStateErrorKind::Oracle {
fn connect(source: ConnectGenesisError) -> Self {
Self(GenesisAppStateErrorKind::Connect {
source,
})
}
Expand Down Expand Up @@ -563,14 +539,8 @@ enum GenesisAppStateErrorKind {
FieldNotSet { name: &'static str },
#[error("`native_asset_base_denomination` field was invalid")]
NativeAssetBaseDenomination { source: ParseTracePrefixedError },
#[error("`market_map` field was invalid")]
MarketMap {
source: market_map::v2::GenesisStateError,
},
#[error("`oracle` field was invalid")]
Oracle {
source: oracle::v2::GenesisStateError,
},
#[error("`connect` field was invalid")]
Connect { source: ConnectGenesisError },
}

#[derive(Debug, thiserror::Error)]
Expand Down
21 changes: 12 additions & 9 deletions crates/astria-sequencer/src/connect/marketmap/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ impl Component for MarketMapComponent {

#[instrument(name = "MarketMapComponent::init_chain", skip(state))]
async fn init_chain<S: StateWrite>(mut state: S, app_state: &Self::AppState) -> Result<()> {
// TODO: put market map authorites and admin in state;
// only required for related actions however

state
.put_market_map(app_state.connect().market_map().market_map.clone())
.wrap_err("failed to put market map")?;
state
.put_params(app_state.connect().market_map().params.clone())
.wrap_err("failed to put params")?;
if let Some(connect) = app_state.connect() {
// TODO: put market map authorites and admin in state;
// only required for related actions however

state
.put_market_map(connect.market_map().market_map.clone())
.wrap_err("failed to put market map")?;
state
.put_params(connect.market_map().params.clone())
.wrap_err("failed to put params")?;
}

Ok(())
}

Expand Down
41 changes: 23 additions & 18 deletions crates/astria-sequencer/src/connect/oracle/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,31 @@ impl Component for OracleComponent {

#[instrument(name = "OracleComponent::init_chain", skip(state))]
async fn init_chain<S: StateWriteExt>(mut state: S, app_state: &Self::AppState) -> Result<()> {
for currency_pair in &app_state.connect().oracle().currency_pair_genesis {
let currency_pair_state = CurrencyPairState {
id: currency_pair.id(),
nonce: currency_pair.nonce(),
price: currency_pair.currency_pair_price().clone(),
};
if let Some(connect) = app_state.connect() {
for currency_pair in &connect.oracle().currency_pair_genesis {
let currency_pair_state = CurrencyPairState {
id: currency_pair.id(),
nonce: currency_pair.nonce(),
price: currency_pair.currency_pair_price().clone(),
};
state
.put_currency_pair_state(
currency_pair.currency_pair().clone(),
currency_pair_state,
)
.wrap_err("failed to write currency pair to state")?;
}

state
.put_next_currency_pair_id(connect.oracle().next_id)
.wrap_err("failed to put next currency pair id")?;
state
.put_currency_pair_state(currency_pair.currency_pair().clone(), currency_pair_state)
.wrap_err("failed to write currency pair to state")?;
.put_num_currency_pairs(connect.oracle().currency_pair_genesis.len() as u64)
.wrap_err("failed to put number of currency pairs")?;
state
.put_num_removed_currency_pairs(0)
.wrap_err("failed to put number of removed currency pairs")?;
}

state
.put_next_currency_pair_id(app_state.connect().oracle().next_id)
.wrap_err("failed to put next currency pair id")?;
state
.put_num_currency_pairs(app_state.connect().oracle().currency_pair_genesis.len() as u64)
.wrap_err("failed to put number of currency pairs")?;
state
.put_num_removed_currency_pairs(0)
.wrap_err("failed to put number of removed currency pairs")?;
Ok(())
}

Expand Down

0 comments on commit fd69353

Please sign in to comment.