Skip to content

Commit

Permalink
optimize subql handler with bulk creation (#646)
Browse files Browse the repository at this point in the history
* optimize subql handler with bulk creation

* lint fix

* fix

* fix

* update
  • Loading branch information
shunjizhan committed Feb 6, 2023
1 parent 4963a65 commit 077bd3e
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 32 deletions.
1 change: 1 addition & 0 deletions bodhi/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
semi: [2, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
Expand Down
1 change: 1 addition & 0 deletions eth-providers/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
semi: [2, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
Expand Down
1 change: 1 addition & 0 deletions eth-rpc-adapter/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
semi: [2, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
Expand Down
3 changes: 2 additions & 1 deletion eth-transactions/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = {
rules: {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
'quote-props': [2, 'as-needed'],
semi: [2, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
'comma-dangle': [2, {
Expand Down
2 changes: 2 additions & 0 deletions evm-subql/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/types/
dist/
1 change: 1 addition & 0 deletions evm-subql/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
indent: [2, 2, { SwitchCase: 1 }],
quotes: [2, 'single'],
semi: [2, 'always'],
'no-trailing-spaces': [2],
'quote-props': [2, 'as-needed'],
'eol-last': [2, 'always'],
'object-curly-spacing': [2, 'always'],
Expand Down
25 changes: 16 additions & 9 deletions evm-subql/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import '@polkadot/api-augment';
import { parseReceiptsFromBlockData } from '@acala-network/eth-providers/lib/utils';
import { SubstrateBlock } from '@subql/types';
import { Log, TransactionReceipt } from '../types';

export const handleBlock = async (substrateBlock: SubstrateBlock): Promise<void> => {
const receipts = await parseReceiptsFromBlockData(
Expand All @@ -10,12 +9,15 @@ export const handleBlock = async (substrateBlock: SubstrateBlock): Promise<void>
substrateBlock.events,
);

for (const [idx, receipt] of receipts.entries()) {
const blockNumber = substrateBlock.block.header.number.toBigInt();
const receiptEntities = [];
const logEntities = [];

receipts.forEach((receipt, idx) => {
const receiptId = `${receipt.blockNumber.toString()}-${idx}`;
const transactionIndex = BigInt(receipt.transactionIndex);
const blockNumber = BigInt(receipt.blockNumber);

await TransactionReceipt.create({

receiptEntities.push({
...receipt,
id: receiptId,
gasUsed: receipt.gasUsed.toBigInt(),
Expand All @@ -25,16 +27,21 @@ export const handleBlock = async (substrateBlock: SubstrateBlock): Promise<void>
status: BigInt(receipt.status),
transactionIndex,
blockNumber,
}).save();
});

await store.bulkCreate('Log', receipt.logs.map(log => ({
receipt.logs.forEach(log => logEntities.push({
...log,
id: `${receiptId}-${log.logIndex}`,
receiptId,
transactionIndex,
blockNumber,
logIndex: BigInt(log.logIndex),
removed: false, // this field was removed by formatter.receipt...
})));
}
}));
});

await Promise.all([
store.bulkCreate('TransactionReceipt', receiptEntities),
store.bulkCreate('Log', logEntities),
]);
};
Loading

0 comments on commit 077bd3e

Please sign in to comment.