Skip to content

Commit

Permalink
Add contract_id to Receipt::Panic (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salka1988 authored Oct 6, 2022
1 parent 0cb216d commit ae3e5e4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ repository = "https://github.com/FuelLabs/fuel-tx"
description = "FuelVM transaction."

[dependencies]
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
fuel-asm = { version = "0.9", default-features = false }
fuel-crypto = { version = "0.6", default-features = false }
fuel-merkle = { version = "0.4", default-features = false }
fuel-types = { version = "0.5", default-features = false }
itertools = { version = "0.10", default-features = false }
rand = { version = "0.8", default-features = false, features = ["std_rng"], optional = true }
num-integer = { version = "0.1", default-features = false }
rand = { version = "0.8", default-features = false, features = ["std_rng"], optional = true }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"], optional = true }
serde_json = { version = "1.0", default-features = false, features = ["alloc"], optional = true }

Expand Down
85 changes: 53 additions & 32 deletions src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use alloc::vec::Vec;
use derivative::Derivative;
use fuel_asm::InstructionResult;
use fuel_types::bytes::{self, padded_len_usize, SizedBytes, WORD_SIZE};
use fuel_types::{Address, AssetId, Bytes32, ContractId, MessageId, Word};

use alloc::vec::Vec;

#[cfg(feature = "std")]
mod receipt_std;

Expand All @@ -15,9 +15,11 @@ use receipt_repr::ReceiptRepr;
pub use script_result::ScriptExecutionResult;

use crate::Output;
use crate::Receipt::Panic;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Derivative)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derivative(Eq, PartialEq, Hash)]
pub enum Receipt {
Call {
id: ContractId,
Expand Down Expand Up @@ -53,6 +55,8 @@ pub enum Receipt {
reason: InstructionResult,
pc: Word,
is: Word,
#[derivative(PartialEq = "ignore", Hash = "ignore")]
contract_id: Option<ContractId>,
},

Revert {
Expand Down Expand Up @@ -183,7 +187,24 @@ impl Receipt {
}

pub const fn panic(id: ContractId, reason: InstructionResult, pc: Word, is: Word) -> Self {
Self::Panic { id, reason, pc, is }
Panic {
id,
reason,
pc,
is,
contract_id: None,
}
}

pub fn with_panic_contract_id(mut self, _contract_id: Option<ContractId>) -> Self {
if let Receipt::Panic {
ref mut contract_id,
..
} = self
{
*contract_id = _contract_id;
}
self
}

pub const fn revert(id: ContractId, ra: Word, pc: Word, is: Word) -> Self {
Expand Down Expand Up @@ -577,69 +598,69 @@ impl Receipt {

fn variant_len_without_data(variant: ReceiptRepr) -> usize {
ContractId::LEN // id
+ WORD_SIZE // pc
+ WORD_SIZE // is
+ match variant {
+ WORD_SIZE // pc
+ WORD_SIZE // is
+ match variant {
ReceiptRepr::Call => {
ContractId::LEN // to
+ WORD_SIZE // amount
+ AssetId::LEN // asset_id
+ WORD_SIZE // gas
+ WORD_SIZE // param1
+ WORD_SIZE // param2
+ WORD_SIZE // amount
+ AssetId::LEN // asset_id
+ WORD_SIZE // gas
+ WORD_SIZE // param1
+ WORD_SIZE // param2
}

ReceiptRepr::Return => WORD_SIZE, // val

ReceiptRepr::ReturnData => {
WORD_SIZE // ptr
+ WORD_SIZE // len
+ Bytes32::LEN // digest
+ WORD_SIZE // len
+ Bytes32::LEN // digest
}

ReceiptRepr::Panic => WORD_SIZE, // reason
ReceiptRepr::Revert => WORD_SIZE, // ra

ReceiptRepr::Log => {
WORD_SIZE // ra
+ WORD_SIZE // rb
+ WORD_SIZE // rc
+ WORD_SIZE // rd
+ WORD_SIZE // rb
+ WORD_SIZE // rc
+ WORD_SIZE // rd
}

ReceiptRepr::LogData => {
WORD_SIZE // ra
+ WORD_SIZE // rb
+ WORD_SIZE // ptr
+ WORD_SIZE // len
+ Bytes32::LEN // digest
+ WORD_SIZE // rb
+ WORD_SIZE // ptr
+ WORD_SIZE // len
+ Bytes32::LEN // digest
}

ReceiptRepr::Transfer => {
ContractId::LEN // to
+ WORD_SIZE // amount
+ AssetId::LEN // digest
+ WORD_SIZE // amount
+ AssetId::LEN // digest
}

ReceiptRepr::TransferOut => {
Address::LEN // to
+ WORD_SIZE // amount
+ AssetId::LEN // digest
+ WORD_SIZE // amount
+ AssetId::LEN // digest
}

ReceiptRepr::ScriptResult => {
WORD_SIZE // status
+ WORD_SIZE // gas_used
+ WORD_SIZE // gas_used
}

ReceiptRepr::MessageOut => {
MessageId::LEN // message_id
+ Address::LEN // sender
+ Address::LEN // recipient
+ WORD_SIZE // amount
+ Bytes32::LEN // nonce
+ WORD_SIZE // len
+ Bytes32::LEN // digest
+ Address::LEN // sender
+ Address::LEN // recipient
+ WORD_SIZE // amount
+ Bytes32::LEN // nonce
+ WORD_SIZE // len
+ Bytes32::LEN // digest
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/receipt/receipt_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ impl io::Read for Receipt {
bytes::store_number_unchecked(buf, *is);
}

Self::Panic { id, reason, pc, is } => {
Self::Panic {
id, reason, pc, is, ..
} => {
let buf = bytes::store_number_unchecked(buf, ReceiptRepr::Panic as Word);

let buf = bytes::store_array_unchecked(buf, id);
Expand Down
6 changes: 5 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::consts::*;

use derivative::Derivative;
use fuel_asm::Opcode;
use fuel_crypto::PublicKey;
use fuel_types::bytes::{self, SizedBytes};
Expand Down Expand Up @@ -40,8 +41,9 @@ pub use validation::ValidationError;
/// Identification of transaction (also called transaction hash)
pub type TxId = Bytes32;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Derivative)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derivative(Eq, PartialEq, Hash)]
pub enum Transaction {
Script {
gas_price: Word,
Expand All @@ -53,6 +55,7 @@ pub enum Transaction {
inputs: Vec<Input>,
outputs: Vec<Output>,
witnesses: Vec<Witness>,
#[derivative(PartialEq = "ignore", Hash = "ignore")]
metadata: Option<Metadata>,
},

Expand All @@ -67,6 +70,7 @@ pub enum Transaction {
inputs: Vec<Input>,
outputs: Vec<Output>,
witnesses: Vec<Witness>,
#[derivative(PartialEq = "ignore", Hash = "ignore")]
metadata: Option<Metadata>,
},
}
Expand Down
10 changes: 10 additions & 0 deletions tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ fn receipt() {
rng.gen(),
rng.gen(),
),
Receipt::panic(
rng.gen(),
InstructionResult::error(
PanicReason::ContractNotInInputs,
Opcode::JI(rng.gen::<Immediate24>() & 0xffffff).into(),
),
rng.gen(),
rng.gen(),
)
.with_panic_contract_id(Some(rng.gen())),
Receipt::script_result(ScriptExecutionResult::Success, rng.gen()),
Receipt::script_result(ScriptExecutionResult::Panic, rng.gen()),
Receipt::script_result(ScriptExecutionResult::Revert, rng.gen()),
Expand Down

0 comments on commit ae3e5e4

Please sign in to comment.