Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: updates unmaintained deps to alloy alternatives #559

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,648 changes: 1,067 additions & 1,581 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ zksync_web3_decl = { git = "https://github.com/matter-labs/zksync-era.git", rev
anyhow = "1.0"
alloy-signer-local = { version = "0.5.4", features = ["mnemonic"] }
alloy-signer = { version = "0.5.4", default-features = false }
alloy-dyn-abi = "0.5.4"
alloy-primitives = { version = "0.5.4" }
alloy-json-abi = "0.5.4"
async-trait = "0.1.85"
chrono = { version = "0.4.31", default-features = false }
clap = { version = "4.2.4", features = ["derive", "env"] }
colored = "2"
ethabi = "16.0.0"
eyre = "0.6"
flate2 = "1.0"
futures = { version = "0.3", features = ["compat"] }
Expand Down Expand Up @@ -85,8 +87,7 @@ url = "2.5.4"
httptest = "0.15.4"
tempdir = "0.3.7"
maplit = "1.0.2"
zksync-web3-rs = "0.1.1"
ethers = { version = "2.0.4", features = ["rustls"] }
alloy-zksync = "0.9.0"
test-case = "3.3.1"
backon = "1.3.0"

Expand Down
8 changes: 5 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ tokio.workspace = true
futures.workspace = true
once_cell.workspace = true

alloy-json-abi.workspace = true
alloy-primitives.workspace = true
alloy-dyn-abi.workspace = true

reqwest.workspace = true
serde.workspace = true
tracing.workspace = true
Expand All @@ -32,7 +36,6 @@ lazy_static.workspace = true
eyre.workspace = true
serde_json.workspace = true
hex.workspace = true
ethabi.workspace = true
itertools.workspace = true
rustc-hash.workspace = true
indexmap.workspace = true
Expand All @@ -45,9 +48,8 @@ url.workspace = true

[dev-dependencies]
maplit.workspace = true
ethers.workspace = true
httptest.workspace = true
tempdir.workspace = true
zksync-web3-rs.workspace = true
alloy-zksync.workspace = true
test-case.workspace = true
backon.workspace = true
32 changes: 13 additions & 19 deletions crates/core/src/console_log.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{collections::HashMap, str::FromStr};

use crate::utils::format_token;
use alloy_dyn_abi::JsonAbiExt;
use alloy_json_abi::{Function, Param, StateMutability};
use alloy_primitives::Selector;
use colored::Colorize;
use ethabi::param_type::Reader;
use ethabi::{Function, Param, StateMutability};
use itertools::Itertools;
use zksync_multivm::interface::Call;
use zksync_types::H160;
Expand All @@ -15,7 +17,7 @@ use zksync_types::H160;
pub struct ConsoleLogHandler {
/// Map from the 4-byte function signature to function itself.
// This contract has many 'log' methods (depending on argument type) - so we have a map here, to be able to parse the arguments.
signature_map: HashMap<[u8; 4], Function>,
signature_map: HashMap<Selector, Function>,
/// The 'fake' hardcoded contract, whose calls with have to log.
target_contract: H160,
}
Expand All @@ -27,7 +29,7 @@ impl Default for ConsoleLogHandler {
Self {
signature_map: get_log_functions()
.into_iter()
.map(|func| (func.short_signature(), func))
.map(|func| (func.selector(), func))
.collect::<HashMap<_, _>>(),
target_contract: H160::from_str(CONSOLE_ADDRESS).unwrap(),
}
Expand Down Expand Up @@ -69,10 +71,10 @@ impl ConsoleLogHandler {
self.signature_map
.get(signature)
.map_or("Unknown log call.".to_owned(), |func| {
let tokens = func.decode_input(&current_call.input.as_slice()[4..]);

let tokens: Result<Vec<alloy_dyn_abi::DynSolValue>, alloy_dyn_abi::Error> =
func.abi_decode_input(&current_call.input.as_slice()[4..], false);
tokens.map_or("Failed to parse inputs for log.".to_owned(), |tokens| {
tokens.iter().map(|t| format!("{}", t)).join(" ")
tokens.iter().map(|t| format_token(t, false)).join(" ")
})
});
Some(message)
Expand All @@ -89,25 +91,17 @@ fn get_log_functions() -> Vec<Function> {
.split_once('(')
.unwrap_or_else(|| panic!("unable to obtain function name for '{}'", func_decl));

#[allow(deprecated)] // for deprecated field `constant`
Function {
name: String::from(name),
inputs: params
.split(',')
.enumerate()
.map(|(index, param)| Param {
name: format!("p{index}"),
kind: Reader::read(param).unwrap_or_else(|err| {
panic!(
"failed deserializing type '{}' for '{}' : {:?}",
param, func_decl, err
)
}),
internal_type: Some(String::from(param)),
.map(|param| {
Param::parse(param).unwrap_or_else(|err| {
panic!("Failed to parse parameter '{}': {:?}", param, err)
})
})
.collect(),
outputs: vec![],
constant: false,
state_mutability: StateMutability::View,
}
})
Expand Down
86 changes: 73 additions & 13 deletions crates/core/src/node/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ impl InMemoryNode {

#[cfg(test)]
mod tests {
use alloy_dyn_abi::{DynSolValue, FunctionExt, JsonAbiExt};
use alloy_json_abi::{Function, Param};
use alloy_primitives::{Address as AlloyAddress, U256 as AlloyU256};
use anvil_zksync_config::constants::DEFAULT_ACCOUNT_BALANCE;
use ethers::abi::{short_signature, AbiEncode, HumanReadableParser, ParamType, Token};
use zksync_types::{
transaction_request::CallRequestBuilder, utils::deployed_address_create, Address,
K256PrivateKey, L2BlockNumber, Nonce, H160, U256,
Expand All @@ -150,12 +152,16 @@ mod tests {
include_bytes!("../deps/test-contracts/Secondary.json"),
);
let secondary_deployed_address = deployed_address_create(from_account, U256::zero());
let alloy_secondary_address = AlloyAddress::from(secondary_deployed_address.0);
let secondary_constructor_calldata =
DynSolValue::Uint(AlloyU256::from(2), 256).abi_encode();

testing::deploy_contract(
node,
H256::repeat_byte(0x1),
&private_key,
secondary_bytecode,
Some((U256::from(2),).encode()),
Some(secondary_constructor_calldata),
Nonce(0),
)
.await;
Expand All @@ -166,15 +172,19 @@ mod tests {
include_bytes!("../deps/test-contracts/Primary.json"),
);
let primary_deployed_address = deployed_address_create(from_account, U256::one());
let primary_constructor_calldata =
DynSolValue::Address(alloy_secondary_address).abi_encode();

testing::deploy_contract(
node,
H256::repeat_byte(0x1),
&private_key,
primary_bytecode,
Some((secondary_deployed_address).encode()),
Some(primary_constructor_calldata),
Nonce(1),
)
.await;

(primary_deployed_address, secondary_deployed_address)
}

Expand All @@ -184,9 +194,28 @@ mod tests {

let (primary_deployed_address, secondary_deployed_address) =
deploy_test_contracts(&node).await;
// trace a call to the primary contract
let func = HumanReadableParser::parse_function("calculate(uint)").unwrap();
let calldata = func.encode_input(&[Token::Uint(U256::from(42))]).unwrap();

let func = Function {
name: "calculate".to_string(),
inputs: vec![Param {
name: "value".to_string(),
ty: "uint256".to_string(),
components: vec![],
internal_type: None,
}],
outputs: vec![Param {
name: "".to_string(),
ty: "uint256".to_string(),
components: vec![],
internal_type: None,
}],
state_mutability: alloy_json_abi::StateMutability::NonPayable,
};

let calldata = func
.abi_encode_input(&[DynSolValue::Uint(AlloyU256::from(42), 256)])
.expect("failed to encode function input");

let request = CallRequestBuilder::default()
.to(Some(primary_deployed_address))
.data(calldata.clone().into())
Expand All @@ -203,9 +232,14 @@ mod tests {
assert!(trace.revert_reason.is_none());

// check that the call was successful
let output =
ethers::abi::decode(&[ParamType::Uint(256)], trace.output.0.as_slice()).unwrap();
assert_eq!(output[0], Token::Uint(U256::from(84)));
let output = func
.abi_decode_output(trace.output.0.as_slice(), true)
.expect("failed to decode output");
assert_eq!(
output[0],
DynSolValue::Uint(AlloyU256::from(84), 256),
"unexpected output"
);

// find the call to primary contract in the trace
let contract_call = trace
Expand All @@ -226,7 +260,12 @@ mod tests {
let subcall = contract_call.calls.first().unwrap();
assert_eq!(subcall.to, secondary_deployed_address);
assert_eq!(subcall.from, primary_deployed_address);
assert_eq!(subcall.output, U256::from(84).encode().into());
assert_eq!(
subcall.output,
func.abi_encode_output(&[DynSolValue::Uint(AlloyU256::from(84), 256)])
.expect("failed to encode function output")
.into()
);
}

#[tokio::test]
Expand All @@ -236,8 +275,22 @@ mod tests {
let (primary_deployed_address, _) = deploy_test_contracts(&node).await;

// trace a call to the primary contract
let func = HumanReadableParser::parse_function("calculate(uint)").unwrap();
let calldata = func.encode_input(&[Token::Uint(U256::from(42))]).unwrap();
let func = Function {
name: "calculate".to_string(),
inputs: vec![Param {
name: "value".to_string(),
ty: "uint256".to_string(),
components: vec![],
internal_type: None,
}],
outputs: vec![],
state_mutability: alloy_json_abi::StateMutability::NonPayable,
};

let calldata = func
.abi_encode_input(&[DynSolValue::Uint(AlloyU256::from(42), 256)])
.expect("failed to encode function input");

let request = CallRequestBuilder::default()
.to(Some(primary_deployed_address))
.data(calldata.into())
Expand Down Expand Up @@ -273,10 +326,17 @@ mod tests {

let (primary_deployed_address, _) = deploy_test_contracts(&node).await;

let func = Function {
name: "shouldRevert".to_string(),
inputs: vec![],
outputs: vec![],
state_mutability: alloy_json_abi::StateMutability::NonPayable,
};

// trace a call to the primary contract
let request = CallRequestBuilder::default()
.to(Some(primary_deployed_address))
.data(short_signature("shouldRevert()", &[]).into())
.data(func.selector().to_vec().into())
.gas(80_000_000.into())
.build();
let trace = node
Expand Down
Loading
Loading