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

Feat: Add Statement Handler #20

Merged
merged 10 commits into from
Feb 5, 2024
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
15 changes: 0 additions & 15 deletions .github/scripts/publish-deploy.sh

This file was deleted.

34 changes: 0 additions & 34 deletions .github/workflows/cli-deploy.yml

This file was deleted.

24 changes: 0 additions & 24 deletions .github/workflows/pr.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ Thumbs.db

.data
.eslintcache

.vscode
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"chaintypes": "src/chaintypes.ts"
},
"dependencies": {
"@cord.network/sdk": "^0.9.1"
"@cord.network/sdk": "0.9.3-1develop1"
}
}
18 changes: 16 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Add the `@index` or `@index(unique: true)` annotation after any non-key field
# https://academy.subquery.network/build/graphql.html#indexing-by-non-primary-key-field


type KVData @jsonField {
key: String
value: String
Expand Down Expand Up @@ -47,7 +46,7 @@ type Call @entity {

section: String
method: String
args:[KVData]
args: [KVData]
timestamp: Date
isSuccess: Boolean

Expand Down Expand Up @@ -92,3 +91,18 @@ type Rating @entity {
entityType: String
ratingType: String
}

type Statement @entity {
id: ID!

method: String
callIndex: String
blockNumber: BigInt
submitter: String
signature: String

statement_id: String
authorization: String
schema_id: String
digest: String
}
117 changes: 67 additions & 50 deletions src/handlers/call.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { Vec } from '@polkadot/types'
import { Call, Extrinsic } from '../types'
import { AnyCall, DispatchedCallData } from './types'
import type { Vec } from "@polkadot/types";
import { Call, Extrinsic } from "../types";
import { AnyCall, DispatchedCallData } from "./types";

import { SubstrateExtrinsic } from "@subql/types";
import { Dispatcher, getBatchInterruptedIndex, getKVData } from './utils';
import { createScore } from './score';
import { Dispatcher, getBatchInterruptedIndex, getKVData } from "./utils";
import { createScore } from "./score";
import { createStatement } from "./statement";

async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): Promise<Call[]> {
const list: any[] = []
const batchInterruptedIndex = getBatchInterruptedIndex(raw)
async function traverExtrinsic(
extrinsic: Extrinsic,
raw: SubstrateExtrinsic
): Promise<Call[]> {
const list: any[] = [];
const batchInterruptedIndex = getBatchInterruptedIndex(raw);

const inner = async (
data: AnyCall,
Expand All @@ -17,74 +21,87 @@ async function traverExtrinsic(extrinsic: Extrinsic, raw: SubstrateExtrinsic): P
isRoot: boolean,
depth: number
) => {
const id = isRoot ? parentCallId : `${parentCallId}-${idx}`
const method = data.method
const section = data.section
const args = data.args

if (method === 'submitDidCall' && section === 'did') {
/* there will be one call below */
const temp = (args[0] as any).call as unknown as AnyCall;
logger.info(`DidSubmitCall: ${temp}`);
await inner(temp, id, 1, false, depth + 1);
const id = isRoot ? parentCallId : `${parentCallId}-${idx}`;
const method = data.method;
const section = data.section;
const args = data.args;

if (method === "submitDidCall" && section === "did") {
/* there will be one call below */
const temp = (args[0] as any).call as unknown as AnyCall;
logger.info(`DidSubmitCall: ${temp}`);
await inner(temp, id, 1, false, depth + 1);
}
const call = new Call(id)
const call = new Call(id);

call.method = method
call.section = section
call.args = getKVData(data.args, data.argsDef)
call.signerId = extrinsic.signerId
call.isSuccess = depth === 0 ? extrinsic.isSuccess : batchInterruptedIndex > idx;
call.timestamp = extrinsic.timestamp
call.method = method;
call.section = section;
call.args = getKVData(data.args, data.argsDef);
call.signerId = extrinsic.signerId;
call.isSuccess =
depth === 0 ? extrinsic.isSuccess : batchInterruptedIndex > idx;
call.timestamp = extrinsic.timestamp;

if (!isRoot) {
call.parentCallId = isRoot ? '' : parentCallId
call.parentCallId = isRoot ? "" : parentCallId;

call.extrinsicId = parentCallId.split('-')[0]
call.extrinsicId = parentCallId.split("-")[0];
} else {
call.extrinsicId = parentCallId
call.extrinsicId = parentCallId;
}

list.push(call)
list.push(call);

/*
await dispatcher.dispatch(
`${call.section}-${call.method}`,
{ call, extrinsic, rawCall: data, rawExtrinsic: raw }
)
*/
if (call.section === 'networkScore') {
logger.info("Scoring call");
await createScore(raw, id as string);
if (call.section === "networkScore") {
logger.info("Scoring call");
await createScore(raw, id as string);
}
if (call.section === "statement") {
logger.info(`${data.method}`);
await createStatement(raw, id as string, data.method);
}

if (depth < 1 && section === 'utility' && (method === 'batch' || method === 'batchAll')) {
const temp = args[0] as unknown as Vec<AnyCall>

await Promise.all(temp.map((item, idx) => inner(item, id, idx, false, depth + 1)))
}
}
if (
depth < 1 &&
section === "utility" &&
(method === "batch" || method === "batchAll")
) {
const temp = args[0] as unknown as Vec<AnyCall>;

await inner(raw.extrinsic.method, extrinsic.id, 0, true, 0)
await Promise.all(
temp.map((item, idx) => inner(item, id, idx, false, depth + 1))
);
}
};

return list
}
await inner(raw.extrinsic.method, extrinsic.id, 0, true, 0);

export async function createCalls (extrinsic: Extrinsic, raw: SubstrateExtrinsic) {
return list;
}

const calls = await traverExtrinsic(extrinsic, raw)
logger.info(`Calls: ${calls}`)
await Promise.all(calls.map(async (item) => item.save()));
export async function createCalls(
extrinsic: Extrinsic,
raw: SubstrateExtrinsic
) {
const calls = await traverExtrinsic(extrinsic, raw);
logger.info(`Calls: ${calls}`);
await Promise.all(calls.map(async (item) => item.save()));
}

export async function ensureCallExist (id: any) {
let data = await Call.get(id)
export async function ensureCallExist(id: any) {
let data = await Call.get(id);

if (!data) {
data = new Call(id)
data = new Call(id);

await data.save()
await data.save();
}

return data
return data;
}
13 changes: 4 additions & 9 deletions src/handlers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import { DispatchedEventData } from "./types";
const dispatch = new Dispatcher<DispatchedEventData>();

dispatch.batchRegist([

//registry
//{ key: 'schema-Create', handler: handleRegistryCreate },

//schema
//{ key: 'schema-Anchor', handler: handleSchemaAnchor },

//stream
//{ key: 'stream-Anchor', handler: handleStreamAnchor },
//{ key: 'stream-Update', handler: handleStreamUpdate },
Expand All @@ -25,7 +22,7 @@ dispatch.batchRegist([
export async function ensureEvent(event: SubstrateEvent) {
const block = await ensureBlock(event.block);
if (!block) {
return null;
return null;
}
const idx = event.idx;
const recordId = `${block.number}-${idx}`;
Expand All @@ -45,14 +42,12 @@ export async function ensureEvent(event: SubstrateEvent) {

export async function createEvent(event: SubstrateEvent) {
const data = await ensureEvent(event);
if (!data)
return;
if (!data) return;
const section = event.event.section;
const method = event.event.method;
const eventData = getKVData(event.event.data);

if (section === 'system' && method === 'ExtrinsicSuccess')
return;
if (section === "system" && method === "ExtrinsicSuccess") return;

data.section = section;
data.method = method;
Expand All @@ -66,7 +61,7 @@ export async function createEvent(event: SubstrateEvent) {
data.extrinsicId = extrinsic.id;
}

// TODO: once we have separate handling of different events based on section and method, uncomment below
// TODO: once we have separate handling of different events based on section and method, uncomment below
await dispatch.dispatch(`${section}-${data.method}`, {
event: data,
rawEvent: event,
Expand Down
64 changes: 64 additions & 0 deletions src/handlers/statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SubstrateExtrinsic } from "@subql/types";
import { Statement } from "../types";

export async function createStatement(
extrinsic: SubstrateExtrinsic,
id: string,
method: string
): Promise<void> {
const data = extrinsic.extrinsic.method;

if (method === "register") {
let arrayed = JSON.parse(JSON.stringify(data));
let statementId = JSON.parse(JSON.stringify(extrinsic.events))[0].event
.data[0];
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = statementId;
statement.authorization = arrayed.args.did_call.call.args.authorization;
statement.schema_id = arrayed.args.did_call.call.args.schema_id;
statement.digest = arrayed.args.did_call.call.args.digest;
await statement.save();
}
if (method === "update") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.digest = arrayed.args.did_call.call.args.digest;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
if (method === "revoke") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
if (method === "restore") {
let arrayed = JSON.parse(JSON.stringify(data));
let statement = new Statement(id);
statement.method = method;
statement.callIndex = arrayed.args.did_call.call.callIndex
statement.blockNumber = extrinsic.block.block.header.number.toBigInt();
statement.submitter = arrayed.args.did_call.submitter;
statement.signature = JSON.stringify(arrayed.args.signature);
statement.statement_id = arrayed.args.did_call.call.args.statement_id;
statement.authorization = arrayed.args.did_call.call.args.authorization;
await statement.save();
}
}