Skip to content
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

fix: fix and optimise fetching the recovery state #2807

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Changes from 1 commit
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
43 changes: 27 additions & 16 deletions src/services/recovery/recovery-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import type { JsonRpcProvider } from '@ethersproject/providers'
import type { TransactionReceipt } from '@ethersproject/abstract-provider'

import type { RecoveryQueueItem, RecoveryState } from '@/store/recoverySlice'
import { hexZeroPad } from 'ethers/lib/utils'

const MAX_PAGE_SIZE = 100

export const _getQueuedTransactionsAdded = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the unit test for this.

transactionsAdded: Array<TransactionAddedEvent>,
txNonce: BigNumber,
): Array<TransactionAddedEvent> => {
// Only queued transactions with queueNonce >= current txNonce
return transactionsAdded.filter(({ args }) => args.queueNonce.gte(txNonce))
}

export const _getRecoveryQueueItem = async (
transactionAdded: TransactionAddedEvent,
txCooldown: BigNumber,
Expand Down Expand Up @@ -48,7 +41,7 @@ export const _getSafeCreationReceipt = memoize(
safeAddress: string
provider: JsonRpcProvider
}): Promise<TransactionReceipt> => {
const url = `${transactionService}/v1/${safeAddress}/creation/`
const url = `${transactionService}api/v1/safes/${safeAddress}/creation/`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems as though there can be a forward slash on some networks. Perhaps we should check if one is required and/or fix the configs.


const { transactionHash } = await fetch(url).then((res) => {
if (res.ok && res.status === 200) {
Expand All @@ -63,6 +56,28 @@ export const _getSafeCreationReceipt = memoize(
({ transactionService, safeAddress }) => transactionService + safeAddress,
)

const queryAddedTransactions = async (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a unit test for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more tests for it's outside function effectively also testing this logic.

delayModifier: Delay,
queueNonce: BigNumber,
txNonce: BigNumber,
startBlockNumber: number,
) => {
if (queueNonce.eq(txNonce)) {
// There are no queued txs
return []
}

const transactionAddedFilter = delayModifier.filters.TransactionAdded()

const diff = queueNonce.sub(txNonce).toNumber()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this into the if below.

if (transactionAddedFilter.topics) {
const queueNonceFilter = Array.from({ length: diff }, (_, idx) => hexZeroPad(txNonce.add(idx).toHexString(), 32))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment explaining this?

transactionAddedFilter.topics[1] = queueNonceFilter
}

return await delayModifier.queryFilter(transactionAddedFilter, startBlockNumber, 'latest')
}

export const getRecoveryState = async ({
delayModifier,
...rest
Expand All @@ -72,21 +87,17 @@ export const getRecoveryState = async ({
safeAddress: string
provider: JsonRpcProvider
}): Promise<RecoveryState[number]> => {
const { blockHash } = await _getSafeCreationReceipt(rest)

const transactionAddedFilter = delayModifier.filters.TransactionAdded()
const { blockNumber } = await _getSafeCreationReceipt(rest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this into queryAddedTransactions so it doesn't call _getSafeCreationReceipt when no queued transactions exist.


const [[modules], txExpiration, txCooldown, txNonce, queueNonce, transactionsAdded] = await Promise.all([
const [[modules], txExpiration, txCooldown, txNonce, queueNonce] = await Promise.all([
delayModifier.getModulesPaginated(SENTINEL_ADDRESS, MAX_PAGE_SIZE),
delayModifier.txExpiration(),
delayModifier.txCooldown(),
delayModifier.txNonce(),
delayModifier.queueNonce(),
// TODO: Improve log retrieval
delayModifier.queryFilter(transactionAddedFilter, blockHash),
])

const queuedTransactionsAdded = _getQueuedTransactionsAdded(transactionsAdded, txNonce)
const queuedTransactionsAdded = await queryAddedTransactions(delayModifier, queueNonce, txNonce, blockNumber)

const queue = await Promise.all(
queuedTransactionsAdded.map((transactionAdded) =>
Expand Down
Loading