Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new table to track subgraph features #4679

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions chain/arweave/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unreachable!("arweave does not support dynamic data sources")
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/cosmos/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unimplemented!("{}", TEMPLATE_ERROR);
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/ethereum/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
self.manifest_idx
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/near/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ impl blockchain::DataSourceTemplate<Chain> for DataSourceTemplate {
fn manifest_idx(&self) -> u32 {
unreachable!("near does not support dynamic data sources")
}

fn kind(&self) -> &str {
&self.kind
}
}

#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions chain/substreams/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ impl blockchain::DataSourceTemplate<Chain> for NoopDataSourceTemplate {
fn manifest_idx(&self) -> u32 {
todo!()
}

fn kind(&self) -> &str {
unimplemented!("{}", TEMPLATE_ERROR);
}
}

#[async_trait]
Expand Down
1 change: 1 addition & 0 deletions core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ async fn create_subgraph_version<C: Blockchain, S: SubgraphStore>(
name,
&manifest.schema,
deployment,
manifest.deployment_features(),
node_id,
network_name,
version_switching_mode,
Expand Down
13 changes: 10 additions & 3 deletions graph/src/blockchain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ impl Block for MockBlock {
}

#[derive(Clone)]
pub struct MockDataSource;
pub struct MockDataSource {
pub api_version: semver::Version,
pub kind: String,
}

impl<C: Blockchain> TryFrom<DataSourceTemplateInfo<C>> for MockDataSource {
type Error = Error;
Expand Down Expand Up @@ -71,7 +74,7 @@ impl<C: Blockchain> DataSource<C> for MockDataSource {
}

fn kind(&self) -> &str {
todo!()
self.kind.as_str()
}

fn network(&self) -> Option<&str> {
Expand All @@ -87,7 +90,7 @@ impl<C: Blockchain> DataSource<C> for MockDataSource {
}

fn api_version(&self) -> semver::Version {
todo!()
self.api_version.clone()
}

fn runtime(&self) -> Option<Arc<Vec<u8>>> {
Expand Down Expand Up @@ -157,6 +160,10 @@ impl<C: Blockchain> DataSourceTemplate<C> for MockDataSourceTemplate {
fn manifest_idx(&self) -> u32 {
todo!()
}

fn kind(&self) -> &str {
todo!()
}
}

#[derive(Clone, Default, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions graph/src/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub trait DataSourceTemplate<C: Blockchain>: Send + Sync + Debug {
fn runtime(&self) -> Option<Arc<Vec<u8>>>;
fn name(&self) -> &str;
fn manifest_idx(&self) -> u32;
fn kind(&self) -> &str;
}

#[async_trait]
Expand Down
3 changes: 2 additions & 1 deletion graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::components::server::index_node::VersionInfo;
use crate::components::transaction_receipt;
use crate::components::versions::ApiVersion;
use crate::data::query::Trace;
use crate::data::subgraph::status;
use crate::data::subgraph::{status, DeploymentFeatures};
use crate::data::value::Object;
use crate::data::{query::QueryTarget, subgraph::schema::*};
use crate::schema::{ApiSchema, InputSchema};
Expand Down Expand Up @@ -70,6 +70,7 @@ pub trait SubgraphStore: Send + Sync + 'static {
name: SubgraphName,
schema: &InputSchema,
deployment: DeploymentCreate,
deployment_features: DeploymentFeatures,
node_id: NodeId,
network: String,
mode: SubgraphVersionSwitchingMode,
Expand Down
4 changes: 4 additions & 0 deletions graph/src/data/subgraph/api_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ impl UnifiedMappingApiVersion {

Ok(UnifiedMappingApiVersion(unified_version))
}

pub fn version(&self) -> Option<&Version> {
self.0.as_ref()
}
}

pub(super) fn format_versions(versions: &BTreeSet<Version>) -> String {
Expand Down
49 changes: 48 additions & 1 deletion graph/src/data/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ pub use features::{SubgraphFeature, SubgraphFeatureValidationError};

use anyhow::{anyhow, Context, Error};
use futures03::{future::try_join3, stream::FuturesOrdered, TryStreamExt as _};
use itertools::Itertools;
use semver::Version;
use serde::{de, ser};
use serde_yaml;
use slog::Logger;
use stable_hash::{FieldAddress, StableHash};
use stable_hash_legacy::SequenceNumber;
use std::{
collections::{BTreeSet, HashMap},
collections::{BTreeSet, HashMap, HashSet},
marker::PhantomData,
};
use thiserror::Error;
Expand Down Expand Up @@ -497,6 +498,15 @@ impl Graft {
}
}

#[derive(Clone, Debug)]
pub struct DeploymentFeatures {
pub id: String,
pub spec_version: String,
pub api_version: Option<String>,
pub features: Vec<String>,
pub data_source_kinds: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BaseSubgraphManifest<C, S, D, T> {
Expand Down Expand Up @@ -663,6 +673,43 @@ impl<C: Blockchain> SubgraphManifest<C> {
.chain(self.data_sources.iter().map(|source| source.api_version()))
}

pub fn deployment_features(&self) -> DeploymentFeatures {
let unified_api_version = self.unified_mapping_api_version().ok();
let api_version = unified_api_version
.map(|v| v.version().map(|v| v.to_string()))
.flatten();

let features: Vec<String> = self
.features
.iter()
.map(|f| f.to_string())
.collect::<Vec<_>>();

let spec_version = self.spec_version.to_string();

let mut data_source_kinds = self
.data_sources
.iter()
.map(|ds| ds.kind().to_string())
.collect::<HashSet<_>>();

let data_source_template_kinds = self
.templates
.iter()
.map(|t| t.kind().to_string())
.collect::<Vec<_>>();

data_source_kinds.extend(data_source_template_kinds);

DeploymentFeatures {
id: self.id.to_string(),
api_version,
features,
spec_version,
data_source_kinds: data_source_kinds.into_iter().collect_vec(),
}
}

pub fn runtimes(&self) -> impl Iterator<Item = Arc<Vec<u8>>> + '_ {
self.templates
.iter()
Expand Down
7 changes: 7 additions & 0 deletions graph/src/data_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ impl<C: Blockchain> DataSourceTemplate<C> {
Self::Offchain(ds) => ds.manifest_idx,
}
}

pub fn kind(&self) -> String {
match self {
Self::Onchain(ds) => ds.kind().to_string(),
Self::Offchain(ds) => ds.kind.to_owned(),
}
}
}

#[derive(Clone, Debug)]
Expand Down
5 changes: 4 additions & 1 deletion graph/src/data_source/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ fn data_source_helpers() {
.unwrap()
.is_duplicate_of(&offchain));

let onchain = DataSource::<MockBlockchain>::Onchain(MockDataSource);
let onchain = DataSource::<MockBlockchain>::Onchain(MockDataSource {
api_version: Version::new(1, 0, 0),
kind: "mock/kind".into(),
});
assert!(onchain.causality_region() == CausalityRegion::ONCHAIN);
assert!(onchain.as_offchain().is_none());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE subgraph_features;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Creates a new table subgraph_features
create table if not exists subgraphs.subgraph_features (
id text primary key,
spec_version text not null,
api_version text null,
features text [] not null DEFAULT '{}',
data_sources text [] not null DEFAULT '{}'
);
65 changes: 64 additions & 1 deletion store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use diesel::{
use graph::{
components::store::DeploymentLocator,
constraint_violation,
data::subgraph::status,
data::subgraph::{status, DeploymentFeatures},
prelude::{
anyhow, bigdecimal::ToPrimitive, serde_json, DeploymentHash, EntityChange,
EntityChangeOperation, NodeId, StoreError, SubgraphName, SubgraphVersionSwitchingMode,
Expand Down Expand Up @@ -78,6 +78,16 @@ table! {
}
}

table! {
subgraphs.subgraph_features (id) {
id -> Text,
spec_version -> Text,
api_version -> Nullable<Text>,
features -> Array<Text>,
data_sources -> Array<Text>,
}
incrypto32 marked this conversation as resolved.
Show resolved Hide resolved
}

table! {
subgraphs.subgraph_version (vid) {
vid -> BigInt,
Expand Down Expand Up @@ -1098,6 +1108,59 @@ impl<'a> Connection<'a> {
}
}

pub fn get_subgraph_features(
&self,
id: String,
) -> Result<Option<DeploymentFeatures>, StoreError> {
use subgraph_features as f;

let conn = self.conn.as_ref();
let features = f::table
.filter(f::id.eq(id))
.select((
f::id,
f::spec_version,
f::api_version,
f::features,
f::data_sources,
))
.first::<(String, String, Option<String>, Vec<String>, Vec<String>)>(conn)
.optional()?;

let features = features.map(|(id, spec_version, api_version, features, data_sources)| {
DeploymentFeatures {
id,
spec_version,
api_version,
features,
data_source_kinds: data_sources,
}
});

Ok(features)
}

pub fn create_subgraph_features(&self, features: DeploymentFeatures) -> Result<(), StoreError> {
use subgraph_features as f;

let conn = self.conn.as_ref();
let changes = (
f::id.eq(features.id),
f::spec_version.eq(features.spec_version),
f::api_version.eq(features.api_version),
f::features.eq(features.features),
f::data_sources.eq(features.data_source_kinds),
);

insert_into(f::table)
.values(changes.clone())
.on_conflict(f::id)
.do_update()
.set(changes)
.execute(conn)?;
Ok(())
}

pub fn assign_subgraph(
&self,
site: &Site,
Expand Down
Loading