Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the data field from canonical serialization and deserialization of receipts #505

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ Description of the upcoming release here.
was done incorrectly, devaluing the `dep_per_unit` part. After the fixing of
this, the execution should become much more expensive.

- [#505](https://github.com/FuelLabs/fuel-vm/pull/505): The `data` field of the `Receipt`
is not part of the canonical serialization and deserialization anymore. The SDK should use the
`Receipt` type instead of `OpaqueReceipt`. The `Receipt.raw_payload` will be removed for the
`fuel-core 0.20`. The `data` field is optional now. The SDK should update serialization and
deserialization for `MessageOut`, `LogData`, and `ReturnData` receipts.

- [#505](https://github.com/FuelLabs/fuel-vm/pull/505): The `len` field of the `Receipt`
is not padded anymore and represents an initial value.

## [Version 0.34.1]

Mainly new opcodes prices and small performance improvements in the `BinaryMerkleTree`.
Expand Down
117 changes: 58 additions & 59 deletions fuel-tx/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ use crate::{
use alloc::vec::Vec;
use derivative::Derivative;
use fuel_asm::PanicInstruction;
use fuel_crypto::Hasher;
use fuel_types::{
bytes::{
self,
padded_len_usize,
SizedBytes,
WORD_SIZE,
},
fmt_truncated_hex,
bytes::SizedBytes,
fmt_option_truncated_hex,
Address,
AssetId,
Bytes32,
Expand Down Expand Up @@ -74,10 +70,11 @@ pub enum Receipt {
ptr: Word,
len: Word,
digest: Bytes32,
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
data: Vec<u8>,
pc: Word,
is: Word,
#[derivative(Debug(format_with = "fmt_option_truncated_hex::<16>"))]
#[derivative(PartialEq = "ignore", Hash = "ignore")]
data: Option<Vec<u8>>,
},

Panic {
Expand Down Expand Up @@ -113,10 +110,11 @@ pub enum Receipt {
ptr: Word,
len: Word,
digest: Bytes32,
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
data: Vec<u8>,
pc: Word,
is: Word,
#[derivative(Debug(format_with = "fmt_option_truncated_hex::<16>"))]
#[derivative(PartialEq = "ignore", Hash = "ignore")]
data: Option<Vec<u8>>,
},

Transfer {
Expand Down Expand Up @@ -149,8 +147,9 @@ pub enum Receipt {
nonce: Nonce,
len: Word,
digest: Bytes32,
#[derivative(Debug(format_with = "fmt_truncated_hex::<16>"))]
data: Vec<u8>,
#[derivative(Debug(format_with = "fmt_option_truncated_hex::<16>"))]
#[derivative(PartialEq = "ignore", Hash = "ignore")]
data: Option<Vec<u8>>,
},
}

Expand Down Expand Up @@ -187,33 +186,39 @@ impl Receipt {
pub fn return_data(
id: ContractId,
ptr: Word,
digest: Bytes32,
data: Vec<u8>,
pc: Word,
is: Word,
data: Vec<u8>,
) -> Self {
let len = bytes::padded_len(&data) as Word;

Self::return_data_with_len(id, ptr, len, digest, data, pc, is)
let digest = Hasher::hash(&data);
Self::return_data_with_len(
id,
ptr,
data.len() as Word,
digest,
pc,
is,
Some(data),
)
}

pub const fn return_data_with_len(
id: ContractId,
ptr: Word,
len: Word,
digest: Bytes32,
data: Vec<u8>,
pc: Word,
is: Word,
data: Option<Vec<u8>>,
) -> Self {
Self::ReturnData {
id,
ptr,
len,
digest,
data,
pc,
is,
data,
}
}

Expand Down Expand Up @@ -272,14 +277,22 @@ impl Receipt {
ra: Word,
rb: Word,
ptr: Word,
digest: Bytes32,
data: Vec<u8>,
pc: Word,
is: Word,
data: Vec<u8>,
) -> Self {
let len = bytes::padded_len(&data) as Word;

Self::log_data_with_len(id, ra, rb, ptr, len, digest, data, pc, is)
let digest = Hasher::hash(&data);
Self::log_data_with_len(
id,
ra,
rb,
ptr,
data.len() as Word,
digest,
pc,
is,
Some(data),
)
}

pub const fn log_data_with_len(
Expand All @@ -289,9 +302,9 @@ impl Receipt {
ptr: Word,
len: Word,
digest: Bytes32,
data: Vec<u8>,
pc: Word,
is: Word,
data: Option<Vec<u8>>,
) -> Self {
Self::LogData {
id,
Expand All @@ -300,9 +313,9 @@ impl Receipt {
ptr,
len,
digest,
data,
pc,
is,
data,
}
}

Expand Down Expand Up @@ -346,7 +359,7 @@ impl Receipt {
Self::ScriptResult { result, gas_used }
}

pub fn message_out_from_tx_output(
pub fn message_out(
txid: &Bytes32,
idx: Word,
sender: Address,
Expand All @@ -357,20 +370,15 @@ impl Receipt {
let nonce = Output::message_nonce(txid, idx);
let digest = Output::message_digest(&data);

Self::message_out(sender, recipient, amount, nonce, digest, data)
}

pub fn message_out(
sender: Address,
recipient: Address,
amount: Word,
nonce: Nonce,
digest: Bytes32,
data: Vec<u8>,
) -> Self {
let len = bytes::padded_len(&data) as Word;

Self::message_out_with_len(sender, recipient, amount, nonce, len, digest, data)
Self::message_out_with_len(
sender,
recipient,
amount,
nonce,
data.len() as Word,
digest,
Some(data),
)
}

pub const fn message_out_with_len(
Expand All @@ -380,7 +388,7 @@ impl Receipt {
nonce: Nonce,
len: Word,
digest: Bytes32,
data: Vec<u8>,
data: Option<Vec<u8>>,
) -> Self {
Self::MessageOut {
sender,
Expand Down Expand Up @@ -541,9 +549,9 @@ impl Receipt {

pub fn data(&self) -> Option<&[u8]> {
match self {
Self::ReturnData { data, .. } => Some(data),
Self::LogData { data, .. } => Some(data),
Self::MessageOut { data, .. } => Some(data),
Self::ReturnData { data, .. } => data.as_ref().map(|data| data.as_slice()),
Self::LogData { data, .. } => data.as_ref().map(|data| data.as_slice()),
Self::MessageOut { data, .. } => data.as_ref().map(|data| data.as_slice()),
_ => None,
}
}
Expand Down Expand Up @@ -609,13 +617,9 @@ impl Receipt {
nonce,
data,
..
} => Some(compute_message_id(
sender,
recipient,
nonce,
*amount,
data.as_slice(),
)),
} => data.as_ref().map(|data| {
compute_message_id(sender, recipient, nonce, *amount, data.as_slice())
}),
_ => None,
}
}
Expand Down Expand Up @@ -677,12 +681,7 @@ fn trim_contract_id(id: Option<&ContractId>) -> Option<&ContractId> {

impl SizedBytes for Receipt {
fn serialized_size(&self) -> usize {
let data_len = self
.data()
.map(|data| WORD_SIZE + padded_len_usize(data.len()))
.unwrap_or(0);

Self::variant_len_without_data(ReceiptRepr::from(self)) + data_len
Self::variant_len_without_data(ReceiptRepr::from(self))
}
}

Expand Down
36 changes: 6 additions & 30 deletions fuel-tx/src/receipt/receipt_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ impl io::Read for Receipt {
ptr,
len,
digest,
data,
pc,
is,
..
} => {
let full_buf = buf;
type S = ReturnDataSizes;
Expand All @@ -117,11 +117,6 @@ impl io::Read for Receipt {
bytes::store_at(buf, S::layout(S::LAYOUT.digest), digest);
bytes::store_number_at(buf, S::layout(S::LAYOUT.pc), *pc);
bytes::store_number_at(buf, S::layout(S::LAYOUT.is), *is);

bytes::store_bytes(
full_buf.get_mut(S::LEN..).ok_or(bytes::eof())?,
data,
)?;
}

Self::Panic {
Expand Down Expand Up @@ -205,9 +200,9 @@ impl io::Read for Receipt {
ptr,
len,
digest,
data,
pc,
is,
..
} => {
let full_buf = buf;
type S = LogDataSizes;
Expand All @@ -230,11 +225,6 @@ impl io::Read for Receipt {
bytes::store_at(buf, S::layout(S::LAYOUT.digest), digest);
bytes::store_number_at(buf, S::layout(S::LAYOUT.pc), *pc);
bytes::store_number_at(buf, S::layout(S::LAYOUT.is), *is);

bytes::store_bytes(
full_buf.get_mut(S::LEN..).ok_or(bytes::eof())?,
data,
)?;
}

Self::Transfer {
Expand Down Expand Up @@ -318,7 +308,7 @@ impl io::Read for Receipt {
nonce,
len,
digest,
data,
..
} => {
let full_buf = buf;
type S = MessageOutSizes;
Expand All @@ -339,11 +329,6 @@ impl io::Read for Receipt {
bytes::store_at(buf, S::layout(S::LAYOUT.nonce), nonce);
bytes::store_number_at(buf, S::layout(S::LAYOUT.len), *len);
bytes::store_at(buf, S::layout(S::LAYOUT.digest), digest);

bytes::store_bytes(
full_buf.get_mut(S::LEN..).ok_or(bytes::eof())?,
data,
)?;
}
}

Expand Down Expand Up @@ -418,13 +403,10 @@ impl io::Write for Receipt {
let pc = bytes::restore_word_at(buf, S::layout(S::LAYOUT.pc));
let is = bytes::restore_word_at(buf, S::layout(S::LAYOUT.is));

let (_, data, _) =
bytes::restore_bytes(full_buf.get(S::LEN..).ok_or(bytes::eof())?)?;

let id = id.into();
let digest = digest.into();

*self = Self::return_data_with_len(id, ptr, len, digest, data, pc, is);
*self = Self::return_data_with_len(id, ptr, len, digest, pc, is, None);
}

ReceiptRepr::Panic => {
Expand Down Expand Up @@ -497,14 +479,11 @@ impl io::Write for Receipt {
let pc = bytes::restore_word_at(buf, S::layout(S::LAYOUT.pc));
let is = bytes::restore_word_at(buf, S::layout(S::LAYOUT.is));

let (_, data, _) =
bytes::restore_bytes(full_buf.get(S::LEN..).ok_or(bytes::eof())?)?;

let id = id.into();
let digest = digest.into();

*self =
Self::log_data_with_len(id, ra, rb, ptr, len, digest, data, pc, is);
Self::log_data_with_len(id, ra, rb, ptr, len, digest, pc, is, None);
}

ReceiptRepr::Transfer => {
Expand Down Expand Up @@ -577,16 +556,13 @@ impl io::Write for Receipt {
let len = bytes::restore_word_at(buf, S::layout(S::LAYOUT.len));
let digest = bytes::restore_at(buf, S::layout(S::LAYOUT.digest));

let (_, data, _) =
bytes::restore_bytes(full_buf.get(S::LEN..).ok_or(bytes::eof())?)?;

let sender = sender.into();
let recipient = recipient.into();
let nonce = nonce.into();
let digest = digest.into();

*self = Self::message_out_with_len(
sender, recipient, amount, nonce, len, digest, data,
sender, recipient, amount, nonce, len, digest, None,
);
}
}
Expand Down
3 changes: 1 addition & 2 deletions fuel-tx/src/receipt/sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,5 @@ mem_layout!(
amount: Word = WORD_SIZE,
nonce: Nonce = {Nonce::LEN},
len: Word = WORD_SIZE,
digest: Bytes32 = {Bytes32::LEN},
data_len: Word = WORD_SIZE
digest: Bytes32 = {Bytes32::LEN}
);
Loading