Skip to content

Commit ae7848d

Browse files
committed
chore: use Cow in scalars/HexString
1 parent b835194 commit ae7848d

File tree

12 files changed

+123
-108
lines changed

12 files changed

+123
-108
lines changed

crates/fuel-core/src/schema/blob.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ impl Blob {
2828
}
2929

3030
#[graphql(complexity = "query_costs().bytecode_read")]
31-
async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString> {
31+
async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString<'_>> {
3232
let query = ctx.read_view()?;
3333
query
3434
.blob_bytecode(self.0)
35-
.map(HexString)
35+
.map(|bytes| HexString(bytes.into()))
3636
.map_err(async_graphql::Error::from)
3737
}
3838
}

crates/fuel-core/src/schema/block.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,13 @@ impl BlockMutation {
424424
pub struct BlockSubscription;
425425

426426
#[Subscription]
427-
impl BlockSubscription {
427+
impl <'a>BlockSubscription {
428428
#[graphql(name = "alpha__new_blocks")]
429-
async fn new_blocks<'a>(
429+
async fn new_blocks(
430430
&self,
431431
ctx: &Context<'a>,
432432
) -> async_graphql::Result<
433-
impl Stream<Item = async_graphql::Result<HexString>> + 'a + use<'a>,
433+
impl Stream<Item = async_graphql::Result<HexString<'a>>> + 'a + use<'a>,
434434
> {
435435
require_expensive_subscriptions(ctx)?;
436436
let worker_state: &graphql_api::worker_service::SharedState =
@@ -440,9 +440,7 @@ impl BlockSubscription {
440440
let stream = BroadcastStream::new(receiver).map(|result| {
441441
result
442442
.map(|bytes| {
443-
// TODO: Avoid cloning the bytes here.
444-
// https://github.com/FuelLabs/fuel-core/issues/2657
445-
HexString(bytes.as_ref().clone())
443+
HexString::from(bytes.as_ref().clone())
446444
})
447445
.map_err(Into::into)
448446
});

crates/fuel-core/src/schema/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ impl Contract {
4848
}
4949

5050
#[graphql(complexity = "query_costs().bytecode_read")]
51-
async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString> {
51+
async fn bytecode(&self, ctx: &Context<'_>) -> async_graphql::Result<HexString<'_>> {
5252
let query = ctx.read_view()?;
5353
query
5454
.contract_bytecode(self.0)
55-
.map(HexString)
55+
.map(|bytes|HexString(bytes.into()))
5656
.map_err(Into::into)
5757
}
5858

crates/fuel-core/src/schema/da_compressed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl From<Vec<u8>> for DaCompressedBlock {
2424

2525
#[Object]
2626
impl DaCompressedBlock {
27-
async fn bytes(&self) -> HexString {
28-
HexString(self.bytes.clone())
27+
async fn bytes(&self) -> HexString<'_> {
28+
HexString(self.bytes.as_slice().into())
2929
}
3030
}
3131

crates/fuel-core/src/schema/message.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Message {
5252
(*self.0.nonce()).into()
5353
}
5454

55-
async fn data(&self) -> HexString {
55+
async fn data(&self) -> HexString<'_> {
5656
self.0.data().clone().into()
5757
}
5858

@@ -90,7 +90,7 @@ impl MessageQuery {
9090
after: Option<String>,
9191
last: Option<i32>,
9292
before: Option<String>,
93-
) -> async_graphql::Result<Connection<HexString, Message, EmptyFields, EmptyFields>>
93+
) -> async_graphql::Result<Connection<HexString<'_>, Message, EmptyFields, EmptyFields>>
9494
{
9595
let query = ctx.read_view()?;
9696
let owner = owner.map(|owner| owner.0);
@@ -221,7 +221,7 @@ impl MessageProof {
221221
self.0.amount.into()
222222
}
223223

224-
async fn data(&self) -> HexString {
224+
async fn data(&self) -> HexString<'_> {
225225
self.0.data.clone().into()
226226
}
227227
}

crates/fuel-core/src/schema/scalars.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::{
2222
},
2323
str::FromStr,
2424
};
25+
use std::borrow::Cow;
2526
pub use tx_pointer::TxPointer;
2627
pub use utxo_id::UtxoId;
2728

@@ -161,10 +162,26 @@ impl CursorType for SortedTxCursor {
161162
}
162163

163164
#[derive(Clone, Debug, derive_more::Into, derive_more::From, PartialEq, Eq)]
164-
pub struct HexString(pub(crate) Vec<u8>);
165+
pub struct HexString<'a>(pub(crate) Cow<'a, [u8]>);
166+
167+
impl From<Vec<u8>> for HexString<'_> {
168+
fn from(vec: Vec<u8>) -> Self {
169+
HexString(Cow::Owned(vec))
170+
}
171+
}
172+
173+
impl <'a>From<&'a [u8]> for HexString<'a> {
174+
fn from(value: &'a [u8]) -> Self { HexString(Cow::Borrowed(value)) }
175+
}
176+
177+
impl From<HexString<'_>> for Vec<u8> {
178+
fn from(hex: HexString) -> Self {
179+
hex.0.into_owned()
180+
}
181+
}
165182

166183
#[Scalar(name = "HexString")]
167-
impl ScalarType for HexString {
184+
impl ScalarType for HexString<'_> {
168185
fn parse(value: Value) -> InputValueResult<Self> {
169186
match &value {
170187
Value::String(value) => {
@@ -179,14 +196,14 @@ impl ScalarType for HexString {
179196
}
180197
}
181198

182-
impl Display for HexString {
199+
impl Display for HexString<'_> {
183200
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
184-
let s = format!("0x{}", hex::encode(&self.0));
201+
let s = format!("0x{}", hex::encode(&self.0.as_ref()));
185202
s.fmt(f)
186203
}
187204
}
188205

189-
impl CursorType for HexString {
206+
impl CursorType for HexString<'_> {
190207
type Error = String;
191208

192209
fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
@@ -198,28 +215,28 @@ impl CursorType for HexString {
198215
}
199216
}
200217

201-
impl FromStr for HexString {
218+
impl FromStr for HexString<'_> {
202219
type Err = String;
203220

204221
fn from_str(s: &str) -> Result<Self, Self::Err> {
205222
let value = s.strip_prefix("0x").unwrap_or(s);
206223
// decode into bytes
207224
let bytes = hex::decode(value).map_err(|e| e.to_string())?;
208-
Ok(HexString(bytes))
225+
Ok(HexString(Cow::Owned(bytes)))
209226
}
210227
}
211228

212-
impl From<fuel_types::Nonce> for HexString {
229+
impl From<fuel_types::Nonce> for HexString<'_> {
213230
fn from(n: fuel_types::Nonce) -> Self {
214-
HexString(n.to_vec())
231+
HexString(Cow::Owned(n.to_vec()))
215232
}
216233
}
217234

218-
impl TryInto<fuel_types::Nonce> for HexString {
235+
impl TryInto<fuel_types::Nonce> for HexString<'_> {
219236
type Error = TryFromSliceError;
220237

221238
fn try_into(self) -> Result<fuel_types::Nonce, Self::Error> {
222-
let bytes: [u8; 32] = self.0.as_slice().try_into()?;
239+
let bytes: [u8; 32] = self.0.as_ref().try_into()?;
223240
Ok(fuel_types::Nonce::from(bytes))
224241
}
225242
}

crates/fuel-core/src/schema/storage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl StorageSlot {
160160
self.key.into()
161161
}
162162

163-
async fn value(&self) -> HexString {
164-
HexString(self.value.clone())
163+
async fn value(&self) -> HexString<'_> {
164+
HexString::from(self.value.as_ref())
165165
}
166166
}

crates/fuel-core/src/schema/tx.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl TxQuery {
124124
async fn dry_run_inner(
125125
&self,
126126
ctx: &Context<'_>,
127-
txs: Vec<HexString>,
127+
txs: Vec<HexString<'_>>,
128128
// If set to false, disable input utxo validation, overriding the configuration of the node.
129129
// This allows for non-existent inputs to be used without signature validation
130130
// for read-only calls.
@@ -375,7 +375,7 @@ impl TxQuery {
375375
#[graphql(
376376
desc = "The original transaction that contains application level logic only"
377377
)]
378-
tx: HexString,
378+
tx: HexString<'_>,
379379
#[graphql(
380380
desc = "Number of blocks into the future to estimate the gas price for"
381381
)]
@@ -519,7 +519,7 @@ impl TxQuery {
519519
async fn estimate_predicates(
520520
&self,
521521
ctx: &Context<'_>,
522-
tx: HexString,
522+
tx: HexString<'_>,
523523
) -> async_graphql::Result<Transaction> {
524524
let query = ctx.read_view()?.into_owned();
525525

@@ -550,7 +550,7 @@ impl TxQuery {
550550
async fn dry_run(
551551
&self,
552552
ctx: &Context<'_>,
553-
txs: Vec<HexString>,
553+
txs: Vec<HexString<'_>>,
554554
// If set to false, disable input utxo validation, overriding the configuration of the node.
555555
// This allows for non-existent inputs to be used without signature validation
556556
// for read-only calls.
@@ -574,7 +574,7 @@ impl TxQuery {
574574
async fn dry_run_record_storage_reads(
575575
&self,
576576
ctx: &Context<'_>,
577-
txs: Vec<HexString>,
577+
txs: Vec<HexString<'_>>,
578578
// If set to false, disable input utxo validation, overriding the configuration of the node.
579579
// This allows for non-existent inputs to be used without signature validation
580580
// for read-only calls.
@@ -627,7 +627,7 @@ impl TxMutation {
627627
async fn dry_run(
628628
&self,
629629
ctx: &Context<'_>,
630-
txs: Vec<HexString>,
630+
txs: Vec<HexString<'_>>,
631631
// If set to false, disable input utxo validation, overriding the configuration of the node.
632632
// This allows for non-existent inputs to be used without signature validation
633633
// for read-only calls.
@@ -648,7 +648,7 @@ impl TxMutation {
648648
async fn submit(
649649
&self,
650650
ctx: &Context<'_>,
651-
tx: HexString,
651+
tx: HexString<'_>,
652652
estimate_predicates: Option<bool>,
653653
) -> async_graphql::Result<Transaction> {
654654
let txpool = ctx.data_unchecked::<TxPool>();
@@ -679,7 +679,7 @@ impl TxMutation {
679679
pub struct TxStatusSubscription;
680680

681681
#[Subscription]
682-
impl TxStatusSubscription {
682+
impl<'a> TxStatusSubscription {
683683
/// Returns a stream of status updates for the given transaction id.
684684
/// If the current status is [`TransactionStatus::Success`], [`TransactionStatus::Failed`],
685685
/// or [`TransactionStatus::SqueezedOut`] the stream will return that and end immediately.
@@ -693,7 +693,7 @@ impl TxStatusSubscription {
693693
/// a status. If this occurs the stream can simply be restarted to return
694694
/// the latest status.
695695
#[graphql(complexity = "query_costs().status_change + child_complexity")]
696-
async fn status_change<'a>(
696+
async fn status_change(
697697
&self,
698698
ctx: &'a Context<'a>,
699699
#[graphql(desc = "The ID of the transaction")] id: TransactionId,
@@ -721,7 +721,7 @@ impl TxStatusSubscription {
721721
}
722722

723723
#[graphql(name = "alpha__preconfirmations")]
724-
async fn preconfirmations<'a>(
724+
async fn preconfirmations(
725725
&self,
726726
ctx: &'a Context<'a>,
727727
) -> async_graphql::Result<
@@ -745,10 +745,10 @@ impl TxStatusSubscription {
745745

746746
/// Submits transaction to the `TxPool` and await either success or failure.
747747
#[graphql(complexity = "query_costs().submit_and_await + child_complexity")]
748-
async fn submit_and_await<'a>(
748+
async fn submit_and_await(
749749
&self,
750750
ctx: &'a Context<'a>,
751-
tx: HexString,
751+
tx: HexString<'a>,
752752
estimate_predicates: Option<bool>,
753753
) -> async_graphql::Result<
754754
impl Stream<Item = async_graphql::Result<TransactionStatus>> + 'a + use<'a>,
@@ -767,10 +767,10 @@ impl TxStatusSubscription {
767767
/// Compared to the `submitAndAwait`, the stream also contains
768768
/// `SubmittedStatus` and potentially preconfirmation as an intermediate state.
769769
#[graphql(complexity = "query_costs().submit_and_await + child_complexity")]
770-
async fn submit_and_await_status<'a>(
770+
async fn submit_and_await_status(
771771
&self,
772772
ctx: &'a Context<'a>,
773-
tx: HexString,
773+
tx: HexString<'a>,
774774
estimate_predicates: Option<bool>,
775775
include_preconfirmation: Option<bool>,
776776
) -> async_graphql::Result<
@@ -788,7 +788,7 @@ impl TxStatusSubscription {
788788

789789
async fn submit_and_await_status<'a>(
790790
ctx: &'a Context<'a>,
791-
tx: HexString,
791+
tx: HexString<'a>,
792792
estimate_predicates: bool,
793793
include_preconfirmation: bool,
794794
) -> async_graphql::Result<
@@ -884,8 +884,8 @@ pub mod schema_types {
884884
// signature verification. They provide a mocked version of the predicate that
885885
// returns `true` even if the signature doesn't match.
886886
pub predicate_address: Address,
887-
pub predicate: HexString,
888-
pub predicate_data: HexString,
887+
pub predicate: HexString<'static>,
888+
pub predicate_data: HexString<'static>,
889889
}
890890

891891
#[derive(async_graphql::InputObject)]

0 commit comments

Comments
 (0)