Merge pull request #8 from dgcoffman/dgc/node-binding-linux-test-harness

Node bindings Linux test harness
This commit is contained in:
Ramana Kumar 2022-11-08 21:08:24 +00:00 committed by GitHub
commit 51912eabf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 11 deletions

View File

@ -15,3 +15,6 @@ rollup.config.js
tsconfig.json tsconfig.json
babel.config.js babel.config.js
*.bak *.bak
Dockerfile
binding.dist.gyp
Makefile

View File

@ -0,0 +1,20 @@
# Exists as a test harness for building and running tests in Linux
FROM node:16
COPY ./dist/ /app/dist/
COPY test.ts /app
COPY trusted_setup.txt /app
COPY kzg.ts /app
COPY kzg.cxx /app
COPY package.json /app
COPY tsconfig.json /app
COPY babel.config.js /app
COPY jest.config.js /app
COPY binding.dist.gyp /app/binding.gyp
WORKDIR /app
RUN yarn install
CMD ["yarn", "jest"]

View File

@ -29,3 +29,10 @@ publish: bundle
mv binding.gyp binding.gyp.bak mv binding.gyp binding.gyp.bak
cp binding.dist.gyp binding.gyp cp binding.dist.gyp binding.gyp
npm publish && mv binding.gyp.bak binding.gyp || mv binding.gyp.bak binding.gyp npm publish && mv binding.gyp.bak binding.gyp || mv binding.gyp.bak binding.gyp
linux-test: bundle
cp ../../src/trusted_setup.txt .
docker build -t "linux-test" .
rm trusted_setup.txt
docker logs --follow `docker run -d linux-test`

View File

@ -16,8 +16,8 @@
"<!@(node -p \"require('node-addon-api').include\")" "<!@(node -p \"require('node-addon-api').include\")"
], ],
"libraries": [ "libraries": [
"<(module_root_dir)/libblst.a", "<(module_root_dir)/c_kzg_4844.o",
"<(module_root_dir)/c_kzg_4844.o" "<(module_root_dir)/libblst.a"
], ],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"], "dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"], "defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
@ -53,6 +53,10 @@
{ {
"files": ["./build/Release/kzg.node"], "files": ["./build/Release/kzg.node"],
"destination": "./dist" "destination": "./dist"
},
{
"files": ["./build/Release/kzg.node"],
"destination": "./"
} }
] ]
} }

View File

@ -16,8 +16,8 @@
"<!@(node -p \"require('node-addon-api').include\")" "<!@(node -p \"require('node-addon-api').include\")"
], ],
"libraries": [ "libraries": [
"<(module_root_dir)/c_kzg_4844.o", "<(module_root_dir)/../../lib/libblst.a",
"<(module_root_dir)/../../lib/libblst.a" "<(module_root_dir)/c_kzg_4844.o"
], ],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"], "dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"] "defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]

View File

@ -155,6 +155,7 @@ Napi::Value ComputeAggregateKzgProof(const Napi::CallbackInfo& info) {
auto kzg_settings = info[1].As<Napi::External<KZGSettings>>().Data(); auto kzg_settings = info[1].As<Napi::External<KZGSettings>>().Data();
auto blobs_count = blobs_param.Length(); auto blobs_count = blobs_param.Length();
auto blobs = (Blob*)calloc(blobs_count, sizeof(Blob)); auto blobs = (Blob*)calloc(blobs_count, sizeof(Blob));
for (uint32_t blob_index = 0; blob_index < blobs_count; blob_index++) { for (uint32_t blob_index = 0; blob_index < blobs_count; blob_index++) {

View File

@ -1,6 +1,6 @@
{ {
"name": "c-kzg", "name": "c-kzg",
"version": "0.0.7", "version": "0.0.9",
"description": "NodeJS bindings for C-KZG", "description": "NodeJS bindings for C-KZG",
"author": "Dan Coffman", "author": "Dan Coffman",
"license": "MIT", "license": "MIT",
@ -12,12 +12,14 @@
"@rollup/plugin-typescript": "^9.0.2", "@rollup/plugin-typescript": "^9.0.2",
"@types/jest": "^29.2.1", "@types/jest": "^29.2.1",
"jest": "^29.2.2", "jest": "^29.2.2",
"node-addon-api": "^5.0.0",
"node-gyp": "^9.3.0", "node-gyp": "^9.3.0",
"prettier": "2.7.1", "prettier": "2.7.1",
"rollup": "^3.2.5", "rollup": "^3.2.5",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"tslib": "^2.4.1", "tslib": "^2.4.1",
"typescript": "^4.8.4" "typescript": "^4.8.4"
},
"dependencies": {
"node-addon-api": "^5.0.0"
} }
} }

View File

@ -1,4 +1,6 @@
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
import { existsSync } from "fs";
import { import {
loadTrustedSetup, loadTrustedSetup,
freeTrustedSetup, freeTrustedSetup,
@ -9,7 +11,12 @@ import {
FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_BLOB,
} from "./kzg"; } from "./kzg";
const SETUP_FILE_PATH = "../../src/trusted_setup.txt"; const setupFileName = "trusted_setup.txt";
const SETUP_FILE_PATH = existsSync(setupFileName)
? setupFileName
: `../../src/${setupFileName}`;
const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT; const BLOB_BYTE_COUNT = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT)); const generateRandomBlob = () => new Uint8Array(randomBytes(BLOB_BYTE_COUNT));
@ -23,10 +30,23 @@ describe("C-KZG", () => {
freeTrustedSetup(); freeTrustedSetup();
}); });
it("computes the correct commitments and aggregate proofs from blobs", () => { it("computes the correct commitments and aggregate proof from blobs", () => {
const blobs = new Array(2).fill(0).map(generateRandomBlob); let blobs = new Array(2).fill(0).map(generateRandomBlob);
const commitments = blobs.map(blobToKzgCommitment); let commitments = blobs.map(blobToKzgCommitment);
const proof = computeAggregateKzgProof(blobs); let proof = computeAggregateKzgProof(blobs);
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true);
});
it("throws an error when blobs is an empty array", () => {
expect(() => computeAggregateKzgProof([])).toThrowError(
"Failed to compute proof",
);
});
it("computes the aggregate proof when for a single blob", () => {
let blobs = new Array(1).fill(0).map(generateRandomBlob);
let commitments = blobs.map(blobToKzgCommitment);
let proof = computeAggregateKzgProof(blobs);
expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true); expect(verifyAggregateKzgProof(blobs, commitments, proof)).toBe(true);
}); });