Compare commits

...

7 Commits

Author SHA1 Message Date
Omar Abdulla 0ba28671c9 Permit size difference in step output 2025-08-11 14:54:10 +03:00
Omar Abdulla 5d4c3b06c4 Box the test steps 2025-08-11 14:49:04 +03:00
Omar Abdulla c32124395d Merge remote-tracking branch 'origin/main' into feature/balance-assertions 2025-08-11 14:44:31 +03:00
Omar Abdulla 029aeccba3 Test balance deserialization 2025-08-11 14:40:01 +03:00
Omar Abdulla 71a05c3f47 Add a balance assertion test step 2025-08-11 14:31:06 +03:00
Omar Abdulla 22e46e0762 Refactor tests to use steps 2025-08-11 14:03:20 +03:00
Omar Abdulla eacff46624 Make metadata serializable 2025-08-11 00:32:42 +03:00
9 changed files with 216 additions and 40 deletions
+4
View File
@@ -8,6 +8,10 @@
{ {
"name": "first", "name": "first",
"inputs": [ "inputs": [
{
"address": "0xdeadbeef00000000000000000000000000000042",
"expected_balance": "1233"
},
{ {
"instance": "WBTC_1", "instance": "WBTC_1",
"method": "#deployer", "method": "#deployer",
@@ -1,4 +1,8 @@
use std::{borrow::Cow, collections::HashSet, path::PathBuf}; use std::{
borrow::Cow,
collections::HashSet,
path::{Path, PathBuf},
};
/// An iterator that finds files of a certain extension in the provided directory. You can think of /// An iterator that finds files of a certain extension in the provided directory. You can think of
/// this a glob pattern similar to: `${path}/**/*.md` /// this a glob pattern similar to: `${path}/**/*.md`
@@ -18,10 +22,10 @@ pub struct FilesWithExtensionIterator {
} }
impl FilesWithExtensionIterator { impl FilesWithExtensionIterator {
pub fn new(root_directory: PathBuf) -> Self { pub fn new(root_directory: impl AsRef<Path>) -> Self {
Self { Self {
allowed_extensions: Default::default(), allowed_extensions: Default::default(),
directories_to_search: vec![root_directory], directories_to_search: vec![root_directory.as_ref().to_path_buf()],
files_matching_allowed_extensions: Default::default(), files_matching_allowed_extensions: Default::default(),
} }
} }
+125 -15
View File
@@ -26,9 +26,11 @@ use revive_dt_format::traits::{ResolutionContext, ResolverApi};
use semver::Version; use semver::Version;
use revive_dt_format::case::{Case, CaseIdx}; use revive_dt_format::case::{Case, CaseIdx};
use revive_dt_format::input::{Calldata, EtherValue, Expected, ExpectedOutput, Method}; use revive_dt_format::input::{
BalanceAssertion, Calldata, EtherValue, Expected, ExpectedOutput, Input, Method,
};
use revive_dt_format::metadata::{ContractInstance, ContractPathAndIdent}; use revive_dt_format::metadata::{ContractInstance, ContractPathAndIdent};
use revive_dt_format::{input::Input, metadata::Metadata}; use revive_dt_format::{input::Step, metadata::Metadata};
use revive_dt_node::Node; use revive_dt_node::Node;
use revive_dt_node_interaction::EthereumNode; use revive_dt_node_interaction::EthereumNode;
use tracing::Instrument; use tracing::Instrument;
@@ -70,6 +72,27 @@ where
} }
} }
pub async fn handle_step(
&mut self,
metadata: &Metadata,
case_idx: CaseIdx,
step: &Step,
node: &T::Blockchain,
) -> anyhow::Result<StepOutput> {
match step {
Step::FunctionCall(input) => {
let (receipt, geth_trace, diff_mode) =
self.handle_input(metadata, case_idx, input, node).await?;
Ok(StepOutput::FunctionCall(receipt, geth_trace, diff_mode))
}
Step::BalanceAssertion(balance_assertion) => {
self.handle_balance_assertion(metadata, case_idx, balance_assertion, node)
.await?;
Ok(StepOutput::BalanceAssertion)
}
}
}
pub async fn handle_input( pub async fn handle_input(
&mut self, &mut self,
metadata: &Metadata, metadata: &Metadata,
@@ -78,7 +101,7 @@ where
node: &T::Blockchain, node: &T::Blockchain,
) -> anyhow::Result<(TransactionReceipt, GethTrace, DiffMode)> { ) -> anyhow::Result<(TransactionReceipt, GethTrace, DiffMode)> {
let deployment_receipts = self let deployment_receipts = self
.handle_contract_deployment(metadata, case_idx, input, node) .handle_input_contract_deployment(metadata, case_idx, input, node)
.await?; .await?;
let execution_receipt = self let execution_receipt = self
.handle_input_execution(input, deployment_receipts, node) .handle_input_execution(input, deployment_receipts, node)
@@ -93,8 +116,21 @@ where
.await .await
} }
pub async fn handle_balance_assertion(
&mut self,
metadata: &Metadata,
_: CaseIdx,
balance_assertion: &BalanceAssertion,
node: &T::Blockchain,
) -> anyhow::Result<()> {
self.handle_balance_assertion_contract_deployment(metadata, balance_assertion, node)
.await?;
Ok(())
}
/// Handles the contract deployment for a given input performing it if it needs to be performed. /// Handles the contract deployment for a given input performing it if it needs to be performed.
async fn handle_contract_deployment( async fn handle_input_contract_deployment(
&mut self, &mut self,
metadata: &Metadata, metadata: &Metadata,
case_idx: CaseIdx, case_idx: CaseIdx,
@@ -462,6 +498,65 @@ where
Ok((execution_receipt, trace, diff)) Ok((execution_receipt, trace, diff))
} }
pub async fn handle_balance_assertion_contract_deployment(
&mut self,
metadata: &Metadata,
balance_assertion: &BalanceAssertion,
node: &T::Blockchain,
) -> anyhow::Result<()> {
let Some(instance) = balance_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_balance_assertion_execution(
&mut self,
BalanceAssertion {
address: address_string,
expected_balance: amount,
}: &BalanceAssertion,
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 balance = node.balance_of(address).await?;
let expected = *amount;
let actual = balance;
if expected != actual {
tracing::error!(%expected, %actual, %address, "Balance assertion failed");
anyhow::bail!(
"Balance 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.
/// ///
@@ -651,21 +746,25 @@ where
return Ok(0); return Ok(0);
} }
let mut inputs_executed = 0; let mut steps_executed = 0;
for (input_idx, input) in self.case.inputs_iterator().enumerate() { for (step_idx, step) in self.case.steps_iterator().enumerate() {
let tracing_span = tracing::info_span!("Handling input", input_idx); let tracing_span = tracing::info_span!("Handling input", step_idx);
let (leader_receipt, _, leader_diff) = self let leader_step_output = self
.leader_state .leader_state
.handle_input(self.metadata, self.case_idx, &input, self.leader_node) .handle_step(self.metadata, self.case_idx, &step, self.leader_node)
.instrument(tracing_span.clone()) .instrument(tracing_span.clone())
.await?; .await?;
let (follower_receipt, _, follower_diff) = self let follower_step_output = self
.follower_state .follower_state
.handle_input(self.metadata, self.case_idx, &input, self.follower_node) .handle_step(self.metadata, self.case_idx, &step, self.follower_node)
.instrument(tracing_span) .instrument(tracing_span)
.await?; .await?;
match (leader_step_output, follower_step_output) {
(
StepOutput::FunctionCall(leader_receipt, _, leader_diff),
StepOutput::FunctionCall(follower_receipt, _, follower_diff),
) => {
if leader_diff == follower_diff { if leader_diff == follower_diff {
tracing::debug!("State diffs match between leader and follower."); tracing::debug!("State diffs match between leader and follower.");
} else { } else {
@@ -679,10 +778,21 @@ where
tracing::trace!("Leader logs: {:?}", leader_receipt.logs()); tracing::trace!("Leader logs: {:?}", leader_receipt.logs());
tracing::trace!("Follower logs: {:?}", follower_receipt.logs()); tracing::trace!("Follower logs: {:?}", follower_receipt.logs());
} }
}
inputs_executed += 1; (StepOutput::BalanceAssertion, StepOutput::BalanceAssertion) => {}
_ => unreachable!("The two step outputs can not be of a different kind"),
} }
Ok(inputs_executed) steps_executed += 1;
}
Ok(steps_executed)
} }
} }
#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum StepOutput {
FunctionCall(TransactionReceipt, GethTrace, DiffMode),
BalanceAssertion,
}
+6 -3
View File
@@ -32,7 +32,7 @@ use revive_dt_core::{
use revive_dt_format::{ use revive_dt_format::{
case::{Case, CaseIdx}, case::{Case, CaseIdx},
corpus::Corpus, corpus::Corpus,
input::Input, input::{Input, Step},
metadata::{ContractInstance, ContractPathAndIdent, Metadata, MetadataFile}, metadata::{ContractInstance, ContractPathAndIdent, Metadata, MetadataFile},
mode::SolcMode, mode::SolcMode,
}; };
@@ -446,9 +446,12 @@ where
// doing the deployments from different accounts and therefore we're not slowed down by // doing the deployments from different accounts and therefore we're not slowed down by
// the nonce. // the nonce.
let deployer_address = case let deployer_address = case
.inputs .steps
.iter() .iter()
.map(|input| input.caller) .filter_map(|step| match step {
Step::FunctionCall(input) => Some(input.caller),
Step::BalanceAssertion(..) => None,
})
.next() .next()
.unwrap_or(Input::default_caller()); .unwrap_or(Input::default_caller());
let leader_tx = TransactionBuilder::<Ethereum>::with_deploy_code( let leader_tx = TransactionBuilder::<Ethereum>::with_deploy_code(
+15 -9
View File
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use revive_dt_common::macros::define_wrapper_type; use revive_dt_common::macros::define_wrapper_type;
use crate::{ use crate::{
input::{Expected, Input}, input::{Expected, Step},
mode::Mode, mode::Mode,
}; };
@@ -12,21 +12,27 @@ pub struct Case {
pub name: Option<String>, pub name: Option<String>,
pub comment: Option<String>, pub comment: Option<String>,
pub modes: Option<Vec<Mode>>, pub modes: Option<Vec<Mode>>,
pub inputs: Vec<Input>, #[serde(rename = "inputs")]
pub steps: Vec<Step>,
pub group: Option<String>, pub group: Option<String>,
pub expected: Option<Expected>, pub expected: Option<Expected>,
pub ignore: Option<bool>, pub ignore: Option<bool>,
} }
impl Case { impl Case {
pub fn inputs_iterator(&self) -> impl Iterator<Item = Input> { #[allow(irrefutable_let_patterns)]
let inputs_len = self.inputs.len(); pub fn steps_iterator(&self) -> impl Iterator<Item = Step> {
self.inputs let steps_len = self.steps.len();
self.steps
.clone() .clone()
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(move |(idx, mut input)| { .map(move |(idx, mut step)| {
if idx + 1 == inputs_len { let Step::FunctionCall(ref mut input) = step else {
return step;
};
if idx + 1 == steps_len {
if input.expected.is_none() { if input.expected.is_none() {
input.expected = self.expected.clone(); input.expected = self.expected.clone();
} }
@@ -36,9 +42,9 @@ impl Case {
// the case? What are we supposed to do with that final expected field on the // the case? What are we supposed to do with that final expected field on the
// case? // case?
input step
} else { } else {
input step
} }
}) })
} }
+27
View File
@@ -17,6 +17,19 @@ use revive_dt_common::macros::define_wrapper_type;
use crate::traits::ResolverApi; use crate::traits::ResolverApi;
use crate::{metadata::ContractInstance, traits::ResolutionContext}; use crate::{metadata::ContractInstance, traits::ResolutionContext};
/// A test step.
///
/// A test step can be anything. It could be an invocation to a function, an assertion, or any other
/// action that needs to be run or executed on the nodes used in the tests.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum Step {
/// A function call or an invocation to some function on some smart contract.
FunctionCall(Box<Input>),
/// A step for performing a balance assertion on some account or contract.
BalanceAssertion(Box<BalanceAssertion>),
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct Input { pub struct Input {
#[serde(default = "Input::default_caller")] #[serde(default = "Input::default_caller")]
@@ -33,6 +46,20 @@ pub struct Input {
pub variable_assignments: Option<VariableAssignments>, pub variable_assignments: Option<VariableAssignments>,
} }
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct BalanceAssertion {
/// 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,
/// The amount of balance to assert that the account or contract has.
pub expected_balance: U256,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)] #[serde(untagged)]
pub enum Expected { pub enum Expected {
+4
View File
@@ -1,5 +1,6 @@
//! This crate implements all node interactions. //! This crate implements all node interactions.
use alloy::primitives::{Address, 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::{TransactionReceipt, TransactionRequest};
use anyhow::Result; use anyhow::Result;
@@ -21,4 +22,7 @@ pub trait EthereumNode {
/// Returns the state diff of the transaction hash in the [TransactionReceipt]. /// Returns the state diff of the transaction hash in the [TransactionReceipt].
fn state_diff(&self, receipt: &TransactionReceipt) -> impl Future<Output = Result<DiffMode>>; fn state_diff(&self, receipt: &TransactionReceipt) -> impl Future<Output = Result<DiffMode>>;
/// Returns the balance of the provided [`Address`] back.
fn balance_of(&self, address: Address) -> impl Future<Output = Result<U256>>;
} }
+9
View File
@@ -371,6 +371,15 @@ impl EthereumNode for GethNode {
_ => anyhow::bail!("expected a diff mode trace"), _ => anyhow::bail!("expected a diff mode trace"),
} }
} }
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
async fn balance_of(&self, address: Address) -> anyhow::Result<U256> {
self.provider()
.await?
.get_balance(address)
.await
.map_err(Into::into)
}
} }
impl ResolverApi for GethNode { impl ResolverApi for GethNode {
+9
View File
@@ -428,6 +428,15 @@ impl EthereumNode for KitchensinkNode {
_ => anyhow::bail!("expected a diff mode trace"), _ => anyhow::bail!("expected a diff mode trace"),
} }
} }
#[tracing::instrument(skip_all, fields(kitchensink_node_id = self.id))]
async fn balance_of(&self, address: Address) -> anyhow::Result<U256> {
self.provider()
.await?
.get_balance(address)
.await
.map_err(Into::into)
}
} }
impl ResolverApi for KitchensinkNode { impl ResolverApi for KitchensinkNode {