Skip to content

Commit

Permalink
Remove finalize_w/_revert in favor of finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
vlopes11 committed Oct 7, 2021
1 parent eb0093f commit 059451e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
16 changes: 4 additions & 12 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ pub trait InterpreterStorage:
fn coinbase(&self) -> Result<Address, Self::DataError>;

/// Called when the execution of a transaction is finished
fn finalize(&mut self) -> Result<(), Self::DataError>;

/// Called when the execution of a transaction is finished with a revert
/// signal.
///
/// All the performed changes must be discarded.
fn finalize_with_revert(&mut self) -> Result<(), Self::DataError>;
/// `revert` flag will be set when the transaction finishes with `RVRT` code
fn finalize(&mut self, revert: bool) -> Result<(), Self::DataError>;

fn storage_contract(&self, id: &ContractId) -> Result<Option<Cow<'_, Contract>>, InterpreterError> {
<Self as Storage<ContractId, Contract>>::get(self, id).map_err(|e| e.into())
Expand Down Expand Up @@ -105,12 +101,8 @@ where
{
type DataError = S::DataError;

fn finalize(&mut self) -> Result<(), Self::DataError> {
<S as InterpreterStorage>::finalize(self.deref_mut())
}

fn finalize_with_revert(&mut self) -> Result<(), Self::DataError> {
<S as InterpreterStorage>::finalize_with_revert(self.deref_mut())
fn finalize(&mut self, revert: bool) -> Result<(), Self::DataError> {
<S as InterpreterStorage>::finalize(self.deref_mut(), revert)
}

fn block_height(&self) -> Result<u32, Self::DataError> {
Expand Down
14 changes: 6 additions & 8 deletions src/data/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ impl MerkleStorage<ContractId, Bytes32, Bytes32> for MemoryStorage {
impl InterpreterStorage for MemoryStorage {
type DataError = Infallible;

fn finalize(&mut self) -> Result<(), Self::DataError> {
self.persisted = self.transacted.clone();

Ok(())
}

fn finalize_with_revert(&mut self) -> Result<(), Self::DataError> {
self.transacted = self.persisted.clone();
fn finalize(&mut self, revert: bool) -> Result<(), Self::DataError> {
if revert {
self.transacted = self.persisted.clone();
} else {
self.persisted = self.transacted.clone();
}

Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions src/interpreter/executors/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ where
}

match state {
ProgramState::Return(_) | ProgramState::ReturnData(_) => self.storage.finalize().map_err(|e| e.into())?,
ProgramState::Revert(_) => self.storage.finalize_with_revert().map_err(|e| e.into())?,
ProgramState::Return(_) | ProgramState::ReturnData(_) => {
self.storage.finalize(false).map_err(|e| e.into())?
}
ProgramState::Revert(_) => self.storage.finalize(true).map_err(|e| e.into())?,

#[cfg(feature = "debug")]
_ => (),
Expand Down

0 comments on commit 059451e

Please sign in to comment.