Skip to content

Commit

Permalink
fix: client syncing, optimist throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuNarayanan3 committed Jul 2, 2024
1 parent 9949f97 commit 1c5aaa3
Show file tree
Hide file tree
Showing 17 changed files with 3,268 additions and 3,064 deletions.
2 changes: 1 addition & 1 deletion bin/start-apps
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#! /bin/bash
docker-compose -f docker/docker-compose.apps.yml -p 'nightfall_3' up proposer
docker-compose -f docker/docker-compose.apps.yml -p 'nightfall_3' up
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ module.exports = {
},
TIMER_CHANGE_PROPOSER_SECOND: Number(process.env.TIMER_CHANGE_PROPOSER_SECOND) || 30,
CHECK_REGISTER_PROPOSER_SECOND: Number(process.env.CHECK_REGISTER_PROPOSER_SECOND) || 10,
ENABLE_CHECK_AND_CHANGE_PROPOSER: process.env.ENABLE_CHECK_AND_CHANGE_PROPOSER || true,
ENABLE_CHECK_AND_CHANGE_PROPOSER: process.env.ENABLE_CHECK_AND_CHANGE_PROPOSER || 'true',
MAX_ROTATE_TIMES: Number(process.env.MAX_ROTATE_TIMES) || 2,
WEBHOOK: {
CALL_WEBHOOK_ON_CONFIRMATION: process.env.CALL_WEBHOOK_ON_CONFIRMATION,
Expand All @@ -816,4 +816,5 @@ module.exports = {
WEBHOOK_PATH: process.env.WEBHOOK_PATH, // For posting optional layer 2 transaction finalization details
WEBHOOK_SIGNING_KEY: process.env.WEBHOOK_SIGNING_KEY,
},
MEMPOOL_TXS_FETCH_LIMIT: Number(process.env.MEMPOOL_TXS_FETCH_LIMIT),
};
2 changes: 1 addition & 1 deletion docker/docker-compose.apps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ services:
ETH_PRIVATE_KEY: ${ETH_PRIVATE_KEY}
PROPOSER_KEY: ${PROPOSER_KEY}
CHALLENGER_KEY: ${CHALLENGER_KEY}
ENABLE_CHECK_AND_CHANGE_PROPOSER: true
# ENABLE_CHECK_AND_CHANGE_PROPOSER: 'false'
# CHECK_REGISTER_PROPOSER_SECOND: 3600
networks:
- nightfall_network
Expand Down
18 changes: 10 additions & 8 deletions docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ services:
ports:
- 9239:9229
command: ['npm', 'run', 'dev']
environment:
- CALL_WEBHOOK_ON_CONFIRMATION=true
- WEBHOOK_PATH=http://host.docker.internal:3099/zk-tx-response
- WEBHOOK_SIGNING_KEY=0x720117fdc01793b32217d32622916babc66350c4bb60905336ec8b29b1f16757
# environment:
# - CALL_WEBHOOK_ON_CONFIRMATION=true
# - WEBHOOK_PATH=http://host.docker.internal:3099/zk-tx-response
# - WEBHOOK_SIGNING_KEY=0x720117fdc01793b32217d32622916babc66350c4bb60905336ec8b29b1f16757

deployer:
build:
Expand All @@ -63,11 +63,11 @@ services:
source: ../config/default.js
target: /app/config/default.js
- type: bind
source: ../nightfall-deployer/migrations
target: /app/migrations
source: ../nightfall-deployer/scripts
target: /app/scripts
- type: bind
source: ../nightfall-deployer/truffle-config.js
target: /app/truffle-config.js
source: ../nightfall-deployer/hardhat.config.js
target: /app/hardhat.config.js
- type: bind
source: ../nightfall-deployer/entrypoint.sh
target: /app/entrypoint.sh
Expand All @@ -91,6 +91,8 @@ services:
- type: bind
source: ../config/default.js
target: /app/config/default.js
# environment:
# MEMPOOL_TXS_FETCH_LIMIT: 5
ports:
- 9229:9229
command: ['npm', 'run', 'dev']
Expand Down
7 changes: 4 additions & 3 deletions nightfall-client/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import {

const app = express();

// Add check for syncing state. If it is in syncing state, just respond 400
// Add check for syncing state. If it is in syncing state, just respond 503
app.use((req, res, next) => {
if (req.path === '/healthcheck') return next();

if (req.app.get('isSyncing')) {
res.sendStatus(400);
return res.status;
return res.status(503).send('Nightfall3 Client is syncing');
}
return next();
});
Expand Down
9 changes: 4 additions & 5 deletions nightfall-client/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from 'config';
import logger from 'common-files/utils/logger.mjs';
import mongo from 'common-files/utils/mongo.mjs';
import { queueManager, pauseQueue, unpauseQueue } from 'common-files/utils/event-queue.mjs';
import { queueManager, pauseQueue } from 'common-files/utils/event-queue.mjs';
import { checkContractsABI } from 'common-files/utils/sync-files.mjs';
import app from './app.mjs';
import rabbitmq from './utils/rabbitmq.mjs';
Expand All @@ -27,10 +27,9 @@ const main = async () => {
await checkContractsABI();
await startEventQueue(queueManager, eventHandlers);
await pauseQueue(0);
initialClientSync().then(() => {
app.set('isSyncing', false);
unpauseQueue(0);
});
await initialClientSync();
app.set('isSyncing', false);
logger.info('Syncing complete, queues unpaused');
} catch (err) {
logger.error(err);
process.exit(1);
Expand Down
22 changes: 10 additions & 12 deletions nightfall-client/src/services/state-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ const genGetCommitments = async (query = {}, proj = {}) => {

// eslint-disable-next-line import/prefer-default-export
export const initialClientSync = async () => {
const allCommitments = await genGetCommitments();
const commitmentBlockNumbers = allCommitments.map(a => a.blockNumber).filter(n => n >= 0);

logger.info(`commitmentBlockNumbers: ${commitmentBlockNumbers}`);

const firstSeenBlockNumber = Math.min(...commitmentBlockNumbers);

logger.info(`firstSeenBlockNumber: ${firstSeenBlockNumber}`);

// fistSeenBlockNumber can be infinity if the commitmentBlockNumbers array is empty
if (firstSeenBlockNumber === Infinity) {
const allCommitments = await genGetCommitments({ isOnChain: { $ne: '-1' } });
if (allCommitments?.length < 1) {
logger.info(`No existing commitments to sync, starting from L1 block ${STATE_GENESIS_BLOCK}`);
await syncState(STATE_GENESIS_BLOCK);
} else {
await syncState(firstSeenBlockNumber);
const commitmentBlockNumbers = allCommitments.map(a => a.blockNumber).filter(n => n >= 0);
logger.info(`commitmentBlockNumbers: ${commitmentBlockNumbers}`);

const lastProcessedBlockNumber = Math.max(...commitmentBlockNumbers);
logger.info(`lastProcessedBlockNumber: ${lastProcessedBlockNumber}`);

await syncState(lastProcessedBlockNumber);
}

unpauseQueue(0); // the queues are paused to start with, so get them going once we are synced
Expand Down
25 changes: 12 additions & 13 deletions nightfall-deployer/contracts/Proposers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import './Config.sol';
import './Utils.sol';
import './Stateful.sol';
import './Certified.sol';
import "hardhat/console.sol";

contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
function initialize() public override(Stateful, Config, Certified) initializer {
Expand All @@ -22,11 +21,12 @@ contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
/**
@dev register proposer with stake
*/
function registerProposer(
string calldata url,
uint256 fee
) external payable nonReentrant onlyCertified {
console.log("registering proposer ", url);
function registerProposer(string calldata url, uint256 fee)
external
payable
nonReentrant
onlyCertified
{
require(
state.numProposers() < maxProposers,
'Proposers: Max number of registered proposers'
Expand All @@ -43,7 +43,6 @@ contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
(bool success, ) = payable(address(state)).call{value: msg.value}('');
require(success, 'Proposers: Transfer failed.');
state.setStakeAccount(msg.sender, stake.amount, stake.challengeLocked);
console.log("Sent stake to state contract");

LinkedAddress memory currentProposer = state.getCurrentProposer();
// cope with this being the first proposer
Expand All @@ -53,7 +52,6 @@ contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
state.setProposerStartBlock(block.number);
emit NewCurrentProposer(currentProposer.thisAddress);
} else {
console.log("This is a new proposer");
// only if it's not a proposer yet
if (state.getProposer(msg.sender).thisAddress == address(0)) {
// else, splice the new proposer into the circular linked list of proposers just behind the current proposer
Expand Down Expand Up @@ -88,7 +86,6 @@ contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
state.setProposer(msg.sender, proposer);
}
}
console.log("Current proposer: ", currentProposer.thisAddress);
state.setCurrentProposer(currentProposer.thisAddress);
state.setNumProposers(state.numProposers() + 1);
}
Expand Down Expand Up @@ -123,10 +120,12 @@ contract Proposers is Stateful, Config, ReentrancyGuardUpgradeable, Certified {
}

// Proposers can change REST API URL or increment stake
function updateProposer(
string calldata url,
uint256 fee
) external payable nonReentrant onlyCertified {
function updateProposer(string calldata url, uint256 fee)
external
payable
nonReentrant
onlyCertified
{
require(
state.getProposer(msg.sender).thisAddress != address(0),
'Proposers: This proposer is not registered or you are not that proposer'
Expand Down
Loading

0 comments on commit 1c5aaa3

Please sign in to comment.