Merge pull request #424 from matthewkeil/mkeil/simplify-default-setup-config
fix: simplify how default setup is found
This commit is contained in:
commit
c9e4634307
|
@ -7,48 +7,23 @@ const path = require("path");
|
||||||
const bindings = require("bindings")("kzg");
|
const bindings = require("bindings")("kzg");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: These two paths are only exported for testing purposes. They are not
|
* NOTE: This path is only exported for testing purposes. It is not announced in
|
||||||
* announced in the type file.
|
* the type file.
|
||||||
*
|
*
|
||||||
* It is critical that these paths are kept in sync with where the trusted setup
|
* It is critical that this path is kept in sync with where the trusted setup
|
||||||
* files will be found. The root setup is in the base src directory with the
|
* file will be found. The path is dictated by the `build` command in the
|
||||||
* primary library code. The dist version is dictated by the `build` command in
|
* Makefile in the bindings/node.js folder.
|
||||||
* the Makefile in the bindings/node.js folder.
|
*
|
||||||
*/
|
* This path works for two situations:
|
||||||
/**
|
* 1) Production case
|
||||||
* Check the production bundle case first.
|
|
||||||
* - this file in BUNDLE_ROOT/dist/lib/kzg.js
|
* - this file in BUNDLE_ROOT/dist/lib/kzg.js
|
||||||
* - trusted_setup in BUNDLE_ROOT/dist/deps/c-kzg/trusted_setup.txt
|
* - trusted_setup in BUNDLE_ROOT/dist/deps/c-kzg/trusted_setup.txt
|
||||||
|
*
|
||||||
|
* 2) Post `build` state before `bundle` command works
|
||||||
|
* - this file in bindings/node.js/lib/kzg.js
|
||||||
|
* - trusted_setup in bindings/node.js/deps/c-kzg/trusted_setup.txt
|
||||||
*/
|
*/
|
||||||
bindings.TRUSTED_SETUP_PATH_IN_DIST = path.resolve(__dirname, "..", "deps", "c-kzg", "trusted_setup.txt");
|
bindings.DEFAULT_TRUSTED_SETUP_PATH = path.resolve(__dirname, "..", "deps", "c-kzg", "trusted_setup.txt");
|
||||||
/**
|
|
||||||
* Check the development case second.
|
|
||||||
* - this file in REPO_ROOT/bindings/node.js/lib/kzg.js
|
|
||||||
* - trusted_setup in REPO_ROOT/src/trusted_setup.txt
|
|
||||||
*/
|
|
||||||
bindings.TRUSTED_SETUP_PATH_IN_SRC = path.resolve(__dirname, "..", "..", "..", "src", "trusted_setup.txt");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Looks in the default locations for the trusted setup file. This is for cases
|
|
||||||
* where the library is loaded without passing a trusted setup. Should only be
|
|
||||||
* used for cases where the Ethereum official mainnet KZG setup is acceptable.
|
|
||||||
*
|
|
||||||
* @returns {string | undefined} - Filepath for trusted_setup.txt if found
|
|
||||||
*/
|
|
||||||
function getDefaultTrustedSetupFilepath() {
|
|
||||||
const locationsToSearch = [
|
|
||||||
// check the production case first
|
|
||||||
bindings.TRUSTED_SETUP_PATH_IN_DIST,
|
|
||||||
// check the development in-repo case second
|
|
||||||
bindings.TRUSTED_SETUP_PATH_IN_SRC,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const filepath of locationsToSearch) {
|
|
||||||
if (fs.existsSync(filepath)) {
|
|
||||||
return filepath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts JSON formatted trusted setup into the native format that
|
* Converts JSON formatted trusted setup into the native format that
|
||||||
|
@ -101,8 +76,8 @@ bindings.getTrustedSetupFilepath = function getTrustedSetupFilepath(filePath) {
|
||||||
throw new Error(`No trusted setup found: ${filePath}`);
|
throw new Error(`No trusted setup found: ${filePath}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
filePath = getDefaultTrustedSetupFilepath();
|
filePath = bindings.DEFAULT_TRUSTED_SETUP_PATH;
|
||||||
if (!filePath) {
|
if (!fs.existsSync(filePath)) {
|
||||||
throw new Error("Default trusted setup not found. Must pass a valid filepath to load c-kzg library");
|
throw new Error("Default trusted setup not found. Must pass a valid filepath to load c-kzg library");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import {randomBytes} from "crypto";
|
import {randomBytes} from "crypto";
|
||||||
import {readFileSync, existsSync, cpSync, rmSync} from "fs";
|
import {readFileSync, existsSync} from "fs";
|
||||||
import {resolve} from "path";
|
import {resolve} from "path";
|
||||||
import {globSync} from "glob";
|
import {globSync} from "glob";
|
||||||
|
|
||||||
|
@ -27,8 +27,7 @@ const {
|
||||||
} = kzg;
|
} = kzg;
|
||||||
// not exported by types, only exported for testing purposes
|
// not exported by types, only exported for testing purposes
|
||||||
const getTrustedSetupFilepath = (kzg as any).getTrustedSetupFilepath as (filePath?: string) => string;
|
const getTrustedSetupFilepath = (kzg as any).getTrustedSetupFilepath as (filePath?: string) => string;
|
||||||
const TRUSTED_SETUP_PATH_IN_DIST = (kzg as any).TRUSTED_SETUP_PATH_IN_DIST as string;
|
const DEFAULT_TRUSTED_SETUP_PATH = (kzg as any).DEFAULT_TRUSTED_SETUP_PATH as string;
|
||||||
const TRUSTED_SETUP_PATH_IN_SRC = (kzg as any).TRUSTED_SETUP_PATH_IN_SRC as string;
|
|
||||||
|
|
||||||
const TEST_SETUP_FILE_PATH_JSON = resolve(__dirname, "__fixtures__", "trusted_setup.json");
|
const TEST_SETUP_FILE_PATH_JSON = resolve(__dirname, "__fixtures__", "trusted_setup.json");
|
||||||
const TEST_SETUP_FILE_PATH_TXT = resolve(__dirname, "__fixtures__", "trusted_setup.txt");
|
const TEST_SETUP_FILE_PATH_TXT = resolve(__dirname, "__fixtures__", "trusted_setup.txt");
|
||||||
|
@ -188,20 +187,13 @@ describe("C-KZG", () => {
|
||||||
expect(getTrustedSetupFilepath(TEST_SETUP_FILE_PATH_TXT)).toEqual(TEST_SETUP_FILE_PATH_TXT);
|
expect(getTrustedSetupFilepath(TEST_SETUP_FILE_PATH_TXT)).toEqual(TEST_SETUP_FILE_PATH_TXT);
|
||||||
});
|
});
|
||||||
describe("default setups", () => {
|
describe("default setups", () => {
|
||||||
beforeEach(() => {
|
beforeAll(() => {
|
||||||
if (!existsSync(TRUSTED_SETUP_PATH_IN_DIST)) {
|
if (!existsSync(DEFAULT_TRUSTED_SETUP_PATH)) {
|
||||||
cpSync(TRUSTED_SETUP_PATH_IN_SRC, TRUSTED_SETUP_PATH_IN_DIST);
|
throw new Error("Default deps/c-kzg/trusted_setup.txt not found for testing");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
it("should return dist setup first", () => {
|
it("should return default trusted_setup filepath", () => {
|
||||||
// both files should be preset right now
|
expect(getTrustedSetupFilepath()).toEqual(DEFAULT_TRUSTED_SETUP_PATH);
|
||||||
expect(getTrustedSetupFilepath()).toEqual(TRUSTED_SETUP_PATH_IN_DIST);
|
|
||||||
});
|
|
||||||
it("should return src setup if dist is missing", () => {
|
|
||||||
// both files should be preset right now
|
|
||||||
rmSync(TRUSTED_SETUP_PATH_IN_DIST);
|
|
||||||
expect(getTrustedSetupFilepath()).toEqual(TRUSTED_SETUP_PATH_IN_SRC);
|
|
||||||
cpSync(TRUSTED_SETUP_PATH_IN_SRC, TRUSTED_SETUP_PATH_IN_DIST);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue