Skip to content

Commit

Permalink
implement new methods for StackState trait of SubstrateStackState
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
0xbillw committed Jan 21, 2025
1 parent 8f7320b commit e010b0d
Showing 1 changed file with 53 additions and 16 deletions.
69 changes: 53 additions & 16 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@ where
struct SubstrateStackSubstate<'config> {
metadata: StackSubstateMetadata<'config>,
deletes: BTreeSet<H160>,
creates: BTreeSet<H160>,
/// Account transient storage (discarded after every transaction. (see EIP-1153))
transient_storage: BTreeMap<(H160, H256), H256>,
logs: Vec<Log>,
parent: Option<Box<SubstrateStackSubstate<'config>>>,
}
Expand All @@ -641,6 +644,8 @@ impl<'config> SubstrateStackSubstate<'config> {
metadata: self.metadata.spit_child(gas_limit, is_static),
parent: None,
deletes: BTreeSet::new(),
creates: BTreeSet::new(),
transient_storage: BTreeMap::new(),
logs: Vec::new(),
};
mem::swap(&mut entering, self);
Expand All @@ -657,6 +662,7 @@ impl<'config> SubstrateStackSubstate<'config> {
self.metadata.swallow_commit(exited.metadata)?;
self.logs.append(&mut exited.logs);
self.deletes.append(&mut exited.deletes);
self.transient_storage.append(&mut exited.transient_storage);

sp_io::storage::commit_transaction();
Ok(())
Expand Down Expand Up @@ -715,6 +721,36 @@ impl<'config> SubstrateStackSubstate<'config> {
.unwrap_or(true)
}
}

pub fn known_transient_storage(&self, address: H160, key: H256) -> Option<H256> {
if let Some(value) = self.transient_storage.get(&(address, key)) {
Some(*value)
} else if let Some(parent) = self.parent.as_ref() {
parent.known_transient_storage(address, key)
} else {
None
}
}

pub fn created(&self, address: H160) -> bool {
if self.creates.contains(&address) {
return true;
}

if let Some(parent) = self.parent.as_ref() {
return parent.created(address);
}

false
}

pub fn set_transient_storage(&mut self, address: H160, key: H256, value: H256) {
self.transient_storage.insert((address, key), value);
}

pub fn set_created(&mut self, address: H160) {
self.creates.insert(address);
}
}

#[derive(Default, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -748,6 +784,8 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> {
substate: SubstrateStackSubstate {
metadata,
deletes: BTreeSet::new(),
creates: BTreeSet::new(),
transient_storage: BTreeMap::new(),
logs: Vec::new(),
parent: None,
},
Expand Down Expand Up @@ -856,10 +894,15 @@ where
)
}

/// FIXME: DO NOT call it for now! The dependent [evm::backend::Backend] trait has new methods added in this version,
/// see this [commit](https://github.com/rust-ethereum/evm/commit/cfb378dac0640a6ac78687dd734241760f6ce125).
fn transient_storage(&self, _address: H160, _index: H256) -> H256 {
todo!()
fn transient_storage(&self, address: H160, index: H256) -> H256 {
self.substate
.known_transient_storage(address, index)
.unwrap_or_else(|| {
self.original_storage
.get(&(address, index))
.cloned()
.unwrap_or_else(|| self.storage(address, index))
})
}
}

Expand Down Expand Up @@ -1251,22 +1294,16 @@ where
}
}

/// FIXME: DO NOT call it for now! The dependent [evm::executor::stack::executor::StackState] trait has new methods added in this version,
/// see this [commit](https://github.com/rust-ethereum/evm/commit/cfb378dac0640a6ac78687dd734241760f6ce125).
fn set_transient_storage(&mut self, _address: H160, _key: H256, _value: H256) {
todo!()
fn set_transient_storage(&mut self, address: H160, key: H256, value: H256) {
self.substate.set_transient_storage(address, key, value)
}

/// FIXME: DO NOT call it for now! The dependent [evm::executor::stack::executor::StackState] trait has new methods added in this version,
/// see this [commit](https://github.com/rust-ethereum/evm/commit/cfb378dac0640a6ac78687dd734241760f6ce125).
fn created(&self, _address: H160) -> bool {
todo!()
fn created(&self, address: H160) -> bool {
self.substate.created(address)
}

/// FIXME: DO NOT call it for now! The dependent [evm::executor::stack::executor::StackState] trait has new methods added in this version,
/// see this [commit](https://github.com/rust-ethereum/evm/commit/cfb378dac0640a6ac78687dd734241760f6ce125).
fn set_created(&mut self, _address: H160) {
todo!()
fn set_created(&mut self, address: H160) {
self.substate.set_created(address)
}
}

Expand Down

0 comments on commit e010b0d

Please sign in to comment.