Compare commits

...

1 Commits

Author SHA1 Message Date
Omar Abdulla b8f63321e2 Implement storage empty assertion 2025-08-11 15:24:26 +03:00
7 changed files with 153 additions and 6 deletions
+8
View File
@@ -12,6 +12,14 @@
"address": "0xdeadbeef00000000000000000000000000000042", "address": "0xdeadbeef00000000000000000000000000000042",
"expected_balance": "1233" "expected_balance": "1233"
}, },
{
"address": "0xdeadbeef00000000000000000000000000000042",
"is_storage_empty": true
},
{
"address": "0xdeadbeef00000000000000000000000000000042",
"is_storage_empty": false
},
{ {
"instance": "WBTC_1", "instance": "WBTC_1",
"method": "#deployer", "method": "#deployer",
+85
View File
@@ -4,6 +4,7 @@ use std::collections::HashMap;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path::PathBuf; use std::path::PathBuf;
use alloy::consensus::EMPTY_ROOT_HASH;
use alloy::hex; use alloy::hex;
use alloy::json_abi::JsonAbi; use alloy::json_abi::JsonAbi;
use alloy::network::{Ethereum, TransactionBuilder}; use alloy::network::{Ethereum, TransactionBuilder};
@@ -28,6 +29,7 @@ use semver::Version;
use revive_dt_format::case::{Case, CaseIdx}; use revive_dt_format::case::{Case, CaseIdx};
use revive_dt_format::input::{ use revive_dt_format::input::{
BalanceAssertion, Calldata, EtherValue, Expected, ExpectedOutput, Input, Method, BalanceAssertion, Calldata, EtherValue, Expected, ExpectedOutput, Input, Method,
StorageEmptyAssertion,
}; };
use revive_dt_format::metadata::{ContractInstance, ContractPathAndIdent}; use revive_dt_format::metadata::{ContractInstance, ContractPathAndIdent};
use revive_dt_format::{input::Step, metadata::Metadata}; use revive_dt_format::{input::Step, metadata::Metadata};
@@ -90,6 +92,11 @@ where
.await?; .await?;
Ok(StepOutput::BalanceAssertion) Ok(StepOutput::BalanceAssertion)
} }
Step::StorageEmptyAssertion(storage_empty) => {
self.handle_storage_empty(metadata, case_idx, storage_empty, node)
.await?;
Ok(StepOutput::StorageEmptyAssertion)
}
} }
} }
@@ -125,7 +132,22 @@ where
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
self.handle_balance_assertion_contract_deployment(metadata, balance_assertion, node) self.handle_balance_assertion_contract_deployment(metadata, balance_assertion, node)
.await?; .await?;
self.handle_balance_assertion_execution(balance_assertion, node)
.await?;
Ok(())
}
pub async fn handle_storage_empty(
&mut self,
metadata: &Metadata,
_: CaseIdx,
storage_empty: &StorageEmptyAssertion,
node: &T::Blockchain,
) -> anyhow::Result<()> {
self.handle_storage_empty_assertion_contract_deployment(metadata, storage_empty, node)
.await?;
self.handle_storage_empty_assertion_execution(storage_empty, node)
.await?;
Ok(()) Ok(())
} }
@@ -557,6 +579,67 @@ where
Ok(()) Ok(())
} }
pub async fn handle_storage_empty_assertion_contract_deployment(
&mut self,
metadata: &Metadata,
storage_empty_assertion: &StorageEmptyAssertion,
node: &T::Blockchain,
) -> anyhow::Result<()> {
let Some(instance) = storage_empty_assertion
.address
.strip_prefix(".address")
.map(ContractInstance::new)
else {
return Ok(());
};
self.get_or_deploy_contract_instance(
&instance,
metadata,
Input::default_caller(),
None,
None,
node,
)
.await?;
Ok(())
}
pub async fn handle_storage_empty_assertion_execution(
&mut self,
StorageEmptyAssertion {
address: address_string,
is_storage_empty,
}: &StorageEmptyAssertion,
node: &T::Blockchain,
) -> anyhow::Result<()> {
let address = Address::from_slice(
Calldata::new_compound([address_string])
.calldata(node, self.default_resolution_context())
.await?
.get(12..32)
.expect("Can't fail"),
);
let storage = node.latest_state_proof(address, Default::default()).await?;
let is_empty = storage.storage_hash == EMPTY_ROOT_HASH;
let expected = is_storage_empty;
let actual = is_empty;
if *expected != actual {
tracing::error!(%expected, %actual, %address, "Storage Empty Assertion failed");
anyhow::bail!(
"Storage Empty Assertion failed - Expected {} but got {} for {} resolved to {}",
expected,
actual,
address_string,
address,
)
};
Ok(())
}
/// Gets the information of a deployed contract or library from the state. If it's found to not /// Gets the information of a deployed contract or library from the state. If it's found to not
/// be deployed then it will be deployed. /// be deployed then it will be deployed.
/// ///
@@ -780,6 +863,7 @@ where
} }
} }
(StepOutput::BalanceAssertion, StepOutput::BalanceAssertion) => {} (StepOutput::BalanceAssertion, StepOutput::BalanceAssertion) => {}
(StepOutput::StorageEmptyAssertion, StepOutput::StorageEmptyAssertion) => {}
_ => unreachable!("The two step outputs can not be of a different kind"), _ => unreachable!("The two step outputs can not be of a different kind"),
} }
@@ -795,4 +879,5 @@ where
pub enum StepOutput { pub enum StepOutput {
FunctionCall(TransactionReceipt, GethTrace, DiffMode), FunctionCall(TransactionReceipt, GethTrace, DiffMode),
BalanceAssertion, BalanceAssertion,
StorageEmptyAssertion,
} }
+1
View File
@@ -451,6 +451,7 @@ where
.filter_map(|step| match step { .filter_map(|step| match step {
Step::FunctionCall(input) => Some(input.caller), Step::FunctionCall(input) => Some(input.caller),
Step::BalanceAssertion(..) => None, Step::BalanceAssertion(..) => None,
Step::StorageEmptyAssertion(..) => None,
}) })
.next() .next()
.unwrap_or(Input::default_caller()); .unwrap_or(Input::default_caller());
+16
View File
@@ -28,6 +28,8 @@ pub enum Step {
FunctionCall(Box<Input>), FunctionCall(Box<Input>),
/// A step for performing a balance assertion on some account or contract. /// A step for performing a balance assertion on some account or contract.
BalanceAssertion(Box<BalanceAssertion>), BalanceAssertion(Box<BalanceAssertion>),
/// A step for asserting that the storage of some contract or account is empty.
StorageEmptyAssertion(Box<StorageEmptyAssertion>),
} }
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
@@ -60,6 +62,20 @@ pub struct BalanceAssertion {
pub expected_balance: U256, pub expected_balance: U256,
} }
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct StorageEmptyAssertion {
/// The address that the balance assertion should be done on.
///
/// This is a string which will be resolved into an address when being processed. Therefore,
/// this could be a normal hex address, a variable such as `Test.address`, or perhaps even a
/// full on variable like `$VARIABLE:Uniswap`. It follows the same resolution rules that are
/// followed in the calldata.
pub address: String,
/// A boolean of whether the storage of the address is empty or not.
pub is_storage_empty: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)] #[serde(untagged)]
pub enum Expected { pub enum Expected {
+9 -2
View File
@@ -1,8 +1,8 @@
//! This crate implements all node interactions. //! This crate implements all node interactions.
use alloy::primitives::{Address, U256}; use alloy::primitives::{Address, StorageKey, U256};
use alloy::rpc::types::trace::geth::{DiffMode, GethDebugTracingOptions, GethTrace}; use alloy::rpc::types::trace::geth::{DiffMode, GethDebugTracingOptions, GethTrace};
use alloy::rpc::types::{TransactionReceipt, TransactionRequest}; use alloy::rpc::types::{EIP1186AccountProofResponse, TransactionReceipt, TransactionRequest};
use anyhow::Result; use anyhow::Result;
/// An interface for all interactions with Ethereum compatible nodes. /// An interface for all interactions with Ethereum compatible nodes.
@@ -25,4 +25,11 @@ pub trait EthereumNode {
/// Returns the balance of the provided [`Address`] back. /// Returns the balance of the provided [`Address`] back.
fn balance_of(&self, address: Address) -> impl Future<Output = Result<U256>>; fn balance_of(&self, address: Address) -> impl Future<Output = Result<U256>>;
/// Returns the latest storage proof of the provided [`Address`]
fn latest_state_proof(
&self,
address: Address,
keys: Vec<StorageKey>,
) -> impl Future<Output = Result<EIP1186AccountProofResponse>>;
} }
+18 -2
View File
@@ -17,14 +17,16 @@ use alloy::{
eips::BlockNumberOrTag, eips::BlockNumberOrTag,
genesis::{Genesis, GenesisAccount}, genesis::{Genesis, GenesisAccount},
network::{Ethereum, EthereumWallet, NetworkWallet}, network::{Ethereum, EthereumWallet, NetworkWallet},
primitives::{Address, BlockHash, BlockNumber, BlockTimestamp, FixedBytes, TxHash, U256}, primitives::{
Address, BlockHash, BlockNumber, BlockTimestamp, FixedBytes, StorageKey, TxHash, U256,
},
providers::{ providers::{
Provider, ProviderBuilder, Provider, ProviderBuilder,
ext::DebugApi, ext::DebugApi,
fillers::{CachedNonceManager, ChainIdFiller, FillProvider, NonceFiller, TxFiller}, fillers::{CachedNonceManager, ChainIdFiller, FillProvider, NonceFiller, TxFiller},
}, },
rpc::types::{ rpc::types::{
TransactionReceipt, TransactionRequest, EIP1186AccountProofResponse, TransactionReceipt, TransactionRequest,
trace::geth::{DiffMode, GethDebugTracingOptions, PreStateConfig, PreStateFrame}, trace::geth::{DiffMode, GethDebugTracingOptions, PreStateConfig, PreStateFrame},
}, },
signers::local::PrivateKeySigner, signers::local::PrivateKeySigner,
@@ -380,6 +382,20 @@ impl EthereumNode for GethNode {
.await .await
.map_err(Into::into) .map_err(Into::into)
} }
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
async fn latest_state_proof(
&self,
address: Address,
keys: Vec<StorageKey>,
) -> anyhow::Result<EIP1186AccountProofResponse> {
self.provider()
.await?
.get_proof(address, keys)
.latest()
.await
.map_err(Into::into)
}
} }
impl ResolverApi for GethNode { impl ResolverApi for GethNode {
+16 -2
View File
@@ -17,7 +17,7 @@ use alloy::{
}, },
primitives::{ primitives::{
Address, B64, B256, BlockHash, BlockNumber, BlockTimestamp, Bloom, Bytes, FixedBytes, Address, B64, B256, BlockHash, BlockNumber, BlockTimestamp, Bloom, Bytes, FixedBytes,
TxHash, U256, StorageKey, TxHash, U256,
}, },
providers::{ providers::{
Provider, ProviderBuilder, Provider, ProviderBuilder,
@@ -25,7 +25,7 @@ use alloy::{
fillers::{CachedNonceManager, ChainIdFiller, FillProvider, NonceFiller, TxFiller}, fillers::{CachedNonceManager, ChainIdFiller, FillProvider, NonceFiller, TxFiller},
}, },
rpc::types::{ rpc::types::{
TransactionReceipt, EIP1186AccountProofResponse, TransactionReceipt,
eth::{Block, Header, Transaction}, eth::{Block, Header, Transaction},
trace::geth::{DiffMode, GethDebugTracingOptions, PreStateConfig, PreStateFrame}, trace::geth::{DiffMode, GethDebugTracingOptions, PreStateConfig, PreStateFrame},
}, },
@@ -437,6 +437,20 @@ impl EthereumNode for KitchensinkNode {
.await .await
.map_err(Into::into) .map_err(Into::into)
} }
#[tracing::instrument(skip_all, fields(kitchensink_node_id = self.id))]
async fn latest_state_proof(
&self,
address: Address,
keys: Vec<StorageKey>,
) -> anyhow::Result<EIP1186AccountProofResponse> {
self.provider()
.await?
.get_proof(address, keys)
.latest()
.await
.map_err(Into::into)
}
} }
impl ResolverApi for KitchensinkNode { impl ResolverApi for KitchensinkNode {