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

Optimise updating events, use Entity.create #63

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea/
.DS_Store
.DS_Store
2 changes: 1 addition & 1 deletion acala/.project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmYy8CJkeYhZCkKpazXE7JKJjCEtFEFWwJCCDPxHXrPdXc
QmQ7ig8R47i64VMnA7Hej4rkZwztbgmjEYitmH556bDhRj
2 changes: 1 addition & 1 deletion acala/.project-karura-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmVpGQbvZLSZ14Pr2gSxbRJgeWp3iPvXeGMjQytYNiPfZd
QmSeBVmzXq5h15izGH71biMAH5rLm94B2uxABZjzc3QJYp
2 changes: 1 addition & 1 deletion acala/.project-mandala-tc7-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmSZnokeTGSNvSV4PG4hvGwcDs6LMLtmXSh1vA3T2QE4aT
QmVFXgwtRYDQjjGHMr6adqV9xuX9PjW8PqMYE2ZenR31LY
3 changes: 2 additions & 1 deletion acala/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"license": "Apache-2.0",
"devDependencies": {
"@acala-network/types": "^6.1.2",
"@polkadot/api": "^11",
"@polkadot/api": "^13",
"@subql/cli": "latest",
"@subql/common-substrate": "^4.3.4",
"@subql/testing": "latest",
"@subql/types": "latest",
"typescript": "^4.1.3"
Expand Down
17 changes: 10 additions & 7 deletions acala/project-karura.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ name: SubQuery Dictionary - Karura
version: 1.0.0
runner:
node:
name: '@subql/node'
version: '>=3.0.0'
name: "@subql/node"
version: ">=3.0.0"
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
name: "@subql/query"
version: "*"
description: >-
A SubQuery Dictionary Project that provides increased indexing speed to all
projects
repository: 'https://github.com/subquery/subql-dictionary'
repository: "https://github.com/subquery/subql-dictionary"
schema:
file: ./schema.graphql
network:
chainId: '0xbaf5aabe40646d11f0ee8abbdc64f4a4b7674925cba08e4a05ff9ebed6e2126b'
endpoint: 'wss://karura.api.onfinality.io/public-ws'
chainId: "0xbaf5aabe40646d11f0ee8abbdc64f4a4b7674925cba08e4a05ff9ebed6e2126b"
endpoint: "wss://karura.api.onfinality.io/public-ws"
chaintypes:
file: ./dist/chaintypes.js
dataSources:
Expand All @@ -30,3 +30,6 @@ dataSources:
handlers:
- handler: handleBlock
kind: substrate/BlockHandler
parent:
reference: "QmPQQA28fxR1hePk25MHNS1vEYRs4Gbz3PXry8G4dfC76N"
untilBlock: 8131033
17 changes: 10 additions & 7 deletions acala/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ name: SubQuery Dictionary - Acala
version: 1.0.0
runner:
node:
name: '@subql/node'
version: '>=3.0.0'
name: "@subql/node"
version: ">=3.0.0"
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
name: "@subql/query"
version: "*"
description: >-
A SubQuery Dictionary Project that provides increased indexing speed to all
projects
repository: 'https://github.com/subquery/subql-dictionary'
repository: "https://github.com/subquery/subql-dictionary"
schema:
file: ./schema.graphql
network:
chainId: '0xfc41b9bd8ef8fe53d58c7ea67c794c7ec9a73daf05e6d54b14ff6342c99ba64c'
endpoint: 'wss://acala-polkadot.api.onfinality.io/public-ws'
chainId: "0xfc41b9bd8ef8fe53d58c7ea67c794c7ec9a73daf05e6d54b14ff6342c99ba64c"
endpoint: "wss://acala-polkadot.api.onfinality.io/public-ws"
chaintypes:
file: ./dist/chaintypes.js
dataSources:
Expand All @@ -30,3 +30,6 @@ dataSources:
handlers:
- handler: handleBlock
kind: substrate/BlockHandler
parent:
reference: "QmUj8yYCE1YU5UNdtm4q4di4GBDEAmL8vprSRWVGrYeEFm"
untilBlock: 7447142
48 changes: 26 additions & 22 deletions acala/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventRecord } from "@polkadot/types/interfaces";
import { SubstrateExtrinsic, SubstrateBlock, SubstrateEvent } from "@subql/types";
import { SpecVersion, Event, Extrinsic, EvmLog, EvmTransaction } from "../types";
import acalaProcessor from '@subql/acala-evm-processor';
import {hexDataSlice, stripZeros} from '@ethersproject/bytes';
import { hexDataSlice, stripZeros } from '@ethersproject/bytes';
import { merge } from 'lodash';

