Skip to content

Commit

Permalink
eth_client: fix get_address_from_secret_key
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Nov 25, 2024
1 parent 45f8d3a commit a9fd832
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
28 changes: 13 additions & 15 deletions crates/l2/proposer/prover_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct ProverServer {
on_chain_proposer_address: Address,
verifier_address: Address,
verifier_private_key: SecretKey,
last_verified_block: u64,
}

/// Enum for the ProverServer <--> ProverClient Communication Protocol.
Expand Down Expand Up @@ -97,15 +96,6 @@ impl ProverServer {
let eth_client = EthClient::new(&eth_config.rpc_url);
let on_chain_proposer_address = committer_config.on_chain_proposer_address;

let last_verified_block =
EthClient::get_last_verified_block(&eth_client, on_chain_proposer_address).await?;

let last_verified_block = if last_verified_block == u64::MAX {
0
} else {
last_verified_block
};

Ok(Self {
ip: config.listen_ip,
port: config.listen_port,
Expand All @@ -114,7 +104,6 @@ impl ProverServer {
on_chain_proposer_address,
verifier_address: config.verifier_address,
verifier_private_key: config.verifier_private_key,
last_verified_block,
})
}

Expand Down Expand Up @@ -213,11 +202,21 @@ impl ProverServer {
async fn handle_connection(&mut self, mut stream: TcpStream) -> Result<(), ProverServerError> {
let buf_reader = BufReader::new(&stream);

let last_verified_block =
EthClient::get_last_verified_block(&self.eth_client, self.on_chain_proposer_address)
.await?;

let last_verified_block = if last_verified_block == u64::MAX {
0
} else {
last_verified_block
};

let data: Result<ProofData, _> = serde_json::de::from_reader(buf_reader);
match data {
Ok(ProofData::Request) => {
if let Err(e) = self
.handle_request(&mut stream, self.last_verified_block + 1)
.handle_request(&mut stream, last_verified_block + 1)
.await
{
warn!("Failed to handle request: {e}");
Expand All @@ -231,10 +230,9 @@ impl ProverServer {

self.handle_proof_submission(block_number, receipt).await?;

if block_number != (self.last_verified_block + 1) {
return Err(ProverServerError::Custom(format!("Prover Client submitted an invalid block_number: {block_number}. The last_proved_block is: {}", self.last_verified_block)));
if block_number != (last_verified_block + 1) {
return Err(ProverServerError::Custom(format!("Prover Client submitted an invalid block_number: {block_number}. The last_proved_block is: {}", last_verified_block)));
}
self.last_verified_block = block_number;
}
Err(e) => {
warn!("Failed to parse request: {e}");
Expand Down
14 changes: 11 additions & 3 deletions crates/l2/utils/eth_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,15 @@ impl EthClient {
tx: &mut EIP1559Transaction,
private_key: &SecretKey,
) -> Result<H256, EthClientError> {
let from = get_address_from_secret_key(private_key);
let from = get_address_from_secret_key(private_key).map_err(|e| {
EthClientError::Custom(format!("Failed to get_address_from_secret_key: {e}"))
})?;
// Sometimes the penalty is a 100%
// Increase max fee per gas by 110% (set it to 210% of the original)
self.bump_eip1559(tx, 1.1);
let wrapped_tx = &mut WrappedTransaction::EIP1559(tx.clone());
self.estimate_gas_for_wrapped_tx(wrapped_tx, from).await?;

if let WrappedTransaction::EIP1559(eip1559) = wrapped_tx {
tx.max_fee_per_gas = eip1559.max_fee_per_gas;
tx.max_priority_fee_per_gas = eip1559.max_fee_per_gas;
Expand All @@ -249,13 +252,16 @@ impl EthClient {
wrapped_tx: &mut WrappedEIP4844Transaction,
private_key: &SecretKey,
) -> Result<H256, EthClientError> {
let from = get_address_from_secret_key(private_key);
let from = get_address_from_secret_key(private_key).map_err(|e| {
EthClientError::Custom(format!("Failed to get_address_from_secret_key: {e}"))
})?;
// Sometimes the penalty is a 100%
// Increase max fee per gas by 110% (set it to 210% of the original)
self.bump_eip4844(wrapped_tx, 1.1);
let wrapped_eip4844 = &mut WrappedTransaction::EIP4844(wrapped_tx.clone());
self.estimate_gas_for_wrapped_tx(wrapped_eip4844, from)
.await?;

if let WrappedTransaction::EIP4844(eip4844) = wrapped_eip4844 {
wrapped_tx.tx.max_fee_per_gas = eip4844.tx.max_fee_per_gas;
wrapped_tx.tx.max_priority_fee_per_gas = eip4844.tx.max_fee_per_gas;
Expand Down Expand Up @@ -286,7 +292,9 @@ impl EthClient {
tx: &mut PrivilegedL2Transaction,
private_key: &SecretKey,
) -> Result<H256, EthClientError> {
let from = get_address_from_secret_key(private_key);
let from = get_address_from_secret_key(private_key).map_err(|e| {
EthClientError::Custom(format!("Failed to get_address_from_secret_key: {e}"))
})?;
// Sometimes the penalty is a 100%
// Increase max fee per gas by 110% (set it to 210% of the original)
self.bump_privileged_l2(tx, 1.1);
Expand Down
16 changes: 11 additions & 5 deletions crates/l2/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::array::TryFromSliceError;

use ethrex_core::Address;
use keccak_hash::{keccak, H256};
use secp256k1::SecretKey;
Expand All @@ -24,10 +26,14 @@ where
hex.serialize(serializer)
}

pub fn get_address_from_secret_key(secret_key: &SecretKey) -> Address {
let mut buffer = [0u8; 32];
let public_key = secret_key.public_key(secp256k1::SECP256K1).serialize();
buffer.copy_from_slice(&public_key[1..]);
pub fn get_address_from_secret_key(secret_key: &SecretKey) -> Result<Address, TryFromSliceError> {
let public_key = secret_key
.public_key(secp256k1::SECP256K1)
.serialize_uncompressed();
let hash = keccak(&public_key[1..]);

// Get the lat 20 bytes of the hash
let address_bytes: [u8; 20] = hash[12..32].try_into()?;

Address::from(keccak(buffer))
Ok(Address::from(address_bytes))
}

0 comments on commit a9fd832

Please sign in to comment.