thegraph_core/proof_of_indexing.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
//! A Proof of Indexing (POI) a cryptographic proof submitted by indexers to demonstrate that they
//! have accurately indexed a subgraph.
//!
//! 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;
/// A Proof of Indexing, "POI", is a cryptographic proof submitted by indexers to demonstrate that
/// they have accurately indexed a subgraph.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias = "poi")]
pub struct ProofOfIndexing(B256);
impl ProofOfIndexing {
/// The "zero" [`ProofOfIndexing`].
///
/// This is a constant value that represents the zero POI. It is equivalent to parsing a zeroed
/// 32-byte array.
pub const ZERO: Self = Self(B256::ZERO);
/// Creates a new [`ProofOfIndexing`].
pub const fn new(bytes: B256) -> Self {
Self(bytes)
}
}
impl std::fmt::Display for ProofOfIndexing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::fmt::Debug for ProofOfIndexing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ProofOfIndexing({})", self.0)
}
}
impl From<B256> for ProofOfIndexing {
fn from(bytes: B256) -> Self {
Self(bytes)
}
}
impl From<[u8; 32]> for ProofOfIndexing {
fn from(value: [u8; 32]) -> Self {
Self(B256::from(value))
}
}
impl From<ProofOfIndexing> for B256 {
fn from(id: ProofOfIndexing) -> Self {
id.0
}
}
impl From<&ProofOfIndexing> for B256 {
fn from(id: &ProofOfIndexing) -> Self {
id.0
}
}
impl AsRef<B256> for ProofOfIndexing {
fn as_ref(&self) -> &B256 {
&self.0
}
}
impl std::ops::Deref for ProofOfIndexing {
type Target = B256;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl PartialEq<B256> for ProofOfIndexing {
fn eq(&self, other: &B256) -> bool {
self.0.eq(other)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for ProofOfIndexing {
fn deserialize<D>(deserializer: D) -> Result<ProofOfIndexing, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = B256::deserialize(deserializer)?;
Ok(ProofOfIndexing(bytes))
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for ProofOfIndexing {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
/// Converts a sequence of string literals containing hex-encoded data into a new
/// [`ProofOfIndexing`] at compile time.
///
/// To create an `ProofOfIndexing` from a string literal (no `0x` prefix) at compile time:
///
/// ```rust
/// use thegraph_core::{proof_of_indexing, ProofOfIndexing};
///
/// const PROOF_OF_INDEXING: ProofOfIndexing =
/// proof_of_indexing!("bb31abb3bb85428d894fb4b3cee8a0889bbe8585939b70910bbdda31b30d2240");
/// ```
///
/// If no argument is provided, the macro will create an `ProofOfIndexing` with the zero POI:
///
/// ```rust
/// use thegraph_core::{proof_of_indexing, ProofOfIndexing};
///
/// const PROOF_OF_INDEXING: ProofOfIndexing = proof_of_indexing!();
///
/// assert_eq!(PROOF_OF_INDEXING, ProofOfIndexing::ZERO);
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! __proof_of_indexing {
() => {
$crate::ProofOfIndexing::ZERO
};
($id:tt) => {
$crate::ProofOfIndexing::new($crate::alloy::primitives::b256!($id))
};
}