Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: simplify how default setup is found #424

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 15 additions & 40 deletions bindings/node.js/lib/kzg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,23 @@ const path = require("path");
const bindings = require("bindings")("kzg");

/**
* NOTE: These two paths are only exported for testing purposes. They are not
* announced in the type file.
* NOTE: This path is only exported for testing purposes. It is not announced in
* the type file.
*
* It is critical that these paths are kept in sync with where the trusted setup
* files will be found. The root setup is in the base src directory with the
* primary library code. The dist version is dictated by the `build` command in
* the Makefile in the bindings/node.js folder.
*/
/**
* Check the production bundle case first.
* It is critical that this path is kept in sync with where the trusted setup
* file will be found. The path is dictated by the `build` command in the
* Makefile in the bindings/node.js folder.
*
* This path works for two situations:
* 1) Production case
* - this file in BUNDLE_ROOT/dist/lib/kzg.js
* - 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");
/**
* 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;
}
}
}
bindings.DEFAULT_TRUSTED_SETUP_PATH = path.resolve(__dirname, "..", "deps", "c-kzg", "trusted_setup.txt");

/**
* Converts JSON formatted trusted setup into the native format that
Expand Down Expand Up @@ -101,8 +76,8 @@ bindings.getTrustedSetupFilepath = function getTrustedSetupFilepath(filePath) {
throw new Error(`No trusted setup found: ${filePath}`);
}
} else {
filePath = getDefaultTrustedSetupFilepath();
if (!filePath) {
filePath = bindings.DEFAULT_TRUSTED_SETUP_PATH;
if (!fs.existsSync(filePath)) {
throw new Error("Default trusted setup not found. Must pass a valid filepath to load c-kzg library");
}
}
Expand Down
22 changes: 7 additions & 15 deletions bindings/node.js/test/kzg.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {randomBytes} from "crypto";
import {readFileSync, existsSync, cpSync, rmSync} from "fs";
import {readFileSync, existsSync} from "fs";
import {resolve} from "path";
import {globSync} from "glob";

Expand Down Expand Up @@ -27,8 +27,7 @@ const {
} = kzg;
// not exported by types, only exported for testing purposes
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 TRUSTED_SETUP_PATH_IN_SRC = (kzg as any).TRUSTED_SETUP_PATH_IN_SRC as string;
const DEFAULT_TRUSTED_SETUP_PATH = (kzg as any).DEFAULT_TRUSTED_SETUP_PATH as string;

const TEST_SETUP_FILE_PATH_JSON = resolve(__dirname, "__fixtures__", "trusted_setup.json");
const TEST_SETUP_FILE_PATH_TXT = resolve(__dirname, "__fixtures__", "trusted_setup.txt");
Expand Down Expand Up @@ -188,20 +187,13 @@ describe("C-KZG", () => {
expect(getTrustedSetupFilepath(TEST_SETUP_FILE_PATH_TXT)).toEqual(TEST_SETUP_FILE_PATH_TXT);
});
describe("default setups", () => {
beforeEach(() => {
if (!existsSync(TRUSTED_SETUP_PATH_IN_DIST)) {
cpSync(TRUSTED_SETUP_PATH_IN_SRC, TRUSTED_SETUP_PATH_IN_DIST);
beforeAll(() => {
if (!existsSync(DEFAULT_TRUSTED_SETUP_PATH)) {
throw new Error("Default deps/c-kzg/trusted_setup.txt not found for testing");
}
});
it("should return dist setup first", () => {
// both files should be preset right now
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);
it("should return default trusted_setup filepath", () => {
expect(getTrustedSetupFilepath()).toEqual(DEFAULT_TRUSTED_SETUP_PATH);
});
});
});
Expand Down
Loading