Skip to content

Commit

Permalink
Merge branch 'develop' into mateu/installer-dropdown-in-topbar
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo authored Jul 23, 2024
2 parents 0c53b32 + 84ffe17 commit 1bac365
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 97 deletions.
14 changes: 2 additions & 12 deletions packages/daemons/src/ethicalMetrics/checkEthicalMetricsStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import { InstalledPackageData } from "@dappnode/types";

/**
* Make sure that on Ethical metrics enabled and existing email,
* the packages exporter, DMS and Ethical metrics are installed and running
* the packages DMS and Ethical metrics are installed and running
*/
export async function checkEthicalMetricsStatus(
dappnodeInstaller: DappnodeInstaller
): Promise<void> {
const exporterDnpName = "dappnode-exporter.dnp.dappnode.eth";
const dmsDnpName = "dms.dnp.dappnode.eth";

try {
Expand All @@ -22,9 +21,8 @@ export async function checkEthicalMetricsStatus(
if (enabled) {
if (!mail && !tgChannelId) throw Error("mail or tgChannelId is required");

// First check for Ethical metrics, then for DMS and last for Exporter
// First check for Ethical metrics, then for DMS
// Ethical Metrics package has DMS as dependency, so it will be installed automatically
// DMS package has Exporter as dependency, so it will be installed automatically

// Check ethical metrics pkg
const ethicalMetricsPkg = await listPackageNoThrow({
Expand All @@ -42,14 +40,6 @@ export async function checkEthicalMetricsStatus(
await packageInstall(dappnodeInstaller, { name: dmsDnpName });
else ensureAllContainersRunning(dmsPkg);

// check exporter pkg
const exporterPkg = await listPackageNoThrow({
dnpName: exporterDnpName,
});
if (!exporterPkg)
await packageInstall(dappnodeInstaller, { name: exporterDnpName });
else ensureAllContainersRunning(exporterPkg);

// Register instance
await register({ mail, tgChannelId });
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dappmanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@dappnode/optimism": "^0.1.0",
"@dappnode/params": "^0.1.0",
"@dappnode/stakers": "^0.1.0",
"@dappnode/toolkit": "^0.1.21",
"@dappnode/toolkit": "^0.1.22",
"@dappnode/types": "^0.1.36",
"@dappnode/upnpc": "^0.1.0",
"@dappnode/utils": "^0.1.0",
Expand All @@ -65,7 +65,7 @@
"cookie-session": "^1.4.0",
"cors": "^2.8.5",
"deepmerge": "^2.2.1",
"ethers": "^6.9.0",
"ethers": "^6.10.0",
"express": "^4.17.1",
"express-fileupload": "^1.4.0",
"express-rate-limit": "^6.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import { getEthUrl } from "@dappnode/installer";
import { getEthersProvider, getEthUrl } from "@dappnode/installer";
import resolverAbi from "./abi/resolverAbi.json" assert { type: "json" };
import ensAbi from "./abi/ens.json" assert { type: "json" };
import { Network, Content, NotFoundError, EnsResolverError } from "./types.js";
Expand Down Expand Up @@ -61,7 +61,7 @@ export function ResolveDomainWithCache(): (domain: string) => Promise<Content> {
return async function (domain: string): Promise<Content> {
const network = parseNetworkFromDomain(domain);
const providerUrl = await _getEthersProviderByNetwork(network);
const provider = new ethers.JsonRpcProvider(providerUrl, "mainnet", { staticNetwork: true });
const provider = await getEthersProvider(providerUrl);
return _resolveDomain(domain, provider);
};
}
Expand Down
5 changes: 2 additions & 3 deletions packages/dappmanager/src/calls/fetchDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getIsUpdated
} from "@dappnode/utils";
import { throttle } from "lodash-es";
import { getEthUrl } from "@dappnode/installer";
import { getEthersProvider } from "@dappnode/installer";
import { DappNodeDirectory } from "@dappnode/toolkit";
import { dappnodeInstaller } from "../index.js";

Expand All @@ -18,8 +18,7 @@ const loadThrottle = 500; // 0.5 seconds
* Fetches all package names in the custom dappnode directory.
*/
export async function fetchDirectory(): Promise<DirectoryItem[]> {
const providerUrl = await getEthUrl();
const directory = new DappNodeDirectory(providerUrl);
const directory = new DappNodeDirectory(await getEthersProvider()) ;

const installedDnpList = await listPackages();

Expand Down
5 changes: 3 additions & 2 deletions packages/dappmanager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@dappnode/hostscriptsservices";
import {
DappnodeInstaller,
getEthersProvider,
getEthUrl,
getIpfsUrl,
postRestartPatch
Expand Down Expand Up @@ -58,9 +59,9 @@ try {
}

// Required db to be initialized
export const dappnodeInstaller = new DappnodeInstaller(ipfsUrl, ethUrl);
export const dappnodeInstaller = new DappnodeInstaller(ipfsUrl, await getEthersProvider());

export const publicRegistry = new DappNodeRegistry(ethUrl, "public");
export const publicRegistry = new DappNodeRegistry(await getEthersProvider(), "public");

// TODO: find a way to move the velow constants to the api itself
const vpnApiClient = getVpnApiClient(params);
Expand Down
2 changes: 1 addition & 1 deletion packages/dyndns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@dappnode/db": "^0.1.0",
"@dappnode/logger": "^0.1.0",
"@dappnode/params": "^0.1.0",
"ethers": "^6.9.0",
"ethers": "^6.10.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/installer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"@dappnode/logger": "^0.1.0",
"@dappnode/params": "^0.1.0",
"@dappnode/schemas": "^0.1.14",
"@dappnode/toolkit": "^0.1.21",
"@dappnode/toolkit": "^0.1.22",
"@dappnode/utils": "^0.1.0",
"deepmerge": "2.2.1",
"ethers": "^6.9.0",
"ethers": "^6.10.0",
"is-ipfs": "8.0.1",
"lodash-es": "4.17.21",
"node-fetch": "^3.3.2",
Expand Down
10 changes: 5 additions & 5 deletions packages/installer/src/dappnodeInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import { computeGlobalEnvsFromDb } from "@dappnode/db";
import { getIsCore } from "@dappnode/utils";
import { sanitizeDependencies } from "./dappGet/utils/sanitizeDependencies.js";
import { parseTimeoutSeconds } from "./utils.js";
import { getEthUrl } from "./ethClient/index.js";
import { getEthersProvider } from "./ethClient/index.js";
import { omit } from "lodash-es";
import { ethers } from "ethers";

/**
* Returns the ipfsUrl to initialize the ipfs instance
Expand All @@ -46,14 +47,13 @@ export function getIpfsUrl(): string {
}

export class DappnodeInstaller extends DappnodeRepository {
constructor(ipfsUrl: string, ethUrl: string, timeout?: number) {
super(ipfsUrl, ethUrl, timeout);
constructor(ipfsUrl: string, ethersProvider: ethers.JsonRpcProvider, timeout?: number) {
super(ipfsUrl, ethersProvider, timeout);
}

private async updateProviders(): Promise<void> {
const newEthUrl = await getEthUrl();
const newIpfsUrl = getIpfsUrl();
super.changeEthProvider(newEthUrl);
super.changeEthProvider(await getEthersProvider());
super.changeIpfsProvider(newIpfsUrl);
}

Expand Down
29 changes: 6 additions & 23 deletions packages/installer/src/ethClient/clientStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parseEthersBlock, parseEthersSyncing } from "@dappnode/utils";
import { logs } from "@dappnode/logger";
import fetch from "node-fetch";
import { params } from "@dappnode/params";
import { getEthersProvider } from "./ethersProvider.js";

/**
* 7200 is the average blocks per day in Ethereum as Mon Nov 28 2022
Expand Down Expand Up @@ -174,23 +175,11 @@ export async function getMultiClientStatus(
async function isSyncedWithRemoteExecution(localUrl: string): Promise<boolean> {
if (db.ethClientFallback.get() === "off") return true;
// Check is synced with remote execution
const latestLocalBlock = await new ethers.JsonRpcProvider(
localUrl,
"mainnet",
{
staticNetwork: true,
}
)
const latestLocalBlock = await (await getEthersProvider(localUrl))
.send("eth_blockNumber", [])
.then(parseEthersBlock);

const latestRemoteBlock = await new ethers.JsonRpcProvider(
params.ETH_MAINNET_RPC_URL_REMOTE,
"mainnet",
{
staticNetwork: true,
}
)
const latestRemoteBlock = await (await getEthersProvider(params.ETH_MAINNET_RPC_URL_REMOTE))
.send("eth_blockNumber", [])
.then(parseEthersBlock);

Expand All @@ -204,9 +193,7 @@ async function isSyncedWithRemoteExecution(localUrl: string): Promise<boolean> {
* @param url "http://geth.dappnode:8545"
*/
async function isSyncing(url: string): Promise<boolean> {
const provider = new ethers.JsonRpcProvider(url, "mainnet", {
staticNetwork: true,
});
const provider = await getEthersProvider(url);
const syncing = await provider
.send("eth_syncing", [])
.then(parseEthersSyncing);
Expand Down Expand Up @@ -238,9 +225,7 @@ async function isApmStateCorrect(url: string): Promise<boolean> {
const result =
"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000342f697066732f516d63516958454c42745363646278464357454a517a69664d54736b4e5870574a7a7a5556776d754e336d4d4361000000000000000000000000";

const provider = new ethers.JsonRpcProvider(url, "mainnet", {
staticNetwork: true,
});
const provider = await getEthersProvider(url);

const res = await provider.send("eth_call", [testTxData, "latest"]);
return res === result;
Expand All @@ -257,9 +242,7 @@ async function isSyncedWithConsensus(
execUrl: string,
consUrl: string
): Promise<boolean> {
const provider = new ethers.JsonRpcProvider(execUrl, "mainnet", {
staticNetwork: true,
});
const provider = await getEthersProvider(execUrl);
const execBlockNumber = await provider.getBlockNumber();
const execBlockHeadersResponse = await fetch(
consUrl + "/eth/v2/beacon/blocks/head"
Expand Down
19 changes: 16 additions & 3 deletions packages/installer/src/ethClient/ethersProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,28 @@ import { getMultiClientStatus } from "./clientStatus.js";
import { EthClientStatusError, EthProviderError } from "@dappnode/types";
import { emitSyncedNotification } from "./syncedNotification.js";
import { ethereumClient } from "./index.js";
import { FetchRequest } from "ethers";

/**
* Returns the url of the JSON RPC an Eth multi-client status and target
* If the package target is not active it returns the remote URL
* @returns initialized ethers instance
*/
export async function getEthersProvider(): Promise<ethers.JsonRpcProvider> {
const url = await getEthUrl();
return new ethers.JsonRpcProvider(url, "mainnet", { staticNetwork: true });
export async function getEthersProvider(
url?: string,
network?: string
): Promise<ethers.JsonRpcProvider> {
const rpcUrl = url ?? (await getEthUrl());
const rpcNetwork = network ?? "mainnet";
const fetchRequest = new FetchRequest(rpcUrl);
const versionData = db.versionData.get();
fetchRequest.setHeader(
"x-dappmanager-version",
`${versionData.version}-${db.versionData.get().commit?.slice(0, 8)}`
);
return new ethers.JsonRpcProvider(fetchRequest, rpcNetwork, {
staticNetwork: true,
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dappnode/toolkit",
"type": "module",
"version": "0.1.21",
"version": "0.1.22",
"description": "The DappNode Toolkit is a collection of shared smart contracts, types, and utility functions that can be used across various DappNode projects. This toolkit aims to simplify development by providing reusable code and a consistent set of interfaces and types. The package includes modules for ACL, APM, Apps, EVMScript, Params, Registry, and Types, each with their own set of submodules and functionalities. The toolkit is designed to be extensible, allowing for the easy addition of new modules as needed. Additionally, the toolkit includes comprehensive testing functionality and documentation to ensure the quality and usability of the code.",
"main": "dist/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/directory/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export class DappNodeDirectory {
* Class constructor
* @param ethUrl - The URL of the Ethereum node to connect to.
*/
constructor(ethUrl: string) {
constructor(ethersProvider: ethers.AbstractProvider) {
this.directoryContract = new ethers.Contract(
directoryAddress,
directoryAbi,
new ethers.JsonRpcProvider(ethUrl, "mainnet", { staticNetwork: true })
ethersProvider
);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/toolkit/src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class DappNodeRegistry {
* @param ethUrl - The URL of the Ethereum node to connect to.
* @param registry - The type of the registry (DNP or Public).
*/
constructor(ethUrl: string, registry: Registry) {
constructor(ethersProvider: ethers.AbstractProvider, registry: Registry) {
this.registry = registry;
if (registry === "dnp") {
this.contractAddress = registryDnpAddress;
Expand All @@ -35,11 +35,10 @@ export class DappNodeRegistry {
this.contractAddress = registryPublicAddress;
this.graphEndpoint = publicRegistryGraphEndpoint;
}

this.registryContract = new ethers.Contract(
this.contractAddress,
registryAbi,
new ethers.JsonRpcProvider(ethUrl, "mainnet", { staticNetwork: true })
ethersProvider
);

this.nameSuffix =
Expand Down
16 changes: 5 additions & 11 deletions packages/toolkit/src/repository/apmRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,22 @@ import { repositoryAbi } from "./params.js";
* ApmRepository is a class to interact with the DAppNode APM Repository Contract.
*/
export class ApmRepository {
private ethProvider: ethers.Provider;
private ethProvider: ethers.AbstractProvider;

/**
* Class constructor
* @param ethUrl - The URL of the Ethereum node to connect to.
*/
constructor(ethUrl: string) {
if (!ethUrl) throw new Error("Ethereum URL is required");
this.ethProvider = new ethers.JsonRpcProvider(ethUrl, "mainnet", {
staticNetwork: true,
});
constructor(ethersProvider: ethers.AbstractProvider) {
this.ethProvider = ethersProvider;
}

/**
* Changes the Ethereum node to connect to.
* @param ethUrl - The URL of the Ethereum node to connect to.
*/
public changeEthProvider(ethUrl: string): void {
if (!ethUrl) throw new Error("Ethereum URL is required");
this.ethProvider = new ethers.JsonRpcProvider(ethUrl, "mainnet", {
staticNetwork: true,
});
public changeEthProvider(ethersProvider: ethers.AbstractProvider): void {
this.ethProvider = ethersProvider;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/toolkit/src/repository/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "./releaseSignature.js";
import { isEnsDomain } from "../isEnsDomain.js";
import { dappnodeRegistry } from "./params.js";
import { ethers } from "ethers";

const source = "ipfs" as const;

Expand All @@ -50,8 +51,8 @@ export class DappnodeRepository extends ApmRepository {
* @param ipfsUrl - The URL of the IPFS network node.
* @param ethUrl - The URL of the Ethereum node to connect to.
*/
constructor(ipfsUrl: string, ethUrl: string, timeout?: number) {
super(ethUrl);
constructor(ipfsUrl: string, ethersProvider: ethers.AbstractProvider, timeout?: number) {
super(ethersProvider);
this.timeout = timeout || 30 * 1000;
this.ipfs = create({ url: ipfsUrl, timeout: this.timeout });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/test/directory/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect } from "chai";
describe.skip("Dappnode Directory", function () {
this.timeout(100000);
const contract = new DappNodeDirectory(
`https://mainnet.infura.io/v3/${process.env.INFURA_MAINNET_KEY}`
new ethers.InfuraProvider("mainnet", process.env.INFURA_MAINNET_KEY)
);

it(`should get directory pkgs`, async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/test/registry/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ describe("Dappnode Registry", function () {
},
];
const contract = new DappNodeRegistry(
`https://mainnet.infura.io/v3/${process.env.INFURA_MAINNET_KEY}`,
new ethers.InfuraProvider("mainnet", process.env.INFURA_MAINNET_KEY),
"dnp"
);
const result = await contract.queryGraphNewRepos<"dnp">();
Expand Down Expand Up @@ -1254,7 +1254,7 @@ describe("Dappnode Registry", function () {
];

const contract = new DappNodeRegistry(
`https://mainnet.infura.io/v3/${process.env.INFURA_MAINNET_KEY}`,
new ethers.InfuraProvider("mainnet", process.env.INFURA_MAINNET_KEY),
"public"
);

Expand Down
Loading

0 comments on commit 1bac365

Please sign in to comment.