Skip to content

Commit

Permalink
feat(store-sync): add more logging to waitForTransaction (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Aug 18, 2023
1 parent c32a926 commit 3e024fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-fans-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

add retry attempts and more logging to `waitForTransaction`
1 change: 1 addition & 0 deletions packages/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"debug": "^4.3.4",
"drizzle-orm": "^0.27.0",
"kysely": "^0.26.1",
"p-retry": "^5.1.2",
"rxjs": "7.5.5",
"sql.js": "^1.8.0",
"superjson": "^1.12.4",
Expand Down
22 changes: 17 additions & 5 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Hex, TransactionReceipt } from "viem";
import { SetRecordOperation, SyncOptions, SyncResult } from "./common";
import { createBlockStream, blockRangeToLogs, groupLogsByBlockNumber } from "@latticexyz/block-logs-stream";
import { filter, map, tap, mergeMap, from, concatMap, share, firstValueFrom } from "rxjs";
import pRetry from "p-retry";
import { blockLogsToStorage } from "./blockLogsToStorage";
import { debug as parentDebug } from "./debug";
import { createIndexerClient } from "./trpc-indexer";
Expand Down Expand Up @@ -148,17 +149,28 @@ export async function createStoreSync<TConfig extends StoreConfig = StoreConfig>
async function waitForTransaction(tx: Hex): Promise<{
receipt: TransactionReceipt;
}> {
// Wait for tx to be mined
const receipt = await publicClient.waitForTransactionReceipt({ hash: tx });
// viem doesn't retry timeouts, so we'll wrap in a retry
const receipt = await pRetry(
(attempt) => {
// Wait for tx to be mined
debug("waiting for tx receipt", tx, "attempt", attempt);
return publicClient.waitForTransactionReceipt({
hash: tx,
timeout: publicClient.pollingInterval * 2 * attempt,
});
},
{ retries: 3 }
);
debug("got tx receipt", tx, receipt);

// If we haven't processed a block yet or we haven't processed the block for the tx, wait for it
if (lastBlockNumberProcessed == null || lastBlockNumberProcessed < receipt.blockNumber) {
debug("waiting for tx block to be processed", tx, receipt.blockNumber);
await firstValueFrom(
blockStorageOperations$.pipe(
filter(({ blockNumber }) => blockNumber != null && blockNumber >= receipt.blockNumber)
)
blockStorageOperations$.pipe(filter(({ blockNumber }) => blockNumber >= receipt.blockNumber))
);
}
debug("tx block was processed", tx, receipt.blockNumber);

return { receipt };
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e024fc

Please sign in to comment.