Skip to content

Commit

Permalink
test(evm): depoly contract before test.
Browse files Browse the repository at this point in the history
  • Loading branch information
andysim3d committed Oct 4, 2024
1 parent 42745d9 commit 178afce
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
27 changes: 21 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ test-utils = [ "mockall" ]

[dev-dependencies]
rundler-provider = { path = ".", features = ["test-utils"] }
alloy-provider = {workspace = true, features=["debug-api", "anvil-node"]}
alloy-node-bindings = "0.4.2"
tokio.workspace = true
alloy-sol-macro.workspace = true
92 changes: 74 additions & 18 deletions crates/provider/src/alloy/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,35 +301,91 @@ where

#[cfg(test)]
mod tests {
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, Address};
use alloy_provider::ProviderBuilder;
use alloy_rpc_client::ClientBuilder;
use alloy_sol_macro::sol;

use crate::{AlloyEvmProvider, EvmProvider};

sol!(
#[allow(missing_docs)]
#[sol(rpc, bytecode="0x6080604052348015600f57600080fd5b506064431060635760405162461bcd60e51b815260206004820152601660248201527f73686f756c64206e6f74206265206465706c6f79656400000000000000000000604482015260640160405180910390fd5b6102b8806100726000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80637b34b62114610030575b600080fd5b61004361003e366004610159565b610055565b60405190815260200160405180910390f35b600080825167ffffffffffffffff81111561007257610072610127565b60405190808252806020026020018201604052801561009b578160200160208202803683370190505b50905060005b83518110156100f3578381815181106100bc576100bc610229565b60200260200101516001600160a01b03163f8282815181106100e0576100e0610229565b60209081029190910101526001016100a1565b50600081604051602001610107919061023f565b60408051601f198184030181529190528051602090910120949350505050565b634e487b7160e01b600052604160045260246000fd5b80356001600160a01b038116811461015457600080fd5b919050565b60006020828403121561016b57600080fd5b813567ffffffffffffffff81111561018257600080fd5b8201601f8101841361019357600080fd5b803567ffffffffffffffff8111156101ad576101ad610127565b8060051b604051601f19603f830116810181811067ffffffffffffffff821117156101da576101da610127565b6040529182526020818401810192908101878411156101f857600080fd5b6020850194505b8385101561021e576102108561013d565b8152602094850194016101ff565b509695505050505050565b634e487b7160e01b600052603260045260246000fd5b602080825282518282018190526000918401906040840190835b81811015610277578351835260209384019390920191600101610259565b50909594505050505056fea2646970667358221220b2b7c1db7d478df6ecb5a14ec979bf9abb43dd1f7b85388b1c502bf5099282d264736f6c634300081a0033")]
contract GetCodeHashes {

constructor() {
require(block.number < 100, "should not be deployed");
}

function getCodeHashes(
address[] memory addresses
) public view returns (bytes32 hash) {
bytes32[] memory hashes = new bytes32[](addresses.length);
for (uint i = 0; i < addresses.length; i++) {
hashes[i] = addresses[i].codehash;
}
bytes memory data = abi.encode(hashes);
return keccak256(data);
}
}
);

sol!(
#[allow(missing_docs)]
#[sol(rpc, bytecode="0x6080604052348015600f57600080fd5b506064431060635760405162461bcd60e51b815260206004820152601660248201527f73686f756c64206e6f74206265206465706c6f79656400000000000000000000604482015260640160405180910390fd5b6102f0806100726000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063694f712d14610030575b600080fd5b61004361003e366004610120565b610059565b6040516100509190610225565b60405180910390f35b604080516060808201835260008083526020830181905292820152905a9050600080866001600160a01b031686866040516100949190610277565b60006040518083038185875af1925050503d80600081146100d1576040519150601f19603f3d011682016040523d82523d6000602084013e6100d6565b606091505b509150915060405180606001604052805a6100f19086610293565b8152921515602084015260409092015295945050505050565b634e487b7160e01b600052604160045260246000fd5b60008060006060848603121561013557600080fd5b83356001600160a01b038116811461014c57600080fd5b925060208401359150604084013567ffffffffffffffff81111561016f57600080fd5b8401601f8101861361018057600080fd5b803567ffffffffffffffff81111561019a5761019a61010a565b604051601f8201601f19908116603f0116810167ffffffffffffffff811182821017156101c9576101c961010a565b6040528181528282016020018810156101e157600080fd5b816020840160208301376000602083830101528093505050509250925092565b60005b8381101561021c578181015183820152602001610204565b50506000910152565b60208152815160208201526020820151151560408201526000604083015160608084015280518060808501526102628160a0860160208501610201565b601f01601f19169290920160a0019392505050565b60008251610289818460208701610201565b9190910192915050565b818103818111156102b457634e487b7160e01b600052601160045260246000fd5b9291505056fea2646970667358221220fd5c4f5bf9f90b6eab39aa46ddeda959ed5909b6e92973ab51c047771409755e64736f6c634300081a0033")]

contract GetGasUsed {
struct GasUsedResult {
uint256 gasUsed;
bool success;
bytes result;
}

constructor() {
require(block.number < 100, "should not be deployed");
}

function getGas(
address target,
uint256 value,
bytes memory data
) public returns (GasUsedResult memory) {
uint256 preGas = gasleft();
(bool success, bytes memory result) = target.call{value : value}(data);
return GasUsedResult({
gasUsed: preGas - gasleft(),
success: success,
result: result
});
}
}

);

#[tokio::test]
async fn test_get_code_hash_unorder() {
let anvil = Anvil::new().spawn();
let url = anvil.endpoint();
let client = ClientBuilder::default().http(url.parse().unwrap());
let alloy_provider = ProviderBuilder::new().on_client(client);
let emv_provider = AlloyEvmProvider::new(alloy_provider);
let alloy_provider = ProviderBuilder::new().on_anvil();

let address_1: Vec<Address> = vec![
address!("d8da6bf26964af9d7eed9e03e53415d37aa96045"),
address!("4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97"),
];
let get_code_hashes_contract = GetCodeHashes::deploy(alloy_provider.clone()).await.unwrap();
let get_gas_used_contract = GetGasUsed::deploy(alloy_provider.clone()).await.unwrap();

let result_1 = emv_provider.get_code_hash(address_1, None).await;
let emv_provider = AlloyEvmProvider::new(alloy_provider);
let address_1 = get_code_hashes_contract.address();
let address_2 = get_gas_used_contract.address();

let result_1 = emv_provider
.get_code_hash(vec![*address_1, *address_2], None)
.await;
let hash_1 = result_1.ok();

let address_2: Vec<Address> = vec![
address!("DeaDDEaDDeAdDeAdDEAdDEaddeAddEAdDEAd0001"),
address!("d8da6bf26964af9d7eed9e03e53415d37aa96045"),
];
let result_2 = emv_provider.get_code_hash(address_2, None).await;
let result_2 = emv_provider
.get_code_hash(vec![*address_2, *address_1], None)
.await;
let hash_2 = result_2.ok();

assert_eq!(hash_1, hash_2);

let result_3 = emv_provider.get_code_hash(vec![*address_1], None).await;
let hash_3 = result_3.ok();

assert_ne!(hash_1, hash_3);
}
}

0 comments on commit 178afce

Please sign in to comment.