Skip to content

Commit

Permalink
Pull commit interval config from contract (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Sagdati <[email protected]>
  • Loading branch information
Salka1988 and segfault-magnet authored May 27, 2024
1 parent 15e57f1 commit 5943d62
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 23 deletions.
4 changes: 1 addition & 3 deletions committer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{net::Ipv4Addr, num::NonZeroU32, path::PathBuf, str::FromStr, time::Duration};
use std::{net::Ipv4Addr, path::PathBuf, str::FromStr, time::Duration};

use clap::{command, Parser};
use eth::{Address, Chain};
Expand Down Expand Up @@ -34,8 +34,6 @@ pub struct EthConfig {
pub chain_id: Chain,
/// Ethereum address of the fuel chain state contract.
pub state_contract_address: Address,
/// The number of fuel blocks between ethereum commits. If set to 1, then every block should be pushed to Ethereum.
pub commit_interval: NonZeroU32,
}

fn parse_chain_id<'de, D>(deserializer: D) -> Result<Chain, D::Error>
Expand Down
10 changes: 7 additions & 3 deletions committer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use setup::{
use tokio_util::sync::CancellationToken;

use crate::setup::shut_down;
use ports::l1::Contract;

pub type L1 = eth::WebsocketClient;
pub type Database = storage::Postgres;
Expand All @@ -34,17 +35,20 @@ async fn main() -> Result<()> {

let metrics_registry = Registry::default();

let (ethereum_rpc, eth_health_check) =
create_l1_adapter(&config, &internal_config, &metrics_registry).await?;

let commit_interval = ethereum_rpc.commit_interval();

let (rx_fuel_block, block_watcher_handle, fuel_health_check) = spawn_block_watcher(
commit_interval,
&config,
&internal_config,
storage.clone(),
&metrics_registry,
cancel_token.clone(),
);

let (ethereum_rpc, eth_health_check) =
create_l1_adapter(&config, &internal_config, &metrics_registry).await?;

let wallet_balance_tracker_handle = spawn_wallet_balance_tracker(
&internal_config,
&metrics_registry,
Expand Down
9 changes: 6 additions & 3 deletions committer/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use std::time::Duration;

use metrics::{prometheus::Registry, HealthChecker, RegistersMetrics};
Expand All @@ -15,6 +16,7 @@ use crate::{
};

pub fn spawn_block_watcher(
commit_interval: NonZeroU32,
config: &Config,
internal_config: &InternalConfig,
storage: Database,
Expand All @@ -28,7 +30,8 @@ pub fn spawn_block_watcher(
let (fuel_adapter, fuel_connection_health) =
create_fuel_adapter(config, internal_config, registry);

let (block_watcher, rx) = create_block_watcher(config, registry, fuel_adapter, storage);
let (block_watcher, rx) =
create_block_watcher(commit_interval, config, registry, fuel_adapter, storage);

let handle = schedule_polling(
internal_config.fuel_polling_interval,
Expand Down Expand Up @@ -105,7 +108,6 @@ pub async fn create_l1_adapter(
config.eth.chain_id,
config.eth.state_contract_address,
&config.eth.wallet_key,
config.eth.commit_interval,
internal_config.eth_errors_before_unhealthy,
)
.await?;
Expand Down Expand Up @@ -157,6 +159,7 @@ fn create_fuel_adapter(
}

fn create_block_watcher(
commit_interval: NonZeroU32,
config: &Config,
registry: &Registry,
fuel_adapter: FuelApi,
Expand All @@ -167,7 +170,7 @@ fn create_block_watcher(
) {
let (tx_fuel_block, rx_fuel_block) = tokio::sync::mpsc::channel(100);
let block_watcher = BlockWatcher::new(
config.eth.commit_interval,
commit_interval,
tx_fuel_block,
fuel_adapter,
storage,
Expand Down
1 change: 0 additions & 1 deletion configurations/development/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
wallet_key = "0x9e56ccf010fa4073274b8177ccaad46fbaf286645310d03ac9bb6afa922a7c36"
chain_id = "hardhat"
state_contract_address = "0xdAad669b06d79Cb48C8cfef789972436dBe6F24d"
commit_interval = 3
rpc = "ws://localhost:8089"

[fuel]
Expand Down
9 changes: 5 additions & 4 deletions e2e/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(test)]
mod tests {
use std::time::Duration;

use anyhow::{anyhow, Result};
use eth::{Chain, WebsocketClient};
use fuel::HttpClient;
use ports::fuel::{Api, FuelPublicKey};
use ports::l1::Contract;
use std::time::Duration;
use validator::{BlockValidator, Validator};

const FUEL_NODE_PORT: u16 = 4000;
Expand All @@ -20,12 +20,13 @@ mod tests {
Chain::AnvilHardhat,
"0xdAad669b06d79Cb48C8cfef789972436dBe6F24d".parse()?,
"0x9e56ccf010fa4073274b8177ccaad46fbaf286645310d03ac9bb6afa922a7c36",
3.try_into()?,
10,
)
.await?;

provider.produce_blocks(3).await?;
provider
.produce_blocks(fuel_contract.commit_interval().into())
.await?;

// time enough to fwd the block to ethereum and for the TIME_TO_FINALIZE (1s) to elapse
tokio::time::sleep(Duration::from_secs(5)).await;
Expand Down
1 change: 0 additions & 1 deletion eth_node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ RUN git clone --no-checkout https://github.com/FuelLabs/fuel-bridge \
&& cd packages/solidity-contracts \
&& rm -rf deploy deployments exports test \
&& cd contracts \
&& sed 's/\(BLOCKS_PER_COMMIT_INTERVAL\) = 10800/\1 = 3/g' -i ./fuelchain/FuelChainState.sol \
&& sed 's/\(TIME_TO_FINALIZE\) = 10800/\1 = 1/g' -i ./fuelchain/FuelChainState.sol

FROM alpine:3.19.1
Expand Down
6 changes: 6 additions & 0 deletions packages/eth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#![deny(unused_crate_dependencies)]

use std::num::NonZeroU32;
use std::pin::Pin;

use async_trait::async_trait;
Expand Down Expand Up @@ -26,6 +28,10 @@ impl Contract for WebsocketClient {
fn event_streamer(&self, height: L1Height) -> Box<dyn EventStreamer + Send + Sync> {
Box::new(self.event_streamer(height.into()))
}

fn commit_interval(&self) -> NonZeroU32 {
self.commit_interval()
}
}

#[async_trait]
Expand Down
12 changes: 6 additions & 6 deletions packages/eth/src/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::num::NonZeroU32;

use ::metrics::{prometheus::core::Collector, HealthChecker, RegistersMetrics};
use ethers::types::{Address, Chain};
use ports::{
l1::Result,
types::{ValidatedFuelBlock, U256},
};
use std::num::NonZeroU32;
use url::Url;

pub use self::event_streamer::EthEventStreamer;
Expand All @@ -29,12 +28,9 @@ impl WebsocketClient {
chain_id: Chain,
contract_address: Address,
wallet_key: &str,
commit_interval: NonZeroU32,
unhealthy_after_n_errors: usize,
) -> ports::l1::Result<Self> {
let provider =
WsConnection::connect(url, chain_id, contract_address, wallet_key, commit_interval)
.await?;
let provider = WsConnection::connect(url, chain_id, contract_address, wallet_key).await?;

Ok(Self {
inner: HealthTrackingMiddleware::new(provider, unhealthy_after_n_errors),
Expand All @@ -54,6 +50,10 @@ impl WebsocketClient {
Ok(self.inner.submit(block).await?)
}

pub(crate) fn commit_interval(&self) -> NonZeroU32 {
self.inner.commit_interval()
}

pub(crate) async fn get_block_number(&self) -> Result<u64> {
Ok(self.inner.get_block_number().await?)
}
Expand Down
18 changes: 16 additions & 2 deletions packages/eth/src/websocket/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_json::Value;
use url::Url;

use super::{event_streamer::EthEventStreamer, health_tracking_middleware::EthApi};
use crate::error::Result;
use crate::error::{Error, Result};

abigen!(
FUEL_STATE_CONTRACT,
Expand All @@ -20,6 +20,7 @@ abigen!(
event CommitSubmitted(uint256 indexed commitHeight, bytes32 blockHash)
function finalized(bytes32 blockHash, uint256 blockHeight) external view whenNotPaused returns (bool)
function blockHashAtCommit(uint256 commitHeight) external view returns (bytes32)
function BLOCKS_PER_COMMIT_INTERVAL() external view returns (uint256)
]"#,
);

Expand Down Expand Up @@ -60,6 +61,10 @@ impl EthApi for WsConnection {
Ok(self.provider.get_balance(address, None).await?)
}

fn commit_interval(&self) -> NonZeroU32 {
self.commit_interval
}

fn event_streamer(&self, eth_block_height: u64) -> EthEventStreamer {
let events = self
.contract
Expand Down Expand Up @@ -94,7 +99,6 @@ impl WsConnection {
chain_id: Chain,
contract_address: Address,
wallet_key: &str,
commit_interval: NonZeroU32,
) -> Result<Self> {
let provider = Provider::<Ws>::connect(url.to_string()).await?;

Expand All @@ -106,6 +110,16 @@ impl WsConnection {
let contract_address = Address::from_slice(contract_address.as_ref());
let contract = FUEL_STATE_CONTRACT::new(contract_address, Arc::new(signer));

let interval_u256 = contract.blocks_per_commit_interval().call().await?;

let commit_interval = u32::try_from(interval_u256)
.map_err(|e| Error::Other(e.to_string()))
.and_then(|value| {
NonZeroU32::new(value).ok_or_else(|| {
Error::Other("l1 contract reported a commit interval of 0".to_string())
})
})?;

Ok(Self {
provider,
contract,
Expand Down
8 changes: 8 additions & 0 deletions packages/eth/src/websocket/health_tracking_middleware.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use ::metrics::{
prometheus::core::Collector, ConnectionHealthTracker, HealthChecker, RegistersMetrics,
};

use std::num::NonZeroU32;

use ports::types::{ValidatedFuelBlock, U256};

use crate::{
Expand All @@ -15,6 +18,7 @@ pub trait EthApi {
async fn submit(&self, block: ValidatedFuelBlock) -> Result<()>;
async fn get_block_number(&self) -> Result<u64>;
async fn balance(&self) -> Result<U256>;
fn commit_interval(&self) -> NonZeroU32;
fn event_streamer(&self, eth_block_height: u64) -> EthEventStreamer;
#[cfg(feature = "test-helpers")]
async fn finalized(&self, block: ValidatedFuelBlock) -> Result<bool>;
Expand Down Expand Up @@ -90,6 +94,10 @@ where
response
}

fn commit_interval(&self) -> NonZeroU32 {
self.adapter.commit_interval()
}

#[cfg(feature = "test-helpers")]
async fn finalized(&self, block: ValidatedFuelBlock) -> Result<bool> {
self.adapter.finalized(block).await
Expand Down
1 change: 1 addition & 0 deletions packages/ports/src/ports/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl From<InvalidL1Height> for Error {
pub trait Contract: Send + Sync {
async fn submit(&self, block: ValidatedFuelBlock) -> Result<()>;
fn event_streamer(&self, height: L1Height) -> Box<dyn EventStreamer + Send + Sync>;
fn commit_interval(&self) -> std::num::NonZeroU32;
}

#[cfg_attr(feature = "test-helpers", mockall::automock)]
Expand Down
5 changes: 5 additions & 0 deletions packages/services/src/block_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ where

#[cfg(test)]
mod tests {
use std::num::NonZeroU32;
use std::time::Duration;

use mockall::predicate;
Expand All @@ -101,6 +102,10 @@ mod tests {
fn event_streamer(&self, height: L1Height) -> Box<dyn EventStreamer + Send + Sync> {
self.contract.event_streamer(height)
}

fn commit_interval(&self) -> NonZeroU32 {
self.contract.commit_interval()
}
}

#[cfg_attr(feature = "test-helpers", mockall::automock)]
Expand Down

0 comments on commit 5943d62

Please sign in to comment.