Skip to content

Commit

Permalink
feat(thegraph-core): re-export alloy meta-crate
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Delgado <[email protected]>
  • Loading branch information
LNSD committed Nov 9, 2024
1 parent b7e08c0 commit dda9513
Show file tree
Hide file tree
Showing 12 changed files with 1,366 additions and 99 deletions.
1,326 changes: 1,279 additions & 47 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions thegraph-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ edition = "2021"
rust-version = "1.71.1"

[features]
alloy-signer-local = ["alloy/signer-local"]
async-graphql-support = ["dep:async-graphql"]
serde = ["dep:serde", "dep:serde_with", "alloy-primitives/serde"]
serde = ["dep:serde", "dep:serde_with", "alloy/serde"]
subgraph-client = [
"serde",
"dep:tracing",
Expand All @@ -23,8 +24,7 @@ subgraph-client = [
]

[dependencies]
alloy-primitives = "0.8"
alloy-signer = { version = "=0.5", features = ["eip712"] }
alloy = { version = "0.6", features = ["eip712", "signers", "sol-types"] }
alloy-sol-types = "0.8"
async-graphql = { version = "7.0", optional = true }
bs58 = "0.5"
Expand All @@ -39,7 +39,7 @@ tracing = { version = "0.1.40", optional = true, default-features = false }
url = "2.5"

[dev-dependencies]
alloy-signer-local = "0.5.0"
alloy = { version = "0.6", features = ["signer-local"] }
assert_matches = "1.5.0"
test-with = { version = "0.14.0", default-features = false }
tokio = { version = "1.37.0", features = ["macros", "rt"] }
Expand Down
14 changes: 9 additions & 5 deletions thegraph-core/src/allocation_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::Address;
use alloy::primitives::Address;

/// A unique identifier for an allocation: the allocation's Ethereum address.
///
Expand Down Expand Up @@ -189,18 +189,22 @@ impl serde::Serialize for AllocationId {
/// If no argument is provided, the macro will create an `AllocationId` with the zero address:
///
/// ```rust
/// use thegraph_core::{Address, allocation_id, AllocationId };
/// use thegraph_core::{
/// alloy::primitives::Address,
/// allocation_id, AllocationId
/// };
///
/// const ALLOCATION_ID: AllocationId = allocation_id!();
///
/// assert_eq!(ALLOCATION_ID, Address::ZERO);
/// ```
#[macro_export]
macro_rules! allocation_id {
#[doc(hidden)]
macro_rules! __allocation_id {
() => {
$crate::AllocationId::new($crate::alloy_primitives::Address::ZERO)
$crate::AllocationId::new($crate::alloy::primitives::Address::ZERO)
};
($value:tt) => {
$crate::AllocationId::new($crate::alloy_primitives::address!($value))
$crate::AllocationId::new($crate::alloy::primitives::address!($value))
};
}
36 changes: 20 additions & 16 deletions thegraph-core/src/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Attestation types and functions for verifying attestations.
use alloy_primitives::{b256, keccak256, Address, ChainId, Signature, B256};
use alloy_signer::SignerSync;
use alloy_sol_types::{eip712_domain, Eip712Domain, SolStruct};
use alloy::{
primitives::{
b256, keccak256, normalize_v, Address, ChainId, PrimitiveSignature as Signature, B256,
},
signers::SignerSync,
sol_types::{eip712_domain, Eip712Domain, SolStruct},
};

use crate::{allocation_id::AllocationId, deployment_id::DeploymentId};

Expand Down Expand Up @@ -38,7 +42,7 @@ pub struct Attestation {
pub v: u8,
}

alloy_sol_types::sol! {
alloy::sol_types::sol! {
/// EIP-712 receipt struct for attestation signing.
struct Receipt {
bytes32 requestCID;
Expand Down Expand Up @@ -144,12 +148,11 @@ pub fn recover_allocation(
domain: &Eip712Domain,
attestation: &Attestation,
) -> Result<AllocationId, VerificationError> {
let signature = Signature::from_rs_and_parity(
attestation.r.into(),
attestation.s.into(),
attestation.v as u64,
)
.map_err(|_| VerificationError::FailedSignerRecovery)?;
// Recover the signature components
let signature_parity =
normalize_v(attestation.v as u64).ok_or(VerificationError::FailedSignerRecovery)?;
let signature_r = attestation.r.into();
let signature_s = attestation.s.into();

// Calculate the signing hash
let msg = Receipt {
Expand All @@ -160,21 +163,22 @@ pub fn recover_allocation(
let signing_hash = msg.eip712_signing_hash(domain);

// Recover the allocation ID from the signature
signature
Signature::new(signature_r, signature_s, signature_parity)
.recover_address_from_prehash(&signing_hash)
.map(Into::into)
.map_err(|_| VerificationError::FailedSignerRecovery)
}

#[cfg(test)]
mod tests {
use alloy_primitives::{b256, ChainId, B256};
use alloy_signer::SignerSync;
use alloy_signer_local::PrivateKeySigner;
use alloy_sol_types::Eip712Domain;
use alloy::{
primitives::{address, b256, Address, ChainId, B256},
signers::{local::PrivateKeySigner, SignerSync},
sol_types::Eip712Domain,
};

use super::{create, eip712_domain, verify, Attestation};
use crate::{address, deployment_id, Address, DeploymentId};
use crate::{deployment_id, DeploymentId};

const CHAIN_ID: ChainId = 1337;
const DISPUTE_MANAGER_ADDRESS: Address = address!("16def7e0108a5467a106DBd7537F8591F470342e");
Expand Down
2 changes: 1 addition & 1 deletion thegraph-core/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A pointer to a block in the chain.
use alloy_primitives::{BlockHash, BlockNumber};
use alloy::primitives::{BlockHash, BlockNumber};

/// A pointer to a block in the chain.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
2 changes: 1 addition & 1 deletion thegraph-core/src/client/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub mod meta {
}

pub mod page {
use alloy_primitives::{BlockHash, BlockNumber};
use alloy::primitives::{BlockHash, BlockNumber};
use indoc::indoc;
use serde::{ser::SerializeMap as _, Deserialize, Serialize, Serializer};
use serde_json::value::RawValue;
Expand Down
2 changes: 1 addition & 1 deletion thegraph-core/src/client/subgraph_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{atomic::AtomicU64, Arc};

use alloy_primitives::aliases::BlockNumber;
use alloy::primitives::BlockNumber;
use serde::de::Deserialize;
use thegraph_graphql_http::{
graphql::IntoDocument, http::request::IntoRequestParameters, http_client::ResponseError,
Expand Down
8 changes: 5 additions & 3 deletions thegraph-core/src/deployment_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::B256;
use alloy::primitives::B256;

/// Subgraph deployment ID parsing error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
Expand Down Expand Up @@ -234,7 +234,8 @@ fn parse_hex_str(value: &str) -> Result<DeploymentId, ParseDeploymentIdError> {
/// assert_eq!(DEPLOYMENT_ID, DeploymentId::ZERO);
/// ```
#[macro_export]
macro_rules! deployment_id {
#[doc(hidden)]
macro_rules! __deployment_id {
() => {
$crate::DeploymentId::ZERO
};
Expand Down Expand Up @@ -275,11 +276,12 @@ pub const fn __parse_cid_v0_const(value: &str) -> B256 {
mod tests {
use std::str::FromStr;

use alloy_primitives::{b256, B256};
use alloy::primitives::{b256, B256};

use super::{
format_cid_v0, parse_cid_v0_str, parse_hex_str, DeploymentId, ParseDeploymentIdError,
};
use crate::deployment_id;

const VALID_CID: &str = "QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz";
const VALID_HEX: &str = "0x7d5a99f603f231d53a4f39d1521f98d2e8bb279cf29bebfd0687dc98458e7f89";
Expand Down
14 changes: 9 additions & 5 deletions thegraph-core/src/indexer_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::Address;
use alloy::primitives::Address;

/// A unique identifier for an indexer: the indexer's Ethereum address.
///
Expand Down Expand Up @@ -189,18 +189,22 @@ impl serde::Serialize for IndexerId {
/// If no argument is provided, the macro will create an `IndexerId` with the zero address:
///
/// ```rust
/// use thegraph_core::{Address, indexer_id, IndexerId};
/// use thegraph_core::{
/// alloy::primitives::Address,
/// indexer_id, IndexerId
/// };
///
/// const INDEXER_ID: IndexerId = indexer_id!();
///
/// assert_eq!(INDEXER_ID, Address::ZERO);
/// ```
#[macro_export]
macro_rules! indexer_id {
#[doc(hidden)]
macro_rules! __indexer_id {
() => {
$crate::IndexerId::new($crate::alloy_primitives::Address::ZERO)
$crate::IndexerId::new($crate::alloy::primitives::Address::ZERO)
};
($value:tt) => {
$crate::IndexerId::new($crate::alloy_primitives::address!($value))
$crate::IndexerId::new($crate::alloy::primitives::address!($value))
};
}
27 changes: 23 additions & 4 deletions thegraph-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
//! Rust core modules for _The Graph_ network.
//!
//! # Re-exports
//!
//! This crate re-exports the `alloy` crate, which provides core types, traits and macros.
//!
//! As this crate exports types from the `alloy` crate, it is recommended to use the re-exported
//! types and traits from this crate instead of importing the `alloy` crate directly in order to
//! avoid future version conflicts.
#[doc(inline)]
pub use alloy_primitives::{address, Address, BlockHash, BlockNumber, BlockTimestamp, ChainId};
#[doc(hidden)]
pub use {::alloy_primitives, ::alloy_signer, ::alloy_sol_types};
// Re-export `alloy` crate
pub use alloy;

#[doc(inline)]
pub use self::{
Expand All @@ -15,6 +21,7 @@ pub use self::{
proof_of_indexing::ProofOfIndexing,
subgraph_id::{ParseSubgraphIdError, SubgraphId},
};
// Re-export functions required by the `deployment_id!(...)` and `subgraph_id!(...)` macros.
#[doc(hidden)]
pub use self::{deployment_id::__parse_cid_v0_const, subgraph_id::__parse_subgraph_id_const};

Expand All @@ -27,3 +34,15 @@ mod deployment_id;
mod indexer_id;
mod proof_of_indexing;
mod subgraph_id;

// Export macros
#[doc(inline)]
pub use __allocation_id as allocation_id;
#[doc(inline)]
pub use __deployment_id as deployment_id;
#[doc(inline)]
pub use __indexer_id as indexer_id;
#[doc(inline, alias = "poi")]
pub use __proof_of_indexing as proof_of_indexing;
#[doc(inline)]
pub use __subgraph_id as subgraph_id;
15 changes: 8 additions & 7 deletions thegraph-core/src/proof_of_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The POI is essentially a signature over a message digest that is generated during the indexing
//! of a subgraph from genesis. Each time a subgraph’s state is updated, so does the message digest.
use alloy_primitives::B256;
use alloy::primitives::B256;

/// A Proof of Indexing, "POI", is a cryptographic proof submitted by indexers to demonstrate that
/// they have accurately indexed a subgraph.
Expand Down Expand Up @@ -107,27 +107,28 @@ impl serde::Serialize for ProofOfIndexing {
/// To create an `ProofOfIndexing` from a string literal (no `0x` prefix) at compile time:
///
/// ```rust
/// use thegraph_core::{poi, ProofOfIndexing};
/// use thegraph_core::{proof_of_indexing, ProofOfIndexing};
///
/// const PROOF_OF_INDEXING: ProofOfIndexing =
/// poi!("bb31abb3bb85428d894fb4b3cee8a0889bbe8585939b70910bbdda31b30d2240");
/// proof_of_indexing!("bb31abb3bb85428d894fb4b3cee8a0889bbe8585939b70910bbdda31b30d2240");
/// ```
///
/// If no argument is provided, the macro will create an `ProofOfIndexing` with the zero POI:
///
/// ```rust
/// use thegraph_core::{poi, ProofOfIndexing};
/// use thegraph_core::{proof_of_indexing, ProofOfIndexing};
///
/// const PROOF_OF_INDEXING: ProofOfIndexing = poi!();
/// const PROOF_OF_INDEXING: ProofOfIndexing = proof_of_indexing!();
///
/// assert_eq!(PROOF_OF_INDEXING, ProofOfIndexing::ZERO);
/// ```
#[macro_export]
macro_rules! poi {
#[doc(hidden)]
macro_rules! __proof_of_indexing {
() => {
$crate::ProofOfIndexing::ZERO
};
($id:tt) => {
$crate::ProofOfIndexing::new($crate::alloy_primitives::b256!($id))
$crate::ProofOfIndexing::new($crate::alloy::primitives::b256!($id))
};
}
11 changes: 6 additions & 5 deletions thegraph-core/src/subgraph_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::B256;
use alloy::primitives::B256;

/// Subgraph ID parsing error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
Expand Down Expand Up @@ -155,7 +155,8 @@ impl std::fmt::Debug for SubgraphId {
/// assert_eq!(SUBGRAPH_ID, SubgraphId::ZERO);
/// ```
#[macro_export]
macro_rules! subgraph_id {
#[doc(hidden)]
macro_rules! __subgraph_id {
() => {
$crate::SubgraphId::ZERO
};
Expand All @@ -174,10 +175,10 @@ pub const fn __parse_subgraph_id_const(value: &str) -> B256 {

#[cfg(test)]
mod tests {
use alloy_primitives::{b256, B256};
use alloy::primitives::{b256, B256};

use super::SubgraphId;
use crate::ParseSubgraphIdError;
use super::{ParseSubgraphIdError, SubgraphId};
use crate::subgraph_id;

const VALID_SUBGRAPH_ID: &str = "7xB3yxxD8okmq4dZPky3eP1nYRgLfZrwMyUQBGo32t4U";

Expand Down

0 comments on commit dda9513

Please sign in to comment.