Skip to content

Commit 107a46c

Browse files
committed
Use references in serializer adapter code
1 parent b2ba2a2 commit 107a46c

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

crates/services/block_aggregator_api/src/blocks/importer_and_db_source/serializer_adapter.rs

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,16 @@ impl BlockSerializer for SerializerAdapter {
125125
type Block = ProtoBlock;
126126

127127
fn serialize_block(&self, block: &FuelBlock) -> crate::result::Result<Self::Block> {
128-
let (header, txs) = block.clone().into_inner();
129-
let proto_header = proto_header_from_header(header);
128+
let proto_header = proto_header_from_header(block.header());
130129
match &block {
131130
FuelBlock::V1(_) => {
132131
let proto_v1_block = ProtoV1Block {
133132
header: Some(proto_header),
134-
transactions: txs.into_iter().map(proto_tx_from_tx).collect(),
133+
transactions: block
134+
.transactions()
135+
.into_iter()
136+
.map(proto_tx_from_tx)
137+
.collect(),
135138
};
136139
Ok(ProtoBlock {
137140
versioned_block: Some(ProtoVersionedBlock::V1(proto_v1_block)),
@@ -141,19 +144,19 @@ impl BlockSerializer for SerializerAdapter {
141144
}
142145
}
143146

144-
fn proto_header_from_header(header: BlockHeader) -> ProtoHeader {
147+
fn proto_header_from_header(header: &BlockHeader) -> ProtoHeader {
145148
let block_id = header.id();
146-
let consensus = *header.consensus();
149+
let consensus = header.consensus();
147150
let versioned_header = match header {
148151
BlockHeader::V1(header) => {
149152
let proto_v1_header =
150-
proto_v1_header_from_v1_header(consensus, block_id, header);
153+
proto_v1_header_from_v1_header(&consensus, &block_id, header);
151154
ProtoVersionedHeader::V1(proto_v1_header)
152155
}
153156
#[cfg(feature = "fault-proving")]
154157
BlockHeader::V2(header) => {
155158
let proto_v2_header =
156-
proto_v2_header_from_v2_header(consensus, block_id, header);
159+
proto_v2_header_from_v2_header(consensus, &block_id, header);
157160
ProtoVersionedHeader::V2(proto_v2_header)
158161
}
159162
};
@@ -164,9 +167,9 @@ fn proto_header_from_header(header: BlockHeader) -> ProtoHeader {
164167
}
165168

166169
fn proto_v1_header_from_v1_header(
167-
consensus: ConsensusHeader<GeneratedConsensusFields>,
168-
block_id: BlockId,
169-
header: BlockHeaderV1,
170+
consensus: &ConsensusHeader<GeneratedConsensusFields>,
171+
block_id: &BlockId,
172+
header: &BlockHeaderV1,
170173
) -> ProtoV1Header {
171174
let application = header.application();
172175
let generated = application.generated;
@@ -190,9 +193,9 @@ fn proto_v1_header_from_v1_header(
190193

191194
#[cfg(feature = "fault-proving")]
192195
fn proto_v2_header_from_v2_header(
193-
consensus: ConsensusHeader<GeneratedConsensusFields>,
194-
block_id: BlockId,
195-
header: BlockHeaderV2,
196+
consensus: &ConsensusHeader<GeneratedConsensusFields>,
197+
block_id: &BlockId,
198+
header: &BlockHeaderV2,
196199
) -> ProtoV2Header {
197200
let application = *header.application();
198201
let generated = application.generated;
@@ -215,7 +218,7 @@ fn proto_v2_header_from_v2_header(
215218
}
216219
}
217220

218-
fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
221+
fn proto_tx_from_tx(tx: &FuelTransaction) -> ProtoTransaction {
219222
match tx {
220223
FuelTransaction::Script(script) => {
221224
let proto_script = ProtoScriptTx {
@@ -224,16 +227,10 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
224227
script: script.script().clone(),
225228
script_data: script.script_data().clone(),
226229
policies: Some(proto_policies_from_policies(script.policies())),
227-
inputs: script
228-
.inputs()
229-
.iter()
230-
.cloned()
231-
.map(proto_input_from_input)
232-
.collect(),
230+
inputs: script.inputs().iter().map(proto_input_from_input).collect(),
233231
outputs: script
234232
.outputs()
235233
.iter()
236-
.cloned()
237234
.map(proto_output_from_output)
238235
.collect(),
239236
witnesses: script
@@ -258,16 +255,10 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
258255
.map(proto_storage_slot_from_storage_slot)
259256
.collect(),
260257
policies: Some(proto_policies_from_policies(create.policies())),
261-
inputs: create
262-
.inputs()
263-
.iter()
264-
.cloned()
265-
.map(proto_input_from_input)
266-
.collect(),
258+
inputs: create.inputs().iter().map(proto_input_from_input).collect(),
267259
outputs: create
268260
.outputs()
269261
.iter()
270-
.cloned()
271262
.map(proto_output_from_output)
272263
.collect(),
273264
witnesses: create
@@ -308,13 +299,11 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
308299
inputs: upgrade
309300
.inputs()
310301
.iter()
311-
.cloned()
312302
.map(proto_input_from_input)
313303
.collect(),
314304
outputs: upgrade
315305
.outputs()
316306
.iter()
317-
.cloned()
318307
.map(proto_output_from_output)
319308
.collect(),
320309
witnesses: upgrade
@@ -337,16 +326,10 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
337326
subsections_number: u32::from(*upload.subsections_number()),
338327
proof_set: upload.proof_set().iter().map(bytes32_to_vec).collect(),
339328
policies: Some(proto_policies_from_policies(upload.policies())),
340-
inputs: upload
341-
.inputs()
342-
.iter()
343-
.cloned()
344-
.map(proto_input_from_input)
345-
.collect(),
329+
inputs: upload.inputs().iter().map(proto_input_from_input).collect(),
346330
outputs: upload
347331
.outputs()
348332
.iter()
349-
.cloned()
350333
.map(proto_output_from_output)
351334
.collect(),
352335
witnesses: upload
@@ -366,16 +349,10 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
366349
blob_id: blob.blob_id().as_ref().to_vec(),
367350
witness_index: u32::from(*blob.bytecode_witness_index()),
368351
policies: Some(proto_policies_from_policies(blob.policies())),
369-
inputs: blob
370-
.inputs()
371-
.iter()
372-
.cloned()
373-
.map(proto_input_from_input)
374-
.collect(),
352+
inputs: blob.inputs().iter().map(proto_input_from_input).collect(),
375353
outputs: blob
376354
.outputs()
377355
.iter()
378-
.cloned()
379356
.map(proto_output_from_output)
380357
.collect(),
381358
witnesses: blob
@@ -393,7 +370,7 @@ fn proto_tx_from_tx(tx: FuelTransaction) -> ProtoTransaction {
393370
}
394371
}
395372

396-
fn proto_input_from_input(input: Input) -> ProtoInput {
373+
fn proto_input_from_input(input: &Input) -> ProtoInput {
397374
match input {
398375
Input::CoinSigned(coin_signed) => ProtoInput {
399376
variant: Some(ProtoInputVariant::CoinSigned(ProtoCoinSignedInput {
@@ -536,15 +513,15 @@ fn proto_contract_output_from_contract(
536513
}
537514
}
538515

539-
fn proto_output_from_output(output: Output) -> ProtoOutput {
516+
fn proto_output_from_output(output: &Output) -> ProtoOutput {
540517
let variant = match output {
541518
Output::Coin {
542519
to,
543520
amount,
544521
asset_id,
545522
} => ProtoOutputVariant::Coin(ProtoCoinOutput {
546523
to: to.as_ref().to_vec(),
547-
amount,
524+
amount: *amount,
548525
asset_id: asset_id.as_ref().to_vec(),
549526
}),
550527
Output::Contract(contract) => {
@@ -556,7 +533,7 @@ fn proto_output_from_output(output: Output) -> ProtoOutput {
556533
asset_id,
557534
} => ProtoOutputVariant::Change(ProtoChangeOutput {
558535
to: to.as_ref().to_vec(),
559-
amount,
536+
amount: *amount,
560537
asset_id: asset_id.as_ref().to_vec(),
561538
}),
562539
Output::Variable {
@@ -565,7 +542,7 @@ fn proto_output_from_output(output: Output) -> ProtoOutput {
565542
asset_id,
566543
} => ProtoOutputVariant::Variable(ProtoVariableOutput {
567544
to: to.as_ref().to_vec(),
568-
amount,
545+
amount: *amount,
569546
asset_id: asset_id.as_ref().to_vec(),
570547
}),
571548
Output::ContractCreated {
@@ -846,7 +823,7 @@ pub fn fuel_block_from_protobuf(
846823
.clone()
847824
.ok_or_else(|| anyhow::anyhow!("Missing protobuf header"))
848825
.map_err(Error::Serialization)?;
849-
partial_header_from_proto_header(proto_header)?
826+
partial_header_from_proto_header(&proto_header)?
850827
}
851828
};
852829
let txs = match versioned_block {
@@ -869,7 +846,7 @@ pub fn fuel_block_from_protobuf(
869846
}
870847

871848
pub fn partial_header_from_proto_header(
872-
proto_header: ProtoHeader,
849+
proto_header: &ProtoHeader,
873850
) -> Result<PartialBlockHeader> {
874851
let partial_header = PartialBlockHeader {
875852
consensus: proto_header_to_empty_consensus_header(&proto_header)?,
@@ -889,7 +866,7 @@ pub fn tx_from_proto_tx(proto_tx: &ProtoTransaction) -> Result<FuelTransaction>
889866
let policies = proto_script
890867
.policies
891868
.clone()
892-
.map(policies_from_proto_policies)
869+
.map(|p| policies_from_proto_policies(&p))
893870
.unwrap_or_default();
894871
let inputs = proto_script
895872
.inputs
@@ -931,7 +908,7 @@ pub fn tx_from_proto_tx(proto_tx: &ProtoTransaction) -> Result<FuelTransaction>
931908
let policies = proto_create
932909
.policies
933910
.clone()
934-
.map(policies_from_proto_policies)
911+
.map(|p| policies_from_proto_policies(&p))
935912
.unwrap_or_default();
936913
let inputs = proto_create
937914
.inputs
@@ -1019,7 +996,7 @@ pub fn tx_from_proto_tx(proto_tx: &ProtoTransaction) -> Result<FuelTransaction>
1019996
let policies = proto_upgrade
1020997
.policies
1021998
.clone()
1022-
.map(policies_from_proto_policies)
999+
.map(|p| policies_from_proto_policies(&p))
10231000
.unwrap_or_default();
10241001
let inputs = proto_upgrade
10251002
.inputs
@@ -1051,7 +1028,7 @@ pub fn tx_from_proto_tx(proto_tx: &ProtoTransaction) -> Result<FuelTransaction>
10511028
let policies = proto_upload
10521029
.policies
10531030
.clone()
1054-
.map(policies_from_proto_policies)
1031+
.map(|p| policies_from_proto_policies(&p))
10551032
.unwrap_or_default();
10561033
let inputs = proto_upload
10571034
.inputs
@@ -1125,7 +1102,7 @@ pub fn tx_from_proto_tx(proto_tx: &ProtoTransaction) -> Result<FuelTransaction>
11251102
let policies = proto_blob
11261103
.policies
11271104
.clone()
1128-
.map(policies_from_proto_policies)
1105+
.map(|p| policies_from_proto_policies(&p))
11291106
.unwrap_or_default();
11301107
let inputs = proto_blob
11311108
.inputs
@@ -1384,11 +1361,11 @@ fn input_from_proto_input(proto_input: &ProtoInput) -> Result<Input> {
13841361
}
13851362
}
13861363

1387-
fn policies_from_proto_policies(proto_policies: ProtoPolicies) -> FuelPolicies {
1364+
fn policies_from_proto_policies(proto_policies: &ProtoPolicies) -> FuelPolicies {
13881365
let ProtoPolicies { bits, values } = proto_policies;
13891366
let mut policies = FuelPolicies::default();
13901367
let bits =
1391-
PoliciesBits::from_bits(bits).expect("Should be able to create from `u32`");
1368+
PoliciesBits::from_bits(*bits).expect("Should be able to create from `u32`");
13921369
if bits.contains(PoliciesBits::Tip)
13931370
&& let Some(tip) = values.first()
13941371
{

0 commit comments

Comments
 (0)