Skip to content

Commit

Permalink
Merge pull request #708 from blockscout/lymarenkolev/bens/change-config
Browse files Browse the repository at this point in the history
[BENS] Fix genome bug with name hash
  • Loading branch information
sevenzing authored Dec 20, 2023
2 parents 22ba428 + d3f8242 commit 0176612
Show file tree
Hide file tree
Showing 19 changed files with 6,510 additions and 201 deletions.

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

This file was deleted.

10 changes: 6 additions & 4 deletions blockscout-ens/bens-logic/examples/resolve_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use bens_logic::subgraphs_reader::{
blockscout::BlockscoutClient, BatchResolveAddressNamesInput, SubgraphReader,
blockscout::BlockscoutClient, BatchResolveAddressNamesInput, NetworkInfo, SubgraphReader,
};
use sqlx::postgres::PgPoolOptions;
use std::{collections::HashMap, sync::Arc, time::Instant};
Expand Down Expand Up @@ -29,9 +29,11 @@ async fn main() -> Result<(), anyhow::Error> {
let eth_client = BlockscoutClient::new("https://eth.blockscout.com".parse().unwrap(), 5, 30);
let rootstock_client =
BlockscoutClient::new("https://rootstock.blockscout.com".parse().unwrap(), 5, 30);
let clients: HashMap<i64, BlockscoutClient> =
HashMap::from_iter([(1, eth_client), (30, rootstock_client)]);
let reader = SubgraphReader::initialize(pool.clone(), clients, true).await?;
let networks = HashMap::from_iter([
(1, NetworkInfo::from_client(eth_client)),
(30, NetworkInfo::from_client(rootstock_client)),
]);
let reader = SubgraphReader::initialize(pool.clone(), networks).await?;

let addresses = vec![
"0x0292f204513eeafe8c032ffc4cb4c7e10eca908c",
Expand Down
60 changes: 46 additions & 14 deletions blockscout-ens/bens-logic/src/hash_name.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use ethers::utils::keccak256;
use ethers::{types::Bytes, utils::keccak256};

/// Implementation of
/// https://docs.ens.domains/contract-api-reference/name-processing#algorithm
pub fn hash_ens_domain_name(name: &str) -> [u8; 32] {
/// with custom empty_label_hash
pub fn hash_ens_domain_name(name: &str, empty_label_hash: Option<Bytes>) -> Bytes {
if name.is_empty() {
[0; 32]
empty_label_hash.unwrap_or_else(|| [0; 32].into())
} else {
let (label, remainder) = name.split_once('.').unwrap_or((name, ""));
let remainder_hash = hash_ens_domain_name(remainder);
let remainder_hash = hash_ens_domain_name(remainder, empty_label_hash);
let label_hash = keccak256(label.as_bytes());
let concatenated: Vec<u8> = remainder_hash.into_iter().chain(label_hash).collect();
keccak256(concatenated)
keccak256(concatenated).into()
}
}

pub fn domain_id(name: &str) -> String {
hex(hash_ens_domain_name(name))
pub fn domain_id(name: &str, empty_label_hash: Option<Bytes>) -> String {
hex(hash_ens_domain_name(name, empty_label_hash))
}

pub fn hex<T>(data: T) -> String
Expand All @@ -28,29 +29,60 @@ where
#[cfg(test)]
mod tests {
use super::*;
use hex::FromHex;
use pretty_assertions::assert_eq;

#[test]
fn it_works() {
fn default_works() {
for (name, expected_hash) in [
(
"",
"0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000",
),
(
"eth",
"93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae",
"0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae",
),
(
"levvv.eth",
"38a7804a53792b0cdefe3e7271b0b85422d620ea4a82df7b7bf750a6d4b297a4",
"0x38a7804a53792b0cdefe3e7271b0b85422d620ea4a82df7b7bf750a6d4b297a4",
),
(
"vitalik.eth",
"ee6c4522aab0003e8d14cd40a6af439055fd2577951148c14b6cea9a53475835",
"0xee6c4522aab0003e8d14cd40a6af439055fd2577951148c14b6cea9a53475835",
),
] {
let hash = hash_ens_domain_name(name);
assert_eq!(hex::encode(hash), expected_hash);
let hash = domain_id(name, None);
assert_eq!(hash, expected_hash);
}
}

#[test]
fn genome_testnet_works() {
for (name, expected_hash) in [
(
"",
"0x1a13b687a5ff1d8ab1a9e189e1507a6abe834a9296cc8cff937905e3dee0c4f6",
),
(
"gno",
"0x634ae5e4e77ee5a262a820f4a9eacd51ac137dd75989e5a5d993f5b1db797fba",
),
(
"levvv.gno",
"0xa3504cdec527495c69c760c85d5be9996252f853b91fd0df04c5b6aa2deb3347",
),
(
"unknown.gno",
"0x7dd2724da2c399aa963a8ecf14e2a017b7f12026dcdf17277f96ac263d0ffbae",
),
] {
let genome_testnet_empty_label = Bytes::from_hex(
"0x1a13b687a5ff1d8ab1a9e189e1507a6abe834a9296cc8cff937905e3dee0c4f6",
)
.expect("valid hex");
let hash = domain_id(name, Some(genome_testnet_empty_label));
assert_eq!(hash, expected_hash);
}
}
}
2 changes: 1 addition & 1 deletion blockscout-ens/bens-logic/src/subgraphs_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ mod schema_selector;
mod sql;
mod types;

pub use reader::{SubgraphReadError, SubgraphReader};
pub use reader::{NetworkInfo, SubgraphReadError, SubgraphReader, SubgraphSettings};
pub use types::*;
Loading

0 comments on commit 0176612

Please sign in to comment.