Skip to content

Commit

Permalink
Clean staker user settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dappnodedev committed Sep 5, 2024
1 parent 075b00f commit dbc9206
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 62 deletions.
27 changes: 9 additions & 18 deletions packages/stakers/src/consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import {
import { StakerComponent } from "./stakerComponent.js";
import { DappnodeInstaller } from "@dappnode/installer";
import * as db from "@dappnode/db";
import { listPackageNoThrow } from "@dappnode/dockerapi";
import { params } from "@dappnode/params";
import { getConsensusUserSettings } from "@dappnode/utils";
import { getDefaultConsensusUserSettings } from "@dappnode/utils";

// TODO: move ethereumClient logic here

Expand Down Expand Up @@ -79,19 +78,15 @@ export class Consensus extends StakerComponent {
async persistSelectedConsensusIfInstalled(network: Network): Promise<void> {
const currentConsensusDnpName = this.DbHandlers[network].get();
if (currentConsensusDnpName) {
const isInstalled = Boolean(await listPackageNoThrow({ dnpName: currentConsensusDnpName }));
const isInstalled = await this.isPackageInstalled(currentConsensusDnpName);

if (!isInstalled) {
// update status in db
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)
};
const userSettings = await this.getUserSettings(network, currentConsensusDnpName);

await this.persistSelectedIfInstalled({
dnpName: currentConsensusDnpName,
Expand All @@ -118,10 +113,13 @@ export class Consensus extends StakerComponent {
}

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

const isPkgInstalled = await this.isPackageInstalled(newConsensusDnpName);

const userSettings = {
environment: mustInstallPkg ? getConsensusUserSettings({ network }).environment : {},
// If the package is not installed, we use the default environment
environment: isPkgInstalled ? {} : getDefaultConsensusUserSettings({ network }).environment,
networks: this.getStakerNetworkSettings(network)
};

Expand All @@ -133,14 +131,7 @@ export class Consensus extends StakerComponent {
const beaconServiceName = "beacon-chain";

return {
rootNetworks: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
},
rootNetworks: this.getComposeRootNetworks(network),
serviceNetworks: {
[beaconServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
Expand Down
21 changes: 7 additions & 14 deletions packages/stakers/src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { StakerComponent } from "./stakerComponent.js";
import { DappnodeInstaller, ethereumClient } from "@dappnode/installer";
import * as db from "@dappnode/db";
import { listPackageNoThrow } from "@dappnode/dockerapi";
import { params } from "@dappnode/params";

// TODO: move ethereumClient logic here
Expand Down Expand Up @@ -72,15 +71,14 @@ export class Execution extends StakerComponent {
async persistSelectedExecutionIfInstalled(network: Network): Promise<void> {
const currentExecutionDnpName = this.DbHandlers[network].get();
if (currentExecutionDnpName) {
const isInstalled = await listPackageNoThrow({
dnpName: currentExecutionDnpName
});
const isInstalled = await this.isPackageInstalled(currentExecutionDnpName);

if (!isInstalled) {
// update status in db
this.DbHandlers[network].set(undefined);
return;
}

await this.persistSelectedIfInstalled({
dnpName: currentExecutionDnpName,
userSettings: this.getUserSettings(network, currentExecutionDnpName)
Expand All @@ -96,7 +94,7 @@ export class Execution extends StakerComponent {
newStakerDnpName: newExecutionDnpName,
dockerNetworkName: params.DOCKER_STAKER_NETWORKS[network],
compatibleClients: Execution.CompatibleExecutions[network],
userSettings: newExecutionDnpName ? this.getUserSettings(network, newExecutionDnpName) : {},
userSettings: this.getUserSettings(network, newExecutionDnpName),
prevClient: prevExecClientDnpName
});

Expand All @@ -112,17 +110,12 @@ export class Execution extends StakerComponent {
}
}

private getUserSettings(network: Network, dnpName: string): UserSettings {
private getUserSettings(network: Network, dnpName: string | null): UserSettings {
if (!dnpName) return {};

return {
networks: {
rootNetworks: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
},
rootNetworks: this.getComposeRootNetworks(network),
serviceNetworks: {
[this.getExecutionServiceName(dnpName)]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
Expand Down
44 changes: 24 additions & 20 deletions packages/stakers/src/mevBoost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class MevBoost extends StakerComponent {
}
await this.persistSelectedIfInstalled({
dnpName: currentMevBoostDnpName,
userSettings: this.getUserSettings([], false, network)
userSettings: this.getUserSettings(network, null)
});
this.DbHandlers[network].set(true);
}
Expand All @@ -82,19 +82,22 @@ export class MevBoost extends StakerComponent {
newStakerDnpName: newMevBoostDnpName,
dockerNetworkName: params.DOCKER_STAKER_NETWORKS[network],
compatibleClients: compatibleMevBoost ? [compatibleMevBoost] : null,
userSettings: newMevBoostDnpName ? this.getUserSettings(newRelays, true, network) : {},
userSettings: newMevBoostDnpName ? this.getUserSettings(network, newRelays) : {},
prevClient: compatibleMevBoost ? compatibleMevBoost.dnpName : null
});
// persist on db
if (Boolean(newMevBoostDnpName) !== this.DbHandlers[network].get())
await this.DbHandlers[network].set(newMevBoostDnpName ? true : false);
}

private getUserSettings(newRelays: string[], shouldSetEnvironment: boolean, network: Network): UserSettings {
return {
environment: shouldSetEnvironment
private getUserSettings(network: Network, newRelays: string[] | null): UserSettings {
const mevBoostServiceName = "mev-boost";

const userSettings: UserSettings = {
// If the package is not installed, we use the default environment
environment: newRelays
? {
"mev-boost": {
[mevBoostServiceName]: {
["RELAYS"]:
newRelays
.join(",")
Expand All @@ -103,23 +106,24 @@ export class MevBoost extends StakerComponent {
}
}
: {},
networks: {
rootNetworks: {
networks: this.getStakerNetworkSettings(network)
};

return userSettings;
}

private getStakerNetworkSettings(network: Network): UserSettings["networks"] {
const mevBoostServiceName = "mev-boost";

return {
rootNetworks: this.getComposeRootNetworks(network),
serviceNetworks: {
[mevBoostServiceName]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
aliases: [`${mevBoostServiceName}.${network}.staker.dappnode`]
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
},
serviceNetworks: {
["mev-boost"]: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
aliases: [`mev-boost.${network}.staker.dappnode`]
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
aliases: [`mev-boost.${network}.dncore.dappnode`]
}
aliases: [`${mevBoostServiceName}.${network}.dncore.dappnode`]
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions packages/stakers/src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,7 @@ export class Signer extends StakerComponent {
private getUserSettings(network: Network): UserSettings {
return {
networks: {
rootNetworks: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
},
rootNetworks: this.getComposeRootNetworks(network),
serviceNetworks: {
web3signer: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
Expand Down
23 changes: 22 additions & 1 deletion packages/stakers/src/stakerComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
UserSettingsAllDnps,
PackageContainer,
StakerItem,
UserSettings
UserSettings,
Network
} from "@dappnode/types";
import { getIsInstalled, getIsUpdated, getIsRunning, fileToGatewayUrl } from "@dappnode/utils";
import { lt } from "semver";
import { isMatch } from "lodash-es";
import { params } from "@dappnode/params";

export class StakerComponent {
protected dappnodeInstaller: DappnodeInstaller;
Expand Down Expand Up @@ -118,6 +120,25 @@ export class StakerComponent {
});
}

protected async isPackageInstalled(dnpName: string): Promise<boolean> {
const dnp = await listPackageNoThrow({ dnpName });

return Boolean(dnp);
}

protected getComposeRootNetworks(network: Network): NonNullable<UserSettings["networks"]>["rootNetworks"] {
return {
rootNetworks: {
[params.DOCKER_STAKER_NETWORKS[network]]: {
external: true
},
[params.DOCKER_PRIVATE_NETWORK_NAME]: {
external: true
}
}
};
}

/**
* Set the staker pkg:
* - ensures the staker pkg is installed
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 { getDefaultConsensusUserSettings as getConsensusUserSettings } from "./stakerUtils.js";
export { getDefaultConsensusUserSettings } from "./stakerUtils.js";
export * from "./ethers.js";
export { shellSafe } from "./shellSafe.js";
export { getIsInstalled } from "./getIsInstalled.js";
Expand Down

0 comments on commit dbc9206

Please sign in to comment.