Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Use PanicReason from fuel_asm
Browse files Browse the repository at this point in the history
  • Loading branch information
vlopes11 committed Dec 1, 2021
1 parent cae1912 commit 8aab702
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 155 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https:/FuelLabs/fuel-tx"
description = "FuelVM transaction."

[dependencies]
fuel-asm = { git = "ssh://[email protected]/FuelLabs/fuel-asm.git", default-features = false }
fuel-asm = { git = "ssh://[email protected]/FuelLabs/fuel-asm.git", default-features = false, branch = "vlopes11/panic-reason" }
fuel-types = { git = "ssh://[email protected]/FuelLabs/fuel-types.git", default-features = false }
itertools = { version = "0.10", optional = true }
rand = { version = "0.8", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

pub mod consts;

pub use fuel_asm::PanicReason;
pub use fuel_types::{Address, Bytes32, Bytes4, Bytes64, Bytes8, Color, ContractId, Salt};

#[cfg(feature = "std")]
Expand All @@ -24,4 +25,4 @@ mod receipt;
pub use transaction::{Input, Metadata, Output, Transaction, ValidationError, Witness};

#[cfg(feature = "std")]
pub use receipt::{PanicReason, Receipt, ScriptResult};
pub use receipt::{Receipt, ScriptResult};
4 changes: 2 additions & 2 deletions src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::convert::TryFrom;
use std::io::{self, Write};
use std::mem;

mod panic_reason;
mod receipt_repr;
mod script_result;

use receipt_repr::ReceiptRepr;

pub use panic_reason::{PanicReason, ScriptResult};
pub use script_result::ScriptResult;

const WORD_SIZE: usize = mem::size_of::<Word>();

Expand Down
151 changes: 0 additions & 151 deletions src/receipt/panic_reason.rs

This file was deleted.

69 changes: 69 additions & 0 deletions src/receipt/script_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use fuel_asm::{Instruction, Opcode, PanicReason};
use fuel_types::Word;

use std::mem;

const WORD_SIZE: usize = mem::size_of::<Word>();

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde-types", derive(serde::Serialize, serde::Deserialize))]
pub struct ScriptResult {
result: PanicReason,
instruction: Instruction,
}

impl ScriptResult {
pub const fn new(result: PanicReason, instruction: Instruction) -> Self {
Self {
result,
instruction,
}
}

pub const fn result(&self) -> &PanicReason {
&self.result
}

pub const fn instruction(&self) -> &Instruction {
&self.instruction
}
}

const RESULT_OFFSET: Word = (WORD_SIZE * 8 - 8) as Word;
const INSTR_OFFSET: Word = ((WORD_SIZE - mem::size_of::<u32>()) * 8 - 8) as Word;

impl From<ScriptResult> for Word {
fn from(r: ScriptResult) -> Word {
let result = Word::from(r.result);
let instruction = u32::from(r.instruction) as Word;

(result << RESULT_OFFSET) | (instruction << INSTR_OFFSET)
}
}

impl From<Word> for ScriptResult {
fn from(val: Word) -> Self {
let result = PanicReason::from(val >> RESULT_OFFSET);
let instruction = Instruction::from((val >> INSTR_OFFSET) as u32);

Self::new(result, instruction)
}
}

impl From<ScriptResult> for Instruction {
fn from(r: ScriptResult) -> Self {
r.instruction
}
}

impl From<ScriptResult> for Opcode {
fn from(r: ScriptResult) -> Self {
r.instruction.into()
}
}

impl From<ScriptResult> for PanicReason {
fn from(r: ScriptResult) -> Self {
r.result
}
}

0 comments on commit 8aab702

Please sign in to comment.