|
| 1 | +import { keccak256 as keccak256WASM } from '@polkadot/wasm-crypto' |
| 2 | +import { keccak256 } from 'ethereum-cryptography/keccak' |
| 3 | +import * as fs from 'fs' |
| 4 | +import { assert, describe, it } from 'vitest' |
| 5 | + |
| 6 | +import { generateClientConfig } from '../../bin/utils.js' |
| 7 | + |
| 8 | +import type { ClientOpts } from '../../src/types.js' |
| 9 | + |
| 10 | +describe('generateClientConfig', () => { |
| 11 | + it('should use chainId over networkId and network name', async () => { |
| 12 | + const opts: ClientOpts = { |
| 13 | + chainId: 11155111, |
| 14 | + networkId: 1, |
| 15 | + network: 'mainnet', |
| 16 | + } |
| 17 | + const { common } = await generateClientConfig(opts) |
| 18 | + assert.equal(common.chainId(), 11155111n) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should fall back to networkId if chainId is not provided', async () => { |
| 22 | + const opts: ClientOpts = { |
| 23 | + networkId: 11155111, |
| 24 | + network: 'mainnet', |
| 25 | + } |
| 26 | + const { common } = await generateClientConfig(opts) |
| 27 | + assert.equal(common.chainId(), 11155111n) |
| 28 | + }) |
| 29 | + |
| 30 | + it('should fall back to network name if both chainId and networkId are missing', async () => { |
| 31 | + const opts: ClientOpts = { |
| 32 | + network: 'sepolia', |
| 33 | + } |
| 34 | + const { common } = await generateClientConfig(opts) |
| 35 | + assert.equal(common.chainName(), 'sepolia') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should initialize WASM crypto when useJsCrypto is false', async () => { |
| 39 | + const opts: ClientOpts = { |
| 40 | + useJsCrypto: false, |
| 41 | + } |
| 42 | + const { common } = await generateClientConfig(opts) |
| 43 | + assert.deepEqual( |
| 44 | + common.customCrypto.keccak256, |
| 45 | + keccak256WASM, |
| 46 | + 'WASM keccak256 should be initialized', |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it('should initialize JS crypto when useJsCrypto is true', async () => { |
| 51 | + const opts: ClientOpts = { |
| 52 | + useJsCrypto: true, |
| 53 | + } |
| 54 | + const { common } = await generateClientConfig(opts) |
| 55 | + assert.deepEqual(common.customCrypto.keccak256, keccak256, 'JS keccak256 should be initialized') |
| 56 | + }) |
| 57 | + |
| 58 | + it('should set bootnodes correctly from a file', async () => { |
| 59 | + const dir = fs.mkdtempSync('test-bootnodes') |
| 60 | + const gethGenesis = `enode://97b85ed04d84f2298f61926eef7ec74a7fff3998f71da1d7eab8c136d8719829150edc2f605b6a741fe6c56af0aa957b0375c38553fd0d0f91c5fa752844c08f@172.16.0.10:30303\nenode://7b58fb9d4fd1bef6fe89b517c4443043294d2650af2bea4c3f7c7f7f1caa40e9c8b22d05d46a77b97867b8d2808c4db351c921a072366a7227ae59a620f6ae39@172.16.0.11:30303\nenode://0257b53da52fa0c141d558d0fc439ea4c338904a762802633daa89fd5770e0cbe306a9e82cc059c4e4293502d5d431e1fa7165f3611840c1bb948d48a7e70b5b@172.16.0.12:30303\nenode://d516e6b38bb8c656bf120fb49d66318952911dcb61e5e950360b7d3ea4ac575228e73cddb7a08a308c6e3f541e284553598f7f5ee3c6c5fe140af350d482aff7@172.16.0.13:30303\nenode://a95a3c8712ff2f09af6a08523164361784fd7ed89e5a68b4f064b3c48fde73646f63296bab822a2133f1cbc73d9aa9726b9374f03ca66b7cbb721779cc6f5a87@172.16.0.14:30303` |
| 61 | + fs.open(`${dir}/bootnodes.txt`, 'w', (err, fd) => { |
| 62 | + if (err !== null) throw err |
| 63 | + fs.write(fd, gethGenesis, (writeErr) => { |
| 64 | + if (writeErr !== null) { |
| 65 | + assert.fail(`Error writing the file: ${writeErr.message}`) |
| 66 | + } else { |
| 67 | + assert.ok(true, 'File created and data written successfully!') |
| 68 | + } |
| 69 | + |
| 70 | + fs.close(fd, (closeErr) => { |
| 71 | + if (closeErr) { |
| 72 | + assert.fail(`Error closing the file:, ${closeErr.message}`) |
| 73 | + } |
| 74 | + }) |
| 75 | + }) |
| 76 | + }) |
| 77 | + const opts: ClientOpts = { |
| 78 | + bootnodes: [`./${dir}/bootnodes.txt`], |
| 79 | + } |
| 80 | + const { config } = await generateClientConfig(opts) |
| 81 | + assert.ok(Array.isArray(config.bootnodes), 'Bootnodes should be an array') |
| 82 | + }) |
| 83 | + |
| 84 | + it('should require an unlocked account when mining', async () => { |
| 85 | + const opts: ClientOpts = { |
| 86 | + mine: true, |
| 87 | + } |
| 88 | + try { |
| 89 | + await generateClientConfig(opts) |
| 90 | + assert.fail('Expected generateClientConfig to throw error when mining without an account') |
| 91 | + } catch (err: any) { |
| 92 | + assert.match(err.message, /Please provide an account to mine blocks/) |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + it('should enable mining when mine=true', async () => { |
| 97 | + const opts: ClientOpts = { |
| 98 | + mine: true, |
| 99 | + dev: true, |
| 100 | + } |
| 101 | + const { config } = await generateClientConfig(opts) |
| 102 | + assert.ok(config.mine, 'Mining should be enabled') |
| 103 | + }) |
| 104 | + |
| 105 | + it('should properly configure Prometheus when enabled', async () => { |
| 106 | + const opts: ClientOpts = { |
| 107 | + prometheus: true, |
| 108 | + prometheusPort: 9090, |
| 109 | + } |
| 110 | + const { metricsServer } = await generateClientConfig(opts) |
| 111 | + assert.ok(metricsServer, 'Prometheus should be enabled and metrics server should be started') |
| 112 | + }) |
| 113 | + |
| 114 | + it('should correctly handle dev mode initialization', async () => { |
| 115 | + const opts: ClientOpts = { |
| 116 | + dev: true, |
| 117 | + dataDir: './test-data', |
| 118 | + } |
| 119 | + const { config } = await generateClientConfig(opts) |
| 120 | + assert.ok(config.mine, 'Mining should be enabled in dev mode') |
| 121 | + assert.ok(config.isSingleNode, 'Single node mode should be enabled') |
| 122 | + }) |
| 123 | + |
| 124 | + it('should properly set logging options', async () => { |
| 125 | + const opts: ClientOpts = { |
| 126 | + logLevel: 'debug', |
| 127 | + } |
| 128 | + const { config } = await generateClientConfig(opts) |
| 129 | + assert.equal(config.logger.level, 'debug', 'Log level should be set to debug') |
| 130 | + }) |
| 131 | +}) |
0 commit comments