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

Perf/evm tx insertion and fix gas used #1390

Merged
merged 2 commits into from
Nov 21, 2023
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
28 changes: 27 additions & 1 deletion hapi-evm/src/models/transaction/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,34 @@ export const add_or_modify = async (transaction: CappedTransaction) => {
return data
}

export const add_or_modify_many = async (transactions: CappedTransaction[]) => {
const mutation = gql`
mutation ($evm_transactions: [evm_transaction_insert_input!]!) {
insert_evm_transaction(
objects: $evm_transactions
on_conflict: {
constraint: transaction_pkey
update_columns: [block_hash, block_number, gas, gas_price, hash]
}
) {
affected_rows
}
}
`
const { insert_evm_transaction_one: data } =
await coreUtil.hasura.default.request<TransactionInsertOneResponse>(
mutation,
{
evm_transactions: transactions
}
)

return data
}

export default {
exist,
get,
add_or_modify
add_or_modify,
add_or_modify_many
}
71 changes: 39 additions & 32 deletions hapi-evm/src/services/block.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Web3 } from 'web3'
import { Block, TransactionInfo, TransactionHash } from 'web3-types'
import {
Block,
TransactionInfo,
TransactionHash,
TransactionReceipt
} from 'web3-types'

import {
defaultModel,
Expand Down Expand Up @@ -45,6 +50,21 @@ const syncFullBlock = async (blockNumber: number | bigint) => {
timestamp: blockTimestamp
}

if (block.transactions?.length && cappedBlock.gas_used <= 0) {
cappedBlock.gas_used = await cappedBlock.transactions.reduce(
async (
total: Promise<number>,
trxHash: TransactionHash
): Promise<number> => {
const transactionReceipt: TransactionReceipt =
await web3.eth.getTransactionReceipt(trxHash.toString())

return (await total) + Number(transactionReceipt?.gasUsed)
},
Promise.resolve(0)
)
}

try {
await blockModel.queries.add_or_modify(cappedBlock)
} catch (error: any) {
Expand All @@ -64,41 +84,28 @@ const syncFullBlock = async (blockNumber: number | bigint) => {
return
}

await incrementTotalTransactions(block.transactions?.length)

const transactionsPromises = [
cappedBlock.transactions.reduce(
async (
acc: Promise<transactionModel.interfaces.CappedTransaction[]>,
trxHash: TransactionHash
): Promise<transactionModel.interfaces.CappedTransaction[]> => {
const transactionExist = await transactionModel.queries.exist(trxHash)

if (transactionExist) {
return acc
}
if (!block.transactions?.length) return

const trx: TransactionInfo = await web3.eth.getTransaction(
trxHash.toString()
)
const cappedTransactions = await Promise.all(
cappedBlock.transactions.map(async (trxHash: TransactionHash) => {
const trx: TransactionInfo = await web3.eth.getTransaction(
trxHash.toString()
)

const customTrx: transactionModel.interfaces.CappedTransaction = {
block_hash: trx.blockHash!.toString(),
block_number: Number(trx.blockNumber),
gas: Number(trx.gas),
gas_price: Number(trx.gasPrice),
hash: trx.hash.toString()
}

await transactionModel.queries.add_or_modify(customTrx)
const customTrx: transactionModel.interfaces.CappedTransaction = {
block_hash: trx.blockHash!.toString(),
block_number: Number(trx.blockNumber),
gas: Number(trx.gas),
gas_price: Number(trx.gasPrice),
hash: trx.hash.toString()
}

return [...(await acc), customTrx]
},
Promise.resolve([])
)
]
return customTrx
})
)

await Promise.all(transactionsPromises)
await transactionModel.queries.add_or_modify_many(cappedTransactions)
await incrementTotalTransactions(block.transactions?.length)
}

const incrementTotalTransactions = async (transactionsCount: number) => {
Expand Down