From 9ac70d05da7f8c6f495828ae397a5214f528fcb2 Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Fri, 31 May 2024 14:16:11 +0200 Subject: [PATCH 1/4] improve(utils): Avoid recomputing relayData hashes This will save redundant recomputation cycles of a relayData hash when it is needed more than once. --- src/interfaces/SpokePool.ts | 1 + src/utils/SpokeUtils.ts | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/interfaces/SpokePool.ts b/src/interfaces/SpokePool.ts index 37b228452..7d2f41037 100644 --- a/src/interfaces/SpokePool.ts +++ b/src/interfaces/SpokePool.ts @@ -19,6 +19,7 @@ export interface RelayData { fillDeadline: number; exclusiveRelayer: string; exclusivityDeadline: number; + _hash?: Record; } export interface Deposit extends RelayData { diff --git a/src/utils/SpokeUtils.ts b/src/utils/SpokeUtils.ts index 554b777f0..facda8a34 100644 --- a/src/utils/SpokeUtils.ts +++ b/src/utils/SpokeUtils.ts @@ -216,7 +216,9 @@ export async function getDepositIdAtBlock(contract: Contract, blockTag: number): * @returns The corresponding RelayData hash. */ export function getRelayDataHash(relayData: RelayData, destinationChainId: number): string { - return ethersUtils.keccak256( + relayData._hash ??= {}; + + return relayData._hash[destinationChainId] ??= ethersUtils.keccak256( ethersUtils.defaultAbiCoder.encode( [ "tuple(" + From dfbd93ef5e370aa5557c3e4ae8c158cc1f9e5842 Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Fri, 31 May 2024 14:23:42 +0200 Subject: [PATCH 2/4] lint --- src/utils/SpokeUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/SpokeUtils.ts b/src/utils/SpokeUtils.ts index facda8a34..863eb0c02 100644 --- a/src/utils/SpokeUtils.ts +++ b/src/utils/SpokeUtils.ts @@ -218,7 +218,7 @@ export async function getDepositIdAtBlock(contract: Contract, blockTag: number): export function getRelayDataHash(relayData: RelayData, destinationChainId: number): string { relayData._hash ??= {}; - return relayData._hash[destinationChainId] ??= ethersUtils.keccak256( + return (relayData._hash[destinationChainId] ??= ethersUtils.keccak256( ethersUtils.defaultAbiCoder.encode( [ "tuple(" + @@ -239,7 +239,7 @@ export function getRelayDataHash(relayData: RelayData, destinationChainId: numbe ], [relayData, destinationChainId] ) - ); + )); } export function getRelayHashFromEvent(e: Deposit | Fill | SlowFillRequest): string { From b29e208d998b15c2a4792f4227057f46f835353e Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Sat, 1 Jun 2024 00:01:06 +0200 Subject: [PATCH 3/4] Fix test --- test/SpokePoolClient.ValidateFill.ts | 18 ++++++++++-------- test/SpokePoolClient.v3Events.ts | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/test/SpokePoolClient.ValidateFill.ts b/test/SpokePoolClient.ValidateFill.ts index fc5e6fb35..5026c7a6c 100644 --- a/test/SpokePoolClient.ValidateFill.ts +++ b/test/SpokePoolClient.ValidateFill.ts @@ -216,7 +216,7 @@ describe("SpokePoolClient: Fill Validation", function () { await spokePoolClient1.update(); expect(spokePoolClient1.getDepositForFill(fill)) - .excludingEvery(["realizedLpFeePct", "quoteBlockNumber"]) + .excludingEvery(["realizedLpFeePct", "quoteBlockNumber", "_hash"]) .to.deep.equal(deposit); }); @@ -631,7 +631,7 @@ describe("SpokePoolClient: Fill Validation", function () { expect(fill_2.relayExecutionInfo.fillType === FillType.FastFill).to.be.true; expect(spokePoolClient1.getDepositForFill(fill_1)) - .excludingEvery(["quoteBlockNumber", "realizedLpFeePct"]) + .excludingEvery(["quoteBlockNumber", "realizedLpFeePct", "_hash"]) .to.deep.equal(deposit_1); expect(spokePoolClient1.getDepositForFill(fill_2)).to.equal(undefined); @@ -652,22 +652,24 @@ describe("SpokePoolClient: Fill Validation", function () { expect(validateFillForDeposit(validFill, deposit_2)).to.be.true; + const _hash = undefined; + // Invalid input amount. - expect(validateFillForDeposit({ ...validFill, inputAmount: toBNWei(1337) }, deposit_2)).to.be.false; + expect(validateFillForDeposit({ ...validFill, inputAmount: toBNWei(1337), _hash }, deposit_2)).to.be.false; // Changed the output token. - expect(validateFillForDeposit(validFill, { ...deposit_2, outputToken: owner.address })).to.be.false; + expect(validateFillForDeposit(validFill, { ...deposit_2, outputToken: owner.address, _hash })).to.be.false; // Changed the output amount. - expect(validateFillForDeposit({ ...validFill, outputAmount: toBNWei(1337) }, deposit_2)).to.be.false; + expect(validateFillForDeposit({ ...validFill, outputAmount: toBNWei(1337), _hash }, deposit_2)).to.be.false; // Invalid depositId. - expect(validateFillForDeposit({ ...validFill, depositId: 1337 }, deposit_2)).to.be.false; + expect(validateFillForDeposit({ ...validFill, depositId: 1337, _hash }, deposit_2)).to.be.false; // Changed the depositor. - expect(validateFillForDeposit({ ...validFill, depositor: relayer.address }, deposit_2)).to.be.false; + expect(validateFillForDeposit({ ...validFill, depositor: relayer.address, _hash }, deposit_2)).to.be.false; // Changed the recipient. - expect(validateFillForDeposit({ ...validFill, recipient: relayer.address }, deposit_2)).to.be.false; + expect(validateFillForDeposit({ ...validFill, recipient: relayer.address, _hash }, deposit_2)).to.be.false; }); }); diff --git a/test/SpokePoolClient.v3Events.ts b/test/SpokePoolClient.v3Events.ts index 17c5edb79..1a184ef4b 100644 --- a/test/SpokePoolClient.v3Events.ts +++ b/test/SpokePoolClient.v3Events.ts @@ -6,7 +6,7 @@ import { DEFAULT_CONFIG_STORE_VERSION } from "../src/clients"; import { MockHubPoolClient, MockSpokePoolClient, MockConfigStoreClient } from "../src/clients/mocks"; import { DepositWithBlock, FillWithBlock, SlowFillRequest, SlowFillRequestWithBlock } from "../src/interfaces"; import { EMPTY_MESSAGE, ZERO_ADDRESS } from "../src/constants"; -import { getCurrentTime, isDefined, randomAddress } from "../src/utils"; +import { getCurrentTime, getRelayDataHash, isDefined, randomAddress } from "../src/utils"; import { createSpyLogger, fillFromDeposit, @@ -209,9 +209,9 @@ describe("SpokePoolClient: Event Filtering", function () { // The SpokePoolClient appends destinationChainId, so check for it specifically. expect(slowFillRequest?.destinationChainId).to.not.be.undefined; expect(slowFillRequest?.destinationChainId).to.equal(destinationChainId); - Object.entries(relayData).forEach( - ([k, v]) => expect(isDefined(v)).to.equal(true) && expect(slowFillRequest?.[k]).to.equal(v) - ); + Object.entries(relayData) + .filter(([k]) => k !== "_hash") + .forEach(([k, v]) => expect(isDefined(v)).to.equal(true) && expect(slowFillRequest?.[k]).to.equal(v)); }); }); From cea26c21d35a1d76fc3405bf0b8ec782eb7b767f Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Sat, 1 Jun 2024 00:05:32 +0200 Subject: [PATCH 4/4] lint --- test/SpokePoolClient.v3Events.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SpokePoolClient.v3Events.ts b/test/SpokePoolClient.v3Events.ts index 1a184ef4b..3ea796b5e 100644 --- a/test/SpokePoolClient.v3Events.ts +++ b/test/SpokePoolClient.v3Events.ts @@ -6,7 +6,7 @@ import { DEFAULT_CONFIG_STORE_VERSION } from "../src/clients"; import { MockHubPoolClient, MockSpokePoolClient, MockConfigStoreClient } from "../src/clients/mocks"; import { DepositWithBlock, FillWithBlock, SlowFillRequest, SlowFillRequestWithBlock } from "../src/interfaces"; import { EMPTY_MESSAGE, ZERO_ADDRESS } from "../src/constants"; -import { getCurrentTime, getRelayDataHash, isDefined, randomAddress } from "../src/utils"; +import { getCurrentTime, isDefined, randomAddress } from "../src/utils"; import { createSpyLogger, fillFromDeposit,