Skip to content

Commit

Permalink
feat(thegraph-core): use 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 8, 2024
1 parent 1346749 commit 7f03a88
Show file tree
Hide file tree
Showing 12 changed files with 1,346 additions and 101 deletions.
1,366 changes: 1,302 additions & 64 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 @@ -10,8 +10,9 @@ rust-version = "1.71.1"

[features]
default = ["serde"]
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 @@ -24,8 +25,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 @@ -40,7 +40,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
6 changes: 3 additions & 3 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 @@ -198,9 +198,9 @@ impl serde::Serialize for AllocationId {
#[macro_export]
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))
};
}
34 changes: 19 additions & 15 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,18 +163,19 @@ 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::{b256, ChainId, B256},
signers::{local::PrivateKeySigner, SignerSync},
sol_types::Eip712Domain,
};

use super::{create, eip712_domain, verify, Attestation};
use crate::{address, deployment_id, Address, DeploymentId};
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
4 changes: 2 additions & 2 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 @@ -275,7 +275,7 @@ 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,
Expand Down
6 changes: 3 additions & 3 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 @@ -198,9 +198,9 @@ impl serde::Serialize for IndexerId {
#[macro_export]
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))
};
}
9 changes: 6 additions & 3 deletions thegraph-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Rust core modules for _The Graph_ network.

#[doc(inline)]
pub use alloy_primitives::{address, Address, BlockHash, BlockNumber, BlockTimestamp, ChainId};
// Re-export `alloy` crate
#[doc(hidden)]
pub use {::alloy_primitives, ::alloy_signer, ::alloy_sol_types};
pub use ::alloy;
// Re-export some primitive `alloy` types for convenience
#[doc(inline)]
pub use alloy::primitives::{address, Address, BlockHash, BlockNumber, BlockTimestamp, ChainId};

#[doc(inline)]
pub use self::{
Expand All @@ -15,6 +17,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 Down
4 changes: 2 additions & 2 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 @@ -128,6 +128,6 @@ macro_rules! poi {
$crate::ProofOfIndexing::ZERO
};
($id:tt) => {
$crate::ProofOfIndexing::new($crate::alloy_primitives::b256!($id))
$crate::ProofOfIndexing::new($crate::alloy::primitives::b256!($id))
};
}
4 changes: 2 additions & 2 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 @@ -174,7 +174,7 @@ 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;
Expand Down

0 comments on commit 7f03a88

Please sign in to comment.