Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #97 from paradigmxyz/fgimenez/remove-eip-3074-inst…
Browse files Browse the repository at this point in the history
…ructions

chore: remove eip3074 instructions
  • Loading branch information
mattsse authored Aug 12, 2024
2 parents ee520df + 9fb0b25 commit de8db8a
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 260 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ reth-tracing = { git = "https://github.com/paradigmxyz/reth.git", rev = "v1.0.4"
clap = "4"
eyre = "0.6.12"
tracing = "0.1.0"
eip3074-instructions = { git = "https://github.com/paradigmxyz/eip3074-instructions.git" }

# misc-testing
rstest = "0.18.2"
2 changes: 0 additions & 2 deletions crates/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ reth-node-api.workspace = true
reth-node-optimism.workspace = true
reth-chainspec.workspace = true

eip3074-instructions.workspace = true

eyre.workspace = true

[features]
Expand Down
49 changes: 0 additions & 49 deletions crates/node/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! precompiles defined by [`alphanet_precompile`].
use alphanet_precompile::{bls12_381, secp256r1};
use eip3074_instructions::{context::InstructionsContext, instructions as eip3074};
use reth::{
primitives::{
revm_primitives::{CfgEnvWithHandlerCfg, TxEnv},
Expand Down Expand Up @@ -57,52 +56,17 @@ impl AlphaNetEvmConfig {
loaded_precompiles
});
}

/// Appends custom instructions to the EVM handler
///
/// This will be invoked when the EVM is created via [ConfigureEvm::evm] or
/// [ConfigureEvm::evm_with_inspector]
///
/// This will use the default mainnet instructions and append additional instructions.
fn append_custom_instructions<EXT, DB>(
handler: &mut EvmHandler<'_, EXT, DB>,
instructions_context: InstructionsContext,
) where
DB: Database,
{
let boxed_instruction_with_op_code =
eip3074::boxed_instructions(instructions_context.clone());

for b in boxed_instruction_with_op_code {
handler.instruction_table.insert_boxed(b.opcode, b.boxed_instruction);
}

instructions_context.clear();
}
}

