Browse Source

We can safely assume that creating a timestamp should not fail

Expect instead of returning a `Result`.
debug-collab-settlement
Daniel Karzel 3 years ago
parent
commit
566aca374b
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 2
      daemon/src/maker_cfd.rs
  2. 8
      daemon/src/model.rs
  3. 34
      daemon/src/model/cfd.rs
  4. 2
      daemon/src/taker_cfd.rs
  5. 2
      daemon/src/wallet.rs
  6. 2
      daemon/tests/harness/mocks/wallet.rs
  7. 4
      daemon/tests/harness/mod.rs

2
daemon/src/maker_cfd.rs

@ -474,7 +474,7 @@ where
quantity, quantity,
CfdState::IncomingOrderRequest { CfdState::IncomingOrderRequest {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
taker_id, taker_id,
}, },

8
daemon/src/model.rs

@ -515,15 +515,15 @@ impl Timestamp {
Self(seconds) Self(seconds)
} }
pub fn now() -> Result<Self> { pub fn now() -> Self {
let seconds: i64 = SystemTime::now() let seconds: i64 = SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.context("time has gone backwards!")? .expect("time not to go backwards")
.as_secs() .as_secs()
.try_into() .try_into()
.context("Unable to convert u64 to i64")?; .expect("seconds of system time to fit into i64");
Ok(Self(seconds)) Self(seconds)
} }
pub fn parse_from_rfc3339(datetime_str: &str) -> Result<Self> { pub fn parse_from_rfc3339(datetime_str: &str) -> Result<Self> {

34
daemon/src/model/cfd.rs

@ -146,7 +146,7 @@ impl Order {
trading_pair: TradingPair::BtcUsd, trading_pair: TradingPair::BtcUsd,
liquidation_price, liquidation_price,
position: Position::Short, position: Position::Short,
creation_timestamp: Timestamp::now()?, creation_timestamp: Timestamp::now(),
settlement_time_interval_hours, settlement_time_interval_hours,
origin, origin,
oracle_event_id, oracle_event_id,
@ -173,7 +173,7 @@ pub struct CfdStateCommon {
impl Default for CfdStateCommon { impl Default for CfdStateCommon {
fn default() -> Self { fn default() -> Self {
Self { Self {
transition_timestamp: Timestamp::now().expect("Unable to get current time"), transition_timestamp: Timestamp::now(),
} }
} }
} }
@ -649,7 +649,7 @@ impl Cfd {
let settlement = SettlementProposal { let settlement = SettlementProposal {
order_id: self.order.id, order_id: self.order.id,
timestamp: Timestamp::now()?, timestamp: Timestamp::now(),
taker: *payout.taker_amount(), taker: *payout.taker_amount(),
maker: *payout.maker_amount(), maker: *payout.maker_amount(),
price: current_price, price: current_price,
@ -718,7 +718,7 @@ impl Cfd {
if let PendingOpen { dlc, .. } = self.state.clone() { if let PendingOpen { dlc, .. } = self.state.clone() {
CfdState::Open { CfdState::Open {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation: None, attestation: None,
@ -733,7 +733,7 @@ impl Cfd {
{ {
CfdState::Open { CfdState::Open {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation, attestation,
@ -770,7 +770,7 @@ impl Cfd {
OpenCommitted { OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: if let Some(attestation) = attestation { cet_status: if let Some(attestation) = attestation {
@ -794,7 +794,7 @@ impl Cfd {
.. ..
} => CfdState::OpenCommitted { } => CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: CetStatus::TimelockExpired, cet_status: CetStatus::TimelockExpired,
@ -805,7 +805,7 @@ impl Cfd {
.. ..
} => CfdState::OpenCommitted { } => CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: CetStatus::Ready(attestation), cet_status: CetStatus::Ready(attestation),
@ -822,7 +822,7 @@ impl Cfd {
tracing::debug!(%order_id, "Was in unexpected state {}, jumping ahead to OpenCommitted", self.state); tracing::debug!(%order_id, "Was in unexpected state {}, jumping ahead to OpenCommitted", self.state);
CfdState::OpenCommitted { CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: match attestation { cet_status: match attestation {
@ -904,7 +904,7 @@ impl Cfd {
self.state = PendingCommit { self.state = PendingCommit {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation, attestation,
@ -931,14 +931,14 @@ impl Cfd {
let new_state = match self.state.clone() { let new_state = match self.state.clone() {
CfdState::PendingOpen { dlc, .. } => CfdState::PendingOpen { CfdState::PendingOpen { dlc, .. } => CfdState::PendingOpen {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation: Some(attestation), attestation: Some(attestation),
}, },
CfdState::Open { dlc, .. } => CfdState::Open { CfdState::Open { dlc, .. } => CfdState::Open {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation: Some(attestation), attestation: Some(attestation),
@ -946,7 +946,7 @@ impl Cfd {
}, },
CfdState::PendingCommit { dlc, .. } => CfdState::PendingCommit { CfdState::PendingCommit { dlc, .. } => CfdState::PendingCommit {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation: Some(attestation), attestation: Some(attestation),
@ -957,7 +957,7 @@ impl Cfd {
.. ..
} => CfdState::OpenCommitted { } => CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: CetStatus::OracleSigned(attestation), cet_status: CetStatus::OracleSigned(attestation),
@ -968,7 +968,7 @@ impl Cfd {
.. ..
} => CfdState::OpenCommitted { } => CfdState::OpenCommitted {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
cet_status: CetStatus::Ready(attestation), cet_status: CetStatus::Ready(attestation),
@ -1003,7 +1003,7 @@ impl Cfd {
self.state = CfdState::PendingCet { self.state = CfdState::PendingCet {
common: CfdStateCommon { common: CfdStateCommon {
transition_timestamp: Timestamp::now()?, transition_timestamp: Timestamp::now(),
}, },
dlc, dlc,
attestation, attestation,
@ -1880,7 +1880,7 @@ impl CollaborativeSettlement {
Ok(Self { Ok(Self {
tx, tx,
timestamp: Timestamp::now().context("Unable to get current time")?, timestamp: Timestamp::now(),
payout, payout,
price, price,
}) })

2
daemon/src/taker_cfd.rs

@ -356,7 +356,7 @@ where
let proposal = RollOverProposal { let proposal = RollOverProposal {
order_id, order_id,
timestamp: Timestamp::now()?, timestamp: Timestamp::now(),
}; };
self.current_pending_proposals.insert( self.current_pending_proposals.insert(

2
daemon/src/wallet.rs

@ -105,7 +105,7 @@ impl Actor {
let wallet_info = WalletInfo { let wallet_info = WalletInfo {
balance: Amount::from_sat(balance), balance: Amount::from_sat(balance),
address, address,
last_updated_at: Timestamp::now()?, last_updated_at: Timestamp::now(),
}; };
Ok(wallet_info) Ok(wallet_info)

2
daemon/tests/harness/mocks/wallet.rs

@ -66,7 +66,7 @@ fn dummy_wallet_info() -> Result<WalletInfo> {
Ok(WalletInfo { Ok(WalletInfo {
balance: bdk::bitcoin::Amount::ONE_BTC, balance: bdk::bitcoin::Amount::ONE_BTC,
address, address,
last_updated_at: Timestamp::now()?, last_updated_at: Timestamp::now(),
}) })
} }

4
daemon/tests/harness/mod.rs

@ -108,7 +108,7 @@ impl Maker {
.unwrap(); .unwrap();
let dummy_quote = Quote { let dummy_quote = Quote {
timestamp: Timestamp::now().unwrap(), timestamp: Timestamp::now(),
bid: Price::new(dec!(10000)).unwrap(), bid: Price::new(dec!(10000)).unwrap(),
ask: Price::new(dec!(10000)).unwrap(), ask: Price::new(dec!(10000)).unwrap(),
}; };
@ -244,7 +244,7 @@ impl Taker {
.unwrap(); .unwrap();
let dummy_quote = Quote { let dummy_quote = Quote {
timestamp: Timestamp::now().unwrap(), timestamp: Timestamp::now(),
bid: Price::new(dec!(10000)).unwrap(), bid: Price::new(dec!(10000)).unwrap(),
ask: Price::new(dec!(10000)).unwrap(), ask: Price::new(dec!(10000)).unwrap(),
}; };

Loading…
Cancel
Save