Skip to content

Commit

Permalink
[bitcoin] Refactor bitcoin relayer to load ready l1 txs from pending …
Browse files Browse the repository at this point in the history
…block contract (#1948)
  • Loading branch information
jolestar authored Jun 20, 2024
1 parent 0e10f84 commit e794377
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 178 deletions.
75 changes: 66 additions & 9 deletions crates/rooch-relayer/src/actor/bitcoin_relayer.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use super::messages::{GetReadyL1BlockMessage, GetReadyL1TxsMessage, SyncTick};
use crate::actor::bitcoin_client_proxy::BitcoinClientProxy;
use crate::Relayer;
use anyhow::Result;
use async_trait::async_trait;
use bitcoin::hashes::Hash;
use bitcoin::Block;
use bitcoincore_rpc::bitcoincore_rpc_json::GetBlockHeaderResult;
use coerce::actor::{context::ActorContext, message::Handler, Actor};
use moveos_types::module_binding::MoveFunctionCaller;
use rooch_config::BitcoinRelayerConfig;
use rooch_executor::proxy::ExecutorProxy;
use rooch_types::{
bitcoin::BitcoinModule,
bitcoin::{pending_block::PendingBlockModule, BitcoinModule},
multichain_id::RoochMultiChainID,
transaction::{L1Block, L1BlockWithBody},
transaction::{L1Block, L1BlockWithBody, L1Transaction},
};
use tracing::{debug, info};
use tracing::{debug, error, info};

pub struct BitcoinRelayer {
genesis_block_height: u64,
Expand Down Expand Up @@ -70,8 +71,8 @@ impl BitcoinRelayer {
}

self.latest_sync_timestamp = chrono::Utc::now().timestamp() as u64;
let bitcoin_module = self.move_caller.as_module_binding::<BitcoinModule>();
let latest_block_height_in_rooch = bitcoin_module.get_latest_block_height()?;
let pending_block_module = self.move_caller.as_module_binding::<PendingBlockModule>();
let latest_block_height_in_rooch = pending_block_module.get_latest_block_height()?;
let latest_block_hash_in_bitcoin = self.rpc_client.get_best_block_hash().await?;
let latest_block_header_info = self
.rpc_client
Expand Down Expand Up @@ -167,12 +168,68 @@ impl BitcoinRelayer {
}))
}
}

fn get_ready_l1_txs(&mut self) -> Result<Vec<L1Transaction>> {
let pending_block_module = self.move_caller.as_module_binding::<PendingBlockModule>();
let pending_txs = pending_block_module.get_ready_pending_txs()?;
match pending_txs {
Some(pending_txs) => {
let block_hash = pending_txs.block_hash;
let mut txs = pending_txs.txs;
if txs.len() > 1 {
// move coinbase tx to the end
let coinbase_tx = txs.remove(0);
txs.push(coinbase_tx);
}
let l1_txs = txs
.into_iter()
.map(|txid| {
L1Transaction::new(
RoochMultiChainID::Bitcoin.multichain_id(),
block_hash.to_vec(),
txid.to_vec(),
)
})
.collect();
Ok(l1_txs)
}
None => Ok(vec![]),
}
}
}

#[async_trait]
impl Actor for BitcoinRelayer {
async fn started(&mut self, _ctx: &mut ActorContext) {}
}

#[async_trait]
impl Handler<SyncTick> for BitcoinRelayer {
async fn handle(&mut self, _message: SyncTick, _ctx: &mut ActorContext) {
if let Err(e) = self.sync_block().await {
error!("BitcoinRelayer sync block error: {:?}", e);
}
}
}

#[async_trait]
impl Relayer for BitcoinRelayer {
async fn relay(&mut self) -> Result<Option<L1BlockWithBody>> {
self.sync_block().await?;
impl Handler<GetReadyL1BlockMessage> for BitcoinRelayer {
async fn handle(
&mut self,
_message: GetReadyL1BlockMessage,
_ctx: &mut ActorContext,
) -> Result<Option<L1BlockWithBody>> {
self.pop_buffer()
}
}

#[async_trait]
impl Handler<GetReadyL1TxsMessage> for BitcoinRelayer {
async fn handle(
&mut self,
_message: GetReadyL1TxsMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<L1Transaction>> {
self.get_ready_l1_txs()
}
}
38 changes: 34 additions & 4 deletions crates/rooch-relayer/src/actor/ethereum_relayer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::Relayer;
use anyhow::Result;
use async_trait::async_trait;
use coerce::actor::{context::ActorContext, message::Handler, Actor};
use ethers::prelude::*;
use rooch_config::EthereumRelayerConfig;
use rooch_types::{
framework::ethereum::BlockHeader,
multichain_id::RoochMultiChainID,
transaction::{L1Block, L1BlockWithBody},
transaction::{L1Block, L1BlockWithBody, L1Transaction},
};
use std::collections::HashSet;
use tracing::info;

use super::messages::{GetReadyL1BlockMessage, GetReadyL1TxsMessage, SyncTick};

pub struct EthereumRelayer {
rpc_client: Provider<Http>,
processed_blocks: HashSet<H256>,
Expand Down Expand Up @@ -69,8 +71,36 @@ impl EthereumRelayer {
}

#[async_trait]
impl Relayer for EthereumRelayer {
async fn relay(&mut self) -> Result<Option<L1BlockWithBody>> {
impl Actor for EthereumRelayer {
async fn started(&mut self, _ctx: &mut ActorContext) {}
}

#[async_trait]
impl Handler<SyncTick> for EthereumRelayer {
async fn handle(&mut self, _message: SyncTick, _ctx: &mut ActorContext) {
//TODO support buffer block
}
}

#[async_trait]
impl Handler<GetReadyL1BlockMessage> for EthereumRelayer {
async fn handle(
&mut self,
_message: GetReadyL1BlockMessage,
_ctx: &mut ActorContext,
) -> Result<Option<L1BlockWithBody>> {
self.relay_ethereum().await
}
}

#[async_trait]
impl Handler<GetReadyL1TxsMessage> for EthereumRelayer {
async fn handle(
&mut self,
_message: GetReadyL1TxsMessage,
_ctx: &mut ActorContext,
) -> Result<Vec<L1Transaction>> {
//TODO
Ok(vec![])
}
}
20 changes: 20 additions & 0 deletions crates/rooch-relayer/src/actor/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use anyhow::Result;
use bitcoincore_rpc::json;
use coerce::actor::{message::Message, scheduler::timer::TimerTick};
use rooch_types::transaction::{L1BlockWithBody, L1Transaction};
use serde::{Deserialize, Serialize};

#[derive(Clone)]
Expand Down Expand Up @@ -48,3 +49,22 @@ pub struct GetBlockHeaderInfoMessage {
impl Message for GetBlockHeaderInfoMessage {
type Result = Result<json::GetBlockHeaderResult>;
}

pub struct GetReadyL1BlockMessage {}

impl Message for GetReadyL1BlockMessage {
type Result = Result<Option<L1BlockWithBody>>;
}

pub struct GetReadyL1TxsMessage {}

impl Message for GetReadyL1TxsMessage {
type Result = Result<Vec<L1Transaction>>;
}

#[derive(Clone)]
pub struct SyncTick {}

impl Message for SyncTick {
type Result = ();
}
1 change: 1 addition & 0 deletions crates/rooch-relayer/src/actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod bitcoin_relayer;
pub mod ethereum_relayer;
pub mod messages;
pub mod relayer;
pub mod relayer_proxy;
Loading

0 comments on commit e794377

Please sign in to comment.