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

Remove ETH transfers primary key, instead sum #211

Open
wants to merge 1 commit 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
35 changes: 25 additions & 10 deletions src/blockchains/eth/eth_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,41 @@ export class ETHWorker extends BaseWorker {
let events: (ETHTransfer | EOB)[] = this.transformPastEvents(fromBlock, toBlock, traces, blocks, receipts);

events.push(...collectEndOfBlocks(fromBlock, toBlock, blocks, this.web3Wrapper))
if (events.length > 0) {
stableSort(events, transactionOrder);
extendEventsWithPrimaryKey(events, this.lastPrimaryKey);

this.lastPrimaryKey += events.length;
const aggregatedTransfers: ETHTransfer[] = aggregateTransfers(events)
if (aggregatedTransfers.length > 0) {
stableSort(aggregatedTransfers, transactionOrder);
}

this.lastExportedBlock = toBlock;

return events;
return aggregatedTransfers;
}

async init(): Promise<void> {
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;
}
function aggregateTransfers(transfers: ETHTransfer[]): ETHTransfer[] {
const groupedTransfers = new Map<string, ETHTransfer>()

transfers.forEach((transfer) => {
// Create a unique key
const key = `${transfer.blockNumber}-${transfer.transactionHash ?? ''}-${transfer.transactionPosition ?? ''}-${transfer.from}-${transfer.to}`

if (groupedTransfers.has(key)) {
const existingTransfer = groupedTransfers.get(key)!
// Sum the 'value' fields
existingTransfer.value += transfer.value
existingTransfer.valueExactBase36 = BigInt(existingTransfer.value).toString(36)
} else {
// Clone the transfer to avoid mutating the original
groupedTransfers.set(key, { ...transfer })
}
});

return Array.from(groupedTransfers.values())
}



31 changes: 2 additions & 29 deletions src/test/eth/worker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import v8 from 'v8';
import { extendEventsWithPrimaryKey, ETHWorker } from '../../blockchains/eth/eth_worker';
import { ETHWorker } from '../../blockchains/eth/eth_worker';
import { EOB } from '../../blockchains/eth/lib/end_of_block';
import * as constants from '../../blockchains/eth/lib/constants';
import { Trace, ETHBlock, ETHTransfer, ETHReceiptsMap } from '../../blockchains/eth/eth_types';
Expand Down Expand Up @@ -55,27 +55,7 @@ describe('Test worker', function () {
});


it('test primary key assignment', async function () {
let events = [feeResult, callResult]
extendEventsWithPrimaryKey(events, 0)


expect(events).toLooseEqual([feeResultWithPrimaryKey, callResultWithPrimaryKey]);
// Overwrite variables and methods that the 'work' method would use internally.
worker.lastConfirmedBlock = 5711193;
worker.lastExportedBlock = 5711192;
worker.fetchData = async function (from: number, to: number) {
return Promise.resolve([[], blockInfos, {}]);
};
worker.transformPastEvents = function () {
return [feeResult, callResult];
};

const result = await worker.work();

expect(result).toLooseEqual([feeResultWithPrimaryKey, callResultWithPrimaryKey, eobWithPrimaryKey]);
});

it('test end of block events', async function () {
worker.lastConfirmedBlock = 5711193;
worker.lastExportedBlock = 5711190;
Expand All @@ -97,13 +77,6 @@ describe('Test worker', function () {
expect(types).toEqual(["EOB", "EOB", "fee", "call", "EOB"]);
})

it('test primary key assignment', async function () {
const events = [feeResult, callResult]
extendEventsWithPrimaryKey(events, 0)

expect(events).toLooseEqual([feeResultWithPrimaryKey, callResultWithPrimaryKey]);
});

});

function ethBlockEvent(blockNumber: number): ETHBlock {
Expand All @@ -117,7 +90,7 @@ function ethBlockEvent(blockNumber: number): ETHBlock {
totalDifficulty: "3",
difficulty: "2",
size: '2',
transactions: []
transactions: []
} satisfies ETHBlock
}

Expand Down