export async function handleBlock(block: SubstrateBlock): Promise<void> {
Expand All @@ -11,7 +11,7 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {

// Check for updates to Spec Version
if (!specVersion) {
specVersion = new SpecVersion(block.specVersion.toString(),block.block.header.number.toBigInt());
specVersion = new SpecVersion(block.specVersion.toString(), block.block.header.number.toBigInt());
await specVersion.save();
}

Expand All @@ -23,34 +23,35 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
.filter(
(evt) =>
!(evt.event.section === "system" &&
evt.event.method === "ExtrinsicSuccess")
evt.event.method === "ExtrinsicSuccess")
)
.map((evt, idx) =>
handleEvent(block.block.header.number.toString(), idx, evt)
);

const baseEventFilter = acalaProcessor.handlerProcessors['substrate/AcalaEvmEvent'].baseFilter[0];
const evmLogs: EvmLog[][] = []
for (const evt of wrappedEvents.filter(evt => {
const baseFilter = acalaProcessor.handlerProcessors['substrate/AcalaEvmEvent'].baseFilter[0];
return evt.event.section === baseFilter.module && evt.event.method === baseFilter.method;

return evt.event.section === baseEventFilter.module && evt.event.method === baseEventFilter.method;
})) {
const logResult = await handleEvmLog(block.block.header.number.toString(), evt);
evmLogs.push(logResult);
}

// Process all calls in block
const calls = wrappedExtrinsics.map((ext, idx) =>
handleCall(`${block.block.header.number.toString()}-${idx}`, ext)
);

const evmTransactions: EvmTransaction[][] = [];
const calls: Extrinsic[] = [];
const baseCallFilter = acalaProcessor.handlerProcessors['substrate/AcalaEvmCall'].baseFilter[0];

for (const ex of wrappedExtrinsics.filter(ex => {
const baseFilter = acalaProcessor.handlerProcessors['substrate/AcalaEvmCall'].baseFilter[0];
return ex.extrinsic.method.section === baseFilter.module && ex.extrinsic.method.method === baseFilter.method && ex.success;
})) {
const transactionResult = await handleEvmTransaction(ex.idx, ex);
evmTransactions.push(transactionResult);
for (let i = 0; i < wrapExtrinsics.length; i++/*const ex of wrappedExtrinsics*/) {
const ex = wrapExtrinsics[i]

if (ex.extrinsic.method.section === baseCallFilter.module && ex.extrinsic.method.method === baseCallFilter.method && ex.success) {
const transactionResult = await handleEvmTransaction(ex.idx, ex);
evmTransactions.push(transactionResult);
}
calls.push(handleCall(`${block.block.header.number.toString()}-${i}`, ex))
}

// Save all data
Expand Down Expand Up @@ -96,10 +97,15 @@ function handleCall(idx: string, extrinsic: SubstrateExtrinsic): Extrinsic {
}

function wrapExtrinsics(wrappedBlock: SubstrateBlock): SubstrateExtrinsic[] {
const groupedEvents = wrappedBlock.events.reduce((acc, evt) => {
if (evt.phase.isApplyExtrinsic) {
acc[evt.phase.asApplyExtrinsic.toNumber()] ??= [];
acc[evt.phase.asApplyExtrinsic.toNumber()].push(evt);
}
return acc;
}, {} as Record<number, EventRecord[]>)
return wrappedBlock.block.extrinsics.map((extrinsic, idx) => {
const events = wrappedBlock.events.filter(
({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eqn(idx)
);
const events = groupedEvents[idx];
return {
idx,
extrinsic,
Expand All @@ -123,9 +129,7 @@ function wrapEvents(events: EventRecord[], block: SubstrateBlock, extrinsics: Su
}, [] as SubstrateEvent[]);
}


async function handleEvmLog(blockNumber: string, event: SubstrateEvent): Promise<EvmLog[]> {

const evmLogs = await acalaProcessor.handlerProcessors['substrate/AcalaEvmEvent'].transformer({
input: event,
ds: {} as any,
Expand Down Expand Up @@ -162,10 +166,10 @@ async function handleEvmTransaction(idx: number, tx: SubstrateExtrinsic): Promis
}

export function inputToFunctionSighash(input: string): string {
return hexDataSlice(input, 0, 4);
return hexDataSlice(input, 0, 4);
}

export function isZero(input: string): boolean {
return stripZeros(input).length === 0;
return stripZeros(input).length === 0;
}

2 changes: 1 addition & 1 deletion aleph-zero/.project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmaYR3CJyhywww1Cf5TMJP15DAcD3YE9ZSNmdLbM7KiQHi
QmdVsGCbqH72oMiHTwr1Jm5N6hHpXnwzsx2PvKMFCbSSMv
3 changes: 2 additions & 1 deletion aleph-zero/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"author": "SubQuery Network",
"license": "Apache-2.0",
"devDependencies": {
"@polkadot/api": "10.1.4",
"@polkadot/api": "^13",
"@subql/cli": "latest",
"@subql/common-substrate": "^4.3.4",
"@subql/types": "latest",
"typescript": "^5.0.4"
},
Expand Down
17 changes: 10 additions & 7 deletions aleph-zero/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ name: SubQuery Dictionary - Aleph-Zero
version: 1.0.0
runner:
node:
name: '@subql/node'
version: '>=3.0.0'
name: "@subql/node"
version: ">=3.0.0"
options:
unfinalizedBlocks: false
historical: false
query:
name: '@subql/query'
version: '*'
name: "@subql/query"
version: "*"
description: >-
A SubQuery Dictionary Project that provides increased indexing speed to all
projects
repository: 'https://github.com/subquery/subql-dictionary'
repository: "https://github.com/subquery/subql-dictionary"
schema:
file: ./schema.graphql
network:
chainId: '0x70255b4d28de0fc4e1a193d7e175ad1ccef431598211c55538f1018651a0344e'
endpoint: 'wss://ws.azero.dev'
chainId: "0x70255b4d28de0fc4e1a193d7e175ad1ccef431598211c55538f1018651a0344e"
endpoint: "wss://ws.azero.dev"
chaintypes:
file: ./dist/chaintypes.js
dataSources:
Expand All @@ -30,3 +30,6 @@ dataSources:
handlers:
- handler: handleBlock
kind: substrate/BlockHandler
parent:
reference: "QmaYR3CJyhywww1Cf5TMJP15DAcD3YE9ZSNmdLbM7KiQHi"
untilBlock: 95985269
13 changes: 9 additions & 4 deletions aleph-zero/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ function handleCall(idx: string, extrinsic: SubstrateExtrinsic): Extrinsic {
}

function wrapExtrinsics(wrappedBlock: SubstrateBlock): SubstrateExtrinsic[] {
const groupedEvents = wrappedBlock.events.reduce((acc, evt) => {
if (evt.phase.isApplyExtrinsic) {
acc[evt.phase.asApplyExtrinsic.toNumber()] ??= [];
acc[evt.phase.asApplyExtrinsic.toNumber()].push(evt);
}
return acc;
}, {} as Record<number, EventRecord[]>)
return wrappedBlock.block.extrinsics.map((extrinsic, idx) => {
const events = wrappedBlock.events.filter(
({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eqn(idx)
);
const events = groupedEvents[idx];
return {
idx,
extrinsic,
Expand All @@ -78,4 +83,4 @@ function wrapExtrinsics(wrappedBlock: SubstrateBlock): SubstrateExtrinsic[] {
events.findIndex((evt) => evt.event.method === "ExtrinsicSuccess") > -1,
};
});
}
}
7 changes: 4 additions & 3 deletions altair/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"author": "SubQuery Network",
"license": "Apache-2.0",
"devDependencies": {
"@polkadot/api": "^9",
"@polkadot/api": "^13",
"@subql/cli": "latest",
"@subql/common-substrate": "^4.3.4",
"@subql/types": "latest",
"typescript": "^4.1.3",
"@subql/cli": "latest"
"typescript": "^4.1.3"
},
"exports": {
"chaintypes": "src/chaintypes.ts"
Expand Down
47 changes: 28 additions & 19 deletions altair/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {

// Check for updates to Spec Version
if (!specVersion) {
specVersion = new SpecVersion(block.specVersion.toString());
specVersion.blockHeight = block.block.header.number.toBigInt();
specVersion = SpecVersion.create({
id: block.specVersion.toString(),
blockHeight: block.block.header.number.toBigInt(),
});
await specVersion.save();
}

Expand All @@ -18,7 +20,7 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
.filter(
(evt) =>
!(evt.event.section === "system" &&
evt.event.method === "ExtrinsicSuccess")
evt.event.method === "ExtrinsicSuccess")
)
.map((evt, idx) =>
handleEvent(block.block.header.number.toString(), idx, evt)
Expand All @@ -43,29 +45,36 @@ function handleEvent(
eventIdx: number,
event: EventRecord
): Event {
const newEvent = new Event(`${blockNumber}-${eventIdx}`);
newEvent.blockHeight = BigInt(blockNumber);
newEvent.module = event.event.section;
newEvent.event = event.event.method;
return newEvent;
return Event.create({
id: `${blockNumber}-${eventIdx}`,
blockHeight: BigInt(blockNumber),
module: event.event.section,
event: event.event.method,
});
}

function handleCall(idx: string, extrinsic: SubstrateExtrinsic): Extrinsic {
const newExtrinsic = new Extrinsic(idx);
newExtrinsic.txHash = extrinsic.extrinsic.hash.toString();
newExtrinsic.module = extrinsic.extrinsic.method.section;
newExtrinsic.call = extrinsic.extrinsic.method.method;
newExtrinsic.blockHeight = extrinsic.block.block.header.number.toBigInt();
newExtrinsic.success = extrinsic.success;
newExtrinsic.isSigned = extrinsic.extrinsic.isSigned;
return newExtrinsic;
return Extrinsic.create({
id: idx,
txHash: extrinsic.extrinsic.hash.toString(),
module: extrinsic.extrinsic.method.section,
call: extrinsic.extrinsic.method.method,
blockHeight: extrinsic.block.block.header.number.toBigInt(),
success: extrinsic.success,
isSigned: extrinsic.extrinsic.isSigned,
});
}

function wrapExtrinsics(wrappedBlock: SubstrateBlock): SubstrateExtrinsic[] {
const groupedEvents = wrappedBlock.events.reduce((acc, evt) => {
if (evt.phase.isApplyExtrinsic) {
acc[evt.phase.asApplyExtrinsic.toNumber()] ??= [];
acc[evt.phase.asApplyExtrinsic.toNumber()].push(evt);
}
return acc;
}, {} as Record<number, EventRecord[]>)
return wrappedBlock.block.extrinsics.map((extrinsic, idx) => {
const events = wrappedBlock.events.filter(
({ phase }) => phase.isApplyExtrinsic && phase.asApplyExtrinsic.eqn(idx)
);
const events = groupedEvents[idx];
return {
idx,
extrinsic,
Expand Down
2 changes: 1 addition & 1 deletion astar/.project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmSmLz9LezK6GMr7TdBzjzD8dwg77miQfm9XtTRZ4p6Emr
QmQhbhi52Veanz3JKj1ei25WLNzwj31MbS86WGzXHBT8cW
2 changes: 1 addition & 1 deletion astar/.project_shibuya-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Qmc78DrvAgmDyvKvkBw5LL4PQarbeRqbScW8i8E6yzpaBx
QmbLB9cR8wwDzjiaVFgMk9ppembmJrd4fxRedVZH21ChTo
Loading