require('../config/loadEnv')();
const assert = require('assert');
const { JsonRpcProvider } = require('ethers');
const { EVM_CANDIDATE_CONFIG } = require('../config/evmCandidateConfig');

async function main() {
  const provider = new JsonRpcProvider(EVM_CANDIDATE_CONFIG.rpcUrl);
  const network = await provider.getNetwork();
  assert.equal(Number(network.chainId), EVM_CANDIDATE_CONFIG.chainId);

  const [validators, peerCountHex, firstBlock] = await Promise.all([
    provider.send('qbft_getValidatorsByBlockNumber', ['latest']),
    provider.send('net_peerCount', []),
    provider.getBlockNumber()
  ]);
  assert.ok(validators.length >= EVM_CANDIDATE_CONFIG.validatorCount, 'QBFT validator quorum is incomplete');
  assert.ok(Number(peerCountHex) >= 1, 'RPC node is disconnected from the canonical QBFT network');
  await new Promise((resolve) => setTimeout(resolve, 12_000));
  const secondBlock = await provider.getBlockNumber();
  assert.ok(secondBlock > firstBlock, 'QBFT block height did not advance');

  console.log(JSON.stringify({
    ok: true,
    chainId: Number(network.chainId),
    consensus: 'QBFT',
    validators: validators.length,
    peers: Number(peerCountHex),
    validatorConnectivityMode: 'public-bridge-and-remote-validator-mesh',
    rpcPeerFloor: 1,
    readOnly: true,
    transactionsSent: 0,
    firstBlock,
    blockNumber: secondBlock,
    advancedBy: secondBlock - firstBlock
  }));
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});
