Skip to content

Commit

Permalink
fix(store-sync): reduce latency in waitForTransaction (#2665)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
alvrs and holic authored Apr 16, 2024
1 parent 6c8ab47 commit de3bc3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-hairs-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

Small optimizations in `waitForTransaction` to parallelize network requests.
11 changes: 9 additions & 2 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
combineLatest,
scan,
identity,
mergeMap,
} from "rxjs";
import { debug as parentDebug } from "./debug";
import { SyncStep } from "./SyncStep";
Expand Down Expand Up @@ -197,7 +198,9 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(
startBlock = range.startBlock;
endBlock = range.endBlock;
}),
concatMap((range) => {
// We use `map` instead of `concatMap` here to send the fetch request immediately when a new block range appears,
// instead of sending the next request only when the previous one completed.
map((range) => {
const storedBlocks = fetchAndStoreLogs({
publicClient,
address,
Expand All @@ -213,6 +216,8 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(

return from(storedBlocks);
}),
// `concatMap` turns the stream of promises into their awaited values
concatMap(identity),
tap(({ blockNumber, logs }) => {
debug("stored", logs.length, "logs for block", blockNumber);
lastBlockNumberProcessed = blockNumber;
Expand Down Expand Up @@ -263,7 +268,9 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(
// This currently blocks for async call on each block processed
// We could potentially speed this up a tiny bit by racing to see if 1) tx exists in processed block or 2) fetch tx receipt for latest block processed
const hasTransaction$ = recentBlocks$.pipe(
concatMap(async (blocks) => {
// We use `mergeMap` instead of `concatMap` here to send the fetch request immediately when a new block range appears,
// instead of sending the next request only when the previous one completed.
mergeMap(async (blocks) => {
const txs = blocks.flatMap((block) => block.logs.map((op) => op.transactionHash).filter(isDefined));
if (txs.includes(tx)) return true;

Expand Down

0 comments on commit de3bc3d

Please sign in to comment.