Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 61 additions & 0 deletions fluxd_chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Which chain the node's fluxd is on, and whether the explorer sync check
* applies to it.
*
* The sync check compares fluxd's height with the Flux explorers, which serve
* mainnet only. On any other chain (testnet, labnet, regtest) the comparison
* always reads "not synced" and the fix restarts a healthy fluxd, so the
* check runs on mainnet alone.
*/

/**
* The chain named by `getblockchaininfo`'s output.
* @param {{stdout?: string}} [result] - The command's result.
* @returns {string|null} The chain, or null when the output names none
* (fluxd not answering, still loading, or unreadable output).
*/
function fluxdChain(result) {
try {
const { chain } = JSON.parse(result.stdout);

return typeof chain === 'string' && chain !== '' ? chain : null;
} catch {
return null;
}
}

/**
* Whether the explorer sync check applies to a node on `chain`.
* @param {string|null|undefined} chain
* @returns {boolean} True on mainnet only; an unknown chain is not mainnet.
*/
function syncCheckApplies(chain) {
return chain === 'main';
}

/**
* One cycle's sync check. The check runs on a random half of cycles; on those,
* fluxd's chain is read and the check runs on mainnet only. The chain is read
* every time, so a fluxd restarted on another chain is seen at its next check.
* @param {object} deps
* @param {() => number} deps.roll - 1 to 4; the check runs above 2.
* @param {() => Promise<{stdout?: string}>} deps.readChainInfo - Runs `getblockchaininfo`.
* @param {() => Promise<void>} deps.checkSync - The explorer sync check.
* @param {(line: string) => void} deps.log
*/
async function runSyncCheck({ roll, readChainInfo, checkSync, log }) {
const skip_sync = roll();
if (skip_sync <= 2) {
log(`Sync check skipped: ${skip_sync} <= 2`);

return;
}
const chain = fluxdChain(await readChainInfo());
if (syncCheckApplies(chain)) {
await checkSync();
} else if (chain !== null) {
log(`Sync check off: the explorers it compares with serve mainnet, and fluxd is on ${chain}`);
}
}

module.exports = { fluxdChain, syncCheckApplies, runSyncCheck };
92 changes: 92 additions & 0 deletions test/fluxd_chain.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const test = require('node:test');
const assert = require('node:assert');

const { fluxdChain, syncCheckApplies, runSyncCheck } = require('../fluxd_chain');

// `flux-cli getblockchaininfo` output, trimmed to the fields around `chain`.
const info = (chain) => ({ stdout: JSON.stringify({ chain, blocks: 9569, headers: 9569 }) });

test('the chain is read from getblockchaininfo', () => {
for (const chain of ['main', 'test', 'labnet', 'regtest']) {
assert.strictEqual(fluxdChain(info(chain)), chain);
}
});

test('an answer without a chain is unknown, never mainnet', () => {
for (const result of [
{ stdout: '' },
{ stdout: "error: couldn't connect to server: unknown (code -1)" },
{ stdout: '{"code":-28,"message":"Loading block index..."}' },
{ stdout: JSON.stringify({ chain: '' }) },
{ stdout: JSON.stringify({ chain: 1 }) },
{ stdout: 'null' },
{},
undefined,
]) {
assert.strictEqual(fluxdChain(result), null, JSON.stringify(result));
}
});

test('the explorer sync check runs on mainnet only', () => {
assert.strictEqual(syncCheckApplies('main'), true);
for (const chain of ['test', 'labnet', 'regtest', null, undefined]) {
assert.strictEqual(syncCheckApplies(chain), false, String(chain));
}
});

// One cycle of runSyncCheck with a fixed roll and fluxd answering `answer`;
// returns what the cycle did.
async function cycle(rollValue, answer) {
const done = { asked: 0, checked: 0, logs: [] };
await runSyncCheck({
roll: () => rollValue,
readChainInfo: async () => {
done.asked += 1;
return answer;
},
checkSync: async () => {
done.checked += 1;
},
log: (line) => done.logs.push(line),
});
return done;
}

test('a cycle that rolls a skip does not ask fluxd or run the check', async () => {
for (const rollValue of [1, 2]) {
const done = await cycle(rollValue, info('main'));
assert.strictEqual(done.asked, 0);
assert.strictEqual(done.checked, 0);
assert.deepStrictEqual(done.logs, [`Sync check skipped: ${rollValue} <= 2`]);
}
});

test('a cycle that rolls the check runs it on mainnet', async () => {
for (const rollValue of [3, 4]) {
const done = await cycle(rollValue, info('main'));
assert.strictEqual(done.asked, 1);
assert.strictEqual(done.checked, 1);
assert.deepStrictEqual(done.logs, []);
}
});

test('on another chain the check does not run and the log says why', async () => {
const done = await cycle(3, info('labnet'));
assert.strictEqual(done.checked, 0);
assert.deepStrictEqual(done.logs, [
'Sync check off: the explorers it compares with serve mainnet, and fluxd is on labnet',
]);
});

test('when fluxd gives no chain the check does not run', async () => {
const done = await cycle(3, { stdout: "error: couldn't connect to server: unknown (code -1)" });
assert.strictEqual(done.asked, 1);
assert.strictEqual(done.checked, 0);
assert.deepStrictEqual(done.logs, []);
});

test('a fluxd restarted on another chain is followed at the next check', async () => {
assert.strictEqual((await cycle(3, info('test'))).checked, 0);
assert.strictEqual((await cycle(3, info('main'))).checked, 1);
assert.strictEqual((await cycle(3, info('test'))).checked, 0);
});
13 changes: 7 additions & 6 deletions watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { bandwidthReport } = require('./bandwidth_report');
const { compareVersions } = require('./versions');
const { discordPayload, sendDiscord, sendTelegram } = require('./alerts');
const { simulateInstallCommand, aptInstallPlan, updateServices } = require('./apt_plan');
const { runSyncCheck } = require('./fluxd_chain');

const execFilePromise = promisify(execFile);

Expand Down Expand Up @@ -2069,12 +2070,12 @@ async function flux_check() {
last_low_eps_benchmark_time = 0;
}
if (zelcash_height != '' && typeof zelcash_height !== 'undefined' && isNumber(zelcash_height)) {
const skip_sync = between(1, 4);
if (skip_sync > 2) {
await Check_Sync(zelcash_height, data_time_utc);
} else {
console.log('Sync check skipped: ' + skip_sync + ' <= 2');
}
await runSyncCheck({
roll: () => between(1, 4),
readChainInfo: () => runShellCommand(`${daemon_cli} getblockchaininfo`, { timeout: 60000 }),
checkSync: () => Check_Sync(zelcash_height, data_time_utc),
log: console.log,
});
}
console.log(
'============================================================[' +
Expand Down