Skip to content

Commit

Permalink
common: issue-878 - wait for block number in confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
dwerner committed Apr 18, 2024
1 parent e65224e commit 4a42c4b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
createMetrics,
Logger,
Metrics,
mutable,
parseGRT,
WritableEventual,
} from '@graphprotocol/common-ts'
import {
invalidReallocateAction,
Expand Down Expand Up @@ -166,4 +168,17 @@ describe('Allocation Manager', () => {
expect(reordered[1]).toStrictEqual(reallocateAction)
expect(reordered[2]).toStrictEqual(queuedAllocateAction)
})

test('waitForEventualSequential() waits for a number to be pushed to the stream', async () => {
const latestBlockNumberStream: WritableEventual<number> = mutable(0)
latestBlockNumberStream.push(1)
latestBlockNumberStream.push(2)
latestBlockNumberStream.push(3)
await allocationManager.waitForBlockNumberOnEventual(
logger,
3,
latestBlockNumberStream,
)
expect(await latestBlockNumberStream.value()).toBe(3)
})
})
32 changes: 32 additions & 0 deletions packages/indexer-common/src/indexer-management/allocations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Eventual,
formatGRT,
Logger,
parseGRT,
Expand Down Expand Up @@ -429,6 +430,8 @@ export class AllocationManager {
)
}

await this.waitForBlockNumber(logger, receipt.blockNumber)

const createAllocationEventLogs = this.network.transactionManager.findEvent(
'AllocationCreated',
this.network.contracts.staking.interface,
Expand Down Expand Up @@ -482,6 +485,33 @@ export class AllocationManager {
}
}

async waitForBlockNumber(logger: Logger, blockNumber: number): Promise<void> {
await this.waitForBlockNumberOnEventual(
logger,
blockNumber,
this.network.networkSubgraph.deployment!.status.map(
(status) => status.latestBlock?.number ?? 0,
),
)
}

async waitForBlockNumberOnEventual(
logger: Logger,
number: number,
sequentialNumberStream: Eventual<number>,
): Promise<void> {
for await (const latestSequentialNumber of sequentialNumberStream.values()) {
if (latestSequentialNumber >= number) {
return
} else {
logger.debug('Waiting for block to be reached', {
blockNumber: number,
latestBlock: latestSequentialNumber,
})
}
}
}

async prepareAllocate(
logger: Logger,
context: TransactionPreparationContext,
Expand Down Expand Up @@ -839,6 +869,8 @@ export class AllocationManager {
)
}

await this.waitForBlockNumber(logger, receipt.blockNumber)

const closeAllocationEventLogs = this.network.transactionManager.findEvent(
'AllocationClosed',
this.network.contracts.staking.interface,
Expand Down

0 comments on commit 4a42c4b

Please sign in to comment.