Skip to content

Commit 953126a

Browse files
committed
types: deduplicate identical Move types between hashi-types and onchain::types
Previously, 10 types (UpgradeCap, ConfigValue, WithdrawalStatus, WithdrawalRequest, PendingWithdrawal, OutputUtxo, DepositRequest, Utxo, UtxoId, UtxoRecord) were defined identically in both hashi-types::move_types and hashi::onchain::types, with trivial field-by-field conversion functions bridging them. Now `onchain::types` re-exports these from `hashi_types::move_types` and the conversion functions are removed. The hashi-types versions gain the additional derives (Clone, PartialEq, Hash, Serialize) that the onchain versions previously carried, along with the small helper methods (`WithdrawalStatus::is_approved`, `is_requested`, `PendingWithdrawal::all_outputs`).
1 parent 60e18ed commit 953126a

5 files changed

Lines changed: 99 additions & 349 deletions

File tree

crates/hashi-types/src/move_types/mod.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,20 @@ pub enum WithdrawalStatus {
258258
Confirmed { txid: BitcoinTxid },
259259
}
260260

261+
impl WithdrawalStatus {
262+
/// Returns true if the status is `Approved`.
263+
pub fn is_approved(&self) -> bool {
264+
matches!(self, Self::Approved)
265+
}
266+
267+
/// Returns true if the status is `Requested`.
268+
pub fn is_requested(&self) -> bool {
269+
matches!(self, Self::Requested)
270+
}
271+
}
272+
261273
/// Rust version of the Move hashi::withdrawal_queue::WithdrawalRequest type.
262-
#[derive(Debug, serde_derive::Deserialize)]
274+
#[derive(Clone, Debug, PartialEq, serde_derive::Deserialize, serde_derive::Serialize)]
263275
pub struct WithdrawalRequest {
264276
pub id: Address,
265277
pub sender: Address,
@@ -281,7 +293,7 @@ pub struct CommittedRequestInfo {
281293
}
282294

283295
/// Rust version of the Move hashi::withdrawal_queue::PendingWithdrawal type.
284-
#[derive(Debug, serde_derive::Deserialize)]
296+
#[derive(Clone, Debug, PartialEq, serde_derive::Deserialize, serde_derive::Serialize)]
285297
pub struct PendingWithdrawal {
286298
pub id: Address,
287299
pub txid: BitcoinTxid,
@@ -296,16 +308,26 @@ pub struct PendingWithdrawal {
296308
pub epoch: u64,
297309
}
298310

311+
impl PendingWithdrawal {
312+
pub fn all_outputs(&self) -> Vec<OutputUtxo> {
313+
let mut outputs = self.withdrawal_outputs.clone();
314+
if let Some(ref change) = self.change_output {
315+
outputs.push(change.clone());
316+
}
317+
outputs
318+
}
319+
}
320+
299321
/// Rust version of the Move hashi::withdrawal_queue::OutputUtxo type.
300-
#[derive(Debug, serde_derive::Deserialize)]
322+
#[derive(Clone, Debug, PartialEq, serde_derive::Deserialize, serde_derive::Serialize)]
301323
pub struct OutputUtxo {
302324
/// In satoshis
303325
pub amount: u64,
304326
pub bitcoin_address: Vec<u8>,
305327
}
306328

307329
/// Rust version of the Move hashi::deposit_queue::DepositRequest type.
308-
#[derive(Debug, serde_derive::Deserialize)]
330+
#[derive(Clone, Debug, PartialEq, serde_derive::Deserialize)]
309331
pub struct DepositRequest {
310332
pub id: Address,
311333
pub sender: Address,
@@ -314,7 +336,7 @@ pub struct DepositRequest {
314336
pub utxo: Utxo,
315337
}
316338

317-
#[derive(Debug, serde_derive::Deserialize)]
339+
#[derive(Clone, Debug, PartialEq, serde_derive::Deserialize, serde_derive::Serialize)]
318340
pub struct Utxo {
319341
pub id: UtxoId,
320342
// In satoshis
@@ -323,15 +345,26 @@ pub struct Utxo {
323345
}
324346

325347
/// Rust version of the Move hashi::utxo_pool::UtxoRecord type.
326-
#[derive(Debug, serde_derive::Deserialize)]
348+
#[derive(Clone, Debug, serde_derive::Deserialize)]
327349
pub struct UtxoRecord {
328350
pub utxo: Utxo,
329351
pub produced_by: Option<Address>,
330352
pub locked_by: Option<Address>,
331353
}
332354

333355
/// txid:vout
334-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde_derive::Deserialize)]
356+
#[derive(
357+
Copy,
358+
Clone,
359+
Debug,
360+
Hash,
361+
PartialEq,
362+
Eq,
363+
PartialOrd,
364+
Ord,
365+
serde_derive::Deserialize,
366+
serde_derive::Serialize,
367+
)]
335368
pub struct UtxoId {
336369
// a 32 byte sha256 of the transaction
337370
pub txid: BitcoinTxid,

crates/hashi/src/leader/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl LeaderService {
389389

390390
info!(deposit_request_id = %deposit_request.id, "Deposit request validated successfully");
391391

392-
let proto_request = deposit_request.to_proto();
392+
let proto_request = deposit_request_to_proto(&deposit_request);
393393
let members = inner
394394
.onchain_state()
395395
.current_committee_members()
@@ -1805,21 +1805,19 @@ impl LeaderService {
18051805
}
18061806
}
18071807

1808-
impl DepositRequest {
1809-
fn to_proto(&self) -> SignDepositConfirmationRequest {
1810-
SignDepositConfirmationRequest {
1811-
id: self.id.as_bytes().to_vec().into(),
1812-
txid: self.utxo.id.txid.as_bytes().to_vec().into(),
1813-
vout: self.utxo.id.vout,
1814-
amount: self.utxo.amount,
1815-
derivation_path: self
1816-
.utxo
1817-
.derivation_path
1818-
.map(|p| p.as_bytes().to_vec().into()),
1819-
timestamp_ms: self.timestamp_ms,
1820-
requester_address: self.sender.as_bytes().to_vec().into(),
1821-
sui_tx_digest: self.sui_tx_digest.as_bytes().to_vec().into(),
1822-
}
1808+
fn deposit_request_to_proto(req: &DepositRequest) -> SignDepositConfirmationRequest {
1809+
SignDepositConfirmationRequest {
1810+
id: req.id.as_bytes().to_vec().into(),
1811+
txid: req.utxo.id.txid.as_bytes().to_vec().into(),
1812+
vout: req.utxo.id.vout,
1813+
amount: req.utxo.amount,
1814+
derivation_path: req
1815+
.utxo
1816+
.derivation_path
1817+
.map(|p| p.as_bytes().to_vec().into()),
1818+
timestamp_ms: req.timestamp_ms,
1819+
requester_address: req.sender.as_bytes().to_vec().into(),
1820+
sui_tx_digest: req.sui_tx_digest.as_bytes().to_vec().into(),
18231821
}
18241822
}
18251823

0 commit comments

Comments
 (0)