From b4d83f8db2e4d331a0077aef993bbf85576fada8 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Wed, 3 Aug 2022 13:40:43 -0700 Subject: [PATCH] More metadata fields --- evm/src/cpu/kernel/context_metadata.rs | 20 +++++++++++++++++++- evm/src/cpu/kernel/global_metadata.rs | 10 ++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/evm/src/cpu/kernel/context_metadata.rs b/evm/src/cpu/kernel/context_metadata.rs index cf0bfc23..5b6ce303 100644 --- a/evm/src/cpu/kernel/context_metadata.rs +++ b/evm/src/cpu/kernel/context_metadata.rs @@ -7,10 +7,20 @@ pub(crate) enum ContextMetadata { ParentProgramCounter = 1, CalldataSize = 2, ReturndataSize = 3, + /// The address of the account associated with this context. + Address = 4, + /// The size of the code under the account associated with this context. + /// While this information could be obtained from the state trie, it is best to cache it since + /// the `CODESIZE` instruction is very cheap. + CodeSize = 5, + /// The address of the caller who spawned this context. + Caller = 6, + /// The value (in wei) deposited by the caller. + CallValue = 7, } impl ContextMetadata { - pub(crate) const COUNT: usize = 4; + pub(crate) const COUNT: usize = 8; pub(crate) fn all() -> [Self; Self::COUNT] { [ @@ -18,6 +28,10 @@ impl ContextMetadata { Self::ParentProgramCounter, Self::CalldataSize, Self::ReturndataSize, + Self::Address, + Self::CodeSize, + Self::Caller, + Self::CallValue, ] } @@ -28,6 +42,10 @@ impl ContextMetadata { ContextMetadata::ParentProgramCounter => "CTX_METADATA_PARENT_PC", ContextMetadata::CalldataSize => "CTX_METADATA_CALLDATA_SIZE", ContextMetadata::ReturndataSize => "CTX_METADATA_RETURNDATA_SIZE", + ContextMetadata::Address => "CTX_METADATA_ADDRESS", + ContextMetadata::CodeSize => "CTX_METADATA_CODE_SIZE", + ContextMetadata::Caller => "CTX_METADATA_CALLER", + ContextMetadata::CallValue => "CTX_METADATA_CALL_VALUE", } } } diff --git a/evm/src/cpu/kernel/global_metadata.rs b/evm/src/cpu/kernel/global_metadata.rs index 23c5a4c7..6343a2e6 100644 --- a/evm/src/cpu/kernel/global_metadata.rs +++ b/evm/src/cpu/kernel/global_metadata.rs @@ -5,19 +5,25 @@ pub(crate) enum GlobalMetadata { /// The largest context ID that has been used so far in this execution. Tracking this allows us /// give each new context a unique ID, so that its memory will be zero-initialized. LargestContext = 0, + /// The address of the sender of the transaction. + Origin = 1, + /// The size of active memory, in bytes. + MemorySize = 2, } impl GlobalMetadata { - pub(crate) const COUNT: usize = 1; + pub(crate) const COUNT: usize = 3; pub(crate) fn all() -> [Self; Self::COUNT] { - [Self::LargestContext] + [Self::LargestContext, Self::Origin, Self::MemorySize] } /// The variable name that gets passed into kernel assembly code. pub(crate) fn var_name(&self) -> &'static str { match self { GlobalMetadata::LargestContext => "GLOBAL_METADATA_LARGEST_CONTEXT", + GlobalMetadata::Origin => "GLOBAL_METADATA_ORIGIN", + GlobalMetadata::MemorySize => "GLOBAL_METADATA_MEMORY_SIZE", } } }