Skip to content

Commit a571b5f

Browse files
committed
chore: use Cow in scalars/HexString
1 parent 6b6dd4a commit a571b5f

File tree

9 files changed

+96
-79
lines changed

9 files changed

+96
-79
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Blob {
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/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Contract {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl From<Vec<u8>> for DaCompressedBlock {
2525
#[Object]
2626
impl DaCompressedBlock {
2727
async fn bytes(&self) -> HexString {
28-
HexString(self.bytes.clone())
28+
HexString(self.bytes.clone().into())
2929
}
3030
}
3131

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,6 @@ impl StorageSlot {
161161
}
162162

163163
async fn value(&self) -> HexString {
164-
HexString(self.value.clone())
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
@@ -122,7 +122,7 @@ impl TxQuery {
122122
async fn dry_run_inner(
123123
&self,
124124
ctx: &Context<'_>,
125-
txs: Vec<HexString>,
125+
txs: Vec<HexString<'_>>,
126126
// If set to false, disable input utxo validation, overriding the configuration of the node.
127127
// This allows for non-existent inputs to be used without signature validation
128128
// for read-only calls.
@@ -373,7 +373,7 @@ impl TxQuery {
373373
#[graphql(
374374
desc = "The original transaction that contains application level logic only"
375375
)]
376-
tx: HexString,
376+
tx: HexString<'_>,
377377
#[graphql(
378378
desc = "Number of blocks into the future to estimate the gas price for"
379379
)]
@@ -517,7 +517,7 @@ impl TxQuery {
517517
async fn estimate_predicates(
518518
&self,
519519
ctx: &Context<'_>,
520-
tx: HexString,
520+
tx: HexString<'_>,
521521
) -> async_graphql::Result<Transaction> {
522522
let query = ctx.read_view()?.into_owned();
523523

@@ -548,7 +548,7 @@ impl TxQuery {
548548
async fn dry_run(
549549
&self,
550550
ctx: &Context<'_>,
551-
txs: Vec<HexString>,
551+
txs: Vec<HexString<'_>>,
552552
// If set to false, disable input utxo validation, overriding the configuration of the node.
553553
// This allows for non-existent inputs to be used without signature validation
554554
// for read-only calls.
@@ -572,7 +572,7 @@ impl TxQuery {
572572
async fn dry_run_record_storage_reads(
573573
&self,
574574
ctx: &Context<'_>,
575-
txs: Vec<HexString>,
575+
txs: Vec<HexString<'_>>,
576576
// If set to false, disable input utxo validation, overriding the configuration of the node.
577577
// This allows for non-existent inputs to be used without signature validation
578578
// for read-only calls.
@@ -625,7 +625,7 @@ impl TxMutation {
625625
async fn dry_run(
626626
&self,
627627
ctx: &Context<'_>,
628-
txs: Vec<HexString>,
628+
txs: Vec<HexString<'_>>,
629629
// If set to false, disable input utxo validation, overriding the configuration of the node.
630630
// This allows for non-existent inputs to be used without signature validation
631631
// for read-only calls.
@@ -646,7 +646,7 @@ impl TxMutation {
646646
async fn submit(
647647
&self,
648648
ctx: &Context<'_>,
649-
tx: HexString,
649+
tx: HexString<'_>,
650650
estimate_predicates: Option<bool>,
651651
) -> async_graphql::Result<Transaction> {
652652
let txpool = ctx.data_unchecked::<TxPool>();
@@ -677,7 +677,7 @@ impl TxMutation {
677677
pub struct TxStatusSubscription;
678678

679679
#[Subscription]
680-
impl TxStatusSubscription {
680+
impl<'a> TxStatusSubscription {
681681
/// Returns a stream of status updates for the given transaction id.
682682
/// If the current status is [`TransactionStatus::Success`], [`TransactionStatus::Failed`],
683683
/// or [`TransactionStatus::SqueezedOut`] the stream will return that and end immediately.
@@ -691,7 +691,7 @@ impl TxStatusSubscription {
691691
/// a status. If this occurs the stream can simply be restarted to return
692692
/// the latest status.
693693
#[graphql(complexity = "query_costs().status_change + child_complexity")]
694-
async fn status_change<'a>(
694+
async fn status_change(
695695
&self,
696696
ctx: &'a Context<'a>,
697697
#[graphql(desc = "The ID of the transaction")] id: TransactionId,
@@ -720,10 +720,10 @@ impl TxStatusSubscription {
720720

721721
/// Submits transaction to the `TxPool` and await either success or failure.
722722
#[graphql(complexity = "query_costs().submit_and_await + child_complexity")]
723-
async fn submit_and_await<'a>(
723+
async fn submit_and_await(
724724
&self,
725725
ctx: &'a Context<'a>,
726-
tx: HexString,
726+
tx: HexString<'a>,
727727
estimate_predicates: Option<bool>,
728728
) -> async_graphql::Result<
729729
impl Stream<Item = async_graphql::Result<TransactionStatus>> + 'a + use<'a>,
@@ -742,10 +742,10 @@ impl TxStatusSubscription {
742742
/// Compared to the `submitAndAwait`, the stream also contains
743743
/// `SubmittedStatus` and potentially preconfirmation as an intermediate state.
744744
#[graphql(complexity = "query_costs().submit_and_await + child_complexity")]
745-
async fn submit_and_await_status<'a>(
746-
&self,
745+
async fn submit_and_await_status(
746+
&'a self,
747747
ctx: &'a Context<'a>,
748-
tx: HexString,
748+
tx: HexString<'a>,
749749
estimate_predicates: Option<bool>,
750750
include_preconfirmation: Option<bool>,
751751
) -> async_graphql::Result<
@@ -763,7 +763,7 @@ impl TxStatusSubscription {
763763

764764
async fn submit_and_await_status<'a>(
765765
ctx: &'a Context<'a>,
766-
tx: HexString,
766+
tx: HexString<'a>,
767767
estimate_predicates: bool,
768768
include_preconfirmation: bool,
769769
) -> async_graphql::Result<
@@ -859,8 +859,8 @@ pub mod schema_types {
859859
// signature verification. They provide a mocked version of the predicate that
860860
// returns `true` even if the signature doesn't match.
861861
pub predicate_address: Address,
862-
pub predicate: HexString,
863-
pub predicate_data: HexString,
862+
pub predicate: HexString<'static>,
863+
pub predicate_data: HexString<'static>,
864864
}
865865

866866
#[derive(async_graphql::InputObject)]

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ use async_graphql::{
1717
use fuel_core_types::fuel_tx;
1818

1919
#[derive(Union)]
20-
pub enum Input {
21-
Coin(InputCoin),
20+
pub enum Input<'a> {
21+
Coin(InputCoin<'a>),
2222
Contract(InputContract),
23-
Message(InputMessage),
23+
Message(InputMessage<'a>),
2424
}
2525

26-
pub struct InputCoin {
26+
pub struct InputCoin<'a> {
2727
utxo_id: UtxoId,
2828
owner: Address,
2929
amount: U64,
3030
asset_id: AssetId,
3131
tx_pointer: TxPointer,
3232
witness_index: u16,
3333
predicate_gas_used: U64,
34-
predicate: HexString,
35-
predicate_data: HexString,
34+
predicate: HexString<'a>,
35+
predicate_data: HexString<'a>,
3636
}
3737

3838
#[Object]
39-
impl InputCoin {
39+
impl InputCoin<'_> {
4040
async fn utxo_id(&self) -> UtxoId {
4141
self.utxo_id
4242
}
@@ -105,20 +105,20 @@ impl InputContract {
105105
}
106106
}
107107

108-
pub struct InputMessage {
108+
pub struct InputMessage<'a> {
109109
sender: Address,
110110
recipient: Address,
111111
amount: U64,
112112
nonce: Nonce,
113113
witness_index: u16,
114114
predicate_gas_used: U64,
115-
data: HexString,
116-
predicate: HexString,
117-
predicate_data: HexString,
115+
data: HexString<'a>,
116+
predicate: HexString<'a>,
117+
predicate_data: HexString<'a>,
118118
}
119119

120120
#[Object]
121-
impl InputMessage {
121+
impl InputMessage<'_> {
122122
async fn sender(&self) -> Address {
123123
self.sender
124124
}
@@ -156,8 +156,8 @@ impl InputMessage {
156156
}
157157
}
158158

159-
impl From<&fuel_tx::Input> for Input {
160-
fn from(input: &fuel_tx::Input) -> Self {
159+
impl <'a> From<&'a fuel_tx::Input> for Input<'a> {
160+
fn from(input: &'a fuel_tx::Input) -> Self {
161161
match input {
162162
fuel_tx::Input::CoinSigned(fuel_tx::input::coin::CoinSigned {
163163
utxo_id,
@@ -175,7 +175,7 @@ impl From<&fuel_tx::Input> for Input {
175175
tx_pointer: TxPointer(*tx_pointer),
176176
witness_index: *witness_index,
177177
predicate_gas_used: 0.into(),
178-
predicate: HexString(Default::default()),
178+
predicate: HexString(vec![].into()),
179179
predicate_data: HexString(Default::default()),
180180
}),
181181
fuel_tx::Input::CoinPredicate(fuel_tx::input::coin::CoinPredicate {
@@ -196,8 +196,8 @@ impl From<&fuel_tx::Input> for Input {
196196
tx_pointer: TxPointer(*tx_pointer),
197197
witness_index: Default::default(),
198198
predicate_gas_used: (*predicate_gas_used).into(),
199-
predicate: HexString(predicate.to_vec()),
200-
predicate_data: HexString(predicate_data.clone()),
199+
predicate: HexString::from(predicate.as_ref()),
200+
predicate_data: HexString::from(predicate_data.as_ref()),
201201
}),
202202
fuel_tx::Input::Contract(contract) => Input::Contract(contract.into()),
203203
fuel_tx::Input::MessageCoinSigned(
@@ -239,8 +239,8 @@ impl From<&fuel_tx::Input> for Input {
239239
witness_index: Default::default(),
240240
predicate_gas_used: (*predicate_gas_used).into(),
241241
data: HexString(Default::default()),
242-
predicate: HexString(predicate.to_vec()),
243-
predicate_data: HexString(predicate_data.clone()),
242+
predicate: HexString::from(predicate.as_ref()),
243+
predicate_data: HexString::from(predicate_data.as_ref()),
244244
}),
245245
fuel_tx::Input::MessageDataSigned(
246246
fuel_tx::input::message::MessageDataSigned {
@@ -259,9 +259,9 @@ impl From<&fuel_tx::Input> for Input {
259259
nonce: Nonce(*nonce),
260260
witness_index: *witness_index,
261261
predicate_gas_used: 0.into(),
262-
data: HexString(data.clone()),
263-
predicate: HexString(Default::default()),
264-
predicate_data: HexString(Default::default()),
262+
data: HexString::from(data.as_ref()),
263+
predicate: HexString(vec![].into()),
264+
predicate_data: HexString(vec![].into()),
265265
}),
266266
fuel_tx::Input::MessageDataPredicate(
267267
fuel_tx::input::message::MessageDataPredicate {
@@ -282,9 +282,9 @@ impl From<&fuel_tx::Input> for Input {
282282
nonce: Nonce(*nonce),
283283
witness_index: Default::default(),
284284
predicate_gas_used: (*predicate_gas_used).into(),
285-
data: HexString(data.clone()),
286-
predicate: HexString(predicate.to_vec()),
287-
predicate_data: HexString(predicate_data.clone()),
285+
data: HexString::from(data.as_ref()),
286+
predicate: HexString::from(predicate.as_ref()),
287+
predicate_data: HexString::from(predicate_data.as_ref()),
288288
}),
289289
}
290290
}

0 commit comments

Comments
 (0)