Skip to content

Commit

Permalink
Reshuffled fields Script and Create transactions to unify part us…
Browse files Browse the repository at this point in the history
…ed by all chargeable transactions
  • Loading branch information
xgreenx committed Mar 22, 2024
1 parent 38e515d commit 1dc819b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 56 deletions.
2 changes: 1 addition & 1 deletion fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ pub mod field {
pub trait StorageSlots {
fn storage_slots(&self) -> &Vec<StorageSlot>;
fn storage_slots_mut(&mut self) -> StorageSlotRef;
fn storage_slots_offset(&self) -> usize;
fn storage_slots_offset_static() -> usize;

/// Returns the offset to the `StorageSlot` at `idx` index, if any.
fn storage_slots_offset_at(&self, idx: usize) -> Option<usize>;
Expand Down
52 changes: 26 additions & 26 deletions fuel-tx/src/transaction/types/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ impl CreateMetadata {
pub struct Create {
pub(crate) bytecode_length: Word,
pub(crate) bytecode_witness_index: u16,
pub(crate) policies: Policies,
pub(crate) salt: Salt,
pub(crate) storage_slots: Vec<StorageSlot>,
pub(crate) policies: Policies,
pub(crate) inputs: Vec<Input>,
pub(crate) outputs: Vec<Output>,
pub(crate) witnesses: Vec<Witness>,
pub(crate) salt: Salt,
#[cfg_attr(feature = "serde", serde(skip))]
#[derivative(PartialEq = "ignore", Hash = "ignore")]
#[canonical(skip)]
Expand Down Expand Up @@ -385,20 +385,6 @@ mod field {
}
}

impl PoliciesField for Create {
fn policies(&self) -> &Policies {
&self.policies
}

fn policies_mut(&mut self) -> &mut Policies {
&mut self.policies
}

fn policies_offset(&self) -> usize {
Self::salt_offset_static() + Salt::LEN
}
}

impl SaltField for Create {
#[inline(always)]
fn salt(&self) -> &Salt {
Expand All @@ -413,11 +399,6 @@ mod field {
#[inline(always)]
fn salt_offset_static() -> usize {
Self::bytecode_witness_index_offset_static() + WORD_SIZE
+ WORD_SIZE // Policies size
+ WORD_SIZE // Storage slots size
+ WORD_SIZE // Inputs size
+ WORD_SIZE // Outputs size
+ WORD_SIZE // Witnesses size
}
}

Expand All @@ -435,19 +416,39 @@ mod field {
}

#[inline(always)]
fn storage_slots_offset(&self) -> usize {
self.policies_offset() + self.policies.size_dynamic()
fn storage_slots_offset_static() -> usize {
Self::salt_offset_static() + Salt::LEN
+ WORD_SIZE // Policies size
+ WORD_SIZE // Storage slots size
+ WORD_SIZE // Inputs size
+ WORD_SIZE // Outputs size
+ WORD_SIZE // Witnesses size
}

fn storage_slots_offset_at(&self, idx: usize) -> Option<usize> {
if idx < self.storage_slots.len() {
Some(self.storage_slots_offset() + idx * StorageSlot::SLOT_SIZE)
Some(Self::storage_slots_offset_static() + idx * StorageSlot::SLOT_SIZE)
} else {
None
}
}
}

impl PoliciesField for Create {
fn policies(&self) -> &Policies {
&self.policies
}

fn policies_mut(&mut self) -> &mut Policies {
&mut self.policies
}

fn policies_offset(&self) -> usize {
Self::storage_slots_offset_static()
+ self.storage_slots.len() * StorageSlot::SLOT_SIZE
}
}

impl Inputs for Create {
#[inline(always)]
fn inputs(&self) -> &Vec<Input> {
Expand All @@ -461,8 +462,7 @@ mod field {

#[inline(always)]
fn inputs_offset(&self) -> usize {
self.storage_slots_offset()
+ self.storage_slots.len() * StorageSlot::SLOT_SIZE
self.policies_offset() + self.policies.size_dynamic()
}

#[inline(always)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 75
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000010101010101010101010101010101010101010101010101010101010101010101000000000000000202020202020202020202020202020202020202020202020202020202020202020303030303030303030303030303030303030303030303030303030303030303000000000000002e00000000000000050505050505050505050505050505050505050505050505050505050505050505
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000001000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000010101010101010101010101010101010101010101010101010101010101010101000000000000000202020202020202020202020202020202020202020202020202020202020202020303030303030303030303030303030303030303030303030303030303030303000000000000002e00000000000000050505050505050505050505050505050505050505050505050505050505050505
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 55
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000007b00000000000f42400000000000000000010101010101010101010101010101010101010101010101010101010101010100000000000000020202020202020202020202020202020202020202020202020202020202020202000000000000000b0505050505050505050505050505050505050505050505050505050505050505000000000000002e0000000000000005000000000000000000000000000186a0000000000000000a000000000000000c0303030303030303030300000000000004040404040404040404040400000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000001000000000000000000000000000000000000000000000001000000000000007b00000000000f42400000000000000000010101010101010101010101010101010101010101010101010101010101010100000000000000020202020202020202020202020202020202020202020202020202020202020202000000000000000b0505050505050505050505050505050505050505050505050505050505050505000000000000002e0000000000000005000000000000000000000000000186a0000000000000000a000000000000000c0303030303030303030300000000000004040404040404040404040400000000
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 124
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030300000000000000040505050505050505050505050505050505050505050505050505050505050505000000000000000000000000000186a00000000000000000000000000000000b000000000000000c0707070707070707070707000000000008080808080808080808080800000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000001000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030300000000000000040505050505050505050505050505050505050505050505050505050505050505000000000000000000000000000186a00000000000000000000000000000000b000000000000000c0707070707070707070707000000000008080808080808080808080800000000
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 173
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000d00000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030300000000000000040505050505050505050505050505050505050505050505050505050505050505000000000000000000000000000186a0000000000000000a000000000000000b000000000000000c060606060606060606060000000000000707070707070707070707000000000008080808080808080808080800000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d0000000000000001000000000000000000000000000000000000000000000001000000000000007b00000000000f424000000000000000020202020202020202020202020202020202020202020202020202020202020202030303030303030303030303030303030303030303030303030303030303030300000000000000040505050505050505050505050505050505050505050505050505050505050505000000000000000000000000000186a0000000000000000a000000000000000b000000000000000c060606060606060606060000000000000707070707070707070707000000000008080808080808080808080800000000
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 30
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000000010101010101010101010101010101010101010101010101010101010101010100000000000000020202020202020202020202020202020202020202020202020202020202020202000000000000000b0505050505050505050505050505050505050505050505050505050505050505000000000000002e00000000000000050000000000000004000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000100000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000000010101010101010101010101010101010101010101010101010101010101010100000000000000020202020202020202020202020202020202020202020202020202020202020202000000000000000b0505050505050505050505050505050505050505050505050505050505050505000000000000002e00000000000000050000000000000004000000000000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 100
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000002020202020202020202020202020202020202020202020202020202020202020203030303030303030303030303030303030303030303030303030303030303030000000000000004050505050505050505050505050505050505050505050505050505050505050500000000000000060000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000100000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000002020202020202020202020202020202020202020202020202020202020202020203030303030303030303030303030303030303030303030303030303030303030000000000000004050505050505050505050505050505050505050505050505050505050505050500000000000000060000000000000000000000000000000000000000000000000000000000000000
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: fuel-tx/src/transaction/types/input/snapshot_tests.rs
assertion_line: 149
expression: hex
---
0000000000000000000000000000000000000000000000000000000000000000000000000000000f0000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000002020202020202020202020202020202020202020202020202020202020202020203030303030303030303030303030303030303030303030303030303030303030000000000000004050505050505050505050505050505050505050505050505050505050505050500000000000000060000000000000000000000000000000a0000000000000000000000000000000007070707070707070707000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000100000000000000000000000000000000000000000000000100000000000003e8000000000000007b00000000000f42400000000000000002020202020202020202020202020202020202020202020202020202020202020203030303030303030303030303030303030303030303030303030303030303030000000000000004050505050505050505050505050505050505050505050505050505050505050500000000000000060000000000000000000000000000000a0000000000000000000000000000000007070707070707070707000000000000
17 changes: 9 additions & 8 deletions fuel-tx/src/transaction/types/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) struct ScriptMetadata {
#[derivative(Eq, PartialEq, Hash, Debug)]
pub struct Script {
pub(crate) script_gas_limit: Word,
pub(crate) receipts_root: Bytes32,
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
pub(crate) script: Vec<u8>,
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
Expand All @@ -67,7 +68,6 @@ pub struct Script {
pub(crate) inputs: Vec<Input>,
pub(crate) outputs: Vec<Output>,
pub(crate) witnesses: Vec<Witness>,
pub(crate) receipts_root: Bytes32,
#[cfg_attr(feature = "serde", serde(skip))]
#[derivative(PartialEq = "ignore", Hash = "ignore")]
#[canonical(skip)]
Expand Down Expand Up @@ -262,12 +262,6 @@ mod field {
#[inline(always)]
fn receipts_root_offset_static() -> usize {
Self::script_gas_limit_offset_static() + WORD_SIZE
+ WORD_SIZE // Script size
+ WORD_SIZE // Script data size
+ WORD_SIZE // Policies size
+ WORD_SIZE // Inputs size
+ WORD_SIZE // Outputs size
+ WORD_SIZE // Witnesses size
}
}

Expand All @@ -284,7 +278,14 @@ mod field {

#[inline(always)]
fn script_offset_static() -> usize {
Self::receipts_root_offset_static() + Bytes32::LEN // Receipts root
Self::receipts_root_offset_static()
+ Bytes32::LEN // Receipts root
+ WORD_SIZE // Script size
+ WORD_SIZE // Script data size
+ WORD_SIZE // Policies size
+ WORD_SIZE // Inputs size
+ WORD_SIZE // Outputs size
+ WORD_SIZE // Witnesses size
}
}

Expand Down
12 changes: 5 additions & 7 deletions fuel-vm/src/tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use rand::{
#[cfg(feature = "alloc")]
use alloc::vec;

use crate::consts::WORD_SIZE;

#[test]
fn transaction_can_be_executed_after_maturity() {
const MATURITY: BlockHeight = BlockHeight::new(1);
Expand Down Expand Up @@ -62,9 +60,8 @@ fn malleable_fields_do_not_affect_validity() {

let params = ConsensusParameters::default();

let tx_size_ptr =
32 + (params.tx_params().max_inputs() as usize * (AssetId::LEN + WORD_SIZE));
let tx_start_ptr = tx_size_ptr + 8;
let tx_start_ptr = params.tx_params().tx_offset();
let tx_size_ptr = tx_start_ptr - 8;

let tx = TransactionBuilder::script(
vec![
Expand Down Expand Up @@ -99,9 +96,10 @@ fn malleable_fields_do_not_affect_validity() {
op::mcl(0x26, 0x27),
// Zero out witness count
op::gtf_args(0x26, 0x00, GTFArgs::Script),
op::sub(0x26, 0x26, 32 + 8), // Offset to get the witness count address
op::subi(0x26, 0x26, 8), // Offset to get the witness count address
op::sub(0x26, 0x26, 0x20), // Offset in relative to the tx bytes
op::add(0x26, 0x26, RegId::HP), // Redirect the pointer to heap
op::sub(0x26, 0x26, 0x20), // Offset in relative to the tx bytes
op::addi(0x26, 0x26, 32 + 8), // Offset of tx bytes in heap
op::sw(0x26, RegId::ZERO, 0), // Zero out the witness count
// Actually hash
op::subi(0x24, 0x21, 64 + 8 - 8), // Offset ptr
Expand Down

0 comments on commit 1dc819b

Please sign in to comment.