Skip to content

Commit

Permalink
remove num_removed_currency_pairs as we don't need it
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Dec 13, 2024
1 parent ebe0340 commit 3cc37df
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 43 deletions.
32 changes: 8 additions & 24 deletions crates/astria-core/src/connect/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub mod v2 {

#[derive(Debug, Clone, PartialEq)]
pub struct CurrencyPairState {
pub price: QuotePrice,
pub price: Option<QuotePrice>,
pub nonce: CurrencyPairNonce,
pub id: CurrencyPairId,
}
Expand All @@ -121,14 +121,11 @@ pub mod v2 {
/// - if the `price` field is missing
/// - if the `price` field is invalid
pub fn try_from_raw(raw: raw::CurrencyPairState) -> Result<Self, CurrencyPairStateError> {
let Some(price) = raw
let price = raw
.price
.map(QuotePrice::try_from_raw)
.transpose()
.map_err(CurrencyPairStateError::quote_price_parse_error)?
else {
return Err(CurrencyPairStateError::missing_price());
};
.map_err(CurrencyPairStateError::quote_price_parse_error)?;
let nonce = CurrencyPairNonce::new(raw.nonce);
let id = CurrencyPairId::new(raw.id);
Ok(Self {
Expand All @@ -141,7 +138,7 @@ pub mod v2 {
#[must_use]
pub fn into_raw(self) -> raw::CurrencyPairState {
raw::CurrencyPairState {
price: Some(self.price.into_raw()),
price: self.price.map(QuotePrice::into_raw),
nonce: self.nonce.get(),
id: self.id.get(),
}
Expand All @@ -153,11 +150,6 @@ pub mod v2 {
pub struct CurrencyPairStateError(CurrencyPairStateErrorKind);

impl CurrencyPairStateError {
#[must_use]
fn missing_price() -> Self {
Self(CurrencyPairStateErrorKind::MissingPrice)
}

#[must_use]
fn quote_price_parse_error(err: QuotePriceError) -> Self {
Self(CurrencyPairStateErrorKind::QuotePriceParseError(err))
Expand All @@ -166,16 +158,14 @@ pub mod v2 {

#[derive(Debug, thiserror::Error)]
enum CurrencyPairStateErrorKind {
#[error("missing price")]
MissingPrice,
#[error("failed to parse quote price")]
QuotePriceParseError(#[source] QuotePriceError),
}

#[derive(Debug, Clone)]
pub struct CurrencyPairGenesis {
pub currency_pair: CurrencyPair,
pub currency_pair_price: QuotePrice,
pub currency_pair_price: Option<QuotePrice>,
pub id: CurrencyPairId,
pub nonce: CurrencyPairNonce,
}
Expand All @@ -201,7 +191,7 @@ pub mod v2 {
}

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

Expand Down Expand Up @@ -232,13 +222,7 @@ pub mod v2 {
.ok_or_else(|| CurrencyPairGenesisError::field_not_set("currency_pair"))?
.try_into()
.map_err(CurrencyPairGenesisError::currency_pair)?;
let currency_pair_price = {
let wire = raw.currency_pair_price.ok_or_else(|| {
CurrencyPairGenesisError::field_not_set("currency_pair_price")
})?;
QuotePrice::try_from_raw(wire)
.map_err(CurrencyPairGenesisError::currency_pair_price)?
};
let currency_pair_price = raw.currency_pair_price.map(QuotePrice::try_from_raw).transpose().map_err(CurrencyPairGenesisError::currency_pair_price)?;

let id = CurrencyPairId::new(raw.id);
let nonce = CurrencyPairNonce::new(raw.nonce);
Expand All @@ -254,7 +238,7 @@ pub mod v2 {
pub fn into_raw(self) -> raw::CurrencyPairGenesis {
raw::CurrencyPairGenesis {
currency_pair: Some(self.currency_pair.into_raw()),
currency_pair_price: Some(self.currency_pair_price.into_raw()),
currency_pair_price: self.currency_pair_price.map(QuotePrice::into_raw),
id: self.id.get(),
nonce: self.nonce.get(),
}
Expand Down
4 changes: 2 additions & 2 deletions crates/astria-core/src/protocol/genesis/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,14 +1165,14 @@ mod tests {
currency_pair_genesis: vec![CurrencyPairGenesis {
id: CurrencyPairId::new(1),
nonce: CurrencyPairNonce::new(0),
currency_pair_price: QuotePrice {
currency_pair_price: Some(QuotePrice {
price: Price::new(3_138_872_234_u128),
block_height: 0,
block_timestamp: pbjson_types::Timestamp {
seconds: 1_720_122_395,
nanos: 0,
},
},
}),
currency_pair: CurrencyPair::from_parts(
"ETH".parse().unwrap(),
"USD".parse().unwrap(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use astria_eyre::eyre::{
};
use async_trait::async_trait;
use cnidarium::StateWrite;
use tracing::debug;

use crate::{
action_handler::ActionHandler,
Expand Down Expand Up @@ -72,15 +73,18 @@ impl ActionHandler for AddCurrencyPairs {
.into();

for pair in &self.pairs {
if state
.get_currency_pair_state(pair)
.await
.wrap_err("failed to get currency pair state")?
.is_some()
{
debug!("currency pair {} already exists, skipping", pair);
continue;
}

let currency_pair_state = CurrencyPairState {
price: QuotePrice {
price: Price::new(0),
block_timestamp: Timestamp {
seconds: timestamp.seconds,
nanos: timestamp.nanos,
},
block_height: state.get_block_height().await?,
},
price: None,
nonce: CurrencyPairNonce::new(0),
id: next_currency_pair_id,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/astria-sequencer/src/app/vote_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,14 +846,14 @@ mod test {

for (pair, pair_id) in [pair_0(), pair_1(), pair_2()] {
let pair_state = CurrencyPairState {
price: QuotePrice {
price: Some(QuotePrice {
price: Price::new(123),
block_timestamp: Timestamp {
seconds: 4,
nanos: 5,
},
block_height: 1,
},
}),
nonce: CurrencyPairNonce::new(1),
id: pair_id,
};
Expand Down
8 changes: 4 additions & 4 deletions crates/astria-sequencer/src/connect/oracle/state_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub(crate) trait StateWriteExt: StateWrite {
.await
.wrap_err("failed to get currency pair state")?
{
state.price = price;
state.price = Some(price);
state.nonce = state
.nonce
.increment()
Expand All @@ -298,7 +298,7 @@ pub(crate) trait StateWriteExt: StateWrite {
self.put_next_currency_pair_id(next_id)
.wrap_err("failed to put next currency pair ID")?;
CurrencyPairState {
price,
Some(price),
nonce: CurrencyPairNonce::new(0),
id,
}
Expand Down Expand Up @@ -383,14 +383,14 @@ mod tests {

fn currency_pair_state(id: u64, nonce: u64) -> CurrencyPairState {
CurrencyPairState {
price: QuotePrice {
price: Some(QuotePrice {
price: Price::new(123),
block_timestamp: Timestamp {
seconds: 4,
nanos: 5,
},
block_height: nonce.checked_add(10).unwrap(),
},
}),
nonce: CurrencyPairNonce::new(nonce),
id: CurrencyPairId::new(id),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ impl From<QuotePrice> for DomainQuotePrice {

#[derive(Debug, BorshSerialize, BorshDeserialize)]
pub(in crate::connect::oracle) struct CurrencyPairState {
price: QuotePrice,
price: Option<QuotePrice>,
nonce: u64,
id: CurrencyPairId,
}

impl From<DomainCurrencyPairState> for CurrencyPairState {
fn from(state: DomainCurrencyPairState) -> Self {
CurrencyPairState {
price: QuotePrice::from(state.price),
price: state.price.map(QuotePrice::from),
nonce: state.nonce.get(),
id: CurrencyPairId::from(state.id),
}
Expand All @@ -95,7 +95,7 @@ impl From<DomainCurrencyPairState> for CurrencyPairState {
impl From<CurrencyPairState> for DomainCurrencyPairState {
fn from(state: CurrencyPairState) -> Self {
Self {
price: DomainQuotePrice::from(state.price),
price: state.price.map(DomainQuotePrice::from),
nonce: CurrencyPairNonce::new(state.nonce),
id: DomainCurrencyPairId::from(state.id),
}
Expand Down

0 comments on commit 3cc37df

Please sign in to comment.