Skip to content

Commit 2e24c6d

Browse files
committed
sui-sdk-types: remove digest wrappers and use Digest everywhere
The digest wrappers were not providing much value so to improve ergonomics we'll use `Digest` everywhere.
1 parent 358569a commit 2e24c6d

File tree

14 files changed

+85
-276
lines changed

14 files changed

+85
-276
lines changed

crates/sui-graphql-client/src/faucet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use sui_types::Address;
5-
use sui_types::TransactionDigest;
5+
use sui_types::Digest;
66

77
use anyhow::bail;
88
use reqwest::StatusCode;
@@ -42,7 +42,7 @@ pub struct FaucetResponse {
4242
pub struct CoinInfo {
4343
pub amount: u64,
4444
pub id: Address,
45-
pub transfer_tx_digest: TransactionDigest,
45+
pub transfer_tx_digest: Digest,
4646
}
4747

4848
#[derive(Serialize, Deserialize, Error, Debug, PartialEq, Eq)]

crates/sui-graphql-client/src/lib.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,14 @@ use streams::stream_paginated_query;
8383

8484
use sui_types::framework::Coin;
8585
use sui_types::Address;
86-
use sui_types::CheckpointDigest;
8786
use sui_types::CheckpointSequenceNumber;
8887
use sui_types::CheckpointSummary;
88+
use sui_types::Digest;
8989
use sui_types::Event;
9090
use sui_types::MovePackage;
9191
use sui_types::Object;
9292
use sui_types::SignedTransaction;
9393
use sui_types::Transaction;
94-
use sui_types::TransactionDigest;
9594
use sui_types::TransactionEffects;
9695
use sui_types::TransactionKind;
9796
use sui_types::TypeTag;
@@ -493,10 +492,7 @@ impl Client {
493492

494493
/// The total number of transaction blocks in the network by the end of the provided
495494
/// checkpoint digest.
496-
pub async fn total_transaction_blocks_by_digest(
497-
&self,
498-
digest: CheckpointDigest,
499-
) -> Result<Option<u64>> {
495+
pub async fn total_transaction_blocks_by_digest(&self, digest: Digest) -> Result<Option<u64>> {
500496
self.internal_total_transaction_blocks(Some(digest.to_string()), None)
501497
.await
502498
}
@@ -655,7 +651,7 @@ impl Client {
655651
/// provided, it will use the last known checkpoint id.
656652
pub async fn checkpoint(
657653
&self,
658-
digest: Option<CheckpointDigest>,
654+
digest: Option<Digest>,
659655
seq_num: Option<u64>,
660656
) -> Result<Option<CheckpointSummary>> {
661657
if digest.is_some() && seq_num.is_some() {
@@ -977,7 +973,7 @@ impl Client {
977973
&self,
978974
filter: Option<EventFilter>,
979975
pagination_filter: PaginationFilter,
980-
) -> Result<Page<(Event, TransactionDigest)>> {
976+
) -> Result<Page<(Event, Digest)>> {
981977
let (after, before, first, last) = self.pagination_filter(pagination_filter).await;
982978

983979
let operation = EventsQuery::build(EventsQueryArgs {
@@ -1001,7 +997,7 @@ impl Client {
1001997
let events_with_digests = ec
1002998
.nodes
1003999
.into_iter()
1004-
.map(|node| -> Result<(Event, TransactionDigest)> {
1000+
.map(|node| -> Result<(Event, Digest)> {
10051001
let event =
10061002
bcs::from_bytes::<Event>(&base64ct::Base64::decode_vec(&node.bcs.0)?)?;
10071003

@@ -1016,7 +1012,7 @@ impl Client {
10161012
)
10171013
})?;
10181014

1019-
let tx_digest = TransactionDigest::from_base58(&tx_digest)?;
1015+
let tx_digest = Digest::from_base58(&tx_digest)?;
10201016

10211017
Ok((event, tx_digest))
10221018
})
@@ -1033,7 +1029,7 @@ impl Client {
10331029
&self,
10341030
filter: Option<EventFilter>,
10351031
streaming_direction: Direction,
1036-
) -> impl Stream<Item = Result<(Event, TransactionDigest)>> + '_ {
1032+
) -> impl Stream<Item = Result<(Event, Digest)>> + '_ {
10371033
stream_paginated_query(
10381034
move |pag_filter| self.events(filter.clone(), pag_filter),
10391035
streaming_direction,
@@ -1489,10 +1485,7 @@ impl Client {
14891485
// ===========================================================================
14901486

14911487
/// Get a transaction by its digest.
1492-
pub async fn transaction(
1493-
&self,
1494-
digest: TransactionDigest,
1495-
) -> Result<Option<SignedTransaction>> {
1488+
pub async fn transaction(&self, digest: Digest) -> Result<Option<SignedTransaction>> {
14961489
let operation = TransactionBlockQuery::build(TransactionBlockArgs {
14971490
digest: digest.to_string(),
14981491
});
@@ -1510,10 +1503,7 @@ impl Client {
15101503
}
15111504

15121505
/// Get a transaction's effects by its digest.
1513-
pub async fn transaction_effects(
1514-
&self,
1515-
digest: TransactionDigest,
1516-
) -> Result<Option<TransactionEffects>> {
1506+
pub async fn transaction_effects(&self, digest: Digest) -> Result<Option<TransactionEffects>> {
15171507
let operation = TransactionBlockEffectsQuery::build(TransactionBlockArgs {
15181508
digest: digest.to_string(),
15191509
});
@@ -1529,7 +1519,7 @@ impl Client {
15291519
/// Get a transaction's data and effects by its digest.
15301520
pub async fn transaction_data_effects(
15311521
&self,
1532-
digest: TransactionDigest,
1522+
digest: Digest,
15331523
) -> Result<Option<TransactionDataEffects>> {
15341524
let operation = TransactionBlockWithEffectsQuery::build(TransactionBlockArgs {
15351525
digest: digest.to_string(),

crates/sui-rpc/src/client/response_ext.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use sui_sdk_types::CheckpointDigest;
1+
use sui_sdk_types::Digest;
22

33
/// Extension trait used to facilitate retrieval of Sui specific data from responses
44
pub trait ResponseExt: sealed::Sealed {
5-
fn chain_id(&self) -> Option<CheckpointDigest>;
5+
fn chain_id(&self) -> Option<Digest>;
66
fn chain(&self) -> Option<&str>;
77
fn epoch(&self) -> Option<u64>;
88
fn checkpoint_height(&self) -> Option<u64>;
@@ -13,10 +13,10 @@ pub trait ResponseExt: sealed::Sealed {
1313
}
1414

1515
impl ResponseExt for http::header::HeaderMap {
16-
fn chain_id(&self) -> Option<CheckpointDigest> {
16+
fn chain_id(&self) -> Option<Digest> {
1717
self.get(crate::headers::X_SUI_CHAIN_ID)
1818
.map(http::header::HeaderValue::as_bytes)
19-
.and_then(|s| CheckpointDigest::from_base58(s).ok())
19+
.and_then(|s| Digest::from_base58(s).ok())
2020
}
2121

2222
fn chain(&self) -> Option<&str> {
@@ -61,7 +61,7 @@ impl ResponseExt for http::header::HeaderMap {
6161
}
6262

6363
impl ResponseExt for tonic::metadata::MetadataMap {
64-
fn chain_id(&self) -> Option<CheckpointDigest> {
64+
fn chain_id(&self) -> Option<Digest> {
6565
self.as_ref().chain_id()
6666
}
6767

@@ -95,7 +95,7 @@ impl ResponseExt for tonic::metadata::MetadataMap {
9595
}
9696

9797
impl<T> ResponseExt for tonic::Response<T> {
98-
fn chain_id(&self) -> Option<CheckpointDigest> {
98+
fn chain_id(&self) -> Option<Digest> {
9999
self.metadata().chain_id()
100100
}
101101

@@ -129,7 +129,7 @@ impl<T> ResponseExt for tonic::Response<T> {
129129
}
130130

131131
impl ResponseExt for tonic::Status {
132-
fn chain_id(&self) -> Option<CheckpointDigest> {
132+
fn chain_id(&self) -> Option<Digest> {
133133
self.metadata().chain_id()
134134
}
135135

crates/sui-sdk-types/src/checkpoint.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use super::CheckpointContentsDigest;
2-
use super::CheckpointDigest;
31
use super::Digest;
42
use super::GasCostSummary;
53
use super::Object;
64
use super::SignedTransaction;
7-
use super::TransactionDigest;
85
use super::TransactionEffects;
9-
use super::TransactionEffectsDigest;
106
use super::TransactionEvents;
117
use super::UserSignature;
128
use super::ValidatorAggregatedSignature;
@@ -126,12 +122,12 @@ pub struct CheckpointSummary {
126122
pub network_total_transactions: u64,
127123

128124
/// The hash of the [`CheckpointContents`] for this checkpoint.
129-
pub content_digest: CheckpointContentsDigest,
125+
pub content_digest: Digest,
130126

131127
/// The hash of the previous `CheckpointSummary`.
132128
///
133129
/// This will be only be `None` for the first, or genesis checkpoint.
134-
pub previous_digest: Option<CheckpointDigest>,
130+
pub previous_digest: Option<Digest>,
135131

136132
/// The running total gas costs of all transactions included in the current epoch so far
137133
/// until this checkpoint.
@@ -213,8 +209,8 @@ impl CheckpointContents {
213209
)]
214210
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
215211
pub struct CheckpointTransactionInfo {
216-
pub transaction: TransactionDigest,
217-
pub effects: TransactionEffectsDigest,
212+
pub transaction: Digest,
213+
pub effects: Digest,
218214
#[cfg_attr(feature = "proptest", any(proptest::collection::size_range(0..=2).lift()))]
219215
pub signatures: Vec<UserSignature>,
220216
}
@@ -277,8 +273,8 @@ mod serialization {
277273

278274
#[derive(serde_derive::Serialize)]
279275
struct Digests<'a> {
280-
transaction: &'a TransactionDigest,
281-
effects: &'a TransactionEffectsDigest,
276+
transaction: &'a Digest,
277+
effects: &'a Digest,
282278
}
283279

284280
struct DigestSeq<'a>(&'a CheckpointContents);
@@ -322,8 +318,8 @@ mod serialization {
322318

323319
#[derive(serde_derive::Deserialize)]
324320
struct ExecutionDigests {
325-
transaction: TransactionDigest,
326-
effects: TransactionEffectsDigest,
321+
transaction: Digest,
322+
effects: Digest,
327323
}
328324

329325
#[derive(serde_derive::Deserialize)]

crates/sui-sdk-types/src/digest.rs

Lines changed: 2 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
derive(serde_derive::Serialize, serde_derive::Deserialize)
1818
)]
1919
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
20+
#[doc(alias = "CheckpointDigest")]
21+
#[doc(alias = "TransactionDigest")]
2022
pub struct Digest(
2123
#[cfg_attr(feature = "serde", serde(with = "DigestSerialization"))] [u8; Self::LENGTH],
2224
);
@@ -201,138 +203,6 @@ impl std::fmt::Display for DigestParseError {
201203

202204
impl std::error::Error for DigestParseError {}
203205

204-
//
205-
// Implement Various Digest wrappers
206-
//
207-
208-
macro_rules! impl_digest {
209-
($t:ident) => {
210-
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
211-
#[cfg_attr(
212-
feature = "serde",
213-
derive(serde_derive::Serialize, serde_derive::Deserialize)
214-
)]
215-
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
216-
pub struct $t(Digest);
217-
218-
impl $t {
219-
pub const LENGTH: usize = Digest::LENGTH;
220-
pub const ZERO: Self = Self::new([0; Self::LENGTH]);
221-
222-
pub const fn new(digest: [u8; Self::LENGTH]) -> Self {
223-
Self(Digest::new(digest))
224-
}
225-
226-
#[cfg(feature = "rand")]
227-
#[cfg_attr(doc_cfg, doc(cfg(feature = "rand")))]
228-
pub fn generate<R>(rng: R) -> Self
229-
where
230-
R: rand_core::RngCore + rand_core::CryptoRng,
231-
{
232-
Self(Digest::generate(rng))
233-
}
234-
235-
pub const fn inner(&self) -> &[u8; Self::LENGTH] {
236-
self.0.inner()
237-
}
238-
239-
pub const fn into_inner(self) -> [u8; Self::LENGTH] {
240-
self.0.into_inner()
241-
}
242-
243-
pub const fn as_bytes(&self) -> &[u8] {
244-
self.0.as_bytes()
245-
}
246-
247-
pub fn from_base58<T: AsRef<[u8]>>(base58: T) -> Result<Self, DigestParseError> {
248-
Digest::from_base58(base58).map(Self)
249-
}
250-
251-
#[allow(clippy::wrong_self_convention)]
252-
pub fn to_base58(&self) -> String {
253-
self.to_string()
254-
}
255-
256-
pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, DigestParseError> {
257-
Digest::from_bytes(bytes).map(Self)
258-
}
259-
}
260-
261-
impl std::str::FromStr for $t {
262-
type Err = DigestParseError;
263-
264-
fn from_str(s: &str) -> Result<Self, Self::Err> {
265-
Self::from_base58(s)
266-
}
267-
}
268-
269-
impl AsRef<[u8]> for $t {
270-
fn as_ref(&self) -> &[u8] {
271-
self.0.as_ref()
272-
}
273-
}
274-
275-
impl AsRef<[u8; Self::LENGTH]> for $t {
276-
fn as_ref(&self) -> &[u8; Self::LENGTH] {
277-
self.0.as_ref()
278-
}
279-
}
280-
281-
impl From<$t> for [u8; $t::LENGTH] {
282-
fn from(digest: $t) -> Self {
283-
digest.into_inner()
284-
}
285-
}
286-
287-
impl From<[u8; Self::LENGTH]> for $t {
288-
fn from(digest: [u8; Self::LENGTH]) -> Self {
289-
Self::new(digest)
290-
}
291-
}
292-
293-
impl From<Digest> for $t {
294-
fn from(digest: Digest) -> Self {
295-
Self(digest)
296-
}
297-
}
298-
299-
impl From<$t> for Digest {
300-
fn from(digest: $t) -> Self {
301-
digest.0
302-
}
303-
}
304-
305-
impl std::fmt::Display for $t {
306-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
307-
std::fmt::Display::fmt(&self.0, f)
308-
}
309-
}
310-
311-
impl std::fmt::Debug for $t {
312-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
313-
f.debug_tuple(stringify!($t))
314-
.field(&format_args!("\"{}\"", self))
315-
.finish()
316-
}
317-
}
318-
319-
impl std::fmt::LowerHex for $t {
320-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
321-
std::fmt::LowerHex::fmt(&self.0, f)
322-
}
323-
}
324-
};
325-
}
326-
327-
impl_digest!(CheckpointDigest);
328-
impl_digest!(CheckpointContentsDigest);
329-
impl_digest!(TransactionDigest);
330-
impl_digest!(TransactionEffectsDigest);
331-
impl_digest!(TransactionEventsDigest);
332-
impl_digest!(ObjectDigest);
333-
impl_digest!(ConsensusCommitDigest);
334-
impl_digest!(EffectsAuxiliaryDataDigest);
335-
336206
// Don't implement like the other digest types since this isn't intended to be serialized
337207
pub type SigningDigest = [u8; Digest::LENGTH];
338208

0 commit comments

Comments
 (0)