From d696e1ddce533a697c47569ca6de2a591066a8e4 Mon Sep 17 00:00:00 2001 From: voidash Date: Wed, 18 Sep 2024 23:30:33 +0545 Subject: [PATCH] reader, state: remove rollupparams to use RelevantTxType --- crates/btcio/src/reader/deposit.rs | 16 ++++-- crates/btcio/src/reader/filter.rs | 57 +++++-------------- crates/btcio/src/reader/query.rs | 20 +++---- crates/chaintsn/src/transition.rs | 2 +- .../src/duty/block_assembly.rs | 15 +---- crates/db/Cargo.toml | 2 +- crates/primitives/Cargo.toml | 2 +- 7 files changed, 36 insertions(+), 78 deletions(-) diff --git a/crates/btcio/src/reader/deposit.rs b/crates/btcio/src/reader/deposit.rs index db7a7c909..d2a63c316 100644 --- a/crates/btcio/src/reader/deposit.rs +++ b/crates/btcio/src/reader/deposit.rs @@ -35,7 +35,7 @@ pub struct DepositInfo { /// outpoint where amount is present pub deposit_outpoint: u32, - /// evm address + /// EE address pub address: Vec, } @@ -46,13 +46,21 @@ pub struct DepositTxConfig { } impl DepositTxConfig { - pub fn new(params: &RollupParams) -> Self { + pub fn from_params(params: &RollupParams) -> Self { Self { magic_bytes: params.rollup_name.clone().into_bytes().to_vec(), address_length: params.deposit_address_length, deposit_quantity: params.deposit_quantity, } } + + pub fn new(magic_bytes: &[u8], addr_len: u8, amount: u64) -> Self { + Self { + magic_bytes: magic_bytes.to_vec(), + address_length: addr_len, + deposit_quantity: amount, + } + } } /// Extracts the DepositInfo from the Deposit Transaction @@ -60,7 +68,6 @@ pub fn extract_deposit_info( tx: &Transaction, config: &DepositTxConfig, ) -> Result { - println!("the transaction is {:?}", tx); for output in tx.output.iter() { let mut instructions = output.script_pubkey.instructions(); @@ -71,7 +78,6 @@ pub fn extract_deposit_info( // magic bytes if let Some(Ok(Instruction::PushBytes(magic_bytes))) = instructions.next() { - println!("{:?}", String::from_utf8(magic_bytes.as_bytes().to_vec())); if magic_bytes.as_bytes() != config.magic_bytes { return Err(DepositParseError::MagicBytesMismatch( magic_bytes.as_bytes().to_vec(), @@ -195,8 +201,6 @@ mod tests { let out = extract_deposit_info(&test_transaction, &get_deposit_tx_config()); - println!("{:?}", out); - assert!(out.is_ok()); let out = out.unwrap(); diff --git a/crates/btcio/src/reader/filter.rs b/crates/btcio/src/reader/filter.rs index 05b417885..ea7e3b2c0 100644 --- a/crates/btcio/src/reader/filter.rs +++ b/crates/btcio/src/reader/filter.rs @@ -1,4 +1,3 @@ -use alpen_express_primitives::params::RollupParams; use bitcoin::{Address, Block, Transaction}; use super::deposit::{extract_deposit_info, DepositTxConfig}; @@ -12,31 +11,27 @@ pub enum RelevantTxType { /// Inscription transactions with given Rollup name. This will be parsed by /// [`InscriptionParser`] which dictates the structure of inscription. RollupInscription(RollupName), - // Add other relevant conditions as needed - Deposit, + /// Deposit transaction + Deposit(RollupName, AddressLength, Amount), } type RollupName = String; +type AddressLength = u8; +type Amount = u64; /// Filter all the relevant [`Transaction`]s in a block based on given [`RelevantTxType`]s -pub fn filter_relevant_txs( - block: &Block, - relevent_types: &[RelevantTxType], - params: &RollupParams, -) -> Vec { - println!("the block tx is {:?}", block.txdata); - +pub fn filter_relevant_txs(block: &Block, relevent_types: &[RelevantTxType]) -> Vec { block .txdata .iter() .enumerate() - .filter(|(_, tx)| is_relevant(tx, relevent_types, params)) + .filter(|(_, tx)| is_relevant(tx, relevent_types)) .map(|(i, _)| i as u32) .collect() } /// Determines if a [`Transaction`] is relevant based on given [`RelevantTxType`]s -fn is_relevant(tx: &Transaction, relevant_types: &[RelevantTxType], params: &RollupParams) -> bool { +fn is_relevant(tx: &Transaction, relevant_types: &[RelevantTxType]) -> bool { relevant_types.iter().any(|rel_type| match rel_type { RelevantTxType::SpentToAddress(address) => tx .output @@ -56,10 +51,9 @@ fn is_relevant(tx: &Transaction, relevant_types: &[RelevantTxType], params: &Rol .is_some() } }, - RelevantTxType::Deposit => { - let config = DepositTxConfig::new(params); + RelevantTxType::Deposit(rollup_name, addr_len, amt) => { + let config = DepositTxConfig::new(rollup_name.as_bytes(), *addr_len, *amt); let deposit_info = extract_deposit_info(tx, &config); - println!("{:?}", deposit_info); deposit_info.is_ok() } @@ -70,7 +64,6 @@ fn is_relevant(tx: &Transaction, relevant_types: &[RelevantTxType], params: &Rol mod test { use std::str::FromStr; - use alpen_test_utils::l2::gen_params; use bitcoin::{ absolute::{Height, LockTime}, block::{Header, Version as BVersion}, @@ -137,13 +130,8 @@ mod test { let tx1 = create_test_tx(vec![create_test_txout(100, &address)]); let tx2 = create_test_tx(vec![create_test_txout(100, &parse_addr(OTHER_ADDR))]); let block = create_test_block(vec![tx1, tx2]); - let params = gen_params(); - let result = filter_relevant_txs( - &block, - &[RelevantTxType::SpentToAddress(address)], - params.rollup(), - ); + let result = filter_relevant_txs(&block, &[RelevantTxType::SpentToAddress(address)]); assert_eq!(result, vec![0]); // Only tx1 matches } @@ -183,25 +171,18 @@ mod test { let rollup_name = "TestRollup".to_string(); let tx = create_inscription_tx(rollup_name.clone()); let block = create_test_block(vec![tx]); - let params = gen_params(); - let result = filter_relevant_txs( - &block, - &[RelevantTxType::RollupInscription(rollup_name)], - params.rollup(), - ); + let result = filter_relevant_txs(&block, &[RelevantTxType::RollupInscription(rollup_name)]); assert_eq!(result, vec![0], "Should filter valid rollup name"); // Test with invalid name let rollup_name = "TestRollup".to_string(); let tx = create_inscription_tx(rollup_name.clone()); let block = create_test_block(vec![tx]); - let params = gen_params(); let result = filter_relevant_txs( &block, &[RelevantTxType::RollupInscription( "invalid_name".to_string(), )], - params.rollup(), ); assert!(result.is_empty(), "Should filter out invalid name"); } @@ -212,25 +193,18 @@ mod test { let tx1 = create_test_tx(vec![create_test_txout(1000, &parse_addr(OTHER_ADDR))]); let tx2 = create_test_tx(vec![create_test_txout(10000, &parse_addr(OTHER_ADDR))]); let block = create_test_block(vec![tx1, tx2]); - let params = gen_params(); - let result = filter_relevant_txs( - &block, - &[RelevantTxType::SpentToAddress(address)], - params.rollup(), - ); + let result = filter_relevant_txs(&block, &[RelevantTxType::SpentToAddress(address)]); assert!(result.is_empty()); // No transactions match } #[test] fn test_filter_relevant_txs_empty_block() { let block = create_test_block(vec![]); - let params = gen_params(); let result = filter_relevant_txs( &block, &[RelevantTxType::SpentToAddress(parse_addr(RELEVANT_ADDR))], - params.rollup(), ); assert!(result.is_empty()); // No transactions match } @@ -242,13 +216,8 @@ mod test { let tx2 = create_test_tx(vec![create_test_txout(100, &parse_addr(OTHER_ADDR))]); let tx3 = create_test_tx(vec![create_test_txout(1000, &address)]); let block = create_test_block(vec![tx1, tx2, tx3]); - let params = gen_params(); - let result = filter_relevant_txs( - &block, - &[RelevantTxType::SpentToAddress(address)], - params.rollup(), - ); + let result = filter_relevant_txs(&block, &[RelevantTxType::SpentToAddress(address)]); assert_eq!(result, vec![0, 2]); // First and third txs match } } diff --git a/crates/btcio/src/reader/query.rs b/crates/btcio/src/reader/query.rs index fa1e44aef..8814dc08f 100644 --- a/crates/btcio/src/reader/query.rs +++ b/crates/btcio/src/reader/query.rs @@ -5,7 +5,7 @@ use std::{ }; use alpen_express_db::traits::Database; -use alpen_express_primitives::params::{Params, RollupParams}; +use alpen_express_primitives::params::Params; use alpen_express_status::StatusTx; use anyhow::bail; use bitcoin::BlockHash; @@ -84,7 +84,6 @@ async fn do_reader_task( &relevant_tx_types, &mut state, &mut status_updates, - params.rollup(), ) .instrument(poll_span) .await @@ -123,9 +122,14 @@ fn derive_relevant_tx_types( ) -> anyhow::Result> { // TODO: Figure out how to do it from chainstate provider // For now we'll just go with filtering Inscription transactions + let rollup = params.rollup(); Ok(vec![ - RelevantTxType::RollupInscription(params.rollup().rollup_name.clone()), - RelevantTxType::Deposit, + RelevantTxType::RollupInscription(rollup.rollup_name.clone()), + RelevantTxType::Deposit( + rollup.rollup_name.clone(), + rollup.deposit_address_length, + rollup.deposit_quantity, + ), ]) } @@ -168,7 +172,6 @@ async fn poll_for_new_blocks( relevant_tx_types: &[RelevantTxType], state: &mut ReaderState, status_updates: &mut Vec, - params: &RollupParams, ) -> anyhow::Result<()> { let chain_info = client.get_blockchain_info().await?; status_updates.push(L1StatusUpdate::RpcConnected(true)); @@ -203,8 +206,6 @@ async fn poll_for_new_blocks( // Now process each block we missed. let scan_start_height = state.next_height(); - println!("the scan height is {}", scan_start_height); - println!("the client height is {}", client_height); for fetch_height in scan_start_height..=client_height { let l1blkid = match fetch_and_process_block( fetch_height, @@ -213,7 +214,6 @@ async fn poll_for_new_blocks( state, status_updates, relevant_tx_types, - params, ) .await { @@ -258,13 +258,11 @@ async fn fetch_and_process_block( state: &mut ReaderState, status_updates: &mut Vec, relevant_tx_types: &[RelevantTxType], - params: &RollupParams, ) -> anyhow::Result { let block = client.get_block_at(height).await?; let txs = block.txdata.len(); - let filtered_txs = filter_relevant_txs(&block, relevant_tx_types, params); - println!("we have the filtered transaction as {:?}", filtered_txs); + let filtered_txs = filter_relevant_txs(&block, relevant_tx_types); let block_data = BlockData::new(height, block, filtered_txs); let l1blkid = block_data.block().block_hash(); trace!(%l1blkid, %height, %txs, "fetched block from client"); diff --git a/crates/chaintsn/src/transition.rs b/crates/chaintsn/src/transition.rs index 3c93012f1..99a426d62 100644 --- a/crates/chaintsn/src/transition.rs +++ b/crates/chaintsn/src/transition.rs @@ -159,7 +159,7 @@ fn process_deposit_transaction( let raw_tx = tx.tx().tx_data(); let constructed_tx = bitcoin::consensus::deserialize::(raw_tx).unwrap(); // convert raw data to DepositTx which is wrapper for Deposit Transaction - let deposit_tx_config = DepositTxConfig::new(params); + let deposit_tx_config = DepositTxConfig::from_params(params); let deposit_info = extract_deposit_info(&constructed_tx, &deposit_tx_config).ok(); if let Some(info) = deposit_info { diff --git a/crates/consensus-logic/src/duty/block_assembly.rs b/crates/consensus-logic/src/duty/block_assembly.rs index aa807082d..4be0525be 100644 --- a/crates/consensus-logic/src/duty/block_assembly.rs +++ b/crates/consensus-logic/src/duty/block_assembly.rs @@ -161,9 +161,6 @@ fn prepare_l1_segment( for (h, _b) in unacc_blocks { let rec = load_header_record(h, l1_prov)?; let deposit_update_tx = extract_deposit_update_tx(h, l1_prov, params)?; - if deposit_update_tx.is_empty() { - println!("got ourselves a deposit tx {:?}", deposit_update_tx); - } payloads.push(L1HeaderPayload::new_bare(h, rec, &deposit_update_tx)); } @@ -183,8 +180,6 @@ fn prepare_l1_segment( // at a reorg exactly on the transition block trace!("computing pivot"); - println!("{:?}", unacc_blocks); - println!("{:?}", maturing_blocks); let Some((pivot_h, _pivot_id)) = find_pivot_block_height(&unacc_blocks, &maturing_blocks) else { // Then we're really screwed. @@ -201,11 +196,7 @@ fn prepare_l1_segment( let mut payloads = Vec::new(); for (h, _b) in fresh_blocks { let rec = load_header_record(*h, l1_prov)?; - println!("working for the height {}", h); let deposit_update_tx = extract_deposit_update_tx(*h, l1_prov, params)?; - if deposit_update_tx.is_empty() { - println!("got ourselves a deposit tx {:?}", deposit_update_tx); - } payloads.push(L1HeaderPayload::new_bare(*h, rec, &deposit_update_tx)); } @@ -233,8 +224,6 @@ fn extract_deposit_update_tx( .get_block_txs(h)? .ok_or(Error::MissingL1BlockHeight(h))?; - println!("got tx ref {:?}", relevant_tx_ref); - let mut deposit_update_tx = Vec::new(); for tx_ref in relevant_tx_ref { let tx = l1_prov.get_tx(tx_ref)?.ok_or(Error::MissingL1Tx)?; @@ -243,9 +232,7 @@ fn extract_deposit_update_tx( let txn = bitcoin::consensus::deserialize::(tx.tx_data()) .map_err(|e| Error::TxDeserializationFailed(h))?; - println!("got txn {:?}", txn); - - let deposit_tx_config: DepositTxConfig = DepositTxConfig::new(params.rollup()); + let deposit_tx_config: DepositTxConfig = DepositTxConfig::from_params(params.rollup()); let deposit_parser = extract_deposit_info(&txn, &deposit_tx_config).ok(); if deposit_parser.is_some() { diff --git a/crates/db/Cargo.toml b/crates/db/Cargo.toml index 4992ac372..4be06a021 100644 --- a/crates/db/Cargo.toml +++ b/crates/db/Cargo.toml @@ -26,5 +26,5 @@ mocks = ["mockall"] stubs = [] [dev-dependencies] -serde_json = { workspace = true } alpen-test-utils = { workspace = true } +serde_json = { workspace = true } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 9836d4f85..3edb9da72 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -23,5 +23,5 @@ alpen-test-utils = { workspace = true } [features] default = ["std", "rand"] -std = ["dep:secp256k1"] rand = ["std", "dep:rand"] +std = ["dep:secp256k1"]