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

Replace H160 in config and cli options of relayer by Bytes20 #2116

Merged
merged 11 commits into from
Aug 27, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

#### Breaking
- [2051](https://github.com/FuelLabs/fuel-core/pull/2051): Misdocumented `CONSENSUS_KEY` environ variable has been removed, use `CONSENSUS_KEY_SECRET` instead. Also raises MSRV to `1.79.0`.
- [2116](https://github.com/FuelLabs/fuel-core/pull/2116): Replace `H160` in config and cli options of relayer by `Bytes20` of `fuel-types`
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

Expand Down
15 changes: 4 additions & 11 deletions bin/fuel-core/src/cli/run/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ use clap::{
};
use core::time::Duration;
use fuel_core::{
relayer::{
Config,
H160,
},
relayer::Config,
types::blockchain::primitives::DaBlockHeight,
};
use std::str::FromStr;
use fuel_core_types::fuel_types::Bytes20;

#[derive(Debug, Clone, Args)]
pub struct RelayerArgs {
Expand All @@ -28,8 +25,8 @@ pub struct RelayerArgs {
pub relayer: Option<url::Url>,

/// Ethereum contract address. Create EthAddress into fuel_types
#[arg(long = "relayer-v2-listening-contracts", value_parser = parse_h160, value_delimiter = ',', env)]
pub eth_v2_listening_contracts: Vec<H160>,
#[arg(long = "relayer-v2-listening-contracts", value_delimiter = ',', env)]
pub eth_v2_listening_contracts: Vec<Bytes20>,

/// Number of da block that the contract is deployed at.
#[clap(long = "relayer-da-deploy-height", default_value_t = Config::DEFAULT_DA_DEPLOY_HEIGHT, env)]
Expand All @@ -53,10 +50,6 @@ pub struct RelayerArgs {
pub syncing_log_frequency_secs: u64,
}

pub fn parse_h160(input: &str) -> Result<H160, <H160 as FromStr>::Err> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to see a test that verifies the decoded value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function on which you added your comment has been removed. Can you clarify what you want to be tested ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to test that parsing of the H160::from_str and Bytes20::from_str returns the same values=)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

H160::from_str(input)
}

impl RelayerArgs {
pub fn into_config(self) -> Option<Config> {
if !self.enable_relayer {
Expand Down
12 changes: 6 additions & 6 deletions crates/services/relayer/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ethers_contract::EthEvent;
use ethers_core::types::{
H160,
H256,
use ethers_core::types::H256;
use fuel_core_types::{
blockchain::primitives::DaBlockHeight,
fuel_types::Bytes20,
};
use fuel_core_types::blockchain::primitives::DaBlockHeight;
use once_cell::sync::Lazy;
use std::{
str::FromStr,
Expand All @@ -26,7 +26,7 @@ pub struct Config {
pub relayer: Option<url::Url>,
// TODO: Create `EthAddress` into `fuel_core_types`.
/// Ethereum contract address.
pub eth_v2_listening_contracts: Vec<H160>,
pub eth_v2_listening_contracts: Vec<Bytes20>,
/// Number of pages or blocks containing logs that
/// should be downloaded in a single call to the da layer
pub log_page_size: u64,
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Default for Config {
Self {
da_deploy_height: DaBlockHeight::from(Self::DEFAULT_DA_DEPLOY_HEIGHT),
relayer: None,
eth_v2_listening_contracts: vec![H160::from_str(
eth_v2_listening_contracts: vec![Bytes20::from_str(
"0x03E4538018285e1c03CCce2F92C9538c87606911",
)
.unwrap()],
Expand Down
1 change: 0 additions & 1 deletion crates/services/relayer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use ethers_core::types::{
Log,
SyncingStatus,
ValueOrArray,
H160,
};
use ethers_providers::{
Http,
Expand Down
9 changes: 7 additions & 2 deletions crates/services/relayer/src/service/get_logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::*;
use ethers_core::types::H160;
use fuel_core_types::{
entities::RelayedTransaction,
fuel_types::Bytes20,
services::relayer::Event,
};
use futures::TryStreamExt;
Expand All @@ -18,7 +20,7 @@ pub struct DownloadedLogs {
/// Download the logs from the DA layer.
pub(crate) fn download_logs<'a, P>(
eth_sync_gap: &state::EthSyncGap,
contracts: Vec<H160>,
contracts: Vec<Bytes20>,
eth_node: &'a P,
page_size: u64,
) -> impl futures::Stream<Item = Result<DownloadedLogs, ProviderError>> + 'a
Expand All @@ -29,7 +31,10 @@ where
futures::stream::try_unfold(
eth_sync_gap.page(page_size),
move |page: Option<state::EthSyncPage>| {
let contracts = contracts.clone();
let contracts = contracts
.iter()
.map(|c| H160::from_slice(c.as_slice()))
.collect();
async move {
match page {
None => Ok(None),
Expand Down
10 changes: 5 additions & 5 deletions crates/services/relayer/src/service/get_logs/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn message(nonce: u64, block_number: u64, contract_address: u32, index: u64) ->
..Default::default()
};
let mut log = message.into_log();
log.address = u32_to_contract(contract_address);
log.address = H160::from_slice(u32_to_contract(contract_address).as_slice());
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
log.block_number = Some(block_number.into());
log.log_index = Some(index.into());
log
Expand All @@ -76,17 +76,17 @@ fn transaction(nonce: u64, block_number: u64, contract_address: u32, index: u64)
..Default::default()
};
let mut log = transaction.into_log();
log.address = u32_to_contract(contract_address);
log.address = H160::from_slice(u32_to_contract(contract_address).as_slice());
log.block_number = Some(block_number.into());
log.log_index = Some(index.into());
log
}

fn contracts(c: &[u32]) -> Vec<H160> {
fn contracts(c: &[u32]) -> Vec<Bytes20> {
c.iter().copied().map(u32_to_contract).collect()
}

fn u32_to_contract(n: u32) -> H160 {
fn u32_to_contract(n: u32) -> Bytes20 {
let address: [u8; 20] = n
.to_ne_bytes()
.into_iter()
Expand All @@ -101,7 +101,7 @@ fn u32_to_contract(n: u32) -> H160 {
#[derive(Clone, Debug)]
struct Input {
eth_gap: RangeInclusive<u64>,
c: Vec<H160>,
c: Vec<Bytes20>,
m: Vec<Log>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
4 changes: 3 additions & 1 deletion crates/services/relayer/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use fuel_core_relayer::{
LogTestHelper,
},
Config,
H160,
};
use fuel_core_services::Service;

Expand Down Expand Up @@ -284,7 +285,8 @@ impl TestContext {
}

fn given_logs(&mut self, mut logs: Vec<Log>) {
let contract_address = self.config.eth_v2_listening_contracts[0];
let contract_address =
H160::from_slice(self.config.eth_v2_listening_contracts[0].as_slice());
for log in &mut logs {
log.address = contract_address;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/tests/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use fuel_core_types::{
Nonce,
},
};
use fuel_types::Bytes20;
use hyper::{
service::{
make_service_fn,
Expand Down Expand Up @@ -374,7 +375,7 @@ async fn can_restart_node_with_relayer_data() {
fn make_message_event(
nonce: Nonce,
block_number: u64,
contract_address: H160,
contract_address: Bytes20,
sender: Option<[u8; 32]>,
recipient: Option<[u8; 32]>,
amount: Option<u64>,
Expand All @@ -389,7 +390,7 @@ fn make_message_event(
data: data.map(Into::into).unwrap_or_default(),
};
let mut log = message.into_log();
log.address = contract_address;
log.address = H160::from_slice(contract_address.as_slice());
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
log.block_number = Some(block_number.into());
log.log_index = Some(log_index.into());
log
Expand Down
Loading