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

feat(store-sync): add more logging to waitForTransaction #1317

Merged
merged 9 commits into from Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-fans-nail.md
@@ -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
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
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.