Skip to content

improve: refactor common interfaces #940

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
11 changes: 7 additions & 4 deletions src/clients/AcrossConfigStoreClient/AcrossConfigStoreClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isArrayOf,
isDefined,
isPositiveInteger,
logToSortableEvent,
max,
paginatedEventQuery,
sortEventsAscendingInPlace,
Expand Down Expand Up @@ -342,7 +343,9 @@ export class AcrossConfigStoreClient extends BaseAbstractClient {
]);

// Events *should* normally be received in ascending order, but explicitly enforce the ordering.
[updatedTokenConfigEvents, updatedGlobalConfigEvents].forEach((events) => sortEventsAscendingInPlace(events));
[updatedTokenConfigEvents, updatedGlobalConfigEvents].forEach((events) =>
sortEventsAscendingInPlace(events.map(logToSortableEvent))
);

const globalConfigUpdateTimes = (
await Promise.all(updatedGlobalConfigEvents.map((event) => this.configStore.provider.getBlock(event.blockNumber)))
Expand Down Expand Up @@ -562,7 +565,7 @@ export class AcrossConfigStoreClient extends BaseAbstractClient {
rateModel: string | undefined;
routeRateModel: RouteRateModelUpdate["routeRateModel"];
} {
const { value, key, transactionHash } = args;
const { value, key, txnRef } = args;
const parsedValue = parseJSONWithNumericString(value) as ParsedTokenConfig;
const l1Token = key;

Expand All @@ -578,7 +581,7 @@ export class AcrossConfigStoreClient extends BaseAbstractClient {
const rateModel = parsedValue.rateModel;
assert(
this.isValidRateModel(rateModel),
`Invalid rateModel UBar for ${l1Token} at transaction ${transactionHash}, ${JSON.stringify(rateModel)}`
`Invalid rateModel UBar for ${l1Token} at transaction ${txnRef}, ${JSON.stringify(rateModel)}`
);
rateModelForToken = JSON.stringify(rateModel);

Expand All @@ -601,7 +604,7 @@ export class AcrossConfigStoreClient extends BaseAbstractClient {
Object.entries(parsedValue.routeRateModel).map(([path, routeRateModel]) => {
assert(
this.isValidRateModel(routeRateModel) &&
`Invalid routeRateModel UBar for ${path} for ${l1Token} at transaction ${transactionHash}, ${JSON.stringify(
`Invalid routeRateModel UBar for ${path} for ${l1Token} at transaction ${txnRef}, ${JSON.stringify(
routeRateModel
)}`
);
Expand Down
2 changes: 1 addition & 1 deletion src/clients/BundleDataClient/utils/FillUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function verifyFillRepayment(
if (!isValidEvmAddress(fill.relayer)) {
// TODO: Handle case where fill was sent on non-EVM chain, in which case the following call would fail
// or return something unexpected. We'd want to return undefined here.
const fillTransaction = await destinationChainProvider.getTransaction(fill.transactionHash);
const fillTransaction = await destinationChainProvider.getTransaction(fill.txnRef);
const destinationRelayer = fillTransaction?.from;
// Repayment chain is still an EVM chain, but the msg.sender is a bytes32 address, so the fill is invalid.
if (!isDefined(destinationRelayer) || !isValidEvmAddress(destinationRelayer)) {
Expand Down
4 changes: 2 additions & 2 deletions src/clients/BundleDataClient/utils/SuperstructUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const V3RelayDataSS = {

const SortableEventSS = {
blockNumber: number(),
transactionIndex: number(),
txnIndex: number(),
logIndex: number(),
transactionHash: string(),
txnRef: string(),
};

const V3DepositSS = {
Expand Down
17 changes: 4 additions & 13 deletions src/clients/HubPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getTokenInfo,
getUsdcSymbol,
getL1TokenInfo,
logToSortableEvent,
} from "../utils";
import { AcrossConfigStoreClient as ConfigStoreClient } from "./AcrossConfigStoreClient/AcrossConfigStoreClient";
import { BaseAbstractClient, isUpdateFailureReason, UpdateFailureReason } from "./BaseAbstractClient";
Expand Down Expand Up @@ -906,9 +907,7 @@ export class HubPoolClient extends BaseAbstractClient {
[
{
spokePool: args.spokePool,
blockNumber: args.blockNumber,
transactionIndex: args.transactionIndex,
logIndex: args.logIndex,
...logToSortableEvent(event),
},
]
);
Expand All @@ -926,10 +925,7 @@ export class HubPoolClient extends BaseAbstractClient {
{
l1Token: args.l1Token,
l2Token: args.destinationToken,
blockNumber: args.blockNumber,
transactionIndex: args.transactionIndex,
logIndex: args.logIndex,
transactionHash: args.transactionHash,
...logToSortableEvent(event),
},
]
);
Expand Down Expand Up @@ -974,12 +970,7 @@ export class HubPoolClient extends BaseAbstractClient {
this.proposedRootBundles.push(
...events["ProposeRootBundle"]
.filter((event) => !this.configOverride.ignoredHubProposedBundles.includes(event.blockNumber))
.map((event) => {
return {
...spreadEventWithBlockNumber(event),
transactionHash: event.transactionHash,
} as ProposedRootBundle;
})
.map((event) => spreadEventWithBlockNumber(event) as ProposedRootBundle)
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/clients/SpokePoolClient/EVMSpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Contract, EventFilter } from "ethers";
import { BigNumber, DepositSearchResult, getNetworkName, InvalidFill, MakeOptional, toBN } from "../../utils";
import {
EventSearchConfig,
logToSortableEvent,
paginatedEventQuery,
sortEventsAscendingInPlace,
spreadEventWithBlockNumber,
Expand Down Expand Up @@ -116,7 +117,7 @@ export class EVMSpokePoolClient extends SpokePoolClient {
}

// Sort all events to ensure they are stored in a consistent order.
events.forEach((events) => sortEventsAscendingInPlace(events));
events.forEach((events) => sortEventsAscendingInPlace(events.map(logToSortableEvent)));

return {
success: true,
Expand Down
2 changes: 1 addition & 1 deletion src/clients/SpokePoolClient/SpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export abstract class SpokePoolClient extends BaseAbstractClient {

// Log any invalid deposits with same deposit id but different params.
const invalidFillsForDeposit = invalidFills.filter((x) => {
const txnUid = `${x.transactionHash}:${x.logIndex}`;
const txnUid = `${x.txnRef}:${x.logIndex}`;
// if txnUid doesn't exist in the invalidFills set, add it now, but log the corresponding fill.
const newInvalidFill = x.depositId.eq(deposit.depositId) && !this.invalidFills.has(txnUid);
if (newInvalidFill) {
Expand Down
10 changes: 5 additions & 5 deletions src/clients/mocks/MockSpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class MockSpokePoolClient extends EVMSpokePoolClient {
}

protected _deposit(event: string, deposit: Omit<Deposit, "messageHash"> & Partial<SortableEvent>): Log {
const { blockNumber, transactionIndex } = deposit;
const { blockNumber, txnIndex } = deposit;
let { depositId, destinationChainId, inputAmount, outputAmount } = deposit;
depositId ??= this.numberOfDeposits;
this.numberOfDeposits = depositId.add(bnOne);
Expand Down Expand Up @@ -171,7 +171,7 @@ export class MockSpokePoolClient extends EVMSpokePoolClient {
topics: topics.map((topic) => topic.toString()),
args,
blockNumber,
transactionIndex,
transactionIndex: txnIndex,
});
}

Expand All @@ -187,7 +187,7 @@ export class MockSpokePoolClient extends EVMSpokePoolClient {
event: string,
fill: Omit<Fill, "messageHash"> & { message: string } & Partial<SortableEvent>
): Log {
const { blockNumber, transactionIndex } = fill;
const { blockNumber, txnIndex } = fill;
let { originChainId, depositId, inputAmount, outputAmount, fillDeadline } = fill;
originChainId ??= random(1, 42161, false);
depositId ??= BigNumber.from(random(1, 100_000, false));
Expand Down Expand Up @@ -260,7 +260,7 @@ export class MockSpokePoolClient extends EVMSpokePoolClient {
topics: topics.map((topic) => topic.toString()),
args,
blockNumber,
transactionIndex,
transactionIndex: txnIndex,
});
}

Expand Down Expand Up @@ -335,7 +335,7 @@ export class MockSpokePoolClient extends EVMSpokePoolClient {
exclusiveRelayer: addressModifier(args.exclusiveRelayer ?? ZERO_ADDRESS),
},
blockNumber: request.blockNumber,
transactionIndex: request.transactionIndex,
transactionIndex: request.txnIndex,
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export type Log = _Log & {

export interface SortableEvent {
blockNumber: number;
transactionIndex: number;
txnIndex: number;
logIndex: number;
transactionHash: string;
txnRef: string;
}

export interface BigNumberForToken {
Expand Down
32 changes: 19 additions & 13 deletions src/utils/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,19 @@ export function getPaginatedBlockRanges({
return ranges;
}

export function logToSortableEvent(log: Log): SortableEvent {
return {
txnIndex: log.transactionIndex,
txnRef: log.transactionHash,
logIndex: log.logIndex,
blockNumber: log.blockNumber,
};
}

export function spreadEventWithBlockNumber(event: Log): SortableEvent {
return {
...spreadEvent(event.args),
blockNumber: event.blockNumber,
transactionIndex: event.transactionIndex,
logIndex: event.logIndex,
transactionHash: event.transactionHash,
...logToSortableEvent(event),
};
}

Expand All @@ -226,8 +232,8 @@ export function sortEventsAscendingInPlace<T extends SortableEvent>(events: T[])
if (ex.blockNumber !== ey.blockNumber) {
return ex.blockNumber - ey.blockNumber;
}
if (ex.transactionIndex !== ey.transactionIndex) {
return ex.transactionIndex - ey.transactionIndex;
if (ex.txnIndex !== ey.txnIndex) {
return ex.txnIndex - ey.txnIndex;
}
return ex.logIndex - ey.logIndex;
});
Expand All @@ -245,8 +251,8 @@ export function sortEventsDescendingInPlace<T extends SortableEvent>(events: T[]
if (ex.blockNumber !== ey.blockNumber) {
return ey.blockNumber - ex.blockNumber;
}
if (ex.transactionIndex !== ey.transactionIndex) {
return ey.transactionIndex - ex.transactionIndex;
if (ex.txnIndex !== ey.txnIndex) {
return ey.txnIndex - ex.txnIndex;
}
return ey.logIndex - ex.logIndex;
});
Expand All @@ -257,16 +263,16 @@ 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;
if (ex.txnIndex !== ey.txnIndex) {
return ex.txnIndex < ey.txnIndex;
}
return ex.logIndex < ey.logIndex;
}

export function getTransactionHashes(events: SortableEvent[]): string[] {
return [...Array.from(new Set(events.map((e) => e.transactionHash)))];
export function getTransactionRefs(events: SortableEvent[]): string[] {
return [...Array.from(new Set(events.map((e) => e.txnRef)))];
}

export function duplicateEvent(a: SortableEvent, b: SortableEvent): boolean {
return a.transactionHash === b.transactionHash && a.logIndex === b.logIndex;
return a.txnRef === b.txnRef && a.logIndex === b.logIndex;
}
19 changes: 11 additions & 8 deletions test/FillUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DepositWithBlock, FillType, FillWithBlock } from "../src/interfaces";
import { bnOne, bnZero, toBN } from "../src/utils";
import { ZERO_ADDRESS } from "../src/constants";
import { ZERO_ADDRESS, ZERO_BYTES } from "../src/constants";
import { originChainId, destinationChainId, repaymentChainId } from "./constants";
import {
createSpyLogger,
Expand Down Expand Up @@ -40,16 +40,17 @@ describe("FillUtils", function () {
inputToken: ZERO_ADDRESS,
outputAmount: toBN(100),
outputToken: ZERO_ADDRESS,
message: "",
message: ZERO_BYTES,
messageHash: ZERO_BYTES,
quoteTimestamp: 0,
recipient: ZERO_ADDRESS,
updatedRecipient: ZERO_ADDRESS,
fillDeadline: 100,
exclusiveRelayer: ZERO_ADDRESS,
exclusivityDeadline: 100,
transactionHash: "0xa",
txnRef: "0xa",
blockNumber: 0,
transactionIndex: 0,
txnIndex: 0,
logIndex: 0,
quoteBlockNumber: 0,
fromLiteChain: false,
Expand All @@ -62,7 +63,9 @@ describe("FillUtils", function () {
updatedOutputAmount: deposit.outputAmount,
updatedRecipient: deposit.recipient,
fillType: FillType.FastFill,
updatedMessageHash: "0x",
},
messageHash: "0x",
relayer,
repaymentChainId,
};
Expand Down Expand Up @@ -115,7 +118,7 @@ describe("FillUtils", function () {
...fill,
relayer: INVALID_EVM_ADDRESS,
};
spokeProvider._setTransaction(fill.transactionHash, {
spokeProvider._setTransaction(fill.txnRef, {
from: relayer,
} as unknown as TransactionResponse);
const result = await verifyFillRepayment(invalidRepaymentFill, spokeProvider, liteChainDeposit, hubPoolClient);
Expand All @@ -130,7 +133,7 @@ describe("FillUtils", function () {
...fill,
relayer: INVALID_EVM_ADDRESS,
};
spokeProvider._setTransaction(fill.transactionHash, {
spokeProvider._setTransaction(fill.txnRef, {
from: relayer,
} as unknown as TransactionResponse);
const result = await verifyFillRepayment(invalidRepaymentFill, spokeProvider, deposit, hubPoolClient);
Expand All @@ -150,7 +153,7 @@ describe("FillUtils", function () {
...fill,
relayer: INVALID_EVM_ADDRESS,
};
spokeProvider._setTransaction(fill.transactionHash, {
spokeProvider._setTransaction(fill.txnRef, {
from: INVALID_EVM_ADDRESS,
} as unknown as TransactionResponse);
const result = await verifyFillRepayment(invalidRepaymentFill, spokeProvider, liteChainDeposit, hubPoolClient);
Expand All @@ -165,7 +168,7 @@ describe("FillUtils", function () {
...fill,
relayer: INVALID_EVM_ADDRESS,
};
spokeProvider._setTransaction(fill.transactionHash, {
spokeProvider._setTransaction(fill.txnRef, {
from: INVALID_EVM_ADDRESS,
} as unknown as TransactionResponse);
const result = await verifyFillRepayment(invalidRepaymentFill, spokeProvider, deposit, hubPoolClient);
Expand Down
4 changes: 2 additions & 2 deletions test/SpokePoolClient.ValidateFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ describe("SpokePoolClient: Fill Validation", function () {
"fromLiteChain",
"toLiteChain",
"blockNumber",
"transactionHash",
"transactionIndex",
"txnRef",
"txnIndex",
"logIndex",
"relayer",
"repaymentChainId",
Expand Down
12 changes: 6 additions & 6 deletions test/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ async function _deposit(
toLiteChain: false,
quoteBlockNumber: 0, // @todo
blockNumber,
transactionHash,
transactionIndex,
txnRef: transactionHash,
txnIndex: transactionIndex,
logIndex,
};
}
Expand Down Expand Up @@ -437,8 +437,8 @@ export async function requestV3SlowFill(
exclusivityDeadline: args.exclusivityDeadline,
exclusiveRelayer: toAddress(args.exclusiveRelayer),
blockNumber,
transactionHash,
transactionIndex,
txnRef: transactionHash,
txnIndex: transactionIndex,
logIndex,
};
}
Expand Down Expand Up @@ -493,8 +493,8 @@ export async function fillV3Relay(
fillType: args.relayExecutionInfo.fillType,
},
blockNumber,
transactionHash,
transactionIndex,
txnRef: transactionHash,
txnIndex: transactionIndex,
logIndex,
};
}
Expand Down