Skip to content

Commit

Permalink
add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Jan 7, 2023
1 parent 6b47d49 commit a7bf518
Show file tree
Hide file tree
Showing 5 changed files with 680 additions and 13 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ clap = { version = "4", features = ["derive"] }
dirs = "4.0"
educe = { version = "0.4", features = ["Debug", "Default"] }
ethereum-interfaces = { git = "https://github.com/ledgerwatch/interfaces" }
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "b27c7b0", features = ["ethers-solc"] }
ethers = { git = "https://github.com/gakonst/ethers-rs.git", rev = "3ed83d5dd38e7819605511631aaddcc3e472427e", features = ["ethers-solc"] }
expanded-pathbuf = "0.1"
hex = { version = "0.4.3", default-features = false, features = ["std"] }
jsonrpsee = { version = "0.16", features = ["server", "macros"] }
Expand All @@ -34,7 +34,7 @@ tracing-subscriber = "0.3"

[build-dependencies]
anyhow = "1"
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "b27c7b0", features = ["ethers-solc"] }
ethers = { git = "https://github.com/gakonst/ethers-rs.git", rev = "3ed83d5dd38e7819605511631aaddcc3e472427e", features = ["ethers-solc"] }
protobuf-src = "1.1.0"
prost-build = "0.11"
tonic-build = "0.8"
Expand Down
40 changes: 40 additions & 0 deletions tests/common/gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use ethers::prelude::abigen;

abigen!(SimpleAccountFactory,
"$CARGO_MANIFEST_DIR/thirdparty/account-abstraction/artifacts/contracts/samples/SimpleAccountFactory.sol/SimpleAccountFactory.json");

abigen!(SimpleAccount,
"$CARGO_MANIFEST_DIR/thirdparty/account-abstraction/artifacts/contracts/samples/SimpleAccount.sol/SimpleAccount.json");

abigen!(
EntryPointContract,
"$CARGO_MANIFEST_DIR/thirdparty/account-abstraction/artifacts/contracts/core/EntryPoint.sol/EntryPoint.json"
);
abigen!(
TestOpcodesAccountFactory,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestOpcodesAccount.sol/TestOpcodesAccountFactory.json"
);
abigen!(
TestOpcodesAccount,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestOpcodesAccount.sol/TestOpcodesAccount.json"
);
abigen!(
TestStorageAccount,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestStorageAccount.sol/TestStorageAccount.json"
);
abigen!(
TestRecursionAccount,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestRecursionAccount.sol/TestRecursionAccount.json"
);
abigen!(
TestStorageAccountFactory,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestStorageAccount.sol/TestStorageAccountFactory.json"
);
abigen!(
TestRulesAccount,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestRulesAccount.sol/TestRulesAccount.json"
);
abigen!(
TestRulesAccountFactory,
"$CARGO_MANIFEST_DIR/thirdparty/bundler/packages/bundler/artifacts/contracts/tests/TestRulesAccount.sol/TestRulesAccountFactory.json"
);
115 changes: 115 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use std::sync::Arc;

use aa_bundler::types::user_operation::UserOperation;
use ethers::{
prelude::k256::ecdsa::SigningKey,
providers::Middleware,
signers::{Signer, Wallet},
types::{Address, Bytes, U256},
};

use self::gen::{
EntryPointContract, TestOpcodesAccount, TestOpcodesAccountFactory, TestRecursionAccount,
TestRulesAccountFactory, TestStorageAccount, TestStorageAccountFactory,
};
pub mod gen;

pub const ANVIL_TEST_KEY_PHRASE: &str =
"test test test test test test test test test test test junk";

pub struct DeployedContract<C> {
contract: C,
pub address: Address,
}
impl<C> DeployedContract<C> {
pub fn new(contract: C, address: Address) -> Self {
Self { contract, address }
}

pub fn contract(&self) -> &C {
&self.contract
}
}

pub async fn deploy_entry_point<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<EntryPointContract<M>>> {
let (entry_point, receipt) = EntryPointContract::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(entry_point, address))
}

pub async fn deploy_test_opcode_account<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<TestOpcodesAccount<M>>> {
let (account, receipt) = TestOpcodesAccount::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(account, address))
}

pub async fn deploy_test_opcode_account_factory<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<TestOpcodesAccountFactory<M>>> {
let (factory, receipt) = TestOpcodesAccountFactory::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(factory, address))
}

pub async fn deploy_test_storage_account_factory<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<TestStorageAccountFactory<M>>> {
let (factory, receipt) = TestStorageAccountFactory::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(factory, address))
}

pub async fn deploy_test_storage_account<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<TestStorageAccount<M>>> {
let (account, receipt) = TestStorageAccount::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(account, address))
}

pub async fn deploy_test_recursion_account<M: Middleware + 'static>(
client: Arc<M>,
entry_point_address: Address,
) -> anyhow::Result<DeployedContract<TestRecursionAccount<M>>> {
let (account, receipt) = TestRecursionAccount::deploy(client, entry_point_address)?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(account, address))
}

pub async fn deploy_test_rules_account_factory<M: Middleware + 'static>(
client: Arc<M>,
) -> anyhow::Result<DeployedContract<TestRulesAccountFactory<M>>> {
let (factory, receipt) = TestRulesAccountFactory::deploy(client, ())?
.send_with_receipt()
.await?;
let address = receipt.contract_address.unwrap();
Ok(DeployedContract::new(factory, address))
}

pub async fn sign(
user_op: &mut UserOperation,
entry_point_address: Address,
chain_id: U256,
key: Wallet<SigningKey>,
) -> anyhow::Result<()> {
let user_op_hash = user_op.hash(entry_point_address, chain_id);
let signature = key.sign_message(user_op_hash.as_bytes()).await?;
user_op.signature = Bytes::from(signature.to_vec());
Ok(())
}
Loading

0 comments on commit a7bf518

Please sign in to comment.