Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

David/staging2 #570

Merged
merged 8 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli/lib/nf3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,17 @@ class Nf3 {
const newGasBlockEmitter = new EventEmitter();
const connection = new WebSocket(this.optimistWsUrl);
this.websockets.push(connection); // save so we can close it properly later
// Ping function to keep WS open. Send beat every 15 seconds
const ping = async () => {
if (!connection) return;
if (connection.readyState !== WebSocket.OPEN) return;
connection.ping();
setTimeout(ping, 15000);
};
connection.onopen = () => {
logger.debug('websocket connection opened');
connection.send('blocks');
ping();
};
connection.onmessage = async message => {
const msg = JSON.parse(message.data);
Expand Down
45 changes: 36 additions & 9 deletions nightfall-optimist/src/event-handlers/subscribe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,55 @@ export async function startEventQueue(callback, ...arg) {
}

export async function subscribeToChallengeWebSocketConnection(callback, ...args) {
wss.on('connection', ws =>
wss.on('connection', ws => {
ws.on('message', message => {
if (message === 'challenge') callback(ws, args);
}),
);
});
ws.on('error', () => {
logger.debug('ERROR challenge WS');
});
ws.on('open', () => {
logger.debug('OPEN challenge WS');
});
ws.on('close', err => {
logger.debug(`CLOSE challenge WS: ${err}`);
});
});
logger.debug('Subscribed to Challenge WebSocket connection');
}

export async function subscribeToBlockAssembledWebSocketConnection(callback, ...args) {
wss.on('connection', ws =>
wss.on('connection', ws => {
ws.on('message', message => {
if (message === 'blocks') callback(ws, args);
}),
);
});
ws.on('error', () => {
logger.debug('ERROR block-assembly WS');
});
ws.on('open', () => {
logger.debug('OPEN block-assembly WS');
});
ws.on('close', msg => {
logger.debug(`CLOSE block-assembly ${msg}`);
});
});
logger.debug('Subscribed to BlockAssembled WebSocket connection');
}

export async function subscribeToInstantWithDrawalWebSocketConnection(callback, ...args) {
wss.on('connection', ws =>
wss.on('connection', ws => {
ws.on('message', message => {
if (message === 'instant') callback(ws, args);
}),
);
});
ws.on('error', () => {
logger.debug('ERROR instant-withdraw');
});
ws.on('open', () => {
logger.debug('OPEN instant-withdraw');
});
ws.on('close', err => {
logger.debug(`CLOSE instant-withdraw ${err}`);
});
});
logger.debug('Subscribed to InstantWithDrawal WebSocket connection');
}
7 changes: 5 additions & 2 deletions nightfall-optimist/src/services/block-assembler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export async function conditionalMakeBlock(proposer) {
transactions.map(t => Transaction.buildSolidityStruct(t)),
)
.encodeABI();
if (ws)
// TODO - check ws readyState is OPEN => CLOSED .WebSocket.OPEN(1), CONNECTING(0), CLOSING(2), CLOSED(3)
// before sending Poposed block. If not Open, try to open it
if (ws) {
await ws.send(
JSON.stringify({
type: 'block',
Expand All @@ -67,7 +69,8 @@ export async function conditionalMakeBlock(proposer) {
transactions,
}),
);
logger.debug('Send unsigned block-assembler transaction to ws client');
logger.debug('Send unsigned block-assembler transaction to ws client');
}
// remove the transactiosn from the mempool so we don't keep making new
// blocks with them
await removeTransactionsFromMemPool(block.transactionHashes);
Expand Down