Skip to content

Commit 2cbff0d

Browse files
committed
improve(BundleDataClient): Add comment about not running binary search on "unsafe" deposit ID's
I'm combing through the SDK for any functions that will need to change once this contracts [PR](across-protocol/contracts#583) is merged and here are some changes: - Added comments to BundleDataClient - Export useful constant `MAX_SAFE_DEPOSIT_ID` - Export useful utility `isUnsafeDepositId()` - Delete unused functions that wouldn't work post spoke pool upgrade because the `V3RelayData.depositId` type will change to uint256
1 parent d794044 commit 2cbff0d

File tree

4 files changed

+22
-102
lines changed

4 files changed

+22
-102
lines changed

src/clients/BundleDataClient/BundleDataClient.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,9 @@ export class BundleDataClient {
899899
bundleInvalidFillsV3.push(fill);
900900
return;
901901
}
902+
// If deposit is using the deterministic relay hash feature, then the following binary search-based
903+
// algorithm will not work. However, it is impossible to emit an infinite fill deadline using
904+
// the unsafeDepositV3 function so there is no need to catch the special case.
902905
const historicalDeposit = await queryHistoricalDepositForFill(originClient, fill);
903906
if (!historicalDeposit.found) {
904907
bundleInvalidFillsV3.push(fill);
@@ -1003,6 +1006,10 @@ export class BundleDataClient {
10031006
// older deposit in case the spoke pool client's lookback isn't old enough to find the matching deposit.
10041007
// We can skip this step if the deposit's fill deadline is not infinite, because we can assume that the
10051008
// spoke pool clients have loaded deposits old enough to cover all fills with a non-infinite fill deadline.
1009+
// We do not need to handle the case where the deposit ID is > uint32 (in which case we wouldn't
1010+
// want to perform a binary search lookup for it because the deposit ID is "unsafe" and cannot be
1011+
// found using such a method) because infinite fill deadlines cannot be produced from the unsafeDepositV3()
1012+
// function.
10061013
if (
10071014
INFINITE_FILL_DEADLINE.eq(slowFillRequest.fillDeadline) &&
10081015
slowFillRequest.blockNumber >= destinationChainBlockRange[0]

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export const { AddressZero: ZERO_ADDRESS } = ethersConstants;
1515
// 2^96 - 1 is a conservative erc20 max allowance.
1616
export const MAX_SAFE_ALLOWANCE = "79228162514264337593543950335";
1717

18+
// The maximum depositId that can be emitted in a depositV3 method is the maximum uint32 value, so
19+
// 2^32 - 1.
20+
export const MAX_SAFE_DEPOSIT_ID = "4294967295";
21+
1822
export const SECONDS_PER_YEAR = 31557600; // 365.25 days per year.
1923

2024
/**

src/utils/SpokeUtils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
22
import { BytesLike, Contract, PopulatedTransaction, providers, utils as ethersUtils } from "ethers";
3-
import { CHAIN_IDs, ZERO_ADDRESS } from "../constants";
3+
import { CHAIN_IDs, MAX_SAFE_DEPOSIT_ID, ZERO_ADDRESS } from "../constants";
44
import { Deposit, Fill, FillStatus, RelayData, SlowFillRequest } from "../interfaces";
55
import { SpokePoolClient } from "../clients";
66
import { chunk } from "./ArrayUtils";
@@ -244,6 +244,16 @@ export function getRelayHashFromEvent(e: Deposit | Fill | SlowFillRequest): stri
244244
return getRelayDataHash(e, e.destinationChainId);
245245
}
246246

247+
export function isUnsafeDepositId(depositId: number): boolean {
248+
// SpokePool.unsafeDepositV3() produces a uint256 depositId by hashing the msg.sender, depositor and input
249+
// uint256 depositNonce. There is a possibility that this resultant uint256 is less than the maxSafeDepositId (i.e.
250+
// the maximum uint32 value) which makes it possible that an unsafeDepositV3's depositId can collide with a safe
251+
// depositV3's depositId, but the chances of a collision are 1 in 2^(256 - 32), so we'll ignore this
252+
// possibility.
253+
const maxSafeDepositId = BigNumber.from(MAX_SAFE_DEPOSIT_ID);
254+
return maxSafeDepositId.lt(depositId);
255+
}
256+
247257
/**
248258
* Find the amount filled for a deposit at a particular block.
249259
* @param spokePool SpokePool contract instance.

src/utils/common.ts

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import assert from "assert";
44
import Decimal from "decimal.js";
55
import { ethers, PopulatedTransaction, providers, VoidSigner } from "ethers";
66
import { getGasPriceEstimate } from "../gasPriceOracle";
7-
import { TypedMessage } from "../interfaces/TypedData";
87
import { BigNumber, BigNumberish, BN, formatUnits, parseUnits, toBN } from "./BigNumberUtils";
98
import { ConvertDecimals } from "./FormattingUtils";
109
import { chainIsOPStack } from "./NetworkUtils";
@@ -323,106 +322,6 @@ async function getLineaGasFees(chainId: number, transport: Transport | undefined
323322
};
324323
}
325324

326-
export type UpdateDepositDetailsMessageType = {
327-
UpdateDepositDetails: [
328-
{
329-
name: "depositId";
330-
type: "uint32";
331-
},
332-
{ name: "originChainId"; type: "uint256" },
333-
{ name: "updatedRelayerFeePct"; type: "int64" },
334-
{ name: "updatedRecipient"; type: "address" },
335-
{ name: "updatedMessage"; type: "bytes" },
336-
];
337-
};
338-
339-
export type UpdateV3DepositDetailsMessageType = {
340-
UpdateDepositDetails: [
341-
{ name: "depositId"; type: "uint32" },
342-
{ name: "originChainId"; type: "uint256" },
343-
{ name: "updatedOutputAmount"; type: "uint256" },
344-
{ name: "updatedRecipient"; type: "address" },
345-
{ name: "updatedMessage"; type: "bytes" },
346-
];
347-
};
348-
349-
/**
350-
* Utility function to get EIP-712 compliant typed data that can be signed with the JSON-RPC method
351-
* `eth_signedTypedDataV4` in MetaMask (https://docs.metamask.io/guide/signing-data.html). The resulting signature
352-
* can then be used to call the method `speedUpDeposit` of a `SpokePool.sol` contract.
353-
* @param depositId The deposit ID to speed up.
354-
* @param originChainId The chain ID of the origin chain.
355-
* @param updatedRelayerFeePct The new relayer fee percentage.
356-
* @param updatedRecipient The new recipient address.
357-
* @param updatedMessage The new message that should be provided to the recipient.
358-
* @return EIP-712 compliant typed data.
359-
*/
360-
export function getUpdateDepositTypedData(
361-
depositId: number,
362-
originChainId: number,
363-
updatedRelayerFeePct: BigNumber,
364-
updatedRecipient: string,
365-
updatedMessage: string
366-
): TypedMessage<UpdateDepositDetailsMessageType> {
367-
return {
368-
types: {
369-
UpdateDepositDetails: [
370-
{ name: "depositId", type: "uint32" },
371-
{ name: "originChainId", type: "uint256" },
372-
{ name: "updatedRelayerFeePct", type: "int64" },
373-
{ name: "updatedRecipient", type: "address" },
374-
{ name: "updatedMessage", type: "bytes" },
375-
],
376-
},
377-
primaryType: "UpdateDepositDetails",
378-
domain: {
379-
name: "ACROSS-V2",
380-
version: "1.0.0",
381-
chainId: originChainId,
382-
},
383-
message: {
384-
depositId,
385-
originChainId,
386-
updatedRelayerFeePct,
387-
updatedRecipient,
388-
updatedMessage,
389-
},
390-
};
391-
}
392-
393-
export function getUpdateV3DepositTypedData(
394-
depositId: number,
395-
originChainId: number,
396-
updatedOutputAmount: BigNumber,
397-
updatedRecipient: string,
398-
updatedMessage: string
399-
): TypedMessage<UpdateV3DepositDetailsMessageType> {
400-
return {
401-
types: {
402-
UpdateDepositDetails: [
403-
{ name: "depositId", type: "uint32" },
404-
{ name: "originChainId", type: "uint256" },
405-
{ name: "updatedOutputAmount", type: "uint256" },
406-
{ name: "updatedRecipient", type: "address" },
407-
{ name: "updatedMessage", type: "bytes" },
408-
],
409-
},
410-
primaryType: "UpdateDepositDetails",
411-
domain: {
412-
name: "ACROSS-V2",
413-
version: "1.0.0",
414-
chainId: originChainId,
415-
},
416-
message: {
417-
depositId,
418-
originChainId,
419-
updatedOutputAmount,
420-
updatedRecipient,
421-
updatedMessage,
422-
},
423-
};
424-
}
425-
426325
export function randomAddress() {
427326
return ethers.utils.getAddress(ethers.utils.hexlify(ethers.utils.randomBytes(20)));
428327
}

0 commit comments

Comments
 (0)