Skip to content

Commit

Permalink
Address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
KarishmaBothara committed May 3, 2023
1 parent b304abf commit c077a67
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions scripts/subscribeEventProof.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,63 +62,61 @@ async function getEventPoofAndSubmit(api, eventId, bridge, txExecutor, newValida
const eventExistsOnEth = await bridge.eventIds(eventId.toString());
if (eventExistsOnEth) return; // return if event proof already exist on Ethereum
const eventProof = await withTimeout(api.derive.ethBridge.eventProof(eventId), timeoutMs);
if (eventProof) {
const newValidators = await extractNewValidators(api, blockHash);
logger.info(`IMP Sending setValidators tx with the account: ${txExecutor.address}`);
logger.info(`IMP Parameters :::`);
logger.info(`IMP newValidators:${newValidators}`);
logger.info(`IMP newValidatorSetId: ${newValidatorSetId}`);
logger.info(`IMP event proof::${JSON.stringify(eventProof)}`);
const currentValidators = await extractCurrentValidators(api, blockHash);
logger.info(`IMP currentValidators:${currentValidators}`);
const proof = {
eventId: eventProof.eventId,
validatorSetId: eventProof.validatorSetId,
r: eventProof.r,
s: eventProof.s,
v: eventProof.v,
validators: currentValidators
};
try {
const gasPrice = await provider.getGasPrice();
logger.info('gas price::', gasPrice.toString());
// Take 5 percent of current gas price
const percentGasPrice = gasPrice.mul(5).div(100);
logger.info('percentGasPrice:',percentGasPrice.toString());
const increasedGasPrice = gasPrice.add(percentGasPrice);
logger.info('Gas price nw;:', gasPrice.toString());

const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice});

logger.info(JSON.stringify(await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice})));
await updateLastEventProcessed(eventId, blockHash.toString());
const balance = await provider.getBalance(txExecutor.address);
logger.info(`IMP Balance is: ${balance}`);

logger.info(`IMP Gas price: ${gasPrice.toString()}`);
const gasRequired = gasEstimated.mul(gasPrice);
logger.info(`IMP Gas required: ${gasRequired.toString()}`);
if (balance.lt(gasRequired.mul(2))) {
const message = ` 🚨 To keep the validator relayer running, topup the eth account ${txExecutor.address} on CENNZnets ${process.env.NETWORK} chain`;
await sendSlackNotification(message);
}
} catch (e) {
logger.warn('Something went wrong:');
logger.error(`IMP Error: ${e.stack}`);
// send slack notification when proof submission fails
const message = ` 🚨 Issue while submitting validator set on ethereum bridge
proof: ${JSON.stringify(proof)}
newValidators: ${newValidators}
newValidatorSetId: ${newValidatorSetId}
on CENNZnets ${process.env.NETWORK} chain`;
await sendSlackNotification(message);
}
} else {
if (!eventProof) {
logger.info(`IMP Could not retrieve event proof for event id ${eventId} from derived
query api.derive.ethBridge.eventProof at ${timeoutMs} timeout`);
// restart
process.exit(1);
}
const newValidators = await extractNewValidators(api, blockHash);
logger.info(`IMP Sending setValidators tx with the account: ${txExecutor.address}`);
logger.info(`IMP Parameters :::`);
logger.info(`IMP newValidators:${newValidators}`);
logger.info(`IMP newValidatorSetId: ${newValidatorSetId}`);
logger.info(`IMP event proof::${JSON.stringify(eventProof)}`);
const currentValidators = await extractCurrentValidators(api, blockHash);
logger.info(`IMP currentValidators:${currentValidators}`);
const proof = {
eventId: eventProof.eventId,
validatorSetId: eventProof.validatorSetId,
r: eventProof.r,
s: eventProof.s,
v: eventProof.v,
validators: currentValidators
};
try {
const gasPrice = await provider.getGasPrice();
logger.info('gas price::', gasPrice.toString());
// Take 5 percent of current gas price
const percentGasPrice = gasPrice.mul(5).div(100);
logger.info('percentGasPrice:',percentGasPrice.toString());
const increasedGasPrice = gasPrice.add(percentGasPrice);
logger.info('Gas price nw;:', gasPrice.toString());

const gasEstimated = await bridge.estimateGas.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: 5000000, gasPrice: increasedGasPrice});

logger.info(JSON.stringify(await bridge.setValidators(newValidators, newValidatorSetId, proof, {gasLimit: gasEstimated.add(BUFFER), gasPrice: increasedGasPrice})));
await updateLastEventProcessed(eventId, blockHash.toString());
const balance = await provider.getBalance(txExecutor.address);
logger.info(`IMP Balance is: ${balance}`);

logger.info(`IMP Gas price: ${gasPrice.toString()}`);
const gasRequired = gasEstimated.mul(gasPrice);
logger.info(`IMP Gas required: ${gasRequired.toString()}`);
if (balance.lt(gasRequired.mul(2))) {
const message = ` 🚨 To keep the validator relayer running, topup the eth account ${txExecutor.address} on CENNZnets ${process.env.NETWORK} chain`;
await sendSlackNotification(message);
}
} catch (e) {
logger.warn('Something went wrong:');
logger.error(`IMP Error: ${e.stack}`);
// send slack notification when proof submission fails
const message = ` 🚨 Issue while submitting validator set on ethereum bridge
proof: ${JSON.stringify(proof)}
newValidators: ${newValidators}
newValidatorSetId: ${newValidatorSetId}
on CENNZnets ${process.env.NETWORK} chain`;
await sendSlackNotification(message);
}
}

async function main (networkName, bridgeContractAddress) {
Expand Down

0 comments on commit c077a67

Please sign in to comment.