Skip to content

Commit

Permalink
Optimise updating events, use Entity.create
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname committed Nov 27, 2024
1 parent 73f481e commit 74d0558
Show file tree
Hide file tree
Showing 26 changed files with 467 additions and 304 deletions.
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
19 changes: 12 additions & 7 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 @@ -23,7 +23,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 Down Expand Up @@ -96,10 +96,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 Down Expand Up @@ -162,10 +167,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;
}

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,
};
});
}
}
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
47 changes: 28 additions & 19 deletions automata/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 @@ -44,29 +46,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
47 changes: 28 additions & 19 deletions basilisk/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 @@ -44,29 +46,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
13 changes: 9 additions & 4 deletions bifrost-parachain/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 Down Expand Up @@ -63,10 +63,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 Down
Loading

0 comments on commit 74d0558

Please sign in to comment.