Skip to content

Commit e010b0d

Browse files
committed
implement new methods for StackState trait of SubstrateStackState type
1 parent 8f7320b commit e010b0d

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

frame/evm/src/runner/stack.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ where
623623
struct SubstrateStackSubstate<'config> {
624624
metadata: StackSubstateMetadata<'config>,
625625
deletes: BTreeSet<H160>,
626+
creates: BTreeSet<H160>,
627+
/// Account transient storage (discarded after every transaction. (see EIP-1153))
628+
transient_storage: BTreeMap<(H160, H256), H256>,
626629
logs: Vec<Log>,
627630
parent: Option<Box<SubstrateStackSubstate<'config>>>,
628631
}
@@ -641,6 +644,8 @@ impl<'config> SubstrateStackSubstate<'config> {
641644
metadata: self.metadata.spit_child(gas_limit, is_static),
642645
parent: None,
643646
deletes: BTreeSet::new(),
647+
creates: BTreeSet::new(),
648+
transient_storage: BTreeMap::new(),
644649
logs: Vec::new(),
645650
};
646651
mem::swap(&mut entering, self);
@@ -657,6 +662,7 @@ impl<'config> SubstrateStackSubstate<'config> {
657662
self.metadata.swallow_commit(exited.metadata)?;
658663
self.logs.append(&mut exited.logs);
659664
self.deletes.append(&mut exited.deletes);
665+
self.transient_storage.append(&mut exited.transient_storage);
660666

661667
sp_io::storage::commit_transaction();
662668
Ok(())
@@ -715,6 +721,36 @@ impl<'config> SubstrateStackSubstate<'config> {
715721
.unwrap_or(true)
716722
}
717723
}
724+
725+
pub fn known_transient_storage(&self, address: H160, key: H256) -> Option<H256> {
726+
if let Some(value) = self.transient_storage.get(&(address, key)) {
727+
Some(*value)
728+
} else if let Some(parent) = self.parent.as_ref() {
729+
parent.known_transient_storage(address, key)
730+
} else {
731+
None
732+
}
733+
}
734+
735+
pub fn created(&self, address: H160) -> bool {
736+
if self.creates.contains(&address) {
737+
return true;
738+
}
739+
740+
if let Some(parent) = self.parent.as_ref() {
741+
return parent.created(address);
742+
}
743+
744+
false
745+
}
746+
747+
pub fn set_transient_storage(&mut self, address: H160, key: H256, value: H256) {
748+
self.transient_storage.insert((address, key), value);
749+
}
750+
751+
pub fn set_created(&mut self, address: H160) {
752+
self.creates.insert(address);
753+
}
718754
}
719755

720756
#[derive(Default, Clone, Eq, PartialEq)]
@@ -748,6 +784,8 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> {
748784
substate: SubstrateStackSubstate {
749785
metadata,
750786
deletes: BTreeSet::new(),
787+
creates: BTreeSet::new(),
788+
transient_storage: BTreeMap::new(),
751789
logs: Vec::new(),
752790
parent: None,
753791
},
@@ -856,10 +894,15 @@ where
856894
)
857895
}
858896

859-
/// FIXME: DO NOT call it for now! The dependent [evm::backend::Backend] trait has new methods added in this version,
860-
/// see this [commit](https://github.com/rust-ethereum/evm/commit/cfb378dac0640a6ac78687dd734241760f6ce125).
861-
fn transient_storage(&self, _address: H160, _index: H256) -> H256 {
862-
todo!()
897+
fn transient_storage(&self, address: H160, index: H256) -> H256 {
898+
self.substate
899+
.known_transient_storage(address, index)
900+
.unwrap_or_else(|| {
901+
self.original_storage
902+
.get(&(address, index))
903+
.cloned()
904+
.unwrap_or_else(|| self.storage(address, index))
905+
})
863906
}
864907
}
865908

@@ -1251,22 +1294,16 @@ where
12511294
}
12521295
}
12531296

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

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

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

0 commit comments

Comments
 (0)