Skip to content

Commit

Permalink
fix: data bus tests on hardhat
Browse files Browse the repository at this point in the history
  • Loading branch information
Amuhar committed Nov 26, 2024
1 parent e7be612 commit 3324225
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 59 deletions.
4 changes: 3 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const config: HardhatUserConfig = {
],
},
},
solidity: '0.8.4',
solidity: {
version: '0.8.4',
},
};

export default config;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node --max-old-space-size=4096 dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down
31 changes: 17 additions & 14 deletions src/contracts/data-bus/data-bus.client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Block } from '@ethersproject/providers';
import { ethers } from 'ethers';
import { formatBytes32String } from 'ethers/lib/utils';
import { Server } from 'ganache';
import {
CHAIN_ID,
FORK_BLOCK,
GANACHE_PORT,
UNLOCKED_ACCOUNTS,
} from './utils/constants';
import { makeServer } from '../../../test/server';
import { GANACHE_PORT } from './utils/constants';
import { DataBusClient } from './data-bus.client';
import {
MessageDepositV1,
Expand All @@ -19,6 +12,11 @@ import {
MessagesNames,
MessageUnvetV1,
} from './data-bus.serializer';
import { HardhatServer } from '../../../test/helpers/hardhat-server';
import { accountImpersonate, setBalance } from '../../../test/helpers/provider';
import { getSecurityOwner } from '../../../test/helpers/dsm';

jest.setTimeout(40_000);

export const randomInt = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;
Expand Down Expand Up @@ -100,25 +98,30 @@ describe('DataBus', () => {
let provider: ethers.providers.JsonRpcProvider;
let owner: ethers.Signer;
let sdk: DataBusClient;
let server: Server<'ethereum'>;
let variants: ReturnType<typeof getVariants>;
let hardhatServer: HardhatServer;
let dsmOwnerAddress: string;

const setupServer = async () => {
server = makeServer(FORK_BLOCK, CHAIN_ID, UNLOCKED_ACCOUNTS, true);
await server.listen(GANACHE_PORT);
hardhatServer = new HardhatServer();
await hardhatServer.start();
};

beforeEach(async () => {
await setupServer();
dsmOwnerAddress = await getSecurityOwner();
await accountImpersonate(dsmOwnerAddress);
await setBalance(dsmOwnerAddress, 100);

// Set up Ganache provider (ensure Ganache is running on port 8545)
provider = new ethers.providers.JsonRpcProvider(
'http://localhost:' + GANACHE_PORT,
);
variants = getVariants(await provider.getBlock('latest'));

// Get the first account as the owner
const accounts = await provider.listAccounts();
owner = provider.getSigner(accounts[0]);
// const accounts = await provider.listAccounts();
owner = provider.getSigner(dsmOwnerAddress); //accounts[0]);

// Deploy the DataBus contract from bytecode
const dataBusBytecode =
Expand All @@ -142,7 +145,7 @@ describe('DataBus', () => {
});

afterEach(async () => {
await server.close();
await hardhatServer.stop();
});

it('should measure gas for sendPingMessage', async () => {
Expand Down
7 changes: 0 additions & 7 deletions src/contracts/data-bus/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
// TODO: get rid of constants
// read from contract with use of helpers in test/
export const SECURITY_MODULE_OWNER =
'0xE92329EC7ddB11D25e25b3c21eeBf11f15eB325d';
export const CHAIN_ID = 17000;
export const GANACHE_PORT = 8545;
export const UNLOCKED_ACCOUNTS = [SECURITY_MODULE_OWNER];
export const FORK_BLOCK = 1894357;
1 change: 1 addition & 0 deletions test/helpers/hardhat-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class HardhatServer {
// Listen for stdout to detect when Hardhat is ready
this.hardhatProcess.stdout.on('data', (data) => {
const output = data.toString();
// console.log(`Hardhat stdout: ${output}`);

// Check for the Hardhat ready message
if (output.includes('Started HTTP and WebSocket JSON-RPC server')) {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TEST_SERVER_URL } from '../constants';

export const testSetupProvider = new JsonRpcProvider(TEST_SERVER_URL);

export const accountImpersonate = async (account): Promise<void> => {
export const accountImpersonate = async (account: string): Promise<void> => {
testSetupProvider.send('hardhat_impersonateAccount', [account]);
};

Expand Down
35 changes: 0 additions & 35 deletions test/server.ts

This file was deleted.

0 comments on commit 3324225

Please sign in to comment.