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

Refactor interpreter instructions to return Result #26

Merged
merged 1 commit into from
Oct 6, 2021
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
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum InterpreterError {
InputNotFound,
OutputNotFound,
WitnessNotFound,
TxMaturity,

#[cfg(feature = "debug")]
DebugStateNotInitialized,
Expand Down
30 changes: 22 additions & 8 deletions src/interpreter/alu.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use super::Interpreter;
use crate::consts::*;
use crate::error::InterpreterError;

use fuel_types::{RegisterId, Word};

impl<S> Interpreter<S> {
pub(crate) fn alu_overflow<B, C>(&mut self, ra: RegisterId, f: fn(B, C) -> (Word, bool), b: B, c: C) {
pub(crate) fn alu_overflow<B, C>(
&mut self,
ra: RegisterId,
f: fn(B, C) -> (Word, bool),
b: B,
c: C,
) -> Result<(), InterpreterError> {
let (result, overflow) = f(b, c);

// TODO If the F_UNSAFEMATH flag is unset, an operation that would have set $err
Expand All @@ -18,31 +25,38 @@ impl<S> Interpreter<S> {

self.registers[ra] = result;

self.inc_pc();
self.inc_pc()
}

pub(crate) fn alu_error<B, C>(&mut self, ra: RegisterId, f: fn(B, C) -> Word, b: B, c: C, err: bool) {
pub(crate) fn alu_error<B, C>(
&mut self,
ra: RegisterId,
f: fn(B, C) -> Word,
b: B,
c: C,
err: bool,
) -> Result<(), InterpreterError> {
self.registers[REG_OF] = 0;
self.registers[REG_ERR] = err as Word;

self.registers[ra] = if err { 0 } else { f(b, c) };

self.inc_pc();
self.inc_pc()
}

pub(crate) fn alu_set(&mut self, ra: RegisterId, b: Word) {
pub(crate) fn alu_set(&mut self, ra: RegisterId, b: Word) -> Result<(), InterpreterError> {
self.registers[REG_OF] = 0;
self.registers[REG_ERR] = 0;

self.registers[ra] = b;

self.inc_pc();
self.inc_pc()
}

pub(crate) fn alu_clear(&mut self) {
pub(crate) fn alu_clear(&mut self) -> Result<(), InterpreterError> {
self.registers[REG_OF] = 0;
self.registers[REG_ERR] = 0;

self.inc_pc();
self.inc_pc()
}
}
44 changes: 11 additions & 33 deletions src/interpreter/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ where
self.storage
.merkle_contract_color_balance_insert(contract, color, balance)?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn mint(&mut self, a: Word) -> Result<(), InterpreterError> {
Expand All @@ -49,9 +47,7 @@ where
self.storage
.merkle_contract_color_balance_insert(contract, color, balance)?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn code_copy(&mut self, a: Word, b: Word, c: Word, d: Word) -> Result<(), InterpreterError> {
Expand Down Expand Up @@ -88,28 +84,22 @@ where
self.try_mem_write(a, &contract.as_ref()[c..cd])?;
}

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn block_hash(&mut self, a: Word, b: Word) -> Result<(), InterpreterError> {
let hash = self.storage.block_hash(b as u32).map_err(|e| e.into())?;

self.try_mem_write(a as usize, hash.as_ref())?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn block_proposer(&mut self, a: Word) -> Result<(), InterpreterError> {
self.coinbase()
.and_then(|data| self.try_mem_write(a as usize, data.as_ref()))?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn code_root(&mut self, a: Word, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -131,9 +121,7 @@ where

self.try_mem_write(a, root.as_ref())?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn code_size(&mut self, ra: RegisterId, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -148,9 +136,7 @@ where

self.registers[ra] = self.contract(contract_id)?.as_ref().as_ref().len() as Word;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn state_read_word(&mut self, ra: RegisterId, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -172,9 +158,7 @@ where
.map(Word::from_be_bytes)
.unwrap_or(0);

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn state_read_qword(&mut self, a: Word, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -197,9 +181,7 @@ where

self.try_mem_write(a, state.as_ref())?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn state_write_word(&mut self, a: Word, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -220,9 +202,7 @@ where

self.storage.merkle_contract_state_insert(contract, key, &value)?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn state_write_qword(&mut self, a: Word, b: Word) -> Result<(), InterpreterError> {
Expand All @@ -240,8 +220,6 @@ where

self.storage.merkle_contract_state_insert(contract, key, value)?;

self.inc_pc();

Ok(())
self.inc_pc()
}
}
12 changes: 3 additions & 9 deletions src/interpreter/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ impl<S> Interpreter<S> {
}
}

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn keccak256(&mut self, a: Word, b: Word, c: Word) -> Result<(), InterpreterError> {
Expand All @@ -56,9 +54,7 @@ impl<S> Interpreter<S> {

self.try_mem_write(a, h.finalize().as_slice())?;

self.inc_pc();

Ok(())
self.inc_pc()
}

pub(crate) fn sha256(&mut self, a: Word, b: Word, c: Word) -> Result<(), InterpreterError> {
Expand All @@ -71,8 +67,6 @@ impl<S> Interpreter<S> {

self.try_mem_write(a, Hasher::hash(&self.memory[b..bc]).as_ref())?;

self.inc_pc();

Ok(())
self.inc_pc()
}
}
Loading