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

Internal tx pos #212

Open
wants to merge 5 commits into
base: master
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
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"@types/micro": "^10.0.0",
"@types/mocha": "^10.0.6",
"@types/lodash": "^4.17.13",
"@types/uuid": "^9.0.8",
"@types/web3": "^1.2.2",
"earl": "^1.3.0",
Expand All @@ -55,4 +56,4 @@
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
}
1 change: 1 addition & 0 deletions src/blockchains/eth/eth_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type ETHTransfer = {
timestamp: number,
transactionHash?: string,
transactionPosition?: number,
internalTxPosition?: number,
type: string,
primaryKey?: number,
}
Expand Down
34 changes: 14 additions & 20 deletions src/blockchains/eth/eth_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { logger } from '../../lib/logger';
import { constructRPCClient } from '../../lib/http_client';
import { injectDAOHackTransfers, DAO_HACK_FORK_BLOCK } from './lib/dao_hack';
import { getGenesisTransfers } from './lib/genesis_transfers';
import { transactionOrder, stableSort } from './lib/util';
import { assignInternalTransactionPosition, transactionOrder } from './lib/util'
import { BaseWorker } from '../../lib/worker_base';
import { Web3Interface, constructWeb3Wrapper, safeCastToNumber } from './lib/web3_wrapper';
import { decodeTransferTrace } from './lib/decode_transfers';
Expand Down Expand Up @@ -101,32 +101,26 @@ export class ETHWorker extends BaseWorker {
setWorkerSleepTime(this, workerContext);
if (workerContext === NO_WORK_SLEEP) return [];

const { fromBlock, toBlock } = nextIntervalCalculator(this);
logger.info(`Fetching transfer events for interval ${fromBlock}:${toBlock}`);
const [traces, blocks, receipts] = await this.fetchData(fromBlock, toBlock);
let events: (ETHTransfer | EOB)[] = this.transformPastEvents(fromBlock, toBlock, traces, blocks, receipts);

const { fromBlock, toBlock } = nextIntervalCalculator(this)
logger.info(`Fetching transfer events for interval ${fromBlock}:${toBlock}`)
const [traces, blocks, receipts] = await this.fetchData(fromBlock, toBlock)
const events: (ETHTransfer | EOB)[] = this.transformPastEvents(fromBlock, toBlock, traces, blocks, receipts)
assignInternalTransactionPosition(events)
events.push(...collectEndOfBlocks(fromBlock, toBlock, blocks, this.web3Wrapper))
if (events.length > 0) {
stableSort(events, transactionOrder);
extendEventsWithPrimaryKey(events, this.lastPrimaryKey);

this.lastPrimaryKey += events.length;
}
events.sort(transactionOrder)

this.lastExportedBlock = toBlock;
this.lastExportedBlock = toBlock

return events;
return events
}

async init(): Promise<void> {
this.lastConfirmedBlock = await this.web3Wrapper.getBlockNumber() - this.settings.CONFIRMATIONS;
this.lastConfirmedBlock = await this.web3Wrapper.getBlockNumber() - this.settings.CONFIRMATIONS
}
}

export function extendEventsWithPrimaryKey<T extends { primaryKey?: number }>(events: T[], lastPrimaryKey: number) {
for (let i = 0; i < events.length; i++) {
events[i].primaryKey = lastPrimaryKey + i + 1;
}
}





2 changes: 2 additions & 0 deletions src/blockchains/eth/lib/decode_transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function decodeTransferTrace(trace: Trace, timestamp: number, web3Wrapper
valueExactBase36: web3Wrapper.parseHexToBase36String(trace['action']['value']),
blockNumber: trace['blockNumber'],
timestamp: timestamp,
transactionHash: trace['transactionHash'],
transactionPosition: trace['transactionPosition'],
type: trace['type']
};
}
Expand Down
11 changes: 6 additions & 5 deletions src/blockchains/eth/lib/end_of_block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type EOB = {
value: number,
valueExactBase36: string,
blockNumber: number,
timestamp: number,
timestamp: number,
transactionHash: string,
transactionPosition: number,
type: string,
Expand All @@ -27,12 +27,13 @@ export const collectEndOfBlocks = (from: number, to: number, blockInfos: Map<num
timestamp: safeCastToNumber(web3Wrapper.parseHexToNumber(blockInfos.get(blockNumber)!!.timestamp)),
transactionHash: "0x0000000000000000000000000000000000000000",
transactionPosition: maxTxPosition,
internalTxPosition: 0,
type: "EOB"
}})
}
}
})
}

const maxTxPosition = Math.pow(2, 31) - 1 // max int32
const maxTxPosition = Math.pow(2, 31) - 1 // max int32
// from - inclusive, to - exclusive
const range = (from: number, to: number, step: number) =>
[...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step);

25 changes: 0 additions & 25 deletions src/blockchains/eth/lib/util.js

This file was deleted.

35 changes: 35 additions & 0 deletions src/blockchains/eth/lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ETHTransfer } from '../eth_types';
const { groupBy } = require('lodash');

export function transactionOrder(a: ETHTransfer, b: ETHTransfer) {
if (a.blockNumber !== b.blockNumber) {
return a.blockNumber - b.blockNumber
}

const transactionPositionA = (a.transactionPosition !== undefined) ? a.transactionPosition : -1
const transactionPositionB = (b.transactionPosition !== undefined) ? b.transactionPosition : -1

if (transactionPositionA !== transactionPositionB) {
return transactionPositionA - transactionPositionB
}

const internalTxPositionA = (a.internalTxPosition !== undefined) ? a.internalTxPosition : -1
const internalTxPositionB = (b.internalTxPosition !== undefined) ? b.internalTxPosition : -1

return internalTxPositionA - internalTxPositionB
}

const ethTransferKey = (transfer: ETHTransfer) => `${transfer.blockNumber}-${transfer.transactionHash ?? ''}-${transfer.transactionPosition ?? ''}-${transfer.from}-${transfer.to}`

export function assignInternalTransactionPosition(transfers: ETHTransfer[], groupByKey: (transfer: ETHTransfer) => string = ethTransferKey): ETHTransfer[] {
const grouped = groupBy(transfers, groupByKey)
const values: ETHTransfer[][] = Object.values(grouped)

return values.flatMap((transfersSameKey: ETHTransfer[]) => {
transfersSameKey.forEach((transfer: ETHTransfer, index: number) => {
transfer.internalTxPosition = index
})
return transfersSameKey
})
}

2 changes: 2 additions & 0 deletions src/test/eth/decode_transfers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ describe('genesis transfers', function () {
const result_expected = {
'from': 'mining_block',
'to': '0x2a65aca4d5fc5b5c859090a6c34d164135398226',
'transactionHash': '0x22f839c82ff455554ec8aa98ee2b9a03d0d5ed4707b46d4a0a217df7d58bda2c',
'transactionPosition': 10,
'value': 5000000000000000000,
'valueExactBase36': '11zk02pzlmwow',
'blockNumber': 710093,
Expand Down
Loading