mirror of
https://github.com/datahaven-xyz/datahaven
synced 2026-05-24 01:38:32 +00:00
This PR significantly refactors and improves the end-to-end testing framework and infrastructure. The primary focus was on simplifying the test suites, improving reliability through better resource management, and hardening the relayer infrastructure. All E2E tests are now passing on the CI and demonstrate consistent reliability when run locally. ### Key Changes #### 1. E2E Test Suite Refactor & Cleanup * **Simplified Test Logic**: Heavily refactored the core test suites (`native-token-transfer.test.ts`, `rewards-message.test.ts`, and `validator-set-update.test.ts`). The new implementation is much cleaner, utilizing shared helpers to reduce boilerplate. * **Utility Consolidation**: Removed redundant utility files (`storage.ts`, `rewards-helpers.ts`) and simplified `events.ts`. Event waiting now uses `rxjs` for Substrate and native `viem` watchers for Ethereum, which is more robust and easier to maintain. * **Better Connector Management**: Unified the creation and cleanup of test clients in `ConnectorFactory`. It now handles the lifecycle of WebSocket connections more gracefully, including clearing the `socketClientCache` to prevent reconnection noise during teardown. #### 2. Infrastructure & Stability * **Relayer Relaunch Policy**: Added a restart policy for Snowbridge relayer containers. They are now configured with `--restart on-failure:5`, ensuring that relayers automatically relaunch if they crash during the sensitive initialization phase. * **WebSocket Integration**: * Updated the `ConnectorFactory` to prefer **WebSockets** for the Ethereum public client, which is essential for efficient, event-heavy E2E testing. * Enhanced `launchKurtosisNetwork` to correctly identify and register the Execution Layer's WebSocket endpoint from Kurtosis. * **Disabled Contract Injection**: This PR temporarily disables the automatic injection of contracts into the genesis state by default. * *Reason*: I encountered issues generating a valid `state-diff.json` for the latest contract versions. Even after applying several workarounds, the injected state remained unstable. As a result, I've reverted to manual contract deployment during the launch sequence for better reliability for now. #### 3. Documentation & Maintenance * Removed obsolete documentation (`event-utilities-guide.md`) that no longer reflects the simplified event-handling API. * Cleaned up `test/launcher/validators.ts` and moved logic into more appropriate helpers. --------- Co-authored-by: Steve Degosserie <723552+stiiifff@users.noreply.github.com>
97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import { firstValueFrom } from "rxjs";
|
|
import { filter as rxFilter, take, timeout } from "rxjs/operators";
|
|
import type { Abi, Address, Log, PublicClient } from "viem";
|
|
import { logger } from "./logger";
|
|
import type { DataHavenApi } from "./papi";
|
|
|
|
export interface WaitForDataHavenEventOptions<T = unknown> {
|
|
api: DataHavenApi;
|
|
pallet: string;
|
|
event: string;
|
|
filter?: (event: T) => boolean;
|
|
/** Default: 30000ms */
|
|
timeout?: number;
|
|
}
|
|
|
|
/** Waits for a DataHaven chain event. Throws on timeout. */
|
|
export async function waitForDataHavenEvent<T = unknown>(
|
|
options: WaitForDataHavenEventOptions<T>
|
|
): Promise<T> {
|
|
const { api, pallet, event, filter, timeout: timeoutMs = 30000 } = options;
|
|
|
|
const watcher = (api.event as any)?.[pallet]?.[event];
|
|
if (!watcher?.watch) {
|
|
throw new Error(`Event ${pallet}.${event} not found in API`);
|
|
}
|
|
|
|
const result = (await firstValueFrom(
|
|
watcher.watch().pipe(
|
|
rxFilter(({ payload }: { payload: T }) => (filter ? filter(payload) : true)),
|
|
take(1),
|
|
timeout({
|
|
first: timeoutMs,
|
|
with: () => {
|
|
throw new Error(`Timeout waiting for ${pallet}.${event} after ${timeoutMs}ms`);
|
|
}
|
|
})
|
|
)
|
|
)) as { payload: T };
|
|
|
|
return result.payload;
|
|
}
|
|
|
|
export interface WaitForEthereumEventOptions<TAbi extends Abi = Abi> {
|
|
client: PublicClient;
|
|
address: Address;
|
|
abi: TAbi;
|
|
eventName: any;
|
|
/** Only indexed event parameters can be filtered */
|
|
args?: any;
|
|
/** Default: 30000ms */
|
|
timeout?: number;
|
|
fromBlock?: bigint;
|
|
}
|
|
|
|
/** Waits for an Ethereum event, throws on timeout. */
|
|
export async function waitForEthereumEvent<TAbi extends Abi = Abi>(
|
|
options: WaitForEthereumEventOptions<TAbi>
|
|
): Promise<Log> {
|
|
const { client, address, abi, eventName, args, timeout = 30000, fromBlock } = options;
|
|
|
|
let unwatch: () => void = () => {};
|
|
let timeoutId!: Timer;
|
|
|
|
const eventPromise = new Promise<Log>((resolve, reject) => {
|
|
unwatch = client.watchContractEvent({
|
|
address,
|
|
abi,
|
|
eventName,
|
|
args,
|
|
fromBlock,
|
|
onLogs: (logs) => {
|
|
if (logs.length === 0) return;
|
|
logger.debug(`Ethereum event ${eventName} received: ${logs.length} logs`);
|
|
resolve(logs[0]);
|
|
},
|
|
onError: (error) => {
|
|
logger.error(`Error watching Ethereum event ${eventName} from ${address}: ${error}`);
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
timeoutId = setTimeout(
|
|
() =>
|
|
reject(new Error(`Timeout waiting for ${eventName} from ${address} after ${timeout}ms`)),
|
|
timeout
|
|
);
|
|
});
|
|
|
|
try {
|
|
return await Promise.race([eventPromise, timeoutPromise]);
|
|
} finally {
|
|
clearTimeout(timeoutId);
|
|
unwatch();
|
|
}
|
|
}
|