diff --git a/crates/client/data-availability/src/bitcoin/mod.rs b/crates/client/data-availability/src/bitcoin/mod.rs index d0d9df0f7b..96c900e3b7 100644 --- a/crates/client/data-availability/src/bitcoin/mod.rs +++ b/crates/client/data-availability/src/bitcoin/mod.rs @@ -4,15 +4,7 @@ pub mod config; use anyhow::Result; use async_trait::async_trait; -use bitcoin_da::Config as BitcoinDAConfig; -// use bitcoin::hash_types::Txid; -// use bitcoin::key::{PrivateKey, PublicKey}; -// use bitcoin::script::PushBytesBuf; -// use bitcoin::secp256k1::{All, KeyPair, Secp256k1, SecretKey, XOnlyPublicKey}; -// use bitcoin::taproot::{LeafVersion, NodeInfo, TapTree, TaprootBuilder}; -// use bitcoin::{opcodes, script as txscript, sighash, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, -// Witness}; -use bitcoin_da::Relayer; +use bitcoin_da::{Config as BitcoinDAConfig, Relayer}; use bitcoincore_rpc::bitcoincore_rpc_json::{GetTransactionResultDetailCategory, ListTransactionResult}; // Bitcoincore RPC imports use bitcoincore_rpc::RpcApi; @@ -21,7 +13,7 @@ use ethers::types::{I256, U256}; use crate::utils::is_valid_http_endpoint; use crate::{DaClient, DaMode}; -// #[derive(Clone, Debug)] +// #[derive(Clone)] pub struct BitcoinClient { relayer: Relayer, mode: DaMode, @@ -59,27 +51,21 @@ impl DaClient for BitcoinClient { last_tx.iter().filter(|tx| tx.detail.category == GetTransactionResultDetailCategory::Send).collect(); filtered_txs.sort_by(|a, b| a.info.blockheight.cmp(&b.info.blockheight)); let most_recent_tx = filtered_txs.last(); - let most_recent_block_hash = match most_recent_tx.map_or(None, |tx| tx.info.blockhash) { - None => return Err(anyhow::anyhow!("No transactions found")), - Some(hash) => hash, - }; - let txid = match most_recent_tx { - None => return Err(anyhow::anyhow!("No transactions found")), - Some(tx) => Some(tx.info.txid), - }; - + let last_data_raw = match most_recent_tx { - Some(tx) => self.relayer.read_transaction(&tx.info.txid, Some(&most_recent_block_hash)) - .map_err(|e| anyhow::anyhow!("bitcoin read err: {e}"))?, + Some(tx) => self + .relayer + .read_transaction(&tx.info.txid, tx.info.blockhash.as_ref()) + .map_err(|e| anyhow::anyhow!("bitcoin read err: {e}"))?, None => return Err(anyhow::anyhow!("No transactions found")), }; - + // change to rollup height Ok(I256::from(1)) } fn get_mode(&self) -> DaMode { - self.mode + self.mode.clone() } } @@ -89,14 +75,9 @@ impl BitcoinClient { return Err(format!("invalid http endpoint, received {}", &conf.host)); } - let bitcoin_da_conf: BitcoinDAConfig = BitcoinDAConfig { - host: conf.host, - user: conf.user, - pass: conf.pass, - }; + let bitcoin_da_conf: BitcoinDAConfig = BitcoinDAConfig { host: conf.host, user: conf.user, pass: conf.pass }; - let client: Relayer = - Relayer::new(&bitcoin_da_conf).map_err(|e| format!("bitcoin new relayer err: {e}"))?; + let client: Relayer = Relayer::new(&bitcoin_da_conf).map_err(|e| format!("bitcoin new relayer err: {e}"))?; Ok(Self { relayer: client, mode: conf.mode }) } diff --git a/crates/node/src/service.rs b/crates/node/src/service.rs index 0ea8e8eaa7..fc47bba53d 100644 --- a/crates/node/src/service.rs +++ b/crates/node/src/service.rs @@ -11,6 +11,8 @@ use futures::prelude::*; use madara_runtime::opaque::Block; use madara_runtime::{self, Hash, RuntimeApi}; use mc_block_proposer::ProposerFactory; +use mc_data_availability::bitcoin::config::BitcoinConfig; +use mc_data_availability::bitcoin::BitcoinClient; use mc_data_availability::celestia::config::CelestiaConfig; use mc_data_availability::celestia::CelestiaClient; use mc_data_availability::ethereum::config::EthereumConfig; @@ -415,15 +417,17 @@ pub fn new_full( DaLayer::Bitcoin => { let bitcoin_conf = BitcoinConfig::try_from_file(&da_path)?; let da_client = BitcoinClient::try_from_config(bitcoin_conf.clone())?; + let mode = bitcoin_conf.mode.clone(); + task_manager.spawn_essential_handle().spawn( "da-worker-update", Some("madara"), - DataAvailabilityWorker::update_state(da_client.clone(), client.clone(), madara_backend.clone()), + DataAvailabilityWorker::update_state(da_client, client.clone(), madara_backend.clone()), ); task_manager.spawn_essential_handle().spawn( "da-worker-prove", Some("madara"), - DataAvailabilityWorker::prove_current_block(da_client.get_mode(), client.clone(), madara_backend), + DataAvailabilityWorker::prove_current_block(mode, client.clone(), madara_backend), ); } } diff --git a/crates/pallets/starknet/src/barknet.rs b/crates/pallets/starknet/src/barknet.rs deleted file mode 100644 index 1b0c07bf53..0000000000 --- a/crates/pallets/starknet/src/barknet.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::io::Write; -use reqwest::{Client, HeaderMap, StatusCode, header::CONTENT_TYPE}; -use serde_json::json; -use flate2::{write::GzEncoder, Compression}; -use prost::Message; - -#[derive(Clone, PartialEq, prost::Message)] -pub struct bitcoin_data { - #[prost(bytes, tag = "1")] - pub protocol_id: std::vec::Vec, - #[prost(bytes, tag = "2")] - pub data: std::vec::Vec, -} - -async fn fetch_state() -> Result { - let client = Client::new(); - let mut headers = HeaderMap::new(); - headers.insert(CONTENT_TYPE, "application/json".parse().unwrap()); - headers.insert("accept", "application/json".parse().unwrap()); - - let url = "http://localhost:9944/"; - let payload = json!({ - "id": 1, - "jsonrpc": "2.0", - "method": "starknet_getBlockWithTxHashes", - "params": ["latest"] - }); - - let response = client.post(url) - .headers(headers) - .json(&payload) - .send().await?; - - if response.status().is_success() { - println!("Fetching state succeeded"); - } else { - println!("Fetching state failed with status: {}", response.status()); - } - - Ok(response.status()) -} - -fn compress_state(data: &Vec) -> Vec { - let mut encoder = GzEncoder::new(Vec::new(), Compression::default()); - encoder.write_all(data).expect("Failed to write data for compression"); - encoder.finish().expect("Failed to finish compression") -} - -fn serialize_state(data: &Vec) -> Result, &'static str> { - let bitcoin_data = bitcoin_data { - protocol_id: PROTOCOL_ID.to_vec(), - data: data.clone(), - }; - - let mut serialized_data = vec![]; - bitcoin_data.encode(&mut serialized_data).map_err(|_| "Failed to serialize data using protobuf")?; - - Ok(serialized_data) -} - -fn push_state(relayer: &Relayer) -> Result<(), &'static str> { - let response = fetch_state()?; - let compressed_data = compress_state(&response); - let serialized_data = serialize_state(&compressed_data)?; - - match relayer.write(&serialized_data) { - Ok(_) => Ok(()), - Err(_) => Err("Failed to write data to Bitcoin") - } -} diff --git a/crates/pallets/starknet/src/lib.rs b/crates/pallets/starknet/src/lib.rs index 4d979e954d..f2a5c24601 100644 --- a/crates/pallets/starknet/src/lib.rs +++ b/crates/pallets/starknet/src/lib.rs @@ -191,10 +191,6 @@ pub mod pallet { >::store_block(UniqueSaturatedInto::::unique_saturated_into( frame_system::Pallet::::block_number(), )); - - // Bitcoin-da implementation - - } /// The block is being initialized. Implement to have something happen.