Skip to content

Commit

Permalink
feat(relayer): Add Prometheus and Winston logging and metric tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-nikolov96 committed Sep 14, 2023
1 parent 4937006 commit a4a81ab
Show file tree
Hide file tree
Showing 31 changed files with 579 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ result
# Nim
nimcache/

# Data
data/

# Circom related
*.ptau
build
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
vendor
*.json
*.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { readFileSync, writeFileSync } from 'fs';
import { exec } from 'child_process';
import { promisify } from 'util';
import * as vkey from './converted-vkey.json';
import { join } from 'path';
import { getGenericLogger } from '../../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();
const promiseExec = promisify(exec);
const network = 'mainnet';

Expand All @@ -30,13 +31,13 @@ let period = 291;

(async () => {
for (let update of UPDATES.slice(1)) {
console.log('Proof convertion...');
logger.info('Proof convertion...');
await promiseExec(
`python ${path.join(__dirname, 'proof_converter.py')} ${proofsDir}/proof${
period - 1
}.json ${proofsDir}/public${period - 1}.json`,
);
console.log('Input generation...');
logger.info('Input generation...');
const proof = JSON.parse(readFileSync(`proof.json`).toString());

writeFileSync(
Expand All @@ -55,8 +56,8 @@ let period = 291;
),
);

console.log('Witness generation...');
console.log(
logger.info('Witness generation...');
logger.info(
await promiseExec(
`${path.join(
__dirname,
Expand All @@ -65,8 +66,8 @@ let period = 291;
),
);

console.log('Proof generation...');
console.log(
logger.info('Proof generation...');
logger.info(
await promiseExec(
`${path.join(
__dirname,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { groth16 } from 'snarkjs';
import { readFileSync } from 'fs';
import { getGenericLogger } from '../../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

(async () => {
for (let i = 291; i <= 416; i++) {
Expand All @@ -22,9 +25,9 @@ import { readFileSync } from 'fs';
const isValid = await groth16.verify(verificationKey, pub, proof);

if (isValid) {
console.log(`Verified recursive proof for period: ${i}`);
logger.info(`Verified recursive proof for period: ${i}`);
} else {
console.log(`Invalid proof`, '\x1b[31m');
logger.info(`Invalid proof`, '\x1b[31m');
process.exit(1);
}
}
Expand Down
6 changes: 5 additions & 1 deletion beacon-light-client/solidity/tasks/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { task } from 'hardhat/config';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

task('accounts', 'Prints the list of accounts', async (_, { ethers }) => {
(await ethers.getSigners()).map(a => console.log(a.address));
logger.info('Getting Signers..');
(await ethers.getSigners()).map(a => logger.info(a.address));
});
20 changes: 11 additions & 9 deletions beacon-light-client/solidity/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { task } from 'hardhat/config';
import { BeaconApi } from '../../../relay/implementations/beacon-api';
import { getConstructorArgs } from './utils';
import { getNetworkConfig } from '../../../relay/utils/get_current_network_config';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

task('deploy', 'Deploy the beacon light client contract')
.addParam('slot', 'The slot ')
.addParam('follownetwork', 'The network to follow')
.setAction(async (args, { run, ethers }) => {
if (args.follownetwork !== 'pratter' && args.follownetwork !== 'mainnet') {
console.warn('This follownetwork is not specified in networkconfig');
logger.warn('This follownetwork is not specified in networkconfig');
return;
}

Expand All @@ -17,8 +20,8 @@ task('deploy', 'Deploy the beacon light client contract')
await run('compile');
const [deployer] = await ethers.getSigners();

console.log('Deploying contracts with the account:', deployer.address);
console.log('Account balance:', (await deployer.getBalance()).toString());
logger.info(`Deploying contracts with the account: ${deployer.address}`);
logger.info(`Account balance: ${(await deployer.getBalance()).toString()}`);

const beaconApi = new BeaconApi(currentConfig.BEACON_REST_API);

Expand All @@ -28,15 +31,14 @@ task('deploy', 'Deploy the beacon light client contract')
...(await getConstructorArgs(beaconApi, args.slot, currentConfig)),
);

console.log('>>> Waiting for BeaconLightClient deployment...');
logger.info('>>> Waiting for BeaconLightClient deployment...');

console.log(
'Deploying transaction hash..',
beaconLightClient.deployTransaction.hash,
logger.info(
`Deploying transaction hash.. ${beaconLightClient.deployTransaction.hash}`,
);

const contract = await beaconLightClient.deployed();

console.log(`>>> ${contract.address}`);
console.log('>>> Done!');
logger.info(`>>> ${contract.address}`);
logger.info('>>> Done!');
});
5 changes: 4 additions & 1 deletion beacon-light-client/solidity/tasks/remove-repeat-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Queue } from 'bullmq';
import { GetUpdate } from '../../../relay/types/types';
import { UPDATE_POLING_QUEUE } from '../../../relay/constants/constants';
import { task } from 'hardhat/config';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

task('remove-repeat-job', 'Run update recuring task')
.addParam('jobkey', 'The job key')
Expand All @@ -21,7 +24,7 @@ task('remove-repeat-job', 'Run update recuring task')
},
});

console.log(await updateQueue.getRepeatableJobs());
logger.info(await updateQueue.getRepeatableJobs());

await updateQueue.removeRepeatableByKey(args.jobkey);
});
36 changes: 32 additions & 4 deletions beacon-light-client/solidity/tasks/start-publishing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { checkConfig } from '../../../libs/typescript/ts-utils/common-utils';
import { Contract, ethers } from 'ethers';
import hashi_abi from './hashi_abi.json';
import { getNetworkConfig } from '../../../relay/utils/get_current_network_config';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';
import { initPrometheusSetup } from '../../../libs/typescript/ts-utils/prometheus-utils';

const logger = getGenericLogger();

task('start-publishing', 'Run relayer')
.addParam('lightclient', 'The address of the BeaconLightClient contract')
Expand All @@ -33,6 +37,13 @@ task('start-publishing', 'Run relayer')
undefined,
true,
)
.addParam(
'prometheusport',
'Port No. (3000-3005) for Node Express server where Prometheus is listening.',
'',
undefined,
true,
)
.setAction(async (args, { ethers, network }) => {
const config = {
REDIS_HOST: process.env.REDIS_HOST,
Expand All @@ -42,10 +53,25 @@ task('start-publishing', 'Run relayer')
checkConfig(config);

if (args.follownetwork !== 'pratter' && args.follownetwork !== 'mainnet') {
console.warn('This follownetwork is not specified in networkconfig');
logger.warn('This follownetwork is not specified in networkconfig');
return;
}

if (args.prometheusport) {
console.log(`Initializing Prometheus on port ${args.prometheusport}`);

let networkName: string = '';
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg === '--network' && i + 1 < process.argv.length) {
networkName = process.argv[i + 1];
break;
}
}

initPrometheusSetup(args.prometheusport, networkName);
}

const currentConfig = getNetworkConfig(args.follownetwork);

let publisher;
Expand All @@ -56,10 +82,12 @@ task('start-publishing', 'Run relayer')
publisher = new ethers.Wallet(args.privatekey, ethers.provider);
}

console.log('Publishing updates with the account:', publisher.address);
console.log('Account balance:', (await publisher.getBalance()).toString());
logger.info(`Publishing updates with the account: ${publisher.address}`);
logger.info(
`Account balance: ${(await publisher.getBalance()).toString()}`,
);

console.log(`Contract address ${args.lightclient}`);
logger.info(`Contract address ${args.lightclient}`);

const lightClientContract = await ethers.getContractAt(
'BeaconLightClient',
Expand Down
5 changes: 4 additions & 1 deletion beacon-light-client/solidity/tasks/verify-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { task } from 'hardhat/config';
import { BeaconApi } from '../../../relay/implementations/beacon-api';
import { getConstructorArgs } from './utils';
import { getNetworkConfig } from '../../../relay/utils/get_current_network_config';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

task('verify-contracts', 'Verify')
.addParam('lightclient', 'The address of the BeaconLightClient contract')
.addParam('slot', 'The slot ')
.addParam('follownetwork', 'The network to follow')
.setAction(async (args, { run }) => {
if (args.follownetwork !== 'pratter' && args.follownetwork !== 'mainnet') {
console.warn('This follownetwork is not specified in networkconfig');
logger.warn('This follownetwork is not specified in networkconfig');
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ethers } from 'hardhat';
import { getFilesInDir, Proof } from './utils';
import { convertProofToSolidityCalldata } from '../../../libs/typescript/ts-utils/zk-utils';
import INITIAL_UPDATE from '../../../vendor/eth2-light-client-updates/prater/capella-updates-94/update_5601823_5609044.json';
import { getGenericLogger } from '../../../libs/typescript/ts-utils/logger';

const logger = getGenericLogger();

describe.only('BeaconLightClientReadyProofs', async function () {
let proofs: Proof[];
Expand Down Expand Up @@ -49,11 +52,11 @@ describe.only('BeaconLightClientReadyProofs', async function () {
});

it('Importing real data', async function () {
console.log(' >>> Begin importing of real updates');
logger.info(' >>> Begin importing of real updates');
for (let i = 1; i < updates.length; i++) {
const proof = await convertProofToSolidityCalldata(proofs[i], publics[i]);

console.log(` >>> Importing update ${i}...`);
logger.info(` >>> Importing update ${i}...`);

const transaction = await blc.light_client_update(
{ ...proof, ...updates[i] },
Expand All @@ -64,7 +67,7 @@ describe.only('BeaconLightClientReadyProofs', async function () {

const result = await transaction.wait();

console.log(` >>> Successfully imported update ${i}!`);
logger.info(` >>> Successfully imported update ${i}!`);
}
});
});
Loading

0 comments on commit a4a81ab

Please sign in to comment.