Skip to content

Commit

Permalink
Ensure nimbus connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dappnodedev committed Sep 5, 2024
1 parent 1dd2918 commit 075b00f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
42 changes: 41 additions & 1 deletion packages/dappmanager/src/calls/packageInstall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Routes } from "@dappnode/types";
import { Network, Routes } from "@dappnode/types";
import { packageInstall as pkgInstall } from "@dappnode/installer";
import { dappnodeInstaller } from "../index.js";
import { Consensus } from "@dappnode/stakers";
import { logs } from "@dappnode/logger";

/**
* Installs a DAppNode Package.
Expand All @@ -26,4 +28,42 @@ export async function packageInstall({
userSettings,
options
});

ensureNimbusConnection(reqName);
}

/**
* Nimbus package will be migrated from a monoservice to a multiservice package.
* beacon-validator will be split into beacon-chain and validator services.
*
* This function ensures both services are properly connected to the staker network
* after installing the new version.
*
* TODO: Remove this once all Nimbus packages are multiservice
*/
function ensureNimbusConnection(dnpName: string): void {
if (!dnpName.includes("nimbus")) {
logs.debug("Not a Nimbus package, skipping network reconnection");
}

logs.info("Ensuring Nimbus services are connected to the staker network");

const consensus: Consensus = new Consensus(dappnodeInstaller);

const nimbusNetwork: Record<string, Network> = {
"nimbus.dnp.dappnode.eth": Network.Mainnet,
"nimbus-prater.dnp.dappnode.eth": Network.Prater,
"nimbus-gnosis.dnp.dappnode.eth": Network.Gnosis,
"nimbus-holesky.dnp.dappnode.eth": Network.Holesky
};

const network = nimbusNetwork[dnpName];

if (!network) {
logs.error("Could not determine the network for the Nimbus package");
return;
}

// Not awaited
consensus.persistSelectedConsensusIfInstalled(network);
}
64 changes: 38 additions & 26 deletions packages/stakers/src/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,16 @@ export class Consensus extends StakerComponent {
this.DbHandlers[network].set(undefined);
return;
}

const userSettings = {
// No need to add default environment if the package is already installed
environment: isInstalled ? {} : getConsensusUserSettings({ network }).environment,
networks: this.getStakerNetworkSettings(network)
};

await this.persistSelectedIfInstalled({
dnpName: currentConsensusDnpName,
userSettings: this.getUserSettings(isInstalled, network)
userSettings
});
await this.DbHandlers[network].set(currentConsensusDnpName);
}
Expand All @@ -97,8 +104,7 @@ export class Consensus extends StakerComponent {
async setNewConsensus(network: Network, newConsensusDnpName: string | null) {
const prevConsClientDnpName = this.DbHandlers[network].get();

const mustInstallPkg = !!newConsensusDnpName && !(await listPackageNoThrow({ dnpName: newConsensusDnpName }));
const userSettings = newConsensusDnpName ? this.getUserSettings(mustInstallPkg, network) : {};
const userSettings = await this.getUserSettings(network, newConsensusDnpName);

await super.setNew({
newStakerDnpName: newConsensusDnpName,
Expand All @@ -111,39 +117,45 @@ export class Consensus extends StakerComponent {
if (newConsensusDnpName !== prevConsClientDnpName) await this.DbHandlers[network].set(newConsensusDnpName);
}

private getUserSettings(shouldSetEnvironment: boolean, network: Network): UserSettings {
private async getUserSettings(network: Network, newConsensusDnpName: string | null): Promise<UserSettings> {
const mustInstallPkg = !!newConsensusDnpName && !(await listPackageNoThrow({ dnpName: newConsensusDnpName }));

const userSettings = {
environment: mustInstallPkg ? getConsensusUserSettings({ network }).environment : {},
networks: this.getStakerNetworkSettings(network)
};

return userSettings;
}

private getStakerNetworkSettings(network: Network): UserSettings["networks"] {
const validatorServiceName = "validator";
const beaconServiceName = "beacon-chain";

const consensusDefaultUserSettings = getConsensusUserSettings({ network });

return {
environment: shouldSetEnvironment ? consensusDefaultUserSettings.environment : {},
networks: {
rootNetworks: {
rootNetworks: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
},
serviceNetworks: {
[beaconServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
aliases: [`${beaconServiceName}.${network}.staker.dappnode`]
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
aliases: [`${beaconServiceName}.${network}.dncore.dappnode`]
}
},
serviceNetworks: {
[beaconServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
aliases: [`${beaconServiceName}.${network}.staker.dappnode`]
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
aliases: [`${beaconServiceName}.${network}.dncore.dappnode`]
}
[validatorServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
aliases: [`${validatorServiceName}.${network}.staker.dappnode`]
},
[validatorServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
aliases: [`${validatorServiceName}.${network}.staker.dappnode`]
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
aliases: [`${validatorServiceName}.${network}.dncore.dappnode`]
}
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
aliases: [`${validatorServiceName}.${network}.dncore.dappnode`]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export * from "./asyncFlows.js";
export * from "./pid.js";
export { urlJoin } from "./urlJoin.js";
export { prettyDnpName } from "./prettyDnpName.js";
export { getConsensusUserSettings } from "./stakerUtils.js";
export { getDefaultConsensusUserSettings as getConsensusUserSettings } from "./stakerUtils.js";
export * from "./ethers.js";
export { shellSafe } from "./shellSafe.js";
export { getIsInstalled } from "./getIsInstalled.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/stakerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { UserSettings, Network } from "@dappnode/types";
* It may be different depending if it is multiservice or monoservice and all the envs are
* set in the same service
*/
export function getConsensusUserSettings({ network }: { network: Network }): UserSettings {
export function getDefaultConsensusUserSettings({ network }: { network: Network }): UserSettings {
const validatorServiceName = "validator";
const beaconServiceName = "beacon-chain";
const beaconValidatorServiceName = "beacon-validator";
Expand Down

0 comments on commit 075b00f

Please sign in to comment.