Skip to content

Commit

Permalink
Add query reverse resolution feature #108
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongFuze committed Oct 25, 2023
1 parent 3ef3eb8 commit 5ec78bd
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 65 deletions.
68 changes: 60 additions & 8 deletions src/config/tdb/migrations/DBImportExport_IdentityGraph.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,27 @@ CREATE OR REPLACE QUERY neighbors_with_source(VERTEX<Identities> p, INT depth=10
}


CREATE QUERY identity_by_source(VERTEX<Identities> p, STRING source) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY identity_by_source(VERTEX<Identities> p, STRING source) FOR GRAPH IdentityGraph SYNTAX V2 {
start (Identities) = {p};
vertices = SELECT v FROM start-((Proof_Forward>|Proof_Backward>):e)-Identities:v
WHERE e.source == source;
PRINT vertices;
}


CREATE QUERY identities_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY identities_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
vertices = SELECT s FROM Identities:s WHERE s.id IN ids;
PRINT vertices;
}

CREATE QUERY identity_owned_by(VERTEX<Identities> p, STRING platform) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY identity_owned_by(VERTEX<Identities> p, STRING platform) FOR GRAPH IdentityGraph SYNTAX V2 {
start (Identities) = {p};
owner = SELECT v FROM start-((<Hold_Identity):e)-Identities:v
WHERE e.source == platform;
PRINT owner;
}

CREATE QUERY hold_nft(STRING id, STRING chain, STRING address) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY hold_nft(STRING id, STRING chain, STRING address) FOR GRAPH IdentityGraph SYNTAX V2 {
SetAccum<EDGE> @@holds;
contract = SELECT v FROM Identities:s-((Hold_Contract>):e)-Contracts:v
WHERE v.chain == chain AND v.address == address AND e.id == id
Expand All @@ -134,7 +134,7 @@ CREATE QUERY hold_nft(STRING id, STRING chain, STRING address) FOR GRAPH Identit
PRINT @@holds AS holds;
}

