From f49b335863903291edb5fd6b643e7f78741d52f1 Mon Sep 17 00:00:00 2001 From: Aniket Baliyan Date: Thu, 7 Nov 2024 12:23:48 +0530 Subject: [PATCH] l1Blocknum --- matching_engine/src/jobs/parser.rs | 4 +++ matching_engine/src/log_processor/gr.rs | 17 ++++++++-- matching_engine/src/log_processor/pm.rs | 29 +++++++++++------ matching_engine/src/utility.rs | 43 +++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 14 deletions(-) diff --git a/matching_engine/src/jobs/parser.rs b/matching_engine/src/jobs/parser.rs index 01a6641..3b18f0d 100644 --- a/matching_engine/src/jobs/parser.rs +++ b/matching_engine/src/jobs/parser.rs @@ -60,6 +60,7 @@ pub struct LogParser { shared_cost_store: Arc>, chain_id: String, max_tasks_size: usize, + rpc_url: String, } impl LogParser { @@ -109,6 +110,7 @@ impl LogParser { shared_cost_store, chain_id, max_tasks_size: 10, // TODO: dynamically adjust latter + rpc_url, } } @@ -198,6 +200,7 @@ impl LogParser { &self.shared_cost_store, &self.matching_engine_key, &self.matching_engine_slave_keys, + &self.rpc_url, ) .await .unwrap(); @@ -209,6 +212,7 @@ impl LogParser { log, &self.generator_registry, &self.shared_generator_store, + &self.rpc_url, ) .await .unwrap(); diff --git a/matching_engine/src/log_processor/gr.rs b/matching_engine/src/log_processor/gr.rs index 0af1e12..ff1f612 100644 --- a/matching_engine/src/log_processor/gr.rs +++ b/matching_engine/src/log_processor/gr.rs @@ -5,6 +5,7 @@ use tokio::sync::RwLock; use crate::generator_lib::*; use crate::log_processor::constants; use crate::utility::{tx_to_string, TokenTracker, TEST_TOKEN_ADDRESS_ONE}; +use crate::utility::get_l1_block_from_l2_block; pub async fn process_generator_registry_logs( log: &Log, @@ -12,6 +13,7 @@ pub async fn process_generator_registry_logs( SignerMiddleware, Wallet>, >, generator_store: &Arc>, + rpc_url: &str, ) -> Result<(), Box> { if constants::GENERATOR_REGISTRY_TOPICS_SKIP .get(&log.topics[0]) @@ -216,11 +218,14 @@ pub async fn process_generator_registry_logs( let address = added_stake_log.generator; let amount = added_stake_log.amount; + let block_l2: U256 = log.block_number.unwrap().as_u64().into(); + let block_l1: U256 = get_l1_block_from_l2_block(rpc_url, block_l2).await?; + generator_store.add_extra_stake( &address, &TEST_TOKEN_ADDRESS_ONE, &amount, - log.block_number.unwrap(), + U64::from(block_l1.as_u64()), log.transaction_index.unwrap(), log.log_index.unwrap(), tx_to_string(&log.transaction_hash.unwrap()), @@ -263,11 +268,14 @@ pub async fn process_generator_registry_logs( let address = remove_stake_log.generator; let amount = remove_stake_log.amount; + let block_l2: U256 = log.block_number.unwrap().as_u64().into(); + let block_l1: U256 = get_l1_block_from_l2_block(rpc_url, block_l2).await?; + generator_store.remove_stake( &address, &TEST_TOKEN_ADDRESS_ONE, &amount, - log.block_number.unwrap(), + U64::from(block_l1.as_u64()), log.transaction_index.unwrap(), log.log_index.unwrap(), tx_to_string(&log.transaction_hash.unwrap()), @@ -402,11 +410,14 @@ pub async fn process_generator_registry_logs( let address = stake_slash_logs.generator; let stake_slashed = stake_slash_logs.stake; + let block_l2: U256 = log.block_number.unwrap().as_u64().into(); + let block_l1: U256 = get_l1_block_from_l2_block(rpc_url, block_l2).await?; + generator_store.remove_stake( &address, &TEST_TOKEN_ADDRESS_ONE, &stake_slashed, - log.block_number.unwrap(), + U64::from(block_l1.as_u64()), log.transaction_index.unwrap(), log.log_index.unwrap(), tx_to_string(&log.transaction_hash.unwrap()), diff --git a/matching_engine/src/log_processor/pm.rs b/matching_engine/src/log_processor/pm.rs index 111761a..1977a6c 100644 --- a/matching_engine/src/log_processor/pm.rs +++ b/matching_engine/src/log_processor/pm.rs @@ -5,6 +5,7 @@ use crate::costs::CostStore; use crate::utility::tx_to_string; use crate::utility::TokenTracker; use crate::utility::TEST_TOKEN_ADDRESS_ONE; +use crate::utility::get_l1_block_from_l2_block; use ethers::prelude::{k256::ecdsa::SigningKey, *}; use std::sync::Arc; @@ -30,6 +31,7 @@ pub async fn process_proof_market_place_logs( cost_store: &Arc>, matching_engine_key: &[u8], matchin_engine_slave_keys: &Vec>, + rpc_url: &str, ) -> Result<(), Box> { if constants::PROOF_MARKET_TOPICS_SKIP .get(&log.topics[0]) @@ -94,7 +96,9 @@ pub async fn process_proof_market_place_logs( .call() .await .unwrap(); + let created_on: U256 = log.block_number.unwrap().as_u64().into(); + let created_on_l1: U256 = get_l1_block_from_l2_block(rpc_url, created_on).await?; let mut ask_to_store = LocalAsk { ask_id: parsed_ask_created_log.ask_id, @@ -110,7 +114,7 @@ pub async fn process_proof_market_place_logs( state: Some(AskState::Create), generator: None, invalid_secret_flag: false, - created_on, + created_on: created_on_l1, }; if parsed_ask_created_log.has_private_inputs { @@ -208,7 +212,9 @@ pub async fn process_proof_market_place_logs( let proof = parsed_proof_created_log.proof; let proof_cycle_completed_on: U256 = log.block_number.unwrap().as_u64().into(); - local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on); + let proof_cycle_completed_on_l1: U256 = get_l1_block_from_l2_block(rpc_url, proof_cycle_completed_on).await?; + + local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on_l1); local_ask_store.modify_state(&ask_id, AskState::Complete); let (generator_address, market_id) = { @@ -227,7 +233,7 @@ pub async fn process_proof_market_place_logs( }); let created_on = { local_ask_store.get_by_ask_id(&ask_id).unwrap().created_on }; - let proof_time = proof_cycle_completed_on.saturating_sub(created_on); + let proof_time = proof_cycle_completed_on_l1.saturating_sub(created_on); local_ask_store.store_valid_proof( &ask_id, @@ -243,7 +249,7 @@ pub async fn process_proof_market_place_logs( &generator_address, &market_id, &proof_generator_cost, - &proof_cycle_completed_on.as_u64().into(), + &proof_cycle_completed_on_l1.as_u64().into(), ); } @@ -372,9 +378,11 @@ pub async fn process_proof_market_place_logs( let mut local_ask_store = { local_ask_store.write().await }; let proof_cycle_completed_on: U256 = log.block_number.unwrap().as_u64().into(); + let proof_cycle_completed_on_l1: U256 = get_l1_block_from_l2_block(rpc_url, proof_cycle_completed_on).await?; + local_ask_store.update_proof_proof_cycle_completed_on( &ask_cancelled_log.ask_id, - proof_cycle_completed_on, + proof_cycle_completed_on_l1, ); local_ask_store.modify_state(&ask_cancelled_log.ask_id, AskState::Complete); local_ask_store.remove_ask_only_if_completed(&ask_cancelled_log.ask_id); @@ -402,7 +410,9 @@ pub async fn process_proof_market_place_logs( let mut local_ask_store = { local_ask_store.write().await }; let proof_cycle_completed_on: U256 = log.block_number.unwrap().as_u64().into(); - local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on); + let proof_cycle_completed_on_l1: U256 = get_l1_block_from_l2_block(rpc_url, proof_cycle_completed_on).await?; + + local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on_l1); local_ask_store.modify_state(&ask_id, AskState::Complete); local_ask_store.note_proof_denied(&ask_id, tx_to_string(&log.transaction_hash.unwrap())); @@ -440,7 +450,7 @@ pub async fn process_proof_market_place_logs( tx_to_string(&log.transaction_hash.unwrap()), &ask.reward, &ask.deadline, - &log.block_number.unwrap_or_default(), + &U64::from(proof_cycle_completed_on_l1.as_u64()), ); } } @@ -471,7 +481,8 @@ pub async fn process_proof_market_place_logs( let mut local_ask_store = { local_ask_store.write().await }; let proof_cycle_completed_on: U256 = log.block_number.unwrap().as_u64().into(); - local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on); + let proof_cycle_completed_on_l1: U256 = get_l1_block_from_l2_block(rpc_url, proof_cycle_completed_on).await?; + local_ask_store.update_proof_proof_cycle_completed_on(&ask_id, proof_cycle_completed_on_l1); local_ask_store.modify_state(&ask_id, AskState::Complete); local_ask_store.note_invalid_inputs(&ask_id, tx_to_string(&log.transaction_hash.unwrap())); @@ -497,7 +508,7 @@ pub async fn process_proof_market_place_logs( &generator_address, &market_id, &proof_generator_cost, - &proof_cycle_completed_on.as_u64().into(), + &proof_cycle_completed_on_l1.as_u64().into(), ); } log::warn!("Complete: invalid input attestation event operation"); diff --git a/matching_engine/src/utility.rs b/matching_engine/src/utility.rs index 2901ee0..4473182 100644 --- a/matching_engine/src/utility.rs +++ b/matching_engine/src/utility.rs @@ -1,8 +1,9 @@ -use ethers::abi::{encode, Token}; +use ethers::prelude::*; +use ethers::abi::{encode, Token, AbiParser, Address}; use ethers::core::rand::seq::SliceRandom; use ethers::core::rand::{self, thread_rng}; use ethers::core::utils::hex::FromHex; -use ethers::types::{Address, Signature, SignatureError, H160, U256}; +use ethers::types::{Address as OtherAddress, Signature, SignatureError, H160, U256, TransactionRequest, Bytes}; use ethers::utils::keccak256; use hex::decode; use im::HashMap; @@ -357,6 +358,44 @@ impl AddAssign for TokenTracker { } } +pub async fn get_l1_block_from_l2_block( + rpc_url: &str, + l2_block_num: U256, +) -> Result> { + // Connect to Arbitrum's L2 endpoint + let provider = Provider::::try_from(rpc_url)?; + + // Define ABI for blockL1Num function + let abi = AbiParser::default().parse(&[ + "function blockL1Num(uint64 l2BlockNum) view returns (uint256)" + ])?; + + // Retrieve the function + let function = abi.function("blockL1Num")?; + + // Encode the data for the function call + let data = function.encode_input(&[Token::Uint(l2_block_num)])?; + + // NodeInterface special address for the call + let node_interface_address = "0x00000000000000000000000000000000000000c8".parse::()?; + + let data_bytes = Bytes::from(data); + + // Create a TransactionRequest + let tx_request = TransactionRequest::new() + .to(node_interface_address) + .data(data_bytes); + + // Call the function via the provider + let result = provider.call(&tx_request.into(), None).await?; + + // Decode the result + let decoded_result: U256 = function.decode_output(&result)?.get(0).cloned().unwrap().into_uint().unwrap(); + + Ok(decoded_result) +} + + pub const USDC_TOKEN_STRING: &str = "0x6543210965432109654321096543210965432109"; pub const TEST_TOKEN_ADDRESS_ONE_STRING: &str = "0x9999888899998888999988889999888899998888"; pub const TEST_TOKEN_ADDRESS_TWO_STRING: &str = "0x1234123412347777666655551234123476547654";