Skip to content

continue-on-error foundations: refactor verification to a separate trait #918

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

Merged
merged 25 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58f6352
Do initial implementation work
Dentosal Feb 18, 2025
ea13a69
Merge branch 'master' into dento/dry-run-collect-errors
Dentosal Feb 21, 2025
5fe2201
Handle missing input contract case only
Dentosal Feb 25, 2025
81f7437
Merge branch 'master' into dento/dry-run-collect-errors
Dentosal Feb 25, 2025
e64efc7
fmt
Dentosal Feb 25, 2025
598261c
Add changelog entry
Dentosal Feb 25, 2025
1c8586e
Refactor the verifier to perform the contract in inputs check
Dentosal Feb 25, 2025
60aef1b
Unify naming
Dentosal Feb 25, 2025
c51aa66
Remove unnecessary generics from Verifier
Dentosal Feb 25, 2025
9bcfbf1
Fix no_std build
Dentosal Feb 25, 2025
a23f5f9
Remove attempt feature
Dentosal Feb 25, 2025
c6707ed
Rename Panic verification to Normal
Dentosal Feb 25, 2025
6be06f5
Update changelog
Dentosal Feb 25, 2025
cd5bb81
Doc improvements
Dentosal Feb 25, 2025
5b395dd
Remove unnecessary type annotation
Dentosal Feb 25, 2025
9cc0677
Remove useless type param S
Dentosal Feb 25, 2025
639ae6f
More cleanup
Dentosal Feb 25, 2025
e832a99
Update fuel-vm/src/interpreter/blockchain.rs
Dentosal Feb 26, 2025
e7c6557
Update fuel-vm/src/interpreter/contract.rs
Dentosal Feb 26, 2025
cd49c99
Update fuel-vm/src/interpreter/contract.rs
Dentosal Feb 26, 2025
68da246
Update fuel-vm/src/interpreter/contract.rs
Dentosal Feb 26, 2025
69c5c2b
Update fuel-vm/src/verification.rs
Dentosal Feb 26, 2025
6d341bb
Update fuel-vm/src/interpreter/flow.rs
Dentosal Feb 26, 2025
cfa7506
Remove unused imports
Dentosal Feb 26, 2025
7b0040f
no_std import fixes
Dentosal Feb 26, 2025
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [900](https://github.com/FuelLabs/fuel-vm/pull/900): Change the error variant `DuplicateMessageInputId` to `DuplicateInputNonce` which now contains a nonce instead of `MessageId` for performance improvements.
- [907](https://github.com/FuelLabs/fuel-vm/pull/907): `StorageRead` and `StorageWrite` traits no longer return the number of bytes written. They already required that the whole buffer is used, but now this is reflected in signature and documentation as well.
- [914](https://github.com/FuelLabs/fuel-vm/pull/914): The built-in profiler is removed. Use the debugger with single-stepping instead.
- [918](https://github.com/FuelLabs/fuel-vm/pull/918): Interpreter is now generic over the validation error handling. No behavioural changes, but the `Interpreter` has one extra type parameter.

### Changed
- [904](https://github.com/FuelLabs/fuel-vm/pull/904): Moved the logic of each opcode into its own function. It helps so reduce the size of the `instruction_inner` function, allowing compiler to do better optimizations. The Mira swaps receive performance improvement in 16.5%.
Expand Down
12 changes: 6 additions & 6 deletions fuel-vm/examples/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ use fuel_vm::{
pub struct FileReadEcal;

impl EcalHandler for FileReadEcal {
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
fn ecal<M, S, Tx, V>(
vm: &mut Interpreter<M, S, Tx, Self, V>,
a: RegId,
b: RegId,
c: RegId,
Expand Down Expand Up @@ -130,8 +130,8 @@ pub struct CounterEcal {
}

impl EcalHandler for CounterEcal {
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
fn ecal<M, S, Tx, V>(
vm: &mut Interpreter<M, S, Tx, Self, V>,
a: RegId,
_b: RegId,
_c: RegId,
Expand Down Expand Up @@ -193,8 +193,8 @@ pub struct SharedCounterEcal {
}

impl EcalHandler for SharedCounterEcal {
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
fn ecal<M, S, Tx, V>(
vm: &mut Interpreter<M, S, Tx, Self, V>,
a: RegId,
_b: RegId,
_c: RegId,
Expand Down
4 changes: 2 additions & 2 deletions fuel-vm/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ 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<M, S, Tx, Ecal>(
vm: &Interpreter<M, S, Tx, Ecal>,
pub fn from_vm_error<M, S, Tx, Ecal, V>(
vm: &Interpreter<M, S, Tx, Ecal, V>,
result: ScriptExecutionResult,
) -> Self
where
Expand Down
55 changes: 19 additions & 36 deletions fuel-vm/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ use crate::{
context::Context,
error::SimpleResult,
state::Debugger,
verification,
};
use alloc::{
collections::BTreeSet,
vec::Vec,
};
use alloc::vec::Vec;
use core::{
mem,
ops::Index,
Expand Down Expand Up @@ -106,21 +110,21 @@ pub struct NotSupportedEcal;

/// VM interpreter.
///
/// The internal state of the VM isn't expose because the intended usage is to
/// The internal state of the VM isn't exposed because the intended usage is to
/// either inspect the resulting receipts after a transaction execution, or the
/// resulting mutated transaction.
///
/// These can be obtained with the help of a [`crate::transactor::Transactor`]
/// or a client implementation.
#[derive(Debug, Clone)]
pub struct Interpreter<M, S, Tx = (), Ecal = NotSupportedEcal> {
pub struct Interpreter<M, S, Tx = (), Ecal = NotSupportedEcal, V = verification::Normal> {
registers: [Word; VM_REGISTER_COUNT],
memory: M,
frames: Vec<CallFrame>,
receipts: ReceiptsCtx,
tx: Tx,
initial_balances: InitialBalances,
input_contracts: alloc::collections::BTreeSet<ContractId>,
input_contracts: BTreeSet<ContractId>,
input_contracts_index_to_output_index: alloc::collections::BTreeMap<u16, u16>,
storage: S,
debugger: Debugger,
Expand All @@ -131,6 +135,7 @@ pub struct Interpreter<M, S, Tx = (), Ecal = NotSupportedEcal> {
/// `append_panic_receipt` and is `PanicContext::None` after consumption.
panic_context: PanicContext,
ecal_state: Ecal,
verifier: V,
}

/// Interpreter parameters
Expand Down Expand Up @@ -204,21 +209,21 @@ pub(crate) enum PanicContext {
ContractId(ContractId),
}

impl<M: Memory, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
impl<M: Memory, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V> {
/// Returns the current state of the VM memory
pub fn memory(&self) -> &MemoryInstance {
self.memory.as_ref()
}
}

impl<M: AsMut<MemoryInstance>, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
impl<M: AsMut<MemoryInstance>, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V> {
/// Returns mutable access to the vm memory
pub fn memory_mut(&mut self) -> &mut MemoryInstance {
self.memory.as_mut()
}
}

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V> {
/// Returns the current state of the registers
pub const fn registers(&self) -> &[Word] {
&self.registers
Expand Down Expand Up @@ -314,6 +319,11 @@ impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal> {
pub fn receipts_mut(&mut self) -> &mut ReceiptsCtx {
&mut self.receipts
}

/// Get verifier state. Note that the default verifier has no state.
pub fn verifier(&self) -> &V {
&self.verifier
}
}

pub(crate) fn flags(flag: Reg<FLAG>) -> Flags {
Expand All @@ -328,13 +338,13 @@ pub(crate) fn is_unsafe_math(flag: Reg<FLAG>) -> bool {
flags(flag).contains(Flags::UNSAFEMATH)
}

impl<M, S, Tx, Ecal> AsRef<S> for Interpreter<M, S, Tx, Ecal> {
impl<M, S, Tx, Ecal, V> AsRef<S> for Interpreter<M, S, Tx, Ecal, V> {
fn as_ref(&self) -> &S {
&self.storage
}
}

impl<M, S, Tx, Ecal> AsMut<S> for Interpreter<M, S, Tx, Ecal> {
impl<M, S, Tx, Ecal, V> AsMut<S> for Interpreter<M, S, Tx, Ecal, V> {
fn as_mut(&mut self) -> &mut S {
&mut self.storage
}
Expand Down Expand Up @@ -675,30 +685,3 @@ impl CheckedMetadata for BlobCheckedMetadata {
}
}
}

pub(crate) struct InputContracts<'vm> {
input_contracts: &'vm alloc::collections::BTreeSet<ContractId>,
panic_context: &'vm mut PanicContext,
}

impl<'vm> InputContracts<'vm> {
pub fn new(
input_contracts: &'vm alloc::collections::BTreeSet<ContractId>,
panic_context: &'vm mut PanicContext,
) -> Self {
Self {
input_contracts,
panic_context,
}
}

/// Checks that the contract is declared in the transaction inputs.
pub fn check(&mut self, contract: &ContractId) -> SimpleResult<()> {
if !self.input_contracts.contains(contract) {
*self.panic_context = PanicContext::ContractId(*contract);
Err(PanicReason::ContractNotInInputs.into())
} else {
Ok(())
}
}
}
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use fuel_types::Word;
mod muldiv;
mod wideint;

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
where
Tx: ExecutableTransaction,
{
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/alu/muldiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use fuel_asm::{
};
use fuel_types::Word;

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
where
Tx: ExecutableTransaction,
{
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/alu/wideint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! wideint_ops {
$t::from_le_bytes(truncated)
}

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
where
M: Memory,
Tx: ExecutableTransaction,
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl RuntimeBalances {

/// Write all assets into the start of VM stack, i.e. at $ssp.
/// Panics if the assets cannot fit.
pub fn to_vm<M, S, Tx, Ecal>(self, vm: &mut Interpreter<M, S, Tx, Ecal>)
pub fn to_vm<M, S, Tx, Ecal, V>(self, vm: &mut Interpreter<M, S, Tx, Ecal, V>)
where
M: Memory,
Tx: ExecutableTransaction,
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::{
WriteRegKey,
};

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
impl<M, S, Tx, Ecal, V> Interpreter<M, S, Tx, Ecal, V>
where
M: Memory,
S: InterpreterStorage,
Expand Down
Loading
Loading