Skip to content

Commit

Permalink
Receipt Error Improvements (#99)
Browse files Browse the repository at this point in the history
* make panic receipt more transparent, make script result handle reverts
  • Loading branch information
Voxelot authored and xgreenx committed Dec 20, 2022
1 parent f1dc6a9 commit 389f419
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 40 deletions.
2 changes: 1 addition & 1 deletion fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod transaction;
pub use builder::TransactionBuilder;

#[cfg(feature = "alloc")]
pub use receipt::Receipt;
pub use receipt::{Receipt, ScriptExecutionResult};

#[cfg(feature = "alloc")]
pub use transaction::{
Expand Down
15 changes: 9 additions & 6 deletions fuel-tx/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ use alloc::vec::Vec;
mod receipt_std;

mod receipt_repr;
mod script_result;

use receipt_repr::ReceiptRepr;

pub use script_result::ScriptExecutionResult;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde-types-minimal",
Expand Down Expand Up @@ -48,7 +51,7 @@ pub enum Receipt {

Panic {
id: ContractId,
reason: Word,
reason: InstructionResult,
pc: Word,
is: Word,
},
Expand Down Expand Up @@ -101,7 +104,7 @@ pub enum Receipt {
},

ScriptResult {
result: InstructionResult,
result: ScriptExecutionResult,
gas_used: Word,
},
}
Expand Down Expand Up @@ -156,7 +159,7 @@ impl Receipt {
}
}

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

Expand Down Expand Up @@ -244,7 +247,7 @@ impl Receipt {
}
}

pub const fn script_result(result: InstructionResult, gas_used: Word) -> Self {
pub const fn script_result(result: ScriptExecutionResult, gas_used: Word) -> Self {
Self::ScriptResult { result, gas_used }
}

Expand Down Expand Up @@ -394,7 +397,7 @@ impl Receipt {
}
}

pub const fn reason(&self) -> Option<Word> {
pub const fn reason(&self) -> Option<InstructionResult> {
match self {
Self::Panic { reason, .. } => Some(*reason),
_ => None,
Expand Down Expand Up @@ -432,7 +435,7 @@ impl Receipt {
}
}

pub const fn result(&self) -> Option<&InstructionResult> {
pub const fn result(&self) -> Option<&ScriptExecutionResult> {
match self {
Self::ScriptResult { result, .. } => Some(result),
_ => None,
Expand Down
5 changes: 3 additions & 2 deletions fuel-tx/src/receipt/receipt_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fuel_asm::InstructionResult;
use fuel_types::bytes::{self, SizedBytes, WORD_SIZE};
use fuel_types::Word;

use crate::receipt::script_result::ScriptExecutionResult;
use std::io::{self, Write};

impl io::Read for Receipt {
Expand Down Expand Up @@ -258,7 +259,7 @@ impl io::Write for Receipt {

let id = id.into();

*self = Self::panic(id, reason, pc, is);
*self = Self::panic(id, InstructionResult::from(reason), pc, is);
}

ReceiptRepr::Revert => {
Expand Down Expand Up @@ -344,7 +345,7 @@ impl io::Write for Receipt {
let (result, buf) = unsafe { bytes::restore_word_unchecked(buf) };
let (gas_used, _) = unsafe { bytes::restore_word_unchecked(buf) };

let result = InstructionResult::from(result);
let result = ScriptExecutionResult::from(result);

*self = Self::script_result(result, gas_used);
}
Expand Down
36 changes: 36 additions & 0 deletions fuel-tx/src/receipt/script_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use fuel_types::Word;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde-types-minimal",
derive(serde::Serialize, serde::Deserialize)
)]
pub enum ScriptExecutionResult {
Success,
Revert,
Panic,
// Generic failure case since any u64 is valid here
GenericFailure(u64),
}

impl From<ScriptExecutionResult> for Word {
fn from(result: ScriptExecutionResult) -> Self {
match result {
ScriptExecutionResult::Success => 0x00,
ScriptExecutionResult::Revert => 0x01,
ScriptExecutionResult::Panic => 0x02,
ScriptExecutionResult::GenericFailure(value) => value,
}
}
}

impl From<Word> for ScriptExecutionResult {
fn from(value: u64) -> Self {
match value {
0x00 => Self::Success,
0x01 => Self::Revert,
0x02 => Self::Panic,
value => Self::GenericFailure(value),
}
}
}
Loading

0 comments on commit 389f419

Please sign in to comment.