-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: Identify reason for deposit <-> fill mismatches (#493)
The combination of abiCoder.encode() and keccak256() when computing relayData hashes seems to be relatively expensive, and this can be a real performance suck when called repeatedly in a tight loop. Additionally, performing a per-field comparison exposes the underlying error when two RelayData objects don't match; this makes it easy to identify the root cause for invalid fills.
- Loading branch information
Showing
5 changed files
with
127 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,38 @@ | ||
import { Deposit, Fill, RelayData, SlowFillRequest } from "../interfaces"; | ||
import { getRelayDataHash } from "./SpokeUtils"; | ||
import { isDefined } from "../utils"; | ||
import { Deposit, RelayData } from "../interfaces"; | ||
|
||
export const FILL_DEPOSIT_COMPARISON_KEYS = [ | ||
export const RELAYDATA_KEYS = [ | ||
"depositId", | ||
"originChainId", | ||
"destinationChainId", | ||
"depositor", | ||
"recipient", | ||
"message", | ||
] as const; | ||
|
||
export const V3_DEPOSIT_COMPARISON_KEYS = [ | ||
...FILL_DEPOSIT_COMPARISON_KEYS, | ||
"inputToken", | ||
"inputAmount", | ||
"outputToken", | ||
"outputAmount", | ||
"fillDeadline", | ||
"exclusivityDeadline", | ||
"exclusiveRelayer", | ||
"message", | ||
] as const; | ||
|
||
export function filledSameDeposit(fillA: Fill, fillB: Fill): boolean { | ||
// Don't bother hashing obvious mismatches. | ||
if (fillA.depositId !== fillB.depositId) { | ||
return false; | ||
} | ||
|
||
const { destinationChainId: chainA } = fillA; | ||
const { destinationChainId: chainB } = fillB; | ||
return getRelayDataHash(fillA, chainA) === getRelayDataHash(fillB, chainB); | ||
} | ||
|
||
// Ensure that each deposit element is included with the same value in the fill. This includes all elements defined | ||
// by the depositor as well as destinationToken, which are pulled from other clients. | ||
export function validateFillForDeposit( | ||
relayData: RelayData & { destinationChainId: number }, // V3Fill, SlowFillRequest... | ||
relayData: RelayData & { destinationChainId: number }, | ||
deposit?: Deposit | ||
): boolean { | ||
): { valid: true } | { valid: false; reason: string } { | ||
if (deposit === undefined) { | ||
return false; | ||
return { valid: false, reason: "Deposit is undefined" }; | ||
} | ||
|
||
return validateV3FillForDeposit(relayData, deposit); | ||
} | ||
// Note: this short circuits when a key is found where the comparison doesn't match. | ||
// TODO: if we turn on "strict" in the tsconfig, the elements of FILL_DEPOSIT_COMPARISON_KEYS will be automatically | ||
// validated against the fields in Fill and Deposit, generating an error if there is a discrepency. | ||
const invalidKey = RELAYDATA_KEYS.find((key) => relayData[key].toString() !== deposit[key].toString()); | ||
|
||
function validateV3FillForDeposit(fill: Fill | SlowFillRequest, deposit: Deposit): boolean { | ||
return getRelayDataHash(fill, fill.destinationChainId) === getRelayDataHash(deposit, deposit.destinationChainId); | ||
return isDefined(invalidKey) | ||
? { valid: false, reason: `${invalidKey} mismatch (${relayData[invalidKey]} != ${deposit[invalidKey]})` } | ||
: { valid: true }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters