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

Add support of incremental quicksync #1794

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
167 changes: 146 additions & 21 deletions desktop/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,45 @@ class NodeManager extends AbstractManager {
isLocalStateFarBehind(qsStatus, 100)
) {
await this.runQuicksyncPrompt(qsStatus);
} else if (
qsStatus.incremental &&
qsStatus.db >= qsStatus.incremental.from &&
qsStatus.incremental.to >= qsStatus.current - 10
) {
hideGenericModal(this.mainWindow.webContents);
const isForce = await showGenericPrompt(this.mainWindow.webContents, {
title: 'Quick sync is not needed',
message: [
`Incremental quicksync available from layer ${qsStatus.incremental.from} to ${qsStatus.incremental.to}`,
`Latest layer in your database: ${qsStatus.db}`,
`Current layer in the network: ${qsStatus.current}`,
'',
'Your node is nearly synced. We recommend you to keep using your local state.',
'Do you want to incrementally update to the trusted state anyway?',
].join('\n'),
confirmTitle: 'Update incrementally!',
cancelTitle: 'No, thanks!',
cancelTimeout: 60,
});
isForce && (await this.runQuicksyncDownload());
} else {
hideGenericModal(this.mainWindow.webContents);
const isForce = await showGenericPrompt(this.mainWindow.webContents, {
title: 'Quick sync is not needed',
message: [
'Incremental update is not available.',
`Latest layer in your database: ${qsStatus.db}`,
`Latest layer in the trusted state: ${qsStatus.available}`,
`Current layer in the network: ${qsStatus.current}`,
'',
'Your node is nearly synced. We recommend you to keep using your local state.',
'Do you want to download the trusted state anyway?',
].join('\n'),
confirmTitle: 'Download anyway!',
confirmTitle: 'Download entire state!',
cancelTitle: 'No, thanks!',
cancelTimeout: 60,
});
isForce && (await this.runQuicksyncDownload());
isForce && (await this.runQuicksyncDownload(isForce));
}
} catch (err) {
logger.error('runQuicksyncByButton', err);
Expand Down Expand Up @@ -484,9 +506,19 @@ class NodeManager extends AbstractManager {
try {
hideGenericModal(this.mainWindow.webContents);

const isIncrementalAvailable =
qsStatus.incremental !== null &&
qsStatus.db >= qsStatus.incremental.from &&
qsStatus.db < qsStatus.incremental.to;

const prompt = await showGenericPrompt(this.mainWindow.webContents, {
title: 'Run a Quicksync?',
message: [
`Incremental update is ${
isIncrementalAvailable
? `available from layer ${qsStatus.incremental?.from} to ${qsStatus.incremental?.to}`
: 'not available'
}`,
`Latest layer in your database: ${qsStatus.db}`,
`Latest layer in the trusted state: ${qsStatus.available}`,
`Current layer in the network: ${qsStatus.current}`,
Expand All @@ -497,7 +529,7 @@ class NodeManager extends AbstractManager {
'',
'You can run the Quicksync from the settings page later.',
].join('\n'),
confirmTitle: 'Yes, download it!',
confirmTitle: 'Yes, run quicksync!',
cancelTitle: 'Sync as usual',
cancelTimeout: 60,
});
Expand All @@ -517,24 +549,33 @@ class NodeManager extends AbstractManager {
const resume = await showGenericPrompt(this.mainWindow.webContents, {
title: 'Resume interrupted quicksync?',
message: [
'You have an incomplete download:',
'You have an incomplete download of the entire state:',
`Layer: ${qsStatus.paused.layer}`,
`Progress: ${downloaded} MB / ${total} MB (${percent}%)`,
'',
'Do you want to resume downloading it?',
`Or download from scratch for layer: ${qsStatus.available}?`,
isIncrementalAvailable
? 'Or delete downloaded data and run incremental quicksync?'
: `Or download from scratch for layer: ${qsStatus.available}?`,
].join('\n'),
confirmTitle: 'Yes, resume',
cancelTitle: 'Download from scratch',
cancelTitle: isIncrementalAvailable
? 'Run incremental quicksync'
: 'Download from scratch',
});
if (!resume) {
const { urlFile, downloadFile } = this.getQuicksyncFiles(
this.getNodeDataPath()
);
await fse.remove(urlFile);
await fse.remove(downloadFile);
if (resume) {
// Continue downloading
await this.runQuicksyncDownload(true);
return;
}
// Otherwise clean up downloaded files
const { urlFile, downloadFile } = this.getQuicksyncFiles(
this.getNodeDataPath()
);
await fse.remove(urlFile);
await fse.remove(downloadFile);
}
// And run quicksync as usual
await this.runQuicksyncDownload();
}
} catch (err) {
Expand Down Expand Up @@ -778,6 +819,9 @@ class NodeManager extends AbstractManager {
};
};

private getNodeStateFile = (nodeData: string) =>
path.resolve(nodeData, 'state.sql');

private checkForPausedQuicksync = async (
nodeData: string
): Promise<PausedQuicksyncStatus | null> => {
Expand Down Expand Up @@ -821,10 +865,74 @@ class NodeManager extends AbstractManager {
}
};

private checkForIncrementalQuicksync = async (nodeDataDir: string) => {
const bin = getQuicksyncPath();
const stateFile = path.resolve(nodeDataDir, 'state.sql');

return new Promise<QuicksyncStatus['incremental']>((resolve, reject) => {
const args = ['incremental-check', '--state-sql', stateFile];
const process = spawn(bin, args);
logger.log('runQuicksync:check', `${bin} ${args.join(' ')}`);

let stdout = '';
let stderr = '';
process.stdout.on('data', (data) => {
stdout += data;
});
process.stderr.on('data', (data) => {
stderr += data;
});

process.on('error', (err) => {
reject(
new Error(
`Cannot run Quicksync tool to check the database: ${err.message}`
)
);
});

process.on('close', async (code) => {
if (code !== 0) {
// TODO: Better error for known codes
return reject(
new Error(
`Quicksync tool failed with exit code ${code}:\n${stderr}`
)
);
}

const stats = stdout
.split('\n')
.filter((l) => l.startsWith('Possible to restore'));

if (stats.length !== 2) {
return reject(
new Error(
`Unknown result of running Quicksync tool:\n${stdout}\n${stderr}`
)
);
}

const result: QuicksyncStatus['incremental'] = {
from: parseInt(stats[0].match(/(\d+)/)?.[0] || '0', 10),
to: parseInt(stats[1].match(/(\d+)/)?.[0] || '0', 10),
};

logger.log('runQuicksyncIncrementalCheck', result);
return resolve(result);
});
}).catch(() => {
return null;
});
};

private quicksyncStatus: QuicksyncStatus | null = null;

private runQuicksyncCheck = async () => {
const bin = getQuicksyncPath();
const nodeDataDir = this.getNodeDataPath();
const paused = await this.checkForPausedQuicksync(nodeDataDir);
const incremental = await this.checkForIncrementalQuicksync(nodeDataDir);
return new Promise<QuicksyncStatus>((resolve, reject) => {
const args = [
'check',
Expand Down Expand Up @@ -886,16 +994,20 @@ class NodeManager extends AbstractManager {
current,
available,
paused,
incremental,
};

this.quicksyncStatus = result;
this.sendToMainWindow(ipcConsts.UPDATE_QUICKSYNC_STATUS, result);
logger.log('runQuicksyncCheck', result);
return resolve(result);
});
});
};

private runQuicksyncDownload = async () => {
private runQuicksyncDownload = async (
forceEntire = false
): Promise<boolean> => {
if (this.nodeProcess) {
showGenericModal(this.mainWindow.webContents, {
title: 'Quicksyncing...',
Expand All @@ -912,17 +1024,30 @@ class NodeManager extends AbstractManager {
}

const bin = getQuicksyncPath();
const args = [
'download',
'--node-data',
this.getNodeDataPath(),
'--go-spacemesh-path',
getNodePath(),
];

const nodeDataPath = this.getNodeDataPath();
const stateFile = this.getNodeStateFile(nodeDataPath);

const isIncrementalAvailable =
this.quicksyncStatus?.incremental &&
this.quicksyncStatus.db >= this.quicksyncStatus.incremental.from &&
this.quicksyncStatus.db < this.quicksyncStatus.incremental.to;

const shouldQuicksyncIncrementally = !forceEntire && isIncrementalAvailable;

const args = shouldQuicksyncIncrementally
? ['incremental', '--state-sql', stateFile]
: [
'download',
'--node-data',
nodeDataPath,
'--go-spacemesh-path',
getNodePath(),
];

logger.log('runQuicksync:download', `${bin} ${args.join(' ')}`);

return new Promise((resolve, reject) => {
return new Promise<boolean>((resolve, reject) => {
if (!isFileExists(bin)) {
reject(new Error(`Quicksync tool is not found: ${bin}`));
return;
Expand Down
2 changes: 1 addition & 1 deletion node/use-version-quicksync
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.16
v0.2.0-alpha.3
4 changes: 4 additions & 0 deletions shared/types/quicksync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export interface QuicksyncStatus {
current: number;
available: number;
paused: PausedQuicksyncStatus | null;
incremental: {
from: number;
to: number;
} | null;
}
Loading