CREATE QUERY nfts(VERTEX<Identities> p, SET<STRING> categories, INT numPerPage = 100, INT pageNum = 0) FOR GRAPH IdentityGraph {
CREATE OR REPLACE QUERY nfts(VERTEX<Identities> p, SET<STRING> categories, INT numPerPage = 100, INT pageNum = 0) FOR GRAPH IdentityGraph {
SetAccum<EDGE> @@edges;
start (Identities) = {p};

Expand All @@ -153,7 +153,59 @@ CREATE QUERY nfts(VERTEX<Identities> p, SET<STRING> categories, INT numPerPage =
PRINT @@edges AS edges;
}

CREATE QUERY domain(STRING name, STRING system) FOR GRAPH IdentityGraph {
CREATE OR REPLACE QUERY reverse_domains(VERTEX<Identities> p, SET<STRING> domainSystems) FOR GRAPH IdentityGraph {
OrAccum @reverse = FALSE;
SetAccum<EDGE> @@reverse_records;

seed (Identities) = {p};
identity_record = SELECT v FROM seed-((Reverse_Resolve>):e1)-Identities:v-((Resolve>):e2)-seed
ACCUM @@reverse_records += e1;
contract_record = SELECT v FROM seed-((Reverse_Resolve_Contract>):e1)-Contracts:v-((Resolve_Contract>):e2)-seed
ACCUM @@reverse_records += e1;

PRINT @@reverse_records as reverse_records;
}


CREATE OR REPLACE QUERY domain2(STRING name, STRING system) FOR GRAPH IdentityGraph {
EDGE record;
EDGE hold;
EDGE reverse_edge;
OrAccum @@reverse;
IF system == "ENS" THEN
resolved = SELECT v FROM Contracts:c-((Resolve_Contract>):e)-Identities:v
WHERE e.system == system AND e.name == name
ACCUM
record = e;
reverse_record = SELECT v FROM Identities:v-((Reverse_Resolve_Contract>):e)-Contracts:c
WHERE e.system == system AND e.name == name
ACCUM
reverse_edge = e,
@@reverse += TRUE;
owner = SELECT s FROM Identities:s-((Hold_Contract>):e)-Contracts:v
WHERE e.id == name
ACCUM
hold = e;
PRINT record, hold, resolved, owner, @@reverse as reverse, reverse_record;
ELSE
resolved = SELECT v FROM Identities:s-((Resolve>):e)-Identities:v
WHERE e.system == system AND e.name == name
ACCUM
record = e;
reverse_record = SELECT v FROM Identities:v-((Reverse_Resolve>):e)-Identities:tgt
WHERE e.system == system AND e.name == name
ACCUM
reverse_edge = e,
@@reverse += TRUE;
owner = SELECT s FROM Identities:s-((Hold_Identity>):e)-Identities:v
WHERE v.platform == system AND v.identity == name
ACCUM
hold = e;
PRINT record, hold, resolved, owner, @@reverse as reverse, reverse_record;
END;
}

CREATE OR REPLACE QUERY domain(STRING name, STRING system) FOR GRAPH IdentityGraph {
EDGE record;
EDGE hold;
IF system == "ENS" THEN
Expand All @@ -179,12 +231,12 @@ CREATE QUERY domain(STRING name, STRING system) FOR GRAPH IdentityGraph {
END;
}

CREATE QUERY contracts_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY contracts_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
vertices = SELECT s FROM Contracts:s WHERE s.id IN ids;
PRINT vertices;
}

CREATE QUERY owners_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
CREATE OR REPLACE QUERY owners_by_ids(SET<STRING> ids) FOR GRAPH IdentityGraph SYNTAX V2 {
vertices = SELECT s FROM Identities:s WHERE s.id IN ids;
PRINT vertices;
}
Expand Down
3 changes: 2 additions & 1 deletion src/config/tdb/migrations/global.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ CREATE VERTEX Contracts(PRIMARY_ID id STRING, id STRING, uuid STRING, category S
CREATE DIRECTED EDGE Proof_Forward(FROM Identities, TO Identities, DISCRIMINATOR(source STRING), created_at DATETIME, uuid STRING, level INT, record_id STRING, updated_at DATETIME, fetcher STRING) WITH REVERSE_EDGE="Proof_Backward"
CREATE DIRECTED EDGE Hold_Identity(FROM Identities, TO Identities, DISCRIMINATOR(source STRING), uuid STRING, transaction STRING, id STRING, created_at DATETIME, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Hold_Contract(FROM Identities, TO Contracts, DISCRIMINATOR(source STRING, transaction STRING, id STRING), uuid STRING, created_at DATETIME, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Resolve(FROM Identities, TO Identities, DISCRIMINATOR(source STRING, system STRING, name STRING), uuid STRING, updated_at DATETIME, fetcher STRING) WITH REVERSE_EDGE="Reverse_Resolve"
CREATE DIRECTED EDGE Resolve(FROM Identities, TO Identities, DISCRIMINATOR(source STRING, system STRING, name STRING), uuid STRING, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Reverse_Resolve(FROM Identities, TO Identities, DISCRIMINATOR(source STRING, system STRING, name STRING), uuid STRING, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Resolve_Contract(FROM Contracts, TO Identities, DISCRIMINATOR(source STRING, system STRING, name STRING), uuid STRING, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Reverse_Resolve_Contract(FROM Identities, TO Contracts, DISCRIMINATOR(source STRING, system STRING, name STRING), uuid STRING, updated_at DATETIME, fetcher STRING)
CREATE DIRECTED EDGE Relation_Unique_TX(FROM Identities, TO Identities, tx_count INT, tx_sum INT, tx_max INT, tx_min INT, updated_at DATETIME)
Expand Down
43 changes: 23 additions & 20 deletions src/config/tdb/migrations/run_loading_jobs.gsql
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,28 @@ RUN LOADING JOB Load_CrossbellSocialFeed_Only


CREATE LOADING JOB Load_DBExport FOR GRAPH IdentityGraph {
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Identities.csv"
TO VERTEX Identities VALUES ($"primary_id", $"id", $"uuid", $"platform", $"identity", $"display_name", $"profile_url", $"avatar_url", $"created_at", $"added_at", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Contracts.csv"
TO VERTEX Contracts VALUES ($"primary_id", $"id", $"uuid", $"category", $"address", $"chain", $"symbol", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Proof_Forward.csv"
TO EDGE Proof_Forward VALUES ($"from", $"to", $"source", $"created_at", $"uuid", $"level", $"record_id", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Hold_Identity.csv"
TO EDGE Hold_Identity VALUES ($"from", $"to", $"source", $"uuid", $"transaction", $"id", $"created_at", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Hold_Contract.csv"
TO EDGE Hold_Contract VALUES ($"from", $"to", $"source", $"transaction", $"id", $"uuid", $"created_at", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Resolve.csv"
TO EDGE Resolve VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Resolve_Contract.csv"
TO EDGE Resolve_Contract VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Reverse_Resolve_Contract.csv"
TO EDGE Reverse_Resolve_Contract VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Relation_Unique_TX.csv"
TO EDGE Relation_Unique_TX VALUES ($"from", $"to", $"tx_count", $"tx_sum", $"tx_max", $"tx_min", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Relation_TX.csv"
TO EDGE Relation_TX VALUES ($"from", $"to", $"transaction_hash", $"transaction_index", $"tx_type", $"tx_status", $"block_hash", $"block_number", $"block_timestamp", $"from_address", $"to_address", $"tx_value", $"contract_address", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Identities.csv"
TO VERTEX Identities VALUES ($"primary_id", $"id", $"uuid", $"platform", $"identity", $"display_name", $"profile_url", $"avatar_url", $"created_at", $"added_at", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Contracts.csv"
TO VERTEX Contracts VALUES ($"primary_id", $"id", $"uuid", $"category", $"address", $"chain", $"symbol", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Proof_Forward.csv"
TO EDGE Proof_Forward VALUES ($"from", $"to", $"source", $"created_at", $"uuid", $"level", $"record_id", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Hold_Identity.csv"
TO EDGE Hold_Identity VALUES ($"from", $"to", $"source", $"uuid", $"transaction", $"id", $"created_at", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Hold_Contract.csv"
TO EDGE Hold_Contract VALUES ($"from", $"to", $"source", $"transaction", $"id", $"uuid", $"created_at", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Resolve.csv"
TO EDGE Resolve VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Resolve_Contract.csv"
TO EDGE Resolve_Contract VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Reverse_Resolve_Contract.csv"
TO EDGE Reverse_Resolve_Contract VALUES ($"from", $"to", $"source", $"system", $"name", $"uuid", $"updated_at", $"fetcher") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Relation_Unique_TX.csv"
TO EDGE Relation_Unique_TX VALUES ($"from", $"to", $"tx_count", $"tx_sum", $"tx_max", $"tx_min", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Relation_TX.csv"
TO EDGE Relation_TX VALUES ($"from", $"to", $"transaction_hash", $"transaction_index", $"tx_type", $"tx_status", $"block_hash", $"block_number", $"block_timestamp", $"from_address", $"to_address", $"tx_value", $"contract_address", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
LOAD "/home/tigergraph/shared_data/export_graphs/GlobalTypes/Social_Feed.tsv"
TO EDGE Social_Feed VALUES ($"from", $"to", $"source", $"action", $"action_count", $"updated_at") USING SEPARATOR = "\t", EOL = "\n", HEADER = "true";
}

RUN LOADING JOB Load_DBExport
14 changes: 10 additions & 4 deletions src/controller/tigergraphql/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::{Error, Result},
tigergraph::{
delete_vertex_and_edge,
edge::{EdgeUnion, HoldRecord},
edge::{resolve::ResolveReverse, EdgeUnion, HoldRecord},
vertex::{Identity, IdentityRecord, IdentityWithSource},
},
upstream::{fetch_all, ContractCategory, DataSource, Platform, Target},
Expand Down Expand Up @@ -123,7 +123,6 @@ impl IdentityRecord {
}

/// Neighbor identity from current. Flattened.
// FIXME: <2023-04-23 SUN> broken of high CPU / bandwidth consumption. Maybe something is wrong with SQL.
async fn neighbor(
&self,
_ctx: &Context<'_>,
Expand All @@ -137,6 +136,7 @@ impl IdentityRecord {
self.neighbors(&client, depth.unwrap_or(1)).await
}

/// Neighbor identity from current. The entire topology can be restored by return records.
async fn neighbor_with_traversal(
&self,
_ctx: &Context<'_>,
Expand All @@ -147,9 +147,15 @@ impl IdentityRecord {
.await
}

/// there's only `platform: lens` identity `ownedBy` is not null
/// Return primary domain names where they would typically only show addresses.
async fn reverse_domains(&self, _ctx: &Context<'_>) -> Result<Vec<ResolveReverse>> {
let client = make_http_client();
self.resolve_reverse_domains(&client).await
}

/// there's only `platform: lens, dotbit, unstoppabledomains, farcaster, space_id` identity `ownedBy` is not null
async fn owned_by(&self, _ctx: &Context<'_>) -> Result<Option<IdentityRecord>> {
if vec![
if !vec![
Platform::Lens,
Platform::Dotbit,
Platform::UnstoppableDomains,
Expand Down
52 changes: 51 additions & 1 deletion src/controller/tigergraphql/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::{Error, Result},
tigergraph::{
delete_vertex_and_edge,
edge::{Resolve, ResolveEdge},
edge::{resolve::ResolveReverse, Resolve, ResolveEdge},
vertex::IdentityRecord,
},
upstream::{
Expand All @@ -15,6 +15,46 @@ use strum::IntoEnumIterator;
use tokio::time::{sleep, Duration};
use uuid::Uuid;

#[Object]
impl ResolveReverse {
/// UUID of this record.
async fn uuid(&self) -> Uuid {
// self.uuid
self.record.uuid
}

/// Data source (upstream) which provides this info.
async fn source(&self) -> DataSource {
self.source
}

/// Domain Name system
async fn system(&self) -> DomainNameSystem {
self.system
}

/// Name of domain (e.g., `vitalik.eth`, `dotbit.bit`)
async fn name(&self) -> String {
self.name.clone()
}

/// Who collects this data.
/// It works as a "data cleansing" or "proxy" between `source`s and us.
async fn fetcher(&self) -> DataFetcher {
self.fetcher
}

/// When this connection is fetched by us RelationService.
async fn updated_at(&self) -> i64 {
self.updated_at.timestamp()
}

/// `reverse`: Return `True` or `False`. Show domain is primary domain or not.
async fn reverse(&self) -> bool {
self.reverse.clone()
}
}

#[Object]
impl ResolveEdge {
/// UUID of this record.
Expand Down Expand Up @@ -61,6 +101,16 @@ impl ResolveEdge {
Some(owner) => Ok(owner),
}
}

/// `reverse`: Return `True` or `False`. Show domain is primary domain or not.
async fn reverse(&self) -> bool {
self.reverse.clone()
}

/// `reverseRecord`: Only have one primary domain linked to an address.
async fn reverse_record(&self) -> Option<IdentityRecord> {
self.reverse_record.clone()
}
}

#[derive(Default)]
Expand Down
Loading

0 comments on commit 5ec78bd

Please sign in to comment.