-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This struct should emulate a functional client and take iterators of `Transaction`s to mutate a storage. This implementation should define the commit/revert/persist/rollback strategy depending on the receipts returned by the VM.
- Loading branch information
Showing
18 changed files
with
180 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::error::InterpreterError; | ||
use crate::interpreter::Interpreter; | ||
|
||
use fuel_tx::{Receipt, Transaction}; | ||
|
||
mod storage; | ||
|
||
pub use storage::MemoryStorage; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct MemoryClient { | ||
storage: MemoryStorage, | ||
} | ||
|
||
impl From<MemoryStorage> for MemoryClient { | ||
fn from(s: MemoryStorage) -> Self { | ||
Self::new(s) | ||
} | ||
} | ||
|
||
impl AsRef<MemoryStorage> for MemoryClient { | ||
fn as_ref(&self) -> &MemoryStorage { | ||
&self.storage | ||
} | ||
} | ||
|
||
impl AsMut<MemoryStorage> for MemoryClient { | ||
fn as_mut(&mut self) -> &mut MemoryStorage { | ||
&mut self.storage | ||
} | ||
} | ||
|
||
impl MemoryClient { | ||
pub const fn new(storage: MemoryStorage) -> Self { | ||
Self { storage } | ||
} | ||
|
||
pub fn transition<I: IntoIterator<Item = Transaction>>( | ||
&mut self, | ||
txs: I, | ||
) -> Result<Vec<Receipt>, InterpreterError> { | ||
let mut interpreter = Interpreter::with_storage(&mut self.storage); | ||
|
||
// A transaction that is considered valid will be reverted if the runtime | ||
// returns an error. | ||
// | ||
// The exception is `InterpreterError::ValidationError` since this means the | ||
// whole set of transactions must also fail. | ||
// | ||
// All the other variants of `InterpreterError` are supressed in this function | ||
// and they will produce isolated `RVRT` operations. | ||
let receipts = txs.into_iter().try_fold(vec![], |mut receipts, tx| { | ||
match interpreter.transact(tx) { | ||
Ok(state) => { | ||
receipts.extend(state.receipts()); | ||
|
||
if !state.receipts().iter().any(|r| matches!(r, Receipt::Revert { .. })) { | ||
interpreter.as_mut().commit(); | ||
} else { | ||
interpreter.as_mut().revert(); | ||
} | ||
|
||
Ok(receipts) | ||
} | ||
|
||
Err(InterpreterError::ValidationError(e)) => { | ||
interpreter.as_mut().rollback(); | ||
|
||
Err(InterpreterError::ValidationError(e)) | ||
} | ||
|
||
// TODO VM is to return a `RVRT` receipt on runtime error. This way, the return of | ||
// `transact` should be `Err` only if `InterpreterError::ValidationError` happens | ||
Err(_) => { | ||
interpreter.as_mut().revert(); | ||
|
||
Ok(receipts) | ||
} | ||
} | ||
})?; | ||
|
||
self.storage.persist(); | ||
|
||
Ok(receipts) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.