impl ConfigureEvm for AlphaNetEvmConfig {
type DefaultExternalContext<'a> = ();

fn evm<DB: Database>(&self, db: DB) -> Evm<'_, Self::DefaultExternalContext<'_>, DB> {
let instructions_context = InstructionsContext::default();
EvmBuilder::default()
.with_db(db)
.optimism()
// add additional precompiles
.append_handler_register(Self::set_precompiles)
// add custom instructions
.append_handler_register_box(Box::new(move |h| {
Self::append_custom_instructions(h, instructions_context.clone());
let post_execution_context = instructions_context.clone();
#[allow(clippy::arc_with_non_send_sync)]
{
h.post_execution.end = Arc::new(move |_, outcome: _| {
post_execution_context.clear();
outcome
});
}
}))
.build()
}

Expand All @@ -111,25 +75,12 @@ impl ConfigureEvm for AlphaNetEvmConfig {
DB: Database,
I: GetInspector<DB>,
{
let instructions_context = InstructionsContext::default();
EvmBuilder::default()
.with_db(db)
.with_external_context(inspector)
.optimism()
// add additional precompiles
.append_handler_register(Self::set_precompiles)
// add custom instructions
.append_handler_register_box(Box::new(move |h| {
Self::append_custom_instructions(h, instructions_context.clone());
let post_execution_context = instructions_context.clone();
#[allow(clippy::arc_with_non_send_sync)]
{
h.post_execution.end = Arc::new(move |_, outcome: _| {
post_execution_context.clear();
outcome
});
}
}))
.append_handler_register(inspector_handle_register)
.build()
}
Expand Down
189 changes: 2 additions & 187 deletions crates/testing/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
use crate::{test_suite::TestSuite, wallet::Wallet};
use crate::test_suite::TestSuite;
use alloy::{
primitives::hex,
providers::{Provider, ProviderBuilder},
sol,
sol_types::SolValue,
};
use alloy_network::EthereumWallet;
use alphanet_node::node::AlphaNetNode;
use once_cell::sync::Lazy;
use reth::{builder::NodeHandle, tasks::TaskManager};
use reth_chainspec::DEV;
use reth_node_builder::NodeBuilder;
use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig};
use reth_primitives::{keccak256, Address, Bytes, U256};
use reth_primitives::Bytes;
use url::Url;

sol!(
#[allow(clippy::too_many_arguments)]
#[allow(missing_docs)]
#[sol(rpc)]
GasSponsorInvoker,
"resources/eip3074/out/GasSponsorInvoker.sol/GasSponsorInvoker.json"
);

sol!(
#[allow(clippy::too_many_arguments)]
#[allow(missing_docs)]
#[sol(rpc)]
SenderRecorder,
"resources/eip3074/out/SenderRecorder.sol/SenderRecorder.json"
);

sol!(
#[allow(clippy::too_many_arguments)]
#[allow(missing_docs)]
Expand All @@ -39,173 +21,6 @@ sol!(
"resources/bls12-381/out/BlsG1AddCaller.sol/BlsG1AddCaller.json"
);

#[tokio::test]
async fn test_eip3074_integration() {
reth_tracing::init_test_tracing();
let tasks = TaskManager::current();
let test_suite = TestSuite::new();

// spin up alphanet node
let chain_spec = Lazy::force(&DEV).clone();
let node_config = NodeConfig::test()
.dev()
.with_chain(chain_spec)
.with_rpc(RpcServerArgs::default().with_unused_ports().with_http());
let NodeHandle { node, .. } = NodeBuilder::new(node_config)
.testing_node(tasks.executor())
.node(AlphaNetNode::default())
.launch()
.await
.unwrap();

let rpc_url = node.rpc_server_handle().http_url().unwrap();
let deployer = test_suite.signer();
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(deployer)
.on_http(Url::parse(&rpc_url).unwrap());
let base_fee = provider.get_gas_price().await.unwrap();

// Deploy sender recorder contract.
let sender_recorder_builder = SenderRecorder::deploy_builder(&provider);
let estimate = sender_recorder_builder.estimate_gas().await.unwrap();
let sender_recorder_address =
sender_recorder_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await.unwrap();
let sender_recorder = SenderRecorder::new(sender_recorder_address, &provider);
let SenderRecorder::lastSenderReturn { _0 } =
sender_recorder.lastSender().call().await.unwrap();
assert_eq!(_0, Address::ZERO);

// Deploy invoker contract.
let invoker_builder = GasSponsorInvoker::deploy_builder(&provider);
let estimate = invoker_builder.estimate_gas().await.unwrap();
let invoker_address =
invoker_builder.gas(estimate).gas_price(base_fee).nonce(1).deploy().await.unwrap();
let invoker = GasSponsorInvoker::new(invoker_address, &provider);

// signer account.
let signer_wallet = Wallet::random();
let signer_account: EthereumWallet = signer_wallet.clone().into();
let signer_address = signer_account.default_signer().address();
let signer_balance = provider.get_balance(signer_address).await.unwrap();
assert_eq!(signer_balance, U256::ZERO);
let signer_nonce = provider.get_transaction_count(signer_address).await.unwrap();

// abi encoded method call.
let binding = sender_recorder.recordSender();
let data = reth_primitives::Bytes(binding.calldata().0.clone());

// commit, digest and signature.
let commit = keccak256((sender_recorder_address, data.clone()).abi_encode_sequence());
let GasSponsorInvoker::getDigestReturn { digest } =
invoker.getDigest(commit, U256::from(signer_nonce)).call().await.unwrap();
let (v, r, s) = signer_wallet.sign_hash(digest).await;

let builder = invoker.sponsorCall(
signer_address,
v,
r.into(),
s.into(),
sender_recorder_address,
data,
U256::ZERO,
);
let estimate = builder.estimate_gas().await.unwrap();
let receipt = builder
.gas(estimate)
.gas_price(base_fee)
.nonce(2)
.send()
.await
.unwrap()
.get_receipt()
.await;
assert!(receipt.is_ok());

let SenderRecorder::lastSenderReturn { _0 } =
sender_recorder.lastSender().call().await.unwrap();
assert_eq!(_0, signer_address);
}

#[tokio::test]
async fn test_eip3074_send_eth() {
reth_tracing::init_test_tracing();
let tasks = TaskManager::current();
let test_suite = TestSuite::new();

// spin up alphanet node
let chain_spec = Lazy::force(&DEV).clone();
let node_config = NodeConfig::test()
.dev()
.with_chain(chain_spec)
.with_rpc(RpcServerArgs::default().with_unused_ports().with_http());
let NodeHandle { node, .. } = NodeBuilder::new(node_config)
.testing_node(tasks.executor())
.node(AlphaNetNode::default())
.launch()
.await
.unwrap();

let rpc_url = node.rpc_server_handle().http_url().unwrap();
let deployer = test_suite.signer();
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(deployer)
.on_http(Url::parse(&rpc_url).unwrap());
let base_fee = provider.get_gas_price().await.unwrap();

// Deploy invoker contract.
let invoker_builder = GasSponsorInvoker::deploy_builder(&provider);
let estimate = invoker_builder.estimate_gas().await.unwrap();
let invoker_address =
invoker_builder.gas(estimate).gas_price(base_fee).nonce(0).deploy().await.unwrap();
let invoker = GasSponsorInvoker::new(invoker_address, &provider);

// signer account.
let signer_wallet = Wallet::new(1);
let signer_account: EthereumWallet = signer_wallet.clone().into();
let signer_address = signer_account.default_signer().address();
let start_signer_balance = provider.get_balance(signer_address).await.unwrap();
assert!(start_signer_balance.ne(&U256::ZERO));
let signer_nonce = provider.get_transaction_count(signer_address).await.unwrap();

let data = reth_primitives::Bytes::new();

// receiver account
let receiver_wallet = Wallet::random();
let receiver_account: EthereumWallet = receiver_wallet.clone().into();
let receiver_address = receiver_account.default_signer().address();
let receiver_balance = provider.get_balance(receiver_address).await.unwrap();
assert_eq!(receiver_balance, U256::ZERO);

// commit, digest and signature.
let commit = keccak256((receiver_address, data.clone()).abi_encode_sequence());
let GasSponsorInvoker::getDigestReturn { digest } =
invoker.getDigest(commit, U256::from(signer_nonce)).call().await.unwrap();
let (v, r, s) = signer_wallet.sign_hash(digest).await;

let amount = U256::from_str_radix("1000000000000000000", 10).unwrap(); // 1 ETH

let builder =
invoker.sponsorCall(signer_address, v, r.into(), s.into(), receiver_address, data, amount);
let estimate = builder.estimate_gas().await.unwrap();
let receipt = builder
.gas(estimate)
.gas_price(base_fee)
.nonce(1)
.send()
.await
.unwrap()
.get_receipt()
.await;
assert!(receipt.is_ok());

let receiver_balance = provider.get_balance(receiver_address).await.unwrap();
assert_eq!(receiver_balance, amount);
let end_signer_balance = provider.get_balance(signer_address).await.unwrap();
assert!(end_signer_balance.eq(&start_signer_balance.saturating_sub(amount)));
}

#[tokio::test]
async fn test_bls12_381_g1_add() {
reth_tracing::init_test_tracing();
Expand Down
12 changes: 0 additions & 12 deletions crates/testing/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use alloy::signers::Signer;
use alloy_network::EthereumWallet;
use alloy_signer_local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner};
use reth_primitives::{B256, U256};

/// Mnemonic used to derive the test accounts
const TEST_MNEMONIC: &str = "test test test test test test test test test test test junk";
Expand Down Expand Up @@ -29,16 +27,6 @@ impl Wallet {
.unwrap();
Self { inner }
}

pub(crate) fn random() -> Self {
let inner = MnemonicBuilder::<English>::default().build_random().unwrap();
Self { inner }
}

pub(crate) async fn sign_hash(&self, message: B256) -> (u8, U256, U256) {
let signature = self.inner.sign_hash(&message).await.unwrap();
(signature.v().y_parity_byte() + 27, signature.r(), signature.s())
}
}
impl From<Wallet> for EthereumWallet {
fn from(val: Wallet) -> Self {
Expand Down

0 comments on commit de8db8a

Please sign in to comment.