Skip to content

Commit

Permalink
Merge pull request #570 from EYBlockchain/david/staging2
Browse files Browse the repository at this point in the history
David/staging2
  • Loading branch information
Westlad authored Mar 17, 2022
2 parents 2f43cb3 + ab26bd3 commit b8f549e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
36 changes: 36 additions & 0 deletions cli/lib/nf3.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,15 @@ class Nf3 {
const emitter = new EventEmitter();
const connection = new WebSocket(this.optimistWsUrl);
this.websockets.push(connection); // save so we can close it properly later
const ping = async () => {
if (!connection) return;
if (connection.readyState !== WebSocket.OPEN) return;
connection.ping();
setTimeout(ping, 15000);
};
connection.onopen = () => {
connection.send('instant');
ping();
};
connection.onmessage = async message => {
const msg = JSON.parse(message.data);
Expand Down Expand Up @@ -667,9 +674,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 Expand Up @@ -725,8 +740,15 @@ class Nf3 {
const newBlockEmitter = new EventEmitter();
const connection = new WebSocket(this.optimistWsUrl);
this.websockets.push(connection); // save so we can close it properly later
const ping = async () => {
if (!connection) return;
if (connection.readyState !== WebSocket.OPEN) return;
connection.ping();
setTimeout(ping, 15000);
};
connection.onopen = () => {
connection.send('blocks');
ping();
};
connection.onmessage = async message => {
const msg = JSON.parse(message.data);
Expand Down Expand Up @@ -771,8 +793,15 @@ class Nf3 {
async startChallenger() {
const connection = new WebSocket(this.optimistWsUrl);
this.websockets.push(connection); // save so we can close it properly later
const ping = async () => {
if (!connection) return;
if (connection.readyState !== WebSocket.OPEN) return;
connection.ping();
setTimeout(ping, 15000);
};
connection.onopen = () => {
connection.send('challenge');
ping();
};
connection.onmessage = async message => {
const msg = JSON.parse(message.data);
Expand All @@ -797,8 +826,15 @@ class Nf3 {
const newChallengeEmitter = new EventEmitter();
const connection = new WebSocket(this.optimistWsUrl);
this.websockets.push(connection); // save so we can close it properly later
const ping = async () => {
if (!connection) return;
if (connection.readyState !== WebSocket.OPEN) return;
connection.ping();
setTimeout(ping, 15000);
};
connection.onopen = () => {
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,29 +72,56 @@ 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');
}

Expand Down
8 changes: 5 additions & 3 deletions nightfall-optimist/src/services/block-assembler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ export async function conditionalMakeBlock(proposer) {
.encodeABI();
unsignedProposeBlockTransactionList.push(unsignedProposeBlockTransaction);
}

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 @@ -107,7 +108,8 @@ export async function conditionalMakeBlock(proposer) {
transactionsList,
}),
);
logger.debug('Send unsigned block-assembler transactions to ws client');
logger.debug('Send unsigned block-assembler transactions to ws client');
}
// remove the transactions from the mempool so we don't keep making new
// blocks with them
const transactionHashes = blockList.reduce(
Expand Down

0 comments on commit b8f549e

Please sign in to comment.