Skip to content

Commit

Permalink
Merge pull request #275 from dappnode/pablo/remove-fee-recipient-as-g…
Browse files Browse the repository at this point in the history
…lobal-env

[ Breaking change ] Remove fee recipient as global env
  • Loading branch information
pablomendezroyo authored Nov 17, 2023
2 parents bc3054e + f7f56e8 commit fa00d50
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 71 deletions.
2 changes: 0 additions & 2 deletions packages/brain/src/calls/getStakerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
network,
executionClient,
consensusClient,
defaultFeeRecipient,
executionClientUrl,
validatorUrl,
beaconchainUrl,
Expand All @@ -19,6 +18,5 @@ export async function getStakerConfig(): Promise<StakerConfig<Network>> {
validatorUrl,
beaconchainUrl,
signerUrl,
defaultFeeRecipient,
};
}
5 changes: 2 additions & 3 deletions packages/brain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ export const {
signerUrl,
token,
host,
defaultFeeRecipient,
tlsCert,
} = loadStakerConfig();
logger.debug(
`Loaded staker config:\n - Network: ${network}\n - Execution client: ${executionClient}\n - Consensus client: ${consensusClient}\n - Execution client url: ${executionClientUrl}\n - Validator url: ${validatorUrl}\n - Beaconcha url: ${beaconchaUrl}\n - Beaconchain url: ${beaconchainUrl}\n - Signer url: ${signerUrl}\n - Token: ${token}\n - Host: ${host}\n - Default fee recipient: ${defaultFeeRecipient}`
`Loaded staker config:\n - Network: ${network}\n - Execution client: ${executionClient}\n - Consensus client: ${consensusClient}\n - Execution client url: ${executionClientUrl}\n - Validator url: ${validatorUrl}\n - Beaconcha url: ${beaconchaUrl}\n - Beaconchain url: ${beaconchainUrl}\n - Signer url: ${signerUrl}\n - Token: ${token}\n - Host: ${host}}`
);

// Create API instances. Must preceed db initialization
Expand Down Expand Up @@ -68,7 +67,7 @@ export const brainDb = new BrainDataBase(
const uiServer = startUiServer(path.resolve(__dirname, params.uiBuildDirName));
const launchpadServer = startLaunchpadApi();

await brainDb.initialize(signerApi, validatorApi, defaultFeeRecipient);
await brainDb.initialize(signerApi, validatorApi);
logger.debug(brainDb.data);

// CRON
Expand Down
19 changes: 6 additions & 13 deletions packages/brain/src/modules/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,14 @@ export class BrainDataBase extends LowSync<StakingBrainDb> {
*/
public async initialize(
signerApi: Web3SignerApi,
validatorApi: ValidatorApi,
defaultFeeRecipient: string | undefined
validatorApi: ValidatorApi
): Promise<void> {
try {
// Important! .read() method must be called before accessing brainDb.data otherwise it will be null
this.read();
// If db.json doesn't exist, db.data will be null
if (this.data === null)
await this.databaseMigration(
signerApi,
validatorApi,
defaultFeeRecipient
);
await this.databaseMigration(signerApi, validatorApi);
else this.setOwnerWriteRead();
} catch (e) {
logger.error(`unable to initialize the db ${this.dbName}`, e);
Expand Down Expand Up @@ -247,12 +242,10 @@ export class BrainDataBase extends LowSync<StakingBrainDb> {
*
* @param signerApi - The signer API
* @param validatorApi - The validator API
* @param defaultFeeRecipient - The default fee recipient to use if the validator API is not available
*/
private async databaseMigration(
signerApi: Web3SignerApi,
validatorApi: ValidatorApi,
defaultFeeRecipient?: string
validatorApi: ValidatorApi
): Promise<void> {
let retries = 0;
while (retries < 10) {
Expand All @@ -279,11 +272,11 @@ export class BrainDataBase extends LowSync<StakingBrainDb> {
})
.catch((e) => {
logger.error(
`Unable to fetch fee recipient for ${pubkeys[0]}. Setting default ${defaultFeeRecipient}}`,
`Unable to fetch fee recipient for ${pubkeys[0]}. Setting default ${params.burnAddress}}`,
e
);
if (defaultFeeRecipient) feeRecipient = defaultFeeRecipient;
else feeRecipient = params.burnAddress;
// TODO: consider setting MEV fee recipient.
feeRecipient = params.burnAddress;
});

logger.info(
Expand Down
43 changes: 6 additions & 37 deletions packages/brain/src/modules/envs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import path from "path";
import fs from "fs";
import { params } from "../../params.js";
import { __dirname } from "../../index.js";
import { isValidEcdsaPubkey } from "@stakingbrain/common";

/**
* Loads the staker config needed to create the base urls for beacon, validator and signer APIs
Expand All @@ -45,7 +44,7 @@ import { isValidEcdsaPubkey } from "@stakingbrain/common";
* @throws consensus client is not valid for <network>: <consensus client>. Valid consensus clients for <network>: <consensus clients>
* This exception is thrown if the consensus client is not valid for the network
*
* @returns executionClientUrl, validatorUrl, beaconchainUrl, beaconchaUrl, signerUrl, token, host, defaultFeeRecipient, tlsCert
* @returns executionClientUrl, validatorUrl, beaconchainUrl, beaconchaUrl, signerUrl, token, host, tlsCert
*/
export function loadStakerConfig(): {
network: Network;
Expand All @@ -58,7 +57,6 @@ export function loadStakerConfig(): {
signerUrl: string;
token: string;
host: string;
defaultFeeRecipient?: string;
tlsCert?: Buffer;
} {
const network = process.env.NETWORK as Network;
Expand All @@ -79,8 +77,7 @@ export function loadStakerConfig(): {
tlsCert: Buffer | undefined;

if (network === "mainnet") {
const { executionClient, consensusClient, defaultFeeRecipient } =
loadEnvs("mainnet");
const { executionClient, consensusClient } = loadEnvs("mainnet");
switch (executionClient) {
case "geth.dnp.dappnode.eth":
executionClientUrl = `http://geth.dappnode:8545`;
Expand Down Expand Up @@ -144,15 +141,10 @@ export function loadStakerConfig(): {
signerUrl: `http://web3signer.web3signer.dappnode:9000`,
token,
host: `brain.web3signer.dappnode`,
defaultFeeRecipient:
defaultFeeRecipient && isValidEcdsaPubkey(defaultFeeRecipient)
? defaultFeeRecipient
: undefined,
tlsCert,
};
} else if (network === "gnosis") {
const { executionClient, consensusClient, defaultFeeRecipient } =
loadEnvs("gnosis");
const { executionClient, consensusClient } = loadEnvs("gnosis");
switch (executionClient) {
case "nethermind-xdai.dnp.dappnode.eth":
executionClientUrl = `http://nethermind-xdai.dappnode:8545`;
Expand Down Expand Up @@ -202,15 +194,10 @@ export function loadStakerConfig(): {
signerUrl: `http://web3signer.web3signer-gnosis.dappnode:9000`,
token,
host: `brain.web3signer-gnosis.dappnode`,
defaultFeeRecipient:
defaultFeeRecipient && isValidEcdsaPubkey(defaultFeeRecipient)
? defaultFeeRecipient
: undefined,
tlsCert,
};
} else if (network === "prater") {
const { executionClient, consensusClient, defaultFeeRecipient } =
loadEnvs("prater");
const { executionClient, consensusClient } = loadEnvs("prater");
switch (executionClient) {
case "goerli-nethermind.dnp.dappnode.eth":
executionClientUrl = `http://goerli-nethermind.dappnode:8545`;
Expand Down Expand Up @@ -274,15 +261,10 @@ export function loadStakerConfig(): {
signerUrl: `http://web3signer.web3signer-prater.dappnode:9000`,
token,
host: `web3signer.web3signer-prater.dappnode`,
defaultFeeRecipient:
defaultFeeRecipient && isValidEcdsaPubkey(defaultFeeRecipient)
? defaultFeeRecipient
: undefined,
tlsCert,
};
} else if (network === "lukso") {
const { executionClient, consensusClient, defaultFeeRecipient } =
loadEnvs("lukso");
const { executionClient, consensusClient } = loadEnvs("lukso");
switch (executionClient) {
case "lukso-erigon.dnp.dappnode.eth":
executionClientUrl = `http://lukso-erigon.dappnode:8545`;
Expand Down Expand Up @@ -340,15 +322,10 @@ export function loadStakerConfig(): {
signerUrl: `http://web3signer.web3signer-lukso.dappnode:9000`,
token,
host: `web3signer.web3signer-lukso.dappnode`,
defaultFeeRecipient:
defaultFeeRecipient && isValidEcdsaPubkey(defaultFeeRecipient)
? defaultFeeRecipient
: undefined,
tlsCert,
};
} else if (network === "holesky") {
const { executionClient, consensusClient, defaultFeeRecipient } =
loadEnvs("holesky");
const { executionClient, consensusClient } = loadEnvs("holesky");
switch (executionClient) {
case "holesky-nethermind.dnp.dappnode.eth":
executionClientUrl = `http://holesky-nethermind.dappnode:8545`;
Expand Down Expand Up @@ -412,10 +389,6 @@ export function loadStakerConfig(): {
signerUrl: `http://web3signer.web3signer-holesky.dappnode:9000`,
token,
host: `web3signer.web3signer-holesky.dappnode`,
defaultFeeRecipient:
defaultFeeRecipient && isValidEcdsaPubkey(defaultFeeRecipient)
? defaultFeeRecipient
: undefined,
tlsCert,
};
} else {
Expand All @@ -432,12 +405,9 @@ function loadEnvs<T extends Network>(
): {
executionClient: ExecutionClient<T>;
consensusClient: ConsensusClient<T>;
defaultFeeRecipient?: string;
} {
const errors = [];

const defaultFeeRecipient =
process.env[`_DAPPNODE_GLOBAL_FEE_RECIPIENT_${network.toUpperCase()}`];
const executionClient =
process.env[`_DAPPNODE_GLOBAL_EXECUTION_CLIENT_${network.toUpperCase()}`];
const consensusClient =
Expand Down Expand Up @@ -564,6 +534,5 @@ function loadEnvs<T extends Network>(
return {
executionClient: executionClient as ExecutionClient<T>,
consensusClient: consensusClient as ConsensusClient<T>,
defaultFeeRecipient,
};
}
18 changes: 3 additions & 15 deletions packages/brain/tests/unit/modules/db/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ describe("DataBase", () => {

expect(db.data).to.be.null;
// initialize
await db.initialize(
signerApi,
validatorApi,
"0x0000000000000000000000000000000000000000"
);
await db.initialize(signerApi, validatorApi);
// print contents of database
console.log(db.data);
// check database
Expand All @@ -120,11 +116,7 @@ describe("DataBase", () => {
const signerApi = sinon.createStubInstance(Web3SignerApi);
const validatorApi = sinon.createStubInstance(ValidatorApi);
sinon.stub(db, <any>"databaseMigration").callsFake(databaseMigration);
await db.initialize(
signerApi,
validatorApi,
"0x0000000000000000000000000000000000000000"
);
await db.initialize(signerApi, validatorApi);

expect(fs.existsSync(testDbName)).to.be.true;
db.read();
Expand All @@ -139,11 +131,7 @@ describe("DataBase", () => {
fs.writeFileSync(testDbName, JSON.stringify({}));
const signerApi = sinon.createStubInstance(Web3SignerApi);
const validatorApi = sinon.createStubInstance(ValidatorApi);
db.initialize(
signerApi,
validatorApi,
"0x0000000000000000000000000000000000000000"
);
db.initialize(signerApi, validatorApi);
expect(fs.existsSync(testDbName)).to.be.true;
db.read();
expect(db.data).to.be.empty;
Expand Down
1 change: 0 additions & 1 deletion packages/common/src/types/network/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface StakerConfig<T extends Network> {
validatorUrl: string;
beaconchainUrl: string;
signerUrl: string;
defaultFeeRecipient?: string;
}

export type ExecutionClient<T extends Network> = T extends "mainnet"
Expand Down

0 comments on commit fa00d50

Please sign in to comment.