Skip to content

Commit

Permalink
Stronger type safety around IPFS CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
neysofu committed Apr 14, 2024
1 parent 8632f27 commit c057bed
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 181 deletions.
112 changes: 105 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
[workspace]
resolver = "2"
members = ["crates/*"]

[workspace.dependencies]
anyhow = "1"
async-graphql = "7"
async-graphql-axum = "7"
async-trait = "0.1.52"
axum = "0.7"
bigdecimal = "0.4"
chrono = "0.4"
cid = "0.11"
clap = "4"
derive_more = "0.99"
diesel = "2"
diesel-async = "0.4"
diesel_async_migrations = "0.12"
futures = "0.3.18"
graphql_client = "0.13"
hex = "0.4.3"
itertools = "0.12"
nanoid = "0.4.0"
num-traits = "0.2"
once_cell = "1.9.0"
prometheus = { version = "0.13", default-features = false }
prometheus_exporter = "0.8.5"
quickcheck = "1"
quickcheck_macros = "1"
rand = "0.8.4"
reqwest = "0.11"
schemars = "0.8"
serde = "1"
serde_json = "1"
serde_yaml = "0.9"
testcontainers = "0.15"
testcontainers-modules = "0.3"
thiserror = "1"
tokio = "1.14.0"
tracing = "0.1.29"
tracing-subscriber = "0.3.2"
tracing-test = "0.2.1"
url = "2.5"
uuid = "1"
4 changes: 2 additions & 2 deletions crates/autogen_config_json_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ edition = "2021"

[build-dependencies]
graphix_lib = { path = "../graphix_lib" }
schemars = "0.8"
serde_json = "1"
schemars = { workspace = true }
serde_json = { workspace = true }
6 changes: 2 additions & 4 deletions crates/autogen_config_json_schema/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ fn main() -> std::io::Result<()> {
// See <https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection>.
println!("cargo:rerun-if-changed=build.rs");

let path = env::current_dir()?.join("schema.json");

let mut f = File::create(&path)?;
let out_path = env::current_dir()?.join("schema.json");
let mut f = File::create(&out_path)?;

let schema = schema_for!(Config);
f.write_all(serde_json::to_string_pretty(&schema).unwrap().as_bytes())?;

println!("Updated: {}", path.display());
Ok(())
}
2 changes: 1 addition & 1 deletion crates/autogen_graphql_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021"

[build-dependencies]
async-graphql = "7"
async-graphql = { workspace = true }
graphix_lib = { path = "../graphix_lib" }
13 changes: 7 additions & 6 deletions crates/autogen_graphql_schema/api_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ type IndexerNetworkSubgraphMetadata {
}


scalar IpfsCid

type MutationRoot {
"""
Launches a divergence investigation, which is a process of comparing
Expand Down Expand Up @@ -313,10 +315,9 @@ input PoisQuery {
"""
network: String
"""
Restricts the query to PoIs for these given subgraph deployments (by
hex-encoded IPFS CID with '0x' prefix).
Restricts the query to PoIs for these given subgraph deployment IDs.
"""
deployments: [String!]!
deployments: [IpfsCid!]!
"""
Restricts the query to PoIs that were collected in the given block
range.
Expand Down Expand Up @@ -356,7 +357,7 @@ type QueryRoot {
"""
The network name of the subgraph deployments to fetch
"""
networkName: String, name: String, ipfsCid: String,
networkName: String, name: String, ipfsCid: IpfsCid,
"""
Upper limit on the number of shown results.
"""
Expand Down Expand Up @@ -388,7 +389,7 @@ type QueryRoot {
"""
Restricts the query to PoIs for these given subgraph deployments (by hex-encoded IPFS CID with '0x' prefix).
"""
deployments: [String!]! = [],
deployments: [IpfsCid!]! = [],
"""
Restricts the query to PoIs that were collected in the given block range.
"""
Expand Down Expand Up @@ -424,7 +425,7 @@ type SubgraphDeployment {
"""
IPFS CID of the subgraph deployment.
"""
cid: String!
cid: IpfsCid!
"""
Human-readable name of the subgraph deployment, if present.
"""
Expand Down
23 changes: 12 additions & 11 deletions crates/common_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
async-graphql = { version = "7", features = ["chrono", "uuid"] }
chrono = "0.4"
diesel = "2"
hex = "0.4"
quickcheck = "1"
schemars = "0.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
uuid = "1"
anyhow = { workspace = true }
async-graphql = { workspace = true, features = ["chrono", "uuid"] }
chrono = { workspace = true }
cid = { workspace = true, features = ["serde", "arb"] }
diesel = { workspace = true }
hex = { workspace = true }
quickcheck = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
uuid = { workspace = true }

[dev-dependencies]
quickcheck_macros = "1"
quickcheck_macros = { workspace = true }
19 changes: 8 additions & 11 deletions crates/common_types/src/hex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::str::FromStr;
use diesel::backend::Backend;
use diesel::deserialize::{FromSql, FromSqlRow};
use diesel::expression::AsExpression;
use diesel::pg::Pg;
use diesel::serialize::ToSql;
use diesel::sql_types;
use hex::FromHex;
Expand All @@ -24,7 +25,7 @@ use serde::{Deserialize, Serialize};
// TODO: The fact that we SQL-encode all kinds of hex strings, even fixed-length
// ones, as variable-length byte sequences is a bit of a wart. Not that big of a
// deal, but maybe there's a better way to do this.
#[diesel(sql_type = ::diesel::sql_types::Binary)]
#[diesel(sql_type = sql_types::Binary)]
pub struct HexString<T>(pub T);

impl<T: ToOwned> HexString<T> {
Expand Down Expand Up @@ -102,29 +103,25 @@ impl<T> schemars::JsonSchema for HexString<T> {
}
}

impl<T, Db> ToSql<sql_types::Binary, Db> for HexString<T>
impl<T> ToSql<sql_types::Binary, Pg> for HexString<T>
where
T: AsRef<[u8]> + Debug,
Db: Backend,
[u8]: ToSql<sql_types::Binary, Db>,
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, Db>,
out: &mut diesel::serialize::Output<'b, '_, Pg>,
) -> diesel::serialize::Result {
ToSql::<sql_types::Binary, Db>::to_sql(self.0.as_ref(), out)
ToSql::<sql_types::Binary, Pg>::to_sql(self.0.as_ref(), out)
}
}

impl<T, Db> FromSql<sql_types::Binary, Db> for HexString<T>
impl<T> FromSql<sql_types::Binary, Pg> for HexString<T>
where
T: TryFrom<Vec<u8>>,
T::Error: Debug,
Db: Backend,
Vec<u8>: FromSql<sql_types::Binary, Db>,
{
fn from_sql(bytes: <Db as Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> {
T::try_from(Vec::from_sql(bytes)?)
fn from_sql(bytes: <Pg as Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> {
T::try_from(FromSql::<sql_types::Binary, Pg>::from_sql(bytes)?)
.map(HexString)
.map_err(|e| anyhow::anyhow!("{:?}", e).into())
}
Expand Down
Loading

0 comments on commit c057bed

Please sign in to comment.