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 1, 2024
1 parent 9949f97 commit 44aeb3d
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 228 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
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
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
181 changes: 0 additions & 181 deletions nightfall-deployer/truffle-config.js

This file was deleted.

13 changes: 11 additions & 2 deletions nightfall-optimist/src/services/block-assembler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {
increaseProposerBlockNotSent,
} from './debug-counters.mjs';

const { MAX_BLOCK_SIZE, MINIMUM_TRANSACTION_SLOTS, PROPOSER_MAX_BLOCK_PERIOD_MILIS } = config;
const {
MAX_BLOCK_SIZE,
MINIMUM_TRANSACTION_SLOTS,
PROPOSER_MAX_BLOCK_PERIOD_MILIS,
MEMPOOL_TXS_FETCH_LIMIT,
} = config;
const { STATE_CONTRACT_NAME } = constants;

let ws;
Expand Down Expand Up @@ -117,8 +122,12 @@ export async function conditionalMakeBlock(proposer) {
const currentTime = new Date().getTime();

if (enableHeartBeatLogging) {
let hbMsg = 'In the mempool there are the following number of transactions';
if (MEMPOOL_TXS_FETCH_LIMIT) {
hbMsg = 'In this throttled mempool batch there are the following number of transactions';
}
logger.info({
msg: 'In the mempool there are the following number of transactions',
msg: hbMsg,
numberTransactions: mempoolTransactions.length,
totalBytes,
});
Expand Down
Loading

0 comments on commit 44aeb3d

Please sign in to comment.