Skip to content

Commit

Permalink
feat: add support for forcing sync from seeds (#3228)
Browse files Browse the repository at this point in the history
## Description
Add support for forcing sync to a seed node. Specify index for the node from peer_seeds list.

## How Has This Been Tested?
Manually.

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
* [x] I'm merging against the `development` branch.
* [x] I have squashed my commits into a single commit.
  • Loading branch information
Cifko committed Aug 26, 2021
1 parent 098f25d commit d132932
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
3 changes: 3 additions & 0 deletions applications/daily_tests/.prettierrc
@@ -0,0 +1,3 @@
{
"endOfLine": "auto"
}
73 changes: 51 additions & 22 deletions applications/daily_tests/automatic_sync_test.js
@@ -1,4 +1,5 @@
const fs = require("fs/promises");
const yargs = require("yargs");
const path = require("path");
const helpers = require("./helpers");
const BaseNodeProcess = require("integration_tests/helpers/baseNodeProcess");
Expand All @@ -9,37 +10,64 @@ const SyncType = {
};

async function main() {
let syncType;
if (process.argv.includes("pruned")) {
syncType = SyncType.Pruned;
} else {
syncType = SyncType.Archival;
}

const { blockHeight, timeDiffMinutes, blockRate } = await run({
log: "./logs/base-node.log",
syncType,
});
const argv = yargs
.option("sync-type", {
type: "string",
description: "Specify the sync type.",
default: SyncType.Archival,
choices: [SyncType.Archival, SyncType.Pruned],
})
.option("force-sync-peer", {
type: "number",
description: "Enable force sync to peer_seeds i-th node.",
})
.option("log", {
alias: "l",
description: "output logs to this file",
type: "string",
default: "./logs/base-node.log",
})
.help()
.alias("help", "h").argv;
try {
const { blockHeight, timeDiffMinutes, blockRate, forcedSyncPeer } =
await run(argv);

console.log(
`${syncType} sync to block height ${blockHeight} took ${timeDiffMinutes} minutes for a rate of ${blockRate} blocks/min`
);
console.log(
`${argv.syncType} sync ${
forcedSyncPeer ? "forced to " + forcedSyncPeer : ""
} to block height ${blockHeight} took ${timeDiffMinutes} minutes for a rate of ${blockRate} blocks/min`
);
} catch (err) {
console.log(err);
}
}

async function run(options) {
let forcedSyncPeer;
const baseNode = new BaseNodeProcess("compile", true);
await baseNode.init();

// Set pruning horizon in config file if `pruned` command line arg is present
if (options.syncType === SyncType.Pruned) {
let config = await fs.readFile(baseNode.baseDir + "/config/config.toml");
let updated_config = config
.toString()
.replace("#pruning_horizon = 0", "pruning_horizon = 1000");
await fs.writeFile(
baseNode.baseDir + "/config/config.toml",
updated_config
);
process.env.TARI_BASE_NODE__WEATHERWAX__PRUNING_HORIZON = 1000;
}

// Check if we have a forced peer index.
if (options.forceSyncPeer !== undefined) {
const config = (
await fs.readFile(baseNode.baseDir + "/config/config.toml")
).toString();
const peer = Array.from(
config.match(/\npeer_seeds = \[(.*?)\]/s)[1].matchAll(/\n[^#]*"(.*?)"/g),
(m) => m[1]
)[options.forceSyncPeer];
if (peer === undefined) {
// Check if index is within bounds of peer_seeds from config.
throw "Forced index out of bounds";
}
process.env.TARI_BASE_NODE__WEATHERWAX__FORCE_SYNC_PEERS = [peer];
forcedSyncPeer = peer;
}

await baseNode.start();
Expand Down Expand Up @@ -82,6 +110,7 @@ async function run(options) {
blockRate: blockRate.toFixed(2),
timeDiffMinutes: timeDiffMinutes.toFixed(2),
blockHeight: syncResult.height,
forcedSyncPeer,
};
} catch (err) {
await baseNode.stop();
Expand Down
12 changes: 7 additions & 5 deletions common/src/configuration/global.rs
Expand Up @@ -401,11 +401,13 @@ fn convert_node_config(

// block sync
let key = config_string("base_node", &net_str, "force_sync_peers");
let force_sync_peers = optional(
cfg.get_array(&key)
.map(|values| values.into_iter().map(|v| v.into_str().unwrap()).collect()),
)?
.unwrap_or_default();
let force_sync_peers = match cfg.get_array(&key) {
Ok(peers) => peers.into_iter().map(|v| v.into_str().unwrap()).collect(),
Err(..) => match cfg.get_str(&key) {
Ok(s) => s.split(',').map(|v| v.to_string()).collect(),
Err(..) => vec![],
},
};

// Liveness auto ping interval
let key = config_string("base_node", &net_str, "auto_ping_interval");
Expand Down

0 comments on commit d132932

Please sign in to comment.