From 85ad049c9d99cc67103bff2ac42cc410fb104022 Mon Sep 17 00:00:00 2001 From: Lorenzo Delgado Date: Fri, 20 Dec 2024 11:22:27 +0100 Subject: [PATCH] refactor: use address and deployment_id initialization macros Signed-off-by: Lorenzo Delgado --- crates/attestation/src/lib.rs | 10 ++-- crates/monitor/src/allocations.rs | 7 ++- crates/monitor/src/client/monitor.rs | 14 ++--- crates/monitor/src/client/subgraph_client.rs | 11 ++-- crates/service/src/database/cost_model.rs | 9 ++- crates/tap-agent/src/test.rs | 8 +-- crates/test-assets/src/lib.rs | 58 ++++++++++---------- 7 files changed, 54 insertions(+), 63 deletions(-) diff --git a/crates/attestation/src/lib.rs b/crates/attestation/src/lib.rs index 9444120e..004fdbac 100644 --- a/crates/attestation/src/lib.rs +++ b/crates/attestation/src/lib.rs @@ -119,7 +119,7 @@ fn wallet_for_allocation( mod tests { use std::str::FromStr; - use alloy::primitives::U256; + use alloy::primitives::{address, U256}; use indexer_allocation::{Allocation, AllocationStatus, SubgraphDeployment}; use test_assets::DISPUTE_MANAGER_ADDRESS; use test_log::test; @@ -142,7 +142,7 @@ mod tests { ) .unwrap() .address(), - Address::from_str("0xfa44c72b753a66591f241c7dc04e8178c30e13af").unwrap() + address!("fa44c72b753a66591f241c7dc04e8178c30e13af") ); assert_eq!( @@ -157,7 +157,7 @@ mod tests { ) .unwrap() .address(), - Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap() + address!("a171cd12c3dde7eb8fe7717a0bcd06f3ffa65658") ); } @@ -166,7 +166,7 @@ mod tests { // Note that we use `derive_key_pair` to derive the private key let allocation = Allocation { - id: Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap(), + id: address!("a171cd12c3dde7eb8fe7717a0bcd06f3ffa65658"), status: AllocationStatus::Null, subgraph_deployment: SubgraphDeployment { id: DeploymentId::from_str( @@ -213,7 +213,7 @@ mod tests { let allocation = Allocation { // Purposefully wrong address - id: Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(), + id: address!("deadbeefcafebabedeadbeefcafebabedeadbeef"), status: AllocationStatus::Null, subgraph_deployment: SubgraphDeployment { id: DeploymentId::from_str( diff --git a/crates/monitor/src/allocations.rs b/crates/monitor/src/allocations.rs index 7d98428f..0c8ec2b2 100644 --- a/crates/monitor/src/allocations.rs +++ b/crates/monitor/src/allocations.rs @@ -92,7 +92,8 @@ pub async fn get_allocations( mod test { const NETWORK_SUBGRAPH_URL: &str = "https://api.thegraph.com/subgraphs/name/graphprotocol/graph-network-arbitrum"; - use std::str::FromStr; + + use alloy::primitives::address; use super::*; use crate::client::{DeploymentDetails, SubgraphClient}; @@ -113,7 +114,7 @@ mod test { async fn test_network_query() { let result = get_allocations( network_subgraph_client().await, - Address::from_str("0x326c584e0f0eab1f1f83c93cc6ae1acc0feba0bc").unwrap(), + address!("326c584e0f0eab1f1f83c93cc6ae1acc0feba0bc"), Duration::from_secs(1712448507), ) .await; @@ -125,7 +126,7 @@ mod test { async fn test_network_query_empty_response() { let result = get_allocations( network_subgraph_client().await, - Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(), + address!("deadbeefcafebabedeadbeefcafebabedeadbeef"), Duration::from_secs(1712448507), ) .await diff --git a/crates/monitor/src/client/monitor.rs b/crates/monitor/src/client/monitor.rs index 900e9f58..dd4948ac 100644 --- a/crates/monitor/src/client/monitor.rs +++ b/crates/monitor/src/client/monitor.rs @@ -62,9 +62,9 @@ pub async fn check_deployment_status( #[cfg(test)] mod tests { - use std::str::FromStr; use serde_json::json; + use thegraph_core::deployment_id; use wiremock::{ matchers::{method, path}, Mock, MockServer, ResponseTemplate, @@ -81,8 +81,7 @@ mod tests { .unwrap() .join("/status") .unwrap(); - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); Mock::given(method("POST")) .and(path("/status")) @@ -121,8 +120,7 @@ mod tests { .unwrap() .join("/status") .unwrap(); - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); Mock::given(method("POST")) .and(path("/status")) @@ -161,8 +159,7 @@ mod tests { .unwrap() .join("/status") .unwrap(); - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); Mock::given(method("POST")) .and(path("/status")) @@ -201,8 +198,7 @@ mod tests { .unwrap() .join("/status") .unwrap(); - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); Mock::given(method("POST")) .and(path("/status")) diff --git a/crates/monitor/src/client/subgraph_client.rs b/crates/monitor/src/client/subgraph_client.rs index 53a85348..70811664 100644 --- a/crates/monitor/src/client/subgraph_client.rs +++ b/crates/monitor/src/client/subgraph_client.rs @@ -251,10 +251,10 @@ impl SubgraphClient { #[cfg(test)] mod test { - use std::str::FromStr; use indexer_query::{current_epoch, user_query, CurrentEpoch, UserQuery}; use serde_json::json; + use thegraph_core::deployment_id; use wiremock::{ matchers::{method, path}, Mock, MockServer, ResponseTemplate, @@ -315,8 +315,7 @@ mod test { #[tokio::test] async fn test_uses_local_deployment_if_healthy_and_synced() { - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); let mock_server_status = MockServer::start().await; mock_server_status @@ -396,8 +395,7 @@ mod test { #[tokio::test] async fn test_uses_query_url_if_local_deployment_is_unhealthy() { - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); let mock_server_status = MockServer::start().await; mock_server_status @@ -477,8 +475,7 @@ mod test { #[tokio::test] async fn test_uses_query_url_if_local_deployment_is_not_synced() { - let deployment = - DeploymentId::from_str("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA").unwrap(); + let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); let mock_server_status = MockServer::start().await; mock_server_status diff --git a/crates/service/src/database/cost_model.rs b/crates/service/src/database/cost_model.rs index afdb1400..1ae10629 100644 --- a/crates/service/src/database/cost_model.rs +++ b/crates/service/src/database/cost_model.rs @@ -194,6 +194,7 @@ pub(crate) mod test { use std::str::FromStr; use sqlx::PgPool; + use thegraph_core::deployment_id; use super::*; @@ -389,8 +390,7 @@ pub(crate) mod test { } // Third test: query for missing cost model - let missing_deployment = - DeploymentId::from_str("Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(); + let missing_deployment = deployment_id!("Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); let models = cost_models(&pool, &[missing_deployment]) .await .expect("cost models query for missing deployment"); @@ -413,7 +413,7 @@ pub(crate) mod test { ) .unwrap(); let deployment_id_from_hash = - DeploymentId::from_str("Qmb5Ysp5oCUXhLA8NmxmYKDAX2nCMnh7Vvb5uffb9n5vss").unwrap(); + deployment_id!("Qmb5Ysp5oCUXhLA8NmxmYKDAX2nCMnh7Vvb5uffb9n5vss"); assert_eq!(deployment_id_from_bytes, deployment_id_from_hash); @@ -454,8 +454,7 @@ pub(crate) mod test { } // Test that querying a non-existing deployment returns the default cost model - let missing_deployment = - DeploymentId::from_str("Qmnononononononononononononononononononononono").unwrap(); + let missing_deployment = deployment_id!("Qmnononononononononononononononononononononono"); let model = cost_model(&pool, &missing_deployment) .await .expect("cost model query") diff --git a/crates/tap-agent/src/test.rs b/crates/tap-agent/src/test.rs index f1cf1702..1220ac4b 100644 --- a/crates/tap-agent/src/test.rs +++ b/crates/tap-agent/src/test.rs @@ -1,10 +1,8 @@ // Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. // SPDX-License-Identifier: Apache-2.0 -use std::str::FromStr; - use alloy::{ - primitives::{hex::ToHexExt, Address}, + primitives::{address, hex::ToHexExt, Address}, signers::local::{coins_bip39::English, MnemonicBuilder, PrivateKeySigner}, sol_types::Eip712Domain, }; @@ -20,9 +18,9 @@ use tap_core::{ lazy_static! { pub static ref ALLOCATION_ID_0: Address = - Address::from_str("0xabababababababababababababababababababab").unwrap(); + address!("abababababababababababababababababababab"); pub static ref ALLOCATION_ID_1: Address = - Address::from_str("0xbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbc").unwrap(); + address!("bcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbc"); // pub static ref SENDER: (PrivateKeySigner, Address) = wallet(0); pub static ref SENDER_2: (PrivateKeySigner, Address) = wallet(1); pub static ref SIGNER: (PrivateKeySigner, Address) = wallet(2); diff --git a/crates/test-assets/src/lib.rs b/crates/test-assets/src/lib.rs index fe08a2f1..2023e555 100644 --- a/crates/test-assets/src/lib.rs +++ b/crates/test-assets/src/lib.rs @@ -16,7 +16,7 @@ use tap_core::{ signed_message::EIP712SignedMessage, tap_eip712_domain, }; -use thegraph_core::DeploymentId; +use thegraph_core::{deployment_id, DeploymentId}; use tokio::sync::Notify; use typed_builder::TypedBuilder; @@ -110,31 +110,31 @@ pub const ESCROW_QUERY_RESPONSE: &str = r#" "#; lazy_static! { - pub static ref NETWORK_SUBGRAPH_DEPLOYMENT: DeploymentId = DeploymentId::from_str("QmU7zqJyHSyUP3yFii8sBtHT8FaJn2WmUnRvwjAUTjwMBP").unwrap(); - pub static ref ESCROW_SUBGRAPH_DEPLOYMENT: DeploymentId = DeploymentId::from_str("Qmb5Ysp5oCUXhLA8NmxmYKDAX2nCMnh7Vvb5uffb9n5vss").unwrap(); + pub static ref NETWORK_SUBGRAPH_DEPLOYMENT: DeploymentId = deployment_id!("QmU7zqJyHSyUP3yFii8sBtHT8FaJn2WmUnRvwjAUTjwMBP"); + pub static ref ESCROW_SUBGRAPH_DEPLOYMENT: DeploymentId = deployment_id!("Qmb5Ysp5oCUXhLA8NmxmYKDAX2nCMnh7Vvb5uffb9n5vss"); pub static ref INDEXER_MNEMONIC: Mnemonic = Mnemonic::from_str( "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", ).unwrap(); pub static ref INDEXER_ADDRESS: Address = - Address::from_str("0xd75c4dbcb215a6cf9097cfbcc70aab2596b96a9c").unwrap(); + address!("d75c4dbcb215a6cf9097cfbcc70aab2596b96a9c"); pub static ref DISPUTE_MANAGER_ADDRESS: Address = - Address::from_str("0xdeadbeefcafebabedeadbeefcafebabedeadbeef").unwrap(); + address!("deadbeefcafebabedeadbeefcafebabedeadbeef"); pub static ref ALLOCATION_ID_0: Address = - Address::from_str("0xfa44c72b753a66591f241c7dc04e8178c30e13af").unwrap(); + address!("fa44c72b753a66591f241c7dc04e8178c30e13af"); pub static ref ALLOCATION_ID_1: Address = - Address::from_str("0xdd975e30aafebb143e54d215db8a3e8fd916a701").unwrap(); + address!("dd975e30aafebb143e54d215db8a3e8fd916a701"); pub static ref ALLOCATION_ID_2: Address = - Address::from_str("0xa171cd12c3dde7eb8fe7717a0bcd06f3ffa65658").unwrap(); + address!("a171cd12c3dde7eb8fe7717a0bcd06f3ffa65658"); pub static ref ALLOCATION_ID_3: Address = - Address::from_str("0x69f961358846fdb64b04e1fd7b2701237c13cd9a").unwrap(); + address!("69f961358846fdb64b04e1fd7b2701237c13cd9a"); @@ -145,7 +145,7 @@ lazy_static! { *ALLOCATION_ID_0, Allocation { id: *ALLOCATION_ID_0, - indexer: Address::from_str("0xd75c4dbcb215a6cf9097cfbcc70aab2596b96a9c").unwrap(), + indexer: address!("d75c4dbcb215a6cf9097cfbcc70aab2596b96a9c"), allocated_tokens: U256::from_str("5081382841000000014901161").unwrap(), created_at_block_hash: "0x99d3fbdc0105f7ccc0cd5bb287b82657fe92db4ea8fb58242dafb90b1c6e2adf".to_string(), @@ -169,7 +169,7 @@ lazy_static! { *ALLOCATION_ID_1, Allocation { id: *ALLOCATION_ID_1, - indexer: Address::from_str("0xd75c4dbcb215a6cf9097cfbcc70aab2596b96a9c").unwrap(), + indexer: address!("d75c4dbcb215a6cf9097cfbcc70aab2596b96a9c"), allocated_tokens: U256::from_str("601726452999999979510903").unwrap(), created_at_block_hash: "0x99d3fbdc0105f7ccc0cd5bb287b82657fe92db4ea8fb58242dafb90b1c6e2adf".to_string(), @@ -193,7 +193,7 @@ lazy_static! { *ALLOCATION_ID_2, Allocation { id: *ALLOCATION_ID_2, - indexer: Address::from_str("0xd75c4dbcb215a6cf9097cfbcc70aab2596b96a9c").unwrap(), + indexer: address!("d75c4dbcb215a6cf9097cfbcc70aab2596b96a9c"), allocated_tokens: U256::from_str("5247998688000000081956387").unwrap(), created_at_block_hash: "0x6e7b7100c37f659236a029f87ce18914643995120f55ab5d01631f11f40fd887".to_string(), @@ -217,7 +217,7 @@ lazy_static! { *ALLOCATION_ID_3, Allocation { id: *ALLOCATION_ID_3, - indexer: Address::from_str("0xd75c4dbcb215a6cf9097cfbcc70aab2596b96a9c").unwrap(), + indexer: address!("d75c4dbcb215a6cf9097cfbcc70aab2596b96a9c"), allocated_tokens: U256::from_str("2502334654999999795109034").unwrap(), created_at_block_hash: "0x6e7b7100c37f659236a029f87ce18914643995120f55ab5d01631f11f40fd887".to_string(), @@ -240,43 +240,43 @@ lazy_static! { ]); pub static ref ESCROW_ACCOUNTS_BALANCES: HashMap = HashMap::from([ - (Address::from_str("0x9858EfFD232B4033E47d90003D41EC34EcaEda94").unwrap(), U256::from(24)), // TAP_SENDER - (Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(), U256::from(42)), - (Address::from_str("0x192c3B6e0184Fa0Cc5B9D2bDDEb6B79Fb216a002").unwrap(), U256::from(2975)), + (address!("9858EfFD232B4033E47d90003D41EC34EcaEda94"), U256::from(24)), // TAP_SENDER + (address!("22d491bde2303f2f43325b2108d26f1eaba1e32b"), U256::from(42)), + (address!("192c3B6e0184Fa0Cc5B9D2bDDEb6B79Fb216a002"), U256::from(2975)), ]); /// Maps signers back to their senders pub static ref ESCROW_ACCOUNTS_SIGNERS_TO_SENDERS: HashMap = HashMap::from([ ( - Address::from_str("0x533661F0fb14d2E8B26223C86a610Dd7D2260892").unwrap(), // TAP_SIGNER - Address::from_str("0x9858EfFD232B4033E47d90003D41EC34EcaEda94").unwrap(), // TAP_SENDER + address!("533661F0fb14d2E8B26223C86a610Dd7D2260892"), // TAP_SIGNER + address!("9858EfFD232B4033E47d90003D41EC34EcaEda94"), // TAP_SENDER ), ( - Address::from_str("0x2740f6fA9188cF53ffB6729DDD21575721dE92ce").unwrap(), - Address::from_str("0x9858EfFD232B4033E47d90003D41EC34EcaEda94").unwrap(), // TAP_SENDER + address!("2740f6fA9188cF53ffB6729DDD21575721dE92ce"), + address!("9858EfFD232B4033E47d90003D41EC34EcaEda94"), // TAP_SENDER ), ( - Address::from_str("0x245059163ff6ee14279aa7b35ea8f0fdb967df6e").unwrap(), - Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(), + address!("245059163ff6ee14279aa7b35ea8f0fdb967df6e"), + address!("22d491bde2303f2f43325b2108d26f1eaba1e32b"), ), ]); pub static ref ESCROW_ACCOUNTS_SENDERS_TO_SIGNERS: HashMap> = HashMap::from([ ( - Address::from_str("0x9858EfFD232B4033E47d90003D41EC34EcaEda94").unwrap(), // TAP_SENDER + address!("9858EfFD232B4033E47d90003D41EC34EcaEda94"), // TAP_SENDER vec![ - Address::from_str("0x533661F0fb14d2E8B26223C86a610Dd7D2260892").unwrap(), // TAP_SIGNER - Address::from_str("0x2740f6fA9188cF53ffB6729DDD21575721dE92ce").unwrap(), + address!("533661F0fb14d2E8B26223C86a610Dd7D2260892"), // TAP_SIGNER + address!("2740f6fA9188cF53ffB6729DDD21575721dE92ce"), ], ), ( - Address::from_str("0x22d491bde2303f2f43325b2108d26f1eaba1e32b").unwrap(), - vec![Address::from_str("0x245059163ff6ee14279aa7b35ea8f0fdb967df6e").unwrap()], + address!("22d491bde2303f2f43325b2108d26f1eaba1e32b"), + vec![address!("245059163ff6ee14279aa7b35ea8f0fdb967df6e")], ), ( - Address::from_str("0x192c3B6e0184Fa0Cc5B9D2bDDEb6B79Fb216a002").unwrap(), + address!("192c3B6e0184Fa0Cc5B9D2bDDEb6B79Fb216a002"), vec![], ), ]); @@ -317,7 +317,7 @@ lazy_static! { use std::time::{SystemTime, UNIX_EPOCH}; -use alloy::primitives::Address; +use alloy::primitives::{address, Address}; #[derive(TypedBuilder)] pub struct SignedReceiptRequest {