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
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added
- [2119](https://github.com/FuelLabs/fuel-core/pull/2119): GraphQL query fields for retrieving information about upgrades.
- [2116](https://github.com/FuelLabs/fuel-core/pull/2116): Replace `H160` in config and cli options of relayer by `Bytes20` of `fuel-types`

## [Version 0.34.0]

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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<Vec<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
1 change: 1 addition & 0 deletions crates/services/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ anyhow = { workspace = true }
async-trait = { workspace = true }
bytes = { version = "1.1", optional = true }
enum-iterator = { workspace = true }
ethereum-types = "0.14.1"
ethers-contract = { version = "2", default-features = false, features = [
"abigen",
] }
Expand Down
30 changes: 24 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<Vec<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 All @@ -70,3 +70,21 @@ impl Default for Config {
}
}
}

mod tests {

#[test]
fn conversion_str_h160_bytes() {
use std::str::FromStr;

let bytes20 = fuel_core_types::fuel_types::Bytes20::from_str(
"0x03E4538018285e1c03CCce2F92C9538c87606911",
)
.unwrap();
let h160 = ethers_core::types::H160::from_str(
"0x03E4538018285e1c03CCce2F92C9538c87606911",
)
.unwrap();
assert_eq!(bytes20.as_slice(), h160.as_bytes());
}
}
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
8 changes: 6 additions & 2 deletions crates/services/relayer/src/service/get_logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use fuel_core_types::{
entities::RelayedTransaction,
fuel_types::Bytes20,
services::relayer::Event,
};
use futures::TryStreamExt;
Expand All @@ -18,7 +19,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 +30,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| ethereum_types::Address::from_slice(c.as_slice()))
.collect();
async move {
match page {
None => Ok(None),
Expand Down
14 changes: 9 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,9 @@ 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 = crate::test_helpers::convert_to_address(
u32_to_contract(contract_address).as_slice(),
);
log.block_number = Some(block_number.into());
log.log_index = Some(index.into());
log
Expand All @@ -76,17 +78,19 @@ 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 = crate::test_helpers::convert_to_address(
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 +105,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: 4 additions & 0 deletions crates/services/relayer/src/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ fn log_default(address: H160, eth_block: u64, topics: Vec<H256>, data: Bytes) ->
removed: Some(false),
}
}

pub fn convert_to_address(bytes: &[u8]) -> ethereum_types::Address {
ethereum_types::Address::from_slice(bytes)
}
4 changes: 3 additions & 1 deletion crates/services/relayer/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ impl TestContext {
}

fn given_logs(&mut self, mut logs: Vec<Log>) {
let contract_address = self.config.eth_v2_listening_contracts[0];
let contract_address = fuel_core_relayer::test_helpers::convert_to_address(
self.config.eth_v2_listening_contracts[0].as_slice(),
);
for log in &mut logs {
log.address = contract_address;
}
Expand Down
17 changes: 8 additions & 9 deletions tests/tests/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ use fuel_core_client::client::{
FuelClient,
};
use fuel_core_poa::service::Mode;
use fuel_core_relayer::{
test_helpers::{
middleware::MockMiddleware,
EvtToLog,
LogTestHelper,
},
H160,
use fuel_core_relayer::test_helpers::{
middleware::MockMiddleware,
EvtToLog,
LogTestHelper,
};
use fuel_core_storage::{
tables::Messages,
Expand All @@ -51,6 +48,7 @@ use fuel_core_types::{
Nonce,
},
};
use fuel_types::Bytes20;
use hyper::{
service::{
make_service_fn,
Expand Down Expand Up @@ -368,7 +366,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 @@ -383,7 +381,8 @@ fn make_message_event(
data: data.map(Into::into).unwrap_or_default(),
};
let mut log = message.into_log();
log.address = contract_address;
log.address =
fuel_core_relayer::test_helpers::convert_to_address(contract_address.as_slice());
log.block_number = Some(block_number.into());
log.log_index = Some(log_index.into());
log
Expand Down
Loading