Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

l1Blocknum #6

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions matching_engine/src/jobs/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct LogParser {
shared_cost_store: Arc<RwLock<CostStore>>,
chain_id: String,
max_tasks_size: usize,
rpc_url: String,
}

impl LogParser {
Expand Down Expand Up @@ -109,6 +110,7 @@ impl LogParser {
shared_cost_store,
chain_id,
max_tasks_size: 10, // TODO: dynamically adjust latter
rpc_url,
}
}

Expand Down Expand Up @@ -198,6 +200,7 @@ impl LogParser {
&self.shared_cost_store,
&self.matching_engine_key,
&self.matching_engine_slave_keys,
&self.rpc_url,
)
.await
.unwrap();
Expand All @@ -209,6 +212,7 @@ impl LogParser {
log,
&self.generator_registry,
&self.shared_generator_store,
&self.rpc_url,
)
.await
.unwrap();
Expand Down
17 changes: 14 additions & 3 deletions matching_engine/src/log_processor/gr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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,
genertor_registry: &bindings::generator_registry::GeneratorRegistry<
SignerMiddleware<Provider<Http>, Wallet<SigningKey>>,
>,
generator_store: &Arc<RwLock<generator_store::GeneratorStore>>,
rpc_url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
if constants::GENERATOR_REGISTRY_TOPICS_SKIP
.get(&log.topics[0])
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down
29 changes: 20 additions & 9 deletions matching_engine/src/log_processor/pm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@ pub async fn process_proof_market_place_logs(
cost_store: &Arc<RwLock<CostStore>>,
matching_engine_key: &[u8],
matchin_engine_slave_keys: &Vec<Vec<u8>>,
rpc_url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
if constants::PROOF_MARKET_TOPICS_SKIP
.get(&log.topics[0])
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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) = {
Expand All @@ -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,
Expand All @@ -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(),
);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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()),
);
}
}
Expand Down Expand Up @@ -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()));

Expand All @@ -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");
Expand Down
43 changes: 41 additions & 2 deletions matching_engine/src/utility.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<U256, Box<dyn Error>> {
// Connect to Arbitrum's L2 endpoint
let provider = Provider::<Http>::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::<OtherAddress>()?;

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";
Expand Down
Loading