|
| 1 | +use std::{ |
| 2 | + array::TryFromSliceError, |
| 3 | + fmt::Display, |
| 4 | + str::FromStr, |
| 5 | +}; |
| 6 | + |
| 7 | +use async_graphql::{ |
| 8 | + EmptyMutation, |
| 9 | + EmptySubscription, |
| 10 | + InputValueError, |
| 11 | + ScalarType, |
| 12 | +}; |
| 13 | +use hex::FromHexError; |
| 14 | + |
| 15 | +use crate::ports; |
| 16 | + |
| 17 | +/// The schema of the state root service. |
| 18 | +pub type Schema<Storage> = |
| 19 | + async_graphql::Schema<Query<Storage>, EmptyMutation, EmptySubscription>; |
| 20 | + |
| 21 | +/// The query type of the schema. |
| 22 | +pub struct Query<Storage> { |
| 23 | + storage: Storage, |
| 24 | +} |
| 25 | + |
| 26 | +impl<Storage> Query<Storage> { |
| 27 | + /// Create a new query object. |
| 28 | + pub fn new(storage: Storage) -> Self { |
| 29 | + Self { storage } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[async_graphql::Object] |
| 34 | +impl<Storage> Query<Storage> |
| 35 | +where |
| 36 | + Storage: ports::GetStateRoot + Send + Sync, |
| 37 | +{ |
| 38 | + async fn state_root( |
| 39 | + &self, |
| 40 | + height: BlockHeight, |
| 41 | + ) -> async_graphql::Result<Option<MerkleRoot>> { |
| 42 | + let state_root = self.storage.state_root_at(height.0.into())?; |
| 43 | + |
| 44 | + Ok(state_root.map(|root| MerkleRoot(*root))) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// GraphQL scalar type for block height. |
| 49 | +#[derive( |
| 50 | + Clone, |
| 51 | + Copy, |
| 52 | + Debug, |
| 53 | + PartialEq, |
| 54 | + Eq, |
| 55 | + PartialOrd, |
| 56 | + Ord, |
| 57 | + derive_more::FromStr, |
| 58 | + derive_more::Display, |
| 59 | +)] |
| 60 | +pub struct BlockHeight(u32); |
| 61 | + |
| 62 | +/// GraphQL scalar type for the merkle root. |
| 63 | +#[derive(Clone, Copy, Debug)] |
| 64 | +pub struct MerkleRoot([u8; 32]); |
| 65 | + |
| 66 | +impl Display for MerkleRoot { |
| 67 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 68 | + write!(f, "0x{}", hex::encode(self.0)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl FromStr for MerkleRoot { |
| 73 | + type Err = MerkleRootParseError; |
| 74 | + |
| 75 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 76 | + let bytes = hex::decode(s.trim_start_matches("0x"))?; |
| 77 | + Ok(MerkleRoot(bytes.as_slice().try_into()?)) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// Error type for the merkle root parsing. |
| 82 | +#[derive(Clone, Debug, derive_more::Display, derive_more::From, derive_more::Error)] |
| 83 | +pub enum MerkleRootParseError { |
| 84 | + /// The merkle root isn't valid hex. |
| 85 | + DecodeFailure(FromHexError), |
| 86 | + /// The merkle root has the wrong number of bytes. |
| 87 | + WrongLength(TryFromSliceError), |
| 88 | +} |
| 89 | + |
| 90 | +macro_rules! impl_scalar_type { |
| 91 | + ($type:ty) => { |
| 92 | + #[async_graphql::Scalar] |
| 93 | + impl ScalarType for $type { |
| 94 | + fn parse( |
| 95 | + value: async_graphql::Value, |
| 96 | + ) -> async_graphql::InputValueResult<Self> { |
| 97 | + let async_graphql::Value::String(text) = value else { |
| 98 | + return Err(InputValueError::expected_type(value)) |
| 99 | + }; |
| 100 | + |
| 101 | + text.parse().map_err(InputValueError::custom) |
| 102 | + } |
| 103 | + |
| 104 | + fn to_value(&self) -> async_graphql::Value { |
| 105 | + async_graphql::Value::String(self.to_string()) |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +impl_scalar_type!(MerkleRoot); |
| 112 | +impl_scalar_type!(BlockHeight); |
0 commit comments