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

update vm to use newer ScriptExecutionResult #105

Merged
merged 2 commits into from
Apr 20, 2022
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ description = "FuelVM interpreter."
[dependencies]
dyn-clone = { version = "1.0", optional = true }
fuel-asm = "0.3"
fuel-crypto = "0.3"
fuel-crypto = "0.4"
fuel-merkle = "0.1"
fuel-storage = "0.1"
fuel-tx = "0.7"
fuel-tx = "0.8"
fuel-types = "0.3"
itertools = "0.10"
secp256k1 = { version = "0.20", features = ["recovery"] }
Expand All @@ -27,7 +27,7 @@ tracing = "0.1"
rand = { version = "0.8", optional = true }

[dev-dependencies]
fuel-tx = { version = "0.7", features = ["random"] }
fuel-tx = { version = "0.8", features = ["random"] }
fuel-vm = { path = ".", default-features = false, features = ["test-helpers"]}

[features]
Expand Down
13 changes: 6 additions & 7 deletions src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use crate::call::CallFrame;
use crate::consts::*;
use crate::interpreter::Interpreter;

use fuel_asm::InstructionResult;
use fuel_tx::Transaction;
use fuel_tx::{ScriptExecutionResult, Transaction};
use fuel_types::{ContractId, Word};

#[derive(Debug)]
Expand All @@ -17,15 +16,15 @@ pub struct Backtrace {
contract: ContractId,
registers: [Word; VM_REGISTER_COUNT],
memory: Vec<u8>,
result: InstructionResult,
result: ScriptExecutionResult,
tx: Transaction,
}

impl Backtrace {
/// Create a backtrace from a vm instance and instruction result.
///
/// This isn't copy-free and shouldn't be provided by default.
pub fn from_vm_error<S>(vm: &Interpreter<S>, result: InstructionResult) -> Self {
pub fn from_vm_error<S>(vm: &Interpreter<S>, result: ScriptExecutionResult) -> Self {
let call_stack = vm.call_stack().to_owned();
let contract = vm.internal_contract_or_default();
let memory = vm.memory().to_owned();
Expand Down Expand Up @@ -64,8 +63,8 @@ impl Backtrace {
self.memory.as_slice()
}

/// [`InstructionResult`] of the error that caused this backtrace.
pub const fn result(&self) -> &InstructionResult {
/// [`ScriptExecutionResult`] of the error that caused this backtrace.
pub const fn result(&self) -> &ScriptExecutionResult {
&self.result
}

Expand All @@ -82,7 +81,7 @@ impl Backtrace {
ContractId,
[Word; VM_REGISTER_COUNT],
Vec<u8>,
InstructionResult,
ScriptExecutionResult,
Transaction,
) {
let Self {
Expand Down
16 changes: 12 additions & 4 deletions src/interpreter/executors/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::prelude::*;
use crate::state::{ExecuteState, ProgramState, StateTransitionRef};
use crate::storage::InterpreterStorage;

use fuel_asm::{InstructionResult, PanicReason};
use fuel_tx::{Input, Output, Receipt, Transaction};
use fuel_asm::PanicReason;
use fuel_tx::{Input, Output, Receipt, ScriptExecutionResult, Transaction};
use fuel_types::{bytes::SerializableVec, Word};

impl<S> Interpreter<S>
Expand Down Expand Up @@ -116,14 +116,22 @@ where

// Catch VM panic and don't propagate, generating a receipt
let (status, program) = match program {
Ok(s) => (InstructionResult::success(), s),
Ok(s) => {
// either a revert or success
let res = if let ProgramState::Revert(_) = &s {
ScriptExecutionResult::Revert
} else {
ScriptExecutionResult::Success
};
(res, s)
}

Err(e) => match e.instruction_result() {
Some(result) => {
self.append_panic_receipt(*result);
self.apply_revert();

(*result, ProgramState::Revert(0))
(ScriptExecutionResult::Panic, ProgramState::Revert(0))
}

// This isn't a specified case of an erroneous program and should be
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ where
let pc = self.registers[REG_PC];
let is = self.registers[REG_IS];

let receipt = Receipt::panic(self.internal_contract_or_default(), Word::from(result), pc, is);
let receipt = Receipt::panic(self.internal_contract_or_default(), result, pc, is);

self.receipts.push(receipt);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ fn alu_err(registers_init: &[(RegisterId, Immediate18)], op: Opcode) {

let result = receipts
.iter()
.find_map(Receipt::result)
.find_map(Receipt::reason)
.map(|r| *r.reason())
.expect("Expected script result");
.expect("Expected panic reason");

assert_eq!(PanicReason::ReservedRegisterNotWritable, result);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/code_coverage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fuel_tx::ScriptExecutionResult;
use std::sync::{Arc, Mutex};

use fuel_vm::consts::*;
Expand Down Expand Up @@ -59,7 +60,7 @@ fn code_coverage() {
let receipts = client.transact(tx_script);

if let Some(Receipt::ScriptResult { result, .. }) = receipts.last() {
assert!(result.is_success());
assert!(matches!(result, ScriptExecutionResult::Success));
} else {
panic!("Missing result receipt");
}
Expand Down
17 changes: 13 additions & 4 deletions tests/profile_gas.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fuel_tx::ScriptExecutionResult;
use std::sync::{Arc, Mutex};

use fuel_vm::consts::*;
Expand Down Expand Up @@ -62,14 +63,22 @@ fn profile_gas() {

if let Some(Receipt::ScriptResult { result, .. }) = receipts.last() {
if count == case_out_of_gas {
assert!(!result.is_success(), "Expected out-of-gas error, got success");
assert!(
matches!(result.reason(), PanicReason::OutOfGas),
!matches!(result, &ScriptExecutionResult::Success),
"Expected out-of-gas error, got success"
);
let panic_reason = receipts
.iter()
.find_map(Receipt::reason)
.map(|r| *r.reason())
.expect("Expected a panic reason.");
assert!(
matches!(panic_reason, PanicReason::OutOfGas),
"Expected out-of-gas error, got {:?}",
result.reason()
panic_reason
);
} else {
assert!(result.is_success());
matches!(result, &ScriptExecutionResult::Success);
}
} else {
panic!("Missing result receipt");
Expand Down