Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e2441be
update
JeffreyDallas Jul 27, 2026
52a3680
save
JeffreyDallas Jul 27, 2026
ad84f73
save
JeffreyDallas Jul 27, 2026
9bf5cce
save
JeffreyDallas Jul 27, 2026
8af5972
save
JeffreyDallas Jul 28, 2026
96dd7d2
save
JeffreyDallas Jul 30, 2026
16cead5
save
JeffreyDallas Jul 30, 2026
e29cf3c
save
JeffreyDallas Jul 30, 2026
404fe9b
save
JeffreyDallas Jul 30, 2026
fd755ec
Merge commit '57fac1ca5db633cf63cdf5f191a135489c40dbdc' into 02379-D-…
JeffreyDallas Jul 30, 2026
fb9b87d
simplify
JeffreyDallas Jul 30, 2026
21b6ddb
save
JeffreyDallas Jul 30, 2026
1265a4a
format
JeffreyDallas Jul 31, 2026
ec0a27f
format
JeffreyDallas Jul 31, 2026
cf0d832
extend test time
JeffreyDallas Jul 31, 2026
9728385
same
JeffreyDallas Jul 31, 2026
1f94a04
merge
JeffreyDallas Aug 8, 2026
b36ed41
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 11, 2026
b534f83
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 12, 2026
5d5b02a
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 13, 2026
f9da5b5
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 15, 2026
6465468
fix
JeffreyDallas Aug 15, 2026
4f8b0b2
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 17, 2026
785626e
fix: prevent restored consensus nodes from replaying into a stale freeze
JeffreyDallas Aug 18, 2026
52a9b6b
fix: scope preconsensus-event trimming to callers that resume live
JeffreyDallas Aug 18, 2026
d8fcb4d
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 18, 2026
0a2a4f7
format
JeffreyDallas Aug 18, 2026
aaabc8f
address PR review feedback: tests, perf, and cleanup
JeffreyDallas Aug 24, 2026
3481e9d
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 28, 2026
2c30618
Merge branch 'main' into 02379-D-start-saved-state
JeffreyDallas Aug 28, 2026
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
212 changes: 109 additions & 103 deletions examples/state-save-and-restore/README.md

Large diffs are not rendered by default.

330 changes: 194 additions & 136 deletions examples/state-save-and-restore/Taskfile.yml
Comment thread
JeffreyDallas marked this conversation as resolved.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0

import fs from 'node:fs';

class OverrideNetworkGenerator {
static main() {
const argumentsMap = this.parseArguments(process.argv.slice(2));
const sourceNetworkPath = this.requireArgument(argumentsMap, 'source-network');
const servicesJsonPath = this.requireArgument(argumentsMap, 'services-json');
const nodeAliasesText = this.requireArgument(argumentsMap, 'node-aliases');
const outputPath = this.requireArgument(argumentsMap, 'output');
const sourceNetwork = JSON.parse(fs.readFileSync(sourceNetworkPath, 'utf8'));
const servicesDocument = JSON.parse(fs.readFileSync(servicesJsonPath, 'utf8'));
const nodeAliases = nodeAliasesText
.split(',')
.map(nodeAlias => nodeAlias.trim())
.filter(nodeAlias => nodeAlias.length > 0);

if (!Array.isArray(sourceNetwork.nodeMetadata)) {
throw new Error('source network JSON is missing nodeMetadata');
}

if (!Array.isArray(servicesDocument.items)) {
throw new Error('services JSON is missing items');
}

if (sourceNetwork.nodeMetadata.length < nodeAliases.length) {
throw new Error(
`source network only has ${String(sourceNetwork.nodeMetadata.length)} nodeMetadata entries for ${String(nodeAliases.length)} aliases`,
);
}

const rewrittenNetwork = structuredClone(sourceNetwork);
let changedEndpointCount = 0;

for (const [nodeIndex, nodeAlias] of nodeAliases.entries()) {
const serviceName = `network-${nodeAlias}-svc`;
Comment thread
JeffreyDallas marked this conversation as resolved.
const service = servicesDocument.items.find(candidateService => candidateService?.metadata?.name === serviceName);

if (!service) {
throw new Error(`service not found in services JSON: ${serviceName}`);
}

const clusterIpAddress = service?.spec?.clusterIP;
if (typeof clusterIpAddress !== 'string' || clusterIpAddress.length === 0 || clusterIpAddress === 'None') {
throw new Error(`service ${serviceName} does not have a usable clusterIP`);
}

const encodedIpAddress = this.encodeIpv4Address(clusterIpAddress);
const nodeMetadata = rewrittenNetwork.nodeMetadata[nodeIndex];
changedEndpointCount += this.rewriteNodeEndpoints(nodeMetadata, encodedIpAddress, clusterIpAddress, serviceName);
}

if (changedEndpointCount === 0) {
throw new Error('override-network.json was not changed; this would not test endpoint remapping');
}

fs.writeFileSync(outputPath, `${JSON.stringify(rewrittenNetwork, null, 2)}\n`);
console.log(`Wrote ${outputPath} with ${String(changedEndpointCount)} rewritten endpoint entries`);
}

static parseArguments(argumentList) {
const argumentsMap = new Map();

for (let argumentIndex = 0; argumentIndex < argumentList.length; argumentIndex += 1) {
const argument = argumentList[argumentIndex];
if (!argument.startsWith('--')) {
throw new Error(`Unexpected argument: ${argument}`);
}

const argumentName = argument.slice(2);
const argumentValue = argumentList[argumentIndex + 1];
if (!argumentValue || argumentValue.startsWith('--')) {
throw new Error(`Missing value for --${argumentName}`);
}

argumentsMap.set(argumentName, argumentValue);
argumentIndex += 1;
}

return argumentsMap;
}

static requireArgument(argumentsMap, argumentName) {
const argumentValue = argumentsMap.get(argumentName);
if (!argumentValue) {
throw new Error(`Missing required argument --${argumentName}`);
}
return argumentValue;
}

static encodeIpv4Address(ipAddressText) {
const octets = ipAddressText.split('.').map(octetText => Number(octetText));

if (octets.length !== 4 || octets.some(octet => !Number.isInteger(octet) || octet < 0 || octet > 255)) {
throw new Error(`Only IPv4 addresses are supported, got: ${ipAddressText}`);
}

return Buffer.from(octets).toString('base64');
}

static rewriteNodeEndpoints(nodeMetadata, encodedIpAddress, clusterIpAddress, serviceName) {
if (!nodeMetadata || typeof nodeMetadata !== 'object') {
throw new Error('nodeMetadata entry is missing or invalid');
}

// The restored roster uses gossip endpoints before the service endpoints are
// needed. Rewrite both representations so the fresh cluster can establish
// peer sync without depending on endpoint names from the source cluster.
let changedEndpointCount = this.rewriteEndpointList(
nodeMetadata?.node,
'gossipEndpoint',
encodedIpAddress,
clusterIpAddress,
serviceName,
);
changedEndpointCount += this.rewriteEndpointList(
nodeMetadata?.node,
'serviceEndpoint',
encodedIpAddress,
clusterIpAddress,
serviceName,
);
changedEndpointCount += this.rewriteEndpointList(
nodeMetadata?.rosterEntry,
'gossipEndpoint',
encodedIpAddress,
clusterIpAddress,
serviceName,
);
return changedEndpointCount;
}

static rewriteEndpointList(parentObject, endpointProperty, encodedIpAddress, clusterIpAddress, serviceName) {
if (!parentObject || typeof parentObject !== 'object') {
throw new Error(`node metadata is missing for ${serviceName}`);
}

const endpoints = parentObject[endpointProperty];
if (!Array.isArray(endpoints) || endpoints.length === 0) {
throw new Error(`node is missing ${endpointProperty} entries for ${serviceName}`);
}

let changedEndpointCount = 0;

parentObject[endpointProperty] = endpoints.map(endpoint => {
const rewrittenEndpoint = {...endpoint};
const existingIpAddress = rewrittenEndpoint.ipAddressV4;

rewrittenEndpoint.ipAddressV4 = encodedIpAddress;
delete rewrittenEndpoint.domainName;

const endpointChanged = existingIpAddress !== encodedIpAddress;
if (endpointChanged) {
changedEndpointCount += 1;
}

return rewrittenEndpoint;
});

console.log(`Updated ${endpointProperty} for ${serviceName} -> ${clusterIpAddress}`);
return changedEndpointCount;
}
}

try {
OverrideNetworkGenerator.main();
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(message);
process.exitCode = 1;
}
92 changes: 0 additions & 92 deletions examples/state-save-and-restore/scripts/init.sh

This file was deleted.

76 changes: 2 additions & 74 deletions resources/cleanup-state-rounds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ for nodeid in */; do

if [ -n "$rounds" ]; then
latest_round=""
pces_source_round=""
highest_round=$(echo "$rounds" | tail -n 1)
highest_round_freeze_state=""
earliest_signed_non_freeze_round=""
latest_signed_non_freeze_round=""
for round in $rounds; do
metadata_file="${round}/stateMetadata.txt"
Expand All @@ -53,9 +51,6 @@ for nodeid in */; do
fi

if [ "$freeze_state" = "false" ] && [ -n "$signing_weight" ] && [ "$signing_weight" = "$total_weight" ]; then
if [ -z "$earliest_signed_non_freeze_round" ]; then
earliest_signed_non_freeze_round="$round"
fi
latest_signed_non_freeze_round="$round"
latest_round="$round"
fi
Expand All @@ -69,83 +64,16 @@ for nodeid in */; do
latest_round="$highest_round"
fi

if [ -z "$pces_source_round" ]; then
pces_source_round="$latest_round"
fi

round_count=$(echo "$rounds" | wc -l)

echo "Node ${nodeid}${realmShard}: Found ${round_count} rounds, keeping state: ${latest_round}, PCES: ${pces_source_round}"
echo "Node ${nodeid}${realmShard}: Found ${round_count} rounds, keeping state: ${latest_round}"

for round in $rounds; do
if [ "$round" != "$latest_round" ] && [ "$round" != "$pces_source_round" ]; then
if [ "$round" != "$latest_round" ]; then
echo " Removing old round: $round"
rm -rf "$round"
fi
done

# Rebuild top-level PCES from the selected pre-freeze round so the event
# creator has the preconsensus events it needs to become active after
# resuming from the kept state.
if [ -d "${pces_source_round}/preconsensus-events" ]; then
top_level_pces="${HEDERA_HAPI_PATH}/data/saved/preconsensus-events"
echo " Rebuilding top-level preconsensus events from round: $pces_source_round"
rm -rf "$top_level_pces"
source_pces_dir="${pces_source_round}/preconsensus-events"
find "$source_pces_dir" -type f -name '*.pces' | while IFS= read -r pces_file; do
relative_pces_path=${pces_file#"$source_pces_dir"/}
pces_node_id=${relative_pces_path%%/*}
pces_filename=${pces_file##*/}
pces_date=${pces_filename%%T*}

year=$(echo "$pces_date" | cut -d- -f1)
month=$(echo "$pces_date" | cut -d- -f2)
day=$(echo "$pces_date" | cut -d- -f3)

if [ -n "$pces_node_id" ] && [ -n "$year" ] && [ -n "$month" ] && [ -n "$day" ] && [ "$pces_date" != "$pces_filename" ]; then
pces_destination_dir="${top_level_pces}/${pces_node_id}/${year}/${month}/${day}"
else
pces_destination_dir="${top_level_pces}/${pces_node_id}"
fi

mkdir -p "$pces_destination_dir"
cp "$pces_file" "$pces_destination_dir/"
done
fi

if [ -d "${latest_round}/preconsensus-events" ]; then
round_pces_dir="${latest_round}/preconsensus-events"
round_pces_tmp="${latest_round}/preconsensus-events.tmp"
echo " Normalizing round preconsensus events for state: $latest_round"
rm -rf "$round_pces_tmp"
mkdir -p "$round_pces_tmp"
find "$round_pces_dir" -type f -name '*.pces' | while IFS= read -r pces_file; do
relative_pces_path=${pces_file#"$round_pces_dir"/}
pces_node_id=${relative_pces_path%%/*}
pces_filename=${pces_file##*/}
pces_date=${pces_filename%%T*}

year=$(echo "$pces_date" | cut -d- -f1)
month=$(echo "$pces_date" | cut -d- -f2)
day=$(echo "$pces_date" | cut -d- -f3)

if [ -n "$pces_node_id" ] && [ -n "$year" ] && [ -n "$month" ] && [ -n "$day" ] && [ "$pces_date" != "$pces_filename" ]; then
pces_destination_dir="${round_pces_tmp}/${pces_node_id}/${year}/${month}/${day}"
else
pces_destination_dir="${round_pces_tmp}/${pces_node_id}"
fi

mkdir -p "$pces_destination_dir"
cp "$pces_file" "$pces_destination_dir/"
done
rm -rf "$round_pces_dir"
mv "$round_pces_tmp" "$round_pces_dir"
fi

if [ "$pces_source_round" != "$latest_round" ] && [ -d "$pces_source_round" ]; then
echo " Removing old round after PCES rebuild: $pces_source_round"
rm -rf "$pces_source_round"
fi
fi

cd ../..
Expand Down
Loading
Loading