Skip to content

refactor(EventUtils): Skip transactionIndex check in event sorting #991

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

Merged
merged 2 commits into from
Apr 25, 2025
Merged
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
32 changes: 7 additions & 25 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,9 @@ export function sortEventsAscending<T extends SortableEvent>(events: T[]): T[] {
// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
// Note: this method should only be used in cases where modifications are acceptable.
export function sortEventsAscendingInPlace<T extends SortableEvent>(events: T[]): T[] {
return events.sort((ex, ey) => {
if (ex.blockNumber !== ey.blockNumber) {
return ex.blockNumber - ey.blockNumber;
}
if (ex.transactionIndex !== ey.transactionIndex) {
return ex.transactionIndex - ey.transactionIndex;
}
return ex.logIndex - ey.logIndex;
});
return events.sort((ex, ey) =>
ex.blockNumber === ey.blockNumber ? ex.logIndex - ey.logIndex : ex.blockNumber - ey.blockNumber
);
}

// This copies the array and sorts it, returning a new array with the new ordering.
Expand All @@ -241,26 +235,14 @@ export function sortEventsDescending<T extends SortableEvent>(events: T[]): T[]
// This sorts the events in place, meaning it modifies the passed array and returns a reference to the same array.
// Note: this method should only be used in cases where modifications are acceptable.
export function sortEventsDescendingInPlace<T extends SortableEvent>(events: T[]): T[] {
return events.sort((ex, ey) => {
if (ex.blockNumber !== ey.blockNumber) {
return ey.blockNumber - ex.blockNumber;
}
if (ex.transactionIndex !== ey.transactionIndex) {
return ey.transactionIndex - ex.transactionIndex;
}
return ey.logIndex - ex.logIndex;
});
return events.sort((ex, ey) =>
ex.blockNumber === ey.blockNumber ? ey.logIndex - ex.logIndex : ey.blockNumber - ex.blockNumber
);
}

// Returns true if ex is older than ey.
export function isEventOlder<T extends SortableEvent>(ex: T, ey: T): boolean {
if (ex.blockNumber !== ey.blockNumber) {
return ex.blockNumber < ey.blockNumber;
}
if (ex.transactionIndex !== ey.transactionIndex) {
return ex.transactionIndex < ey.transactionIndex;
}
return ex.logIndex < ey.logIndex;
return ex.blockNumber === ey.blockNumber ? ex.logIndex < ey.logIndex : ex.blockNumber < ey.blockNumber;
}

export function getTransactionHashes(events: SortableEvent[]): string[] {
Expand Down