From 155421cb1b7f49472742a95564bb649ef971b240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 5 Sep 2019 15:03:03 +0200 Subject: [PATCH 01/13] add 0.8.3 spec test dir --- .gitmodules | 2 +- packages/spec-test-cases | 2 +- yarn.lock | 9 --------- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.gitmodules b/.gitmodules index 65bd94b93830..3ab95dd10f26 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "spec-test-cases-module"] path = packages/spec-test-cases url = https://github.com/ChainSafe/eth2.0-spec-tests.git - branch = v0.8.1 + branch = v0.8.3 diff --git a/packages/spec-test-cases b/packages/spec-test-cases index aaa1673f5081..ae6dd9011df0 160000 --- a/packages/spec-test-cases +++ b/packages/spec-test-cases @@ -1 +1 @@ -Subproject commit aaa1673f508103e11304833e0456e4149f880065 +Subproject commit ae6dd9011df05fab8c7e651c09cf9c940973bf81 diff --git a/yarn.lock b/yarn.lock index 37c2b3505263..9420e9666558 100644 --- a/yarn.lock +++ b/yarn.lock @@ -652,15 +652,6 @@ version "0.1.3" resolved "https://registry.yarnpkg.com/@chainsafe/bit-utils/-/bit-utils-0.1.3.tgz#d29a69c161526fda8bf0880da3d62a4a7100e169" -"@chainsafe/eth2.0-spec-test-util@^0.2.3": - version "0.2.3" - resolved "https://registry.yarnpkg.com/@chainsafe/eth2.0-spec-test-util/-/eth2.0-spec-test-util-0.2.3.tgz#f9223de4d61774f2483932fb608fc0da29eb04a0" - dependencies: - bn.js "^4.11.8" - camelcase "^5.3.1" - chai "^4.2.0" - js-yaml "^3.13.1" - "@chainsafe/milagro-crypto-js@0.1.3": version "0.1.3" resolved "https://registry.yarnpkg.com/@chainsafe/milagro-crypto-js/-/milagro-crypto-js-0.1.3.tgz#0813b5991f53e9573174637ce9ec96576218a09b" From dac2c42224be3618b3305c7024388bb9496dec9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 5 Sep 2019 15:04:38 +0200 Subject: [PATCH 02/13] fix bug in spec test util --- packages/eth2.0-spec-test-util/src/single.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/eth2.0-spec-test-util/src/single.ts b/packages/eth2.0-spec-test-util/src/single.ts index 837beb60e495..041876ccfd48 100644 --- a/packages/eth2.0-spec-test-util/src/single.ts +++ b/packages/eth2.0-spec-test-util/src/single.ts @@ -8,6 +8,7 @@ import {schema} from "./yaml/schema"; import {expect} from 'chai'; import deepMerge from "deepmerge"; import profiler from "v8-profiler-next"; +import path from "path"; export enum InputType { @@ -34,7 +35,7 @@ export interface ISpecTestOptions { * Optionally pass function to transform loaded values * (values from input files) */ - inputProcessing?: {[K in keyof TestCase]: (value: any) => any}; + inputProcessing?: {[K: string]: (value: any) => any}; shouldError?: (testCase: TestCase) => boolean; @@ -131,7 +132,7 @@ function loadInputFiles( if(isDirectory(file)) { return false; } - const extension = options.inputTypes[basename(file)] || InputType.SSZ; + const extension = options.inputTypes[path.parse(file).name] || InputType.SSZ; return file.endsWith(extension); }) .forEach((file) => { From 294412f8f685f80da8f9f8a26a1fe948898205b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 5 Sep 2019 15:06:46 +0200 Subject: [PATCH 03/13] upgrade bls tests to 0.8.3 --- packages/bls/package.json | 2 +- .../bls/test/spec/aggregate_pubkeys.test.ts | 44 +++++++++-------- packages/bls/test/spec/aggregate_sigs.test.ts | 42 +++++++++------- packages/bls/test/spec/g2_compressed.test.ts | 23 --------- .../bls/test/spec/g2_uncompressed.test.ts | 28 ----------- packages/bls/test/spec/msg_hash_compressed.ts | 40 ++++++++++++++++ .../bls/test/spec/msg_hash_uncompressed.ts | 35 ++++++++++++++ packages/bls/test/spec/priv_to_public.test.ts | 33 ++++++++----- packages/bls/test/spec/sign_message.test.ts | 48 ++++++++++++------- 9 files changed, 178 insertions(+), 117 deletions(-) delete mode 100644 packages/bls/test/spec/g2_compressed.test.ts delete mode 100644 packages/bls/test/spec/g2_uncompressed.test.ts create mode 100644 packages/bls/test/spec/msg_hash_compressed.ts create mode 100644 packages/bls/test/spec/msg_hash_uncompressed.ts diff --git a/packages/bls/package.json b/packages/bls/package.json index 6846aa8b1d00..e3f3e9e1c8b8 100644 --- a/packages/bls/package.json +++ b/packages/bls/package.json @@ -52,7 +52,7 @@ "@babel/register": "^7.0.0", "@babel/runtime": "^7.4.4", "@chainsafe/benchmark-utils": "^0.1.0", - "@chainsafe/eth2.0-spec-test-util": "^0.2.3", + "@chainsafe/eth2.0-spec-test-util": "0.4.1", "@types/assert": "^1.4.2", "@types/chai": "^4.1.7", "@types/mocha": "^5.2.5", diff --git a/packages/bls/test/spec/aggregate_pubkeys.test.ts b/packages/bls/test/spec/aggregate_pubkeys.test.ts index 09a911f03616..d90533169501 100644 --- a/packages/bls/test/spec/aggregate_pubkeys.test.ts +++ b/packages/bls/test/spec/aggregate_pubkeys.test.ts @@ -1,21 +1,27 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; -import bls from "../../src"; -import {BLSSignature} from "@chainsafe/eth2.0-types"; +import bls, {PublicKey} from "../../src"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import path from "path"; -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/aggregate_pubkeys/aggregate_pubkeys.yaml"), - bls.aggregatePubkeys, - ({input}) => { - const sigs: BLSSignature[] = []; - input.forEach((sig: string) => { - sigs.push(Buffer.from(sig.replace('0x', ''), 'hex')); - }); - return [ - sigs - ]; - }, - ({output}) => output, - (result) => `0x${result.toString('hex')}`, - () => false, +interface AggregatePubKeysTestCase { + data: { + input: string[]; + output: string; + }; +} + +describeDirectorySpecTest( + "aggregate pubkeys", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/aggregate_pubkeys/small"), + (testCase => { + const result = bls.aggregatePubkeys(testCase.data.input.map(pubKey => { + return Buffer.from(pubKey.replace("0x", ""), "hex"); + })); + return `0x${result.toString('hex')}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } ); diff --git a/packages/bls/test/spec/aggregate_sigs.test.ts b/packages/bls/test/spec/aggregate_sigs.test.ts index 4a494a207aab..4df88dfd6435 100644 --- a/packages/bls/test/spec/aggregate_sigs.test.ts +++ b/packages/bls/test/spec/aggregate_sigs.test.ts @@ -1,21 +1,27 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; +import path from "path"; import bls from "../../src"; -import {BLSPubkey} from "../../src/types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/aggregate_sigs/aggregate_sigs.yaml"), - bls.aggregateSignatures, - ({input}) => { - const pubKeys: BLSPubkey[] = []; - input.forEach((pubKey: string) => { - pubKeys.push(Buffer.from(pubKey.replace('0x', ''), 'hex')); - }); - return [ - pubKeys - ]; - }, - ({output}) => output, - (result) => `0x${result.toString('hex')}`, - () => false, +interface AggregateSigsTestCase { + data: { + input: string[]; + output: string; + }; +} + +describeDirectorySpecTest( + "aggregate sigs", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/aggregate_sigs/small"), + (testCase => { + const result = bls.aggregateSignatures(testCase.data.input.map(pubKey => { + return Buffer.from(pubKey.replace("0x", ""), "hex"); + })); + return `0x${result.toString('hex')}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } ); diff --git a/packages/bls/test/spec/g2_compressed.test.ts b/packages/bls/test/spec/g2_compressed.test.ts deleted file mode 100644 index e0cf37fac3d5..000000000000 --- a/packages/bls/test/spec/g2_compressed.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; -import {padLeft} from "../../src/helpers/utils"; -import {G2point} from "../../src/helpers/g2point"; - -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/msg_hash_g2_compressed/g2_compressed.yaml"), - G2point.hashToG2, - ({input}) => { - const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8); - return [ - Buffer.from(input.message.replace('0x', ''), 'hex'), - domain - ]; - }, - ({output}) => { - const xReExpected = padLeft(Buffer.from(output[0].replace('0x', ''), 'hex'), 48); - const xImExpected = padLeft(Buffer.from(output[1].replace('0x', ''), 'hex'), 48); - return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex'); - }, - (result: G2point) => `0x${result.toBytesCompressed().toString('hex')}`, - () => false, -); diff --git a/packages/bls/test/spec/g2_uncompressed.test.ts b/packages/bls/test/spec/g2_uncompressed.test.ts deleted file mode 100644 index ec9550b9406f..000000000000 --- a/packages/bls/test/spec/g2_uncompressed.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; -import {padLeft} from "../../src/helpers/utils"; -import {G2point} from "../../src/helpers/g2point"; - -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/msg_hash_g2_uncompressed/g2_uncompressed.yaml"), - G2point.hashToG2, - ({input}) => { - const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8); - return [ - Buffer.from(input.message.replace('0x', ''), 'hex'), - domain - ]; - }, - ({output}) => { - return '0x' + G2point.fromUncompressedInput( - Buffer.from(output[0][0].replace('0x', ''), 'hex'), - Buffer.from(output[0][1].replace('0x', ''), 'hex'), - Buffer.from(output[1][0].replace('0x', ''), 'hex'), - Buffer.from(output[1][1].replace('0x', ''), 'hex'), - Buffer.from(output[2][0].replace('0x', ''), 'hex'), - Buffer.from(output[2][1].replace('0x', ''), 'hex'), - ).toBytesCompressed().toString('hex'); - }, - (result: G2point) => `0x${result.toBytesCompressed().toString('hex')}`, - () => false, -); diff --git a/packages/bls/test/spec/msg_hash_compressed.ts b/packages/bls/test/spec/msg_hash_compressed.ts new file mode 100644 index 000000000000..a5d738c9ab9a --- /dev/null +++ b/packages/bls/test/spec/msg_hash_compressed.ts @@ -0,0 +1,40 @@ +import path from "path"; +import {padLeft} from "../../src/helpers/utils"; +import {G2point} from "../../src/helpers/g2point"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; + +interface MsgHHashUnCompressed { + data: { + input: { + message: string, + domain: string + }; + output: string[][]; + }; +} + +describeDirectorySpecTest( + "msg_hash_uncompressed", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_uncompressed/small"), + (testCase => { + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); + const input = Buffer.from(testCase.data.input.message, "hex"); + const result = G2point.hashToG2(input, domain); + return `0x${result.toBytesCompressed().toString('hex')}` + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => { + return '0x' + G2point.fromUncompressedInput( + Buffer.from(testCase.data.output[0][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[0][1].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[1][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[1][1].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[2][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[2][1].replace('0x', ''), 'hex'), + ).toBytesCompressed().toString('hex'); + }) + } +); \ No newline at end of file diff --git a/packages/bls/test/spec/msg_hash_uncompressed.ts b/packages/bls/test/spec/msg_hash_uncompressed.ts new file mode 100644 index 000000000000..3a9b2f6b012b --- /dev/null +++ b/packages/bls/test/spec/msg_hash_uncompressed.ts @@ -0,0 +1,35 @@ +import path from "path"; +import {padLeft} from "../../src/helpers/utils"; +import {G2point} from "../../src/helpers/g2point"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; + +interface MsgHHashCOmpressed { + data: { + input: { + message: string, + domain: string + }; + output: string[]; + }; +} + +describeDirectorySpecTest( + "msg_hash_compressed", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_compressed/small"), + (testCase => { + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); + const input = Buffer.from(testCase.data.input.message, "hex"); + const result = G2point.hashToG2(input, domain); + return `0x${result.toBytesCompressed().toString('hex')}` + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => { + const xReExpected = padLeft(Buffer.from(testCase.data.output[0].replace('0x', ''), 'hex'), 48); + const xImExpected = padLeft(Buffer.from(testCase.data.output[1].replace('0x', ''), 'hex'), 48); + return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex'); + }) + } +); \ No newline at end of file diff --git a/packages/bls/test/spec/priv_to_public.test.ts b/packages/bls/test/spec/priv_to_public.test.ts index 75b08f8d2796..edd6c0d8f3c8 100644 --- a/packages/bls/test/spec/priv_to_public.test.ts +++ b/packages/bls/test/spec/priv_to_public.test.ts @@ -1,14 +1,25 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; import bls from "../../src"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import path from "path"; -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/priv_to_pub/priv_to_pub.yaml"), - bls.generatePublicKey, - ({input}) => { - return [Buffer.from(input.replace('0x', ''), 'hex')]; - }, - ({output}) => output, - (result) => `0x${result.toString('hex')}`, - () => false, +interface PrivToPubTestCase { + data: { + input: string; + output: string; + }; +} + +describeDirectorySpecTest( + "priv_to_pub", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/priv_to_pub/small"), + (testCase => { + const result = bls.generatePublicKey(Buffer.from(testCase.data.input.replace('0x', ''), 'hex')); + return `0x${result.toString('hex')}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } ); diff --git a/packages/bls/test/spec/sign_message.test.ts b/packages/bls/test/spec/sign_message.test.ts index 11178386cd6b..ffabf540f0e1 100644 --- a/packages/bls/test/spec/sign_message.test.ts +++ b/packages/bls/test/spec/sign_message.test.ts @@ -1,20 +1,34 @@ -import {join} from "path"; -import {describeSpecTest} from "@chainsafe/eth2.0-spec-test-util"; +import path from "path"; import bls from "../../src"; import {padLeft} from "../../src/helpers/utils"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; -describeSpecTest( - join(__dirname, "../../../spec-test-cases/tests/bls/sign_msg/sign_msg.yaml"), - bls.sign, - ({input}) => { - const domain = padLeft(Buffer.from(input.domain.replace('0x', ''), 'hex'), 8); - return [ - Buffer.from(input.privkey.replace('0x', ''), 'hex'), - Buffer.from(input.message.replace('0x', ''), 'hex'), - domain - ]; - }, - ({output}) => output, - (result) => `0x${result.toString('hex')}`, - () => false, -); +interface SignMessageTestCase { + data: { + input: { + privkey: string, + message: string, + domain: string + }; + output: string; + }; +} + +describeDirectorySpecTest( + "priv_to_pub", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/sign_msg/small"), + (testCase => { + const signature = bls.sign( + Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"), + Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"), + padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8) + ); + return `0x${signature.toString("hex")}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } +); \ No newline at end of file From 0d9e2625e12ff215c329bf7c45c1b490c12c2a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 5 Sep 2019 18:51:27 -0400 Subject: [PATCH 04/13] working bls on 0.8.3 tests --- packages/bls/package.json | 4 +- .../bls/test/spec/aggregate_pubkeys.test.ts | 2 +- .../bls/test/spec/msg_hash_compressed.test.ts | 35 ++++++++++++++ packages/bls/test/spec/msg_hash_compressed.ts | 40 ---------------- .../test/spec/msg_hash_uncompressed.test.ts | 40 ++++++++++++++++ .../bls/test/spec/msg_hash_uncompressed.ts | 35 -------------- packages/bls/test/spec/sign_message.test.ts | 46 +++++++++---------- packages/eth2.0-spec-test-util/package.json | 6 +-- 8 files changed, 104 insertions(+), 104 deletions(-) create mode 100644 packages/bls/test/spec/msg_hash_compressed.test.ts delete mode 100644 packages/bls/test/spec/msg_hash_compressed.ts create mode 100644 packages/bls/test/spec/msg_hash_uncompressed.test.ts delete mode 100644 packages/bls/test/spec/msg_hash_uncompressed.ts diff --git a/packages/bls/package.json b/packages/bls/package.json index e3f3e9e1c8b8..a02a004434b6 100644 --- a/packages/bls/package.json +++ b/packages/bls/package.json @@ -28,7 +28,7 @@ "pretest": "yarn check-types", "prepublishOnly": "yarn build", "test:unit": "nyc --cache-dir .nyc_output/.cache -r lcov -e .ts mocha --colors -r ./.babel-register 'test/unit/**/*.test.ts' && nyc report", - "test:spec": "mocha --colors -r ./.babel-register 'test/spec/**/*.test.ts'", + "test:spec": "mocha --colors -r ts-node/register 'test/spec/**/*.test.ts'", "test:spec-min": "yarn run test:spec", "test": "yarn test:unit && yarn test:spec", "coverage": "codecov -F bls", @@ -64,7 +64,7 @@ "codecov": "^3.1.0", "eslint": "^5.14.1", "js-yaml": "^3.13.1", - "mocha": "^5.2.0", + "mocha": "^6.2.0", "nyc": "^13.3.0", "sinon": "^7.2.7", "supertest": "^4.0.2", diff --git a/packages/bls/test/spec/aggregate_pubkeys.test.ts b/packages/bls/test/spec/aggregate_pubkeys.test.ts index d90533169501..76d7b03f28c5 100644 --- a/packages/bls/test/spec/aggregate_pubkeys.test.ts +++ b/packages/bls/test/spec/aggregate_pubkeys.test.ts @@ -1,4 +1,4 @@ -import bls, {PublicKey} from "../../src"; +import bls from "../../src"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; import path from "path"; diff --git a/packages/bls/test/spec/msg_hash_compressed.test.ts b/packages/bls/test/spec/msg_hash_compressed.test.ts new file mode 100644 index 000000000000..31b81a7eb442 --- /dev/null +++ b/packages/bls/test/spec/msg_hash_compressed.test.ts @@ -0,0 +1,35 @@ +import path from "path"; +import {padLeft} from "../../src/helpers/utils"; +import {G2point} from "../../src/helpers/g2point"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; + +interface MsgHHashCOmpressed { + data: { + input: { + message: string; + domain: string; + }; + output: string[]; + }; +} + +describeDirectorySpecTest( + "msg_hash_compressed", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_compressed/small"), + (testCase => { + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); + const input = Buffer.from(testCase.data.input.message.replace('0x', ''), "hex"); + const result = G2point.hashToG2(input, domain); + return `0x${result.toBytesCompressed().toString('hex')}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => { + const xReExpected = padLeft(Buffer.from(testCase.data.output[0].replace('0x', ''), 'hex'), 48); + const xImExpected = padLeft(Buffer.from(testCase.data.output[1].replace('0x', ''), 'hex'), 48); + return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex'); + }) + } +); \ No newline at end of file diff --git a/packages/bls/test/spec/msg_hash_compressed.ts b/packages/bls/test/spec/msg_hash_compressed.ts deleted file mode 100644 index a5d738c9ab9a..000000000000 --- a/packages/bls/test/spec/msg_hash_compressed.ts +++ /dev/null @@ -1,40 +0,0 @@ -import path from "path"; -import {padLeft} from "../../src/helpers/utils"; -import {G2point} from "../../src/helpers/g2point"; -import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; - -interface MsgHHashUnCompressed { - data: { - input: { - message: string, - domain: string - }; - output: string[][]; - }; -} - -describeDirectorySpecTest( - "msg_hash_uncompressed", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_uncompressed/small"), - (testCase => { - const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); - const input = Buffer.from(testCase.data.input.message, "hex"); - const result = G2point.hashToG2(input, domain); - return `0x${result.toBytesCompressed().toString('hex')}` - }), - { - inputTypes: { - data: InputType.YAML, - }, - getExpected: (testCase => { - return '0x' + G2point.fromUncompressedInput( - Buffer.from(testCase.data.output[0][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[0][1].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[1][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[1][1].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[2][0].replace('0x', ''), 'hex'), - Buffer.from(testCase.data.output[2][1].replace('0x', ''), 'hex'), - ).toBytesCompressed().toString('hex'); - }) - } -); \ No newline at end of file diff --git a/packages/bls/test/spec/msg_hash_uncompressed.test.ts b/packages/bls/test/spec/msg_hash_uncompressed.test.ts new file mode 100644 index 000000000000..b5899090c842 --- /dev/null +++ b/packages/bls/test/spec/msg_hash_uncompressed.test.ts @@ -0,0 +1,40 @@ +import path from "path"; +import {padLeft} from "../../src/helpers/utils"; +import {G2point} from "../../src/helpers/g2point"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; + +interface MsgHHashUnCompressed { + data: { + input: { + message: string; + domain: string; + }; + output: string[][]; + }; +} + +describeDirectorySpecTest( + "msg_hash_uncompressed", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_uncompressed/small"), + (testCase => { + const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); + const input = Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"); + const result = G2point.hashToG2(input, domain); + return `0x${result.toBytesCompressed().toString('hex')}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => { + return '0x' + G2point.fromUncompressedInput( + Buffer.from(testCase.data.output[0][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[0][1].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[1][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[1][1].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[2][0].replace('0x', ''), 'hex'), + Buffer.from(testCase.data.output[2][1].replace('0x', ''), 'hex'), + ).toBytesCompressed().toString('hex'); + }) + } +); \ No newline at end of file diff --git a/packages/bls/test/spec/msg_hash_uncompressed.ts b/packages/bls/test/spec/msg_hash_uncompressed.ts deleted file mode 100644 index 3a9b2f6b012b..000000000000 --- a/packages/bls/test/spec/msg_hash_uncompressed.ts +++ /dev/null @@ -1,35 +0,0 @@ -import path from "path"; -import {padLeft} from "../../src/helpers/utils"; -import {G2point} from "../../src/helpers/g2point"; -import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; - -interface MsgHHashCOmpressed { - data: { - input: { - message: string, - domain: string - }; - output: string[]; - }; -} - -describeDirectorySpecTest( - "msg_hash_compressed", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/msg_hash_compressed/small"), - (testCase => { - const domain = padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8); - const input = Buffer.from(testCase.data.input.message, "hex"); - const result = G2point.hashToG2(input, domain); - return `0x${result.toBytesCompressed().toString('hex')}` - }), - { - inputTypes: { - data: InputType.YAML, - }, - getExpected: (testCase => { - const xReExpected = padLeft(Buffer.from(testCase.data.output[0].replace('0x', ''), 'hex'), 48); - const xImExpected = padLeft(Buffer.from(testCase.data.output[1].replace('0x', ''), 'hex'), 48); - return '0x' + Buffer.concat([xReExpected, xImExpected]).toString('hex'); - }) - } -); \ No newline at end of file diff --git a/packages/bls/test/spec/sign_message.test.ts b/packages/bls/test/spec/sign_message.test.ts index ffabf540f0e1..5aebb9e9999c 100644 --- a/packages/bls/test/spec/sign_message.test.ts +++ b/packages/bls/test/spec/sign_message.test.ts @@ -4,31 +4,31 @@ import {padLeft} from "../../src/helpers/utils"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; interface SignMessageTestCase { - data: { - input: { - privkey: string, - message: string, - domain: string - }; - output: string; + data: { + input: { + privkey: string; + message: string; + domain: string; }; + output: string; + }; } describeDirectorySpecTest( - "priv_to_pub", - path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/sign_msg/small"), - (testCase => { - const signature = bls.sign( - Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"), - Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"), - padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8) - ); - return `0x${signature.toString("hex")}`; - }), - { - inputTypes: { - data: InputType.YAML, - }, - getExpected: (testCase => testCase.data.output) - } + "priv_to_pub", + path.join(__dirname, "../../../spec-test-cases/tests/general/phase0/bls/sign_msg/small"), + (testCase => { + const signature = bls.sign( + Buffer.from(testCase.data.input.privkey.replace("0x", ""), "hex"), + Buffer.from(testCase.data.input.message.replace("0x", ""), "hex"), + padLeft(Buffer.from(testCase.data.input.domain.replace('0x', ''), 'hex'), 8) + ); + return `0x${signature.toString("hex")}`; + }), + { + inputTypes: { + data: InputType.YAML, + }, + getExpected: (testCase => testCase.data.output) + } ); \ No newline at end of file diff --git a/packages/eth2.0-spec-test-util/package.json b/packages/eth2.0-spec-test-util/package.json index 5c3bdb69fcb4..f2b69ebf94b4 100644 --- a/packages/eth2.0-spec-test-util/package.json +++ b/packages/eth2.0-spec-test-util/package.json @@ -44,6 +44,8 @@ "bn.js": "^5.0.0", "camelcase": "^5.3.1", "deepmerge": "^4.0.0", + "chai": "^4.2.0", + "mocha": "^6.2.0", "js-yaml": "^3.13.1", "v8-profiler-next": "^1.1.1" }, @@ -53,9 +55,7 @@ "@types/chai": "^4.1.7", "@types/deepmerge": "^2.2.0", "@types/js-yaml": "^3.12.1", - "@types/mocha": "^5.2.7", - "chai": "^4.2.0", - "mocha": "^6.2.0" + "@types/mocha": "^5.2.7" }, "peerDependencies": { "chai": "^4.2.0", From 74a69ea7bb627bd2a8ddad31010f4c6c163d8190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 5 Sep 2019 19:03:45 -0400 Subject: [PATCH 05/13] upgrade epoch processing spec tests to 0.8.3 --- .../crosslinks/crosslinks_mainnet.test.ts | 46 ++++++++++--------- .../crosslinks/crosslinks_minimal.test.ts | 44 +++++++++--------- .../final_updates_mainnet.test.ts | 40 ++++++++-------- .../final_updates_minimal.test.ts | 40 ++++++++-------- ...ification_and_finalization_mainnet.test.ts | 40 ++++++++-------- ...ification_and_finalization_minimal.test.ts | 40 ++++++++-------- .../registry_updates_mainnet.test.ts | 42 +++++++++-------- .../registry_updates_minimal.test.ts | 40 ++++++++-------- .../slashings/slashings_mainnet.test.ts | 42 +++++++++-------- .../slashings/slashings_minimal.test.ts | 41 +++++++++-------- .../test/utils/specTestTypes/stateTestCase.ts | 4 ++ yarn.lock | 41 ----------------- 12 files changed, 222 insertions(+), 238 deletions(-) create mode 100644 packages/lodestar/test/utils/specTestTypes/stateTestCase.ts diff --git a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts index 9547798a8dc2..e3c12a233007 100644 --- a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts @@ -1,31 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; -import {expect} from "chai"; -import {equals} from "@chainsafe/ssz"; -import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processCrosslinks} from "../../../../src/chain/stateTransition/epoch/crosslinks"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; import {BeaconState} from "@chainsafe/eth2.0-types"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {equals} from "@chainsafe/ssz"; +import {expect} from "chai"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/crosslinks/crosslinks_mainnet.yaml"), - (state) => { +describeDirectorySpecTest( + "epoch_croslinks", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/crosslinks/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processCrosslinks(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts index 502ca7899998..a4a3a86c73ab 100644 --- a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {expect} from "chai"; -import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processCrosslinks} from "../../../../src/chain/stateTransition/epoch/crosslinks"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {equals} from "@chainsafe/ssz"; +import {expect} from "chai"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/crosslinks/crosslinks_minimal.yaml"), - (state) => { +describeDirectorySpecTest( + "epoch_croslinks", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/crosslinks/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processCrosslinks(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts index aaee68391f0c..afc2396cddd0 100644 --- a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processFinalUpdates} from "../../../../src/chain/stateTransition/epoch/finalUpdates"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/final_updates/final_updates_mainnet.yaml"), - (state) => { +describeDirectorySpecTest( + "epoch_croslinks", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/final_updates/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processFinalUpdates(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - (_, i) => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts index 337002145209..365ea2be3a61 100644 --- a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processFinalUpdates} from "../../../../src/chain/stateTransition/epoch/finalUpdates"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/final_updates/final_updates_minimal.yaml"), - (state) => { +describeDirectorySpecTest( + "epoch_croslinks_min", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/final_updates/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processFinalUpdates(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - (_, i) => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts index fac6200c44e7..1a82b13720c7 100644 --- a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processJustificationAndFinalization} from "../../../../src/chain/stateTransition/epoch/justification"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/justification_and_finalization/justification_and_finalization_mainnet.yaml"), - (state) => { +describeDirectorySpecTest( + "justification and finalization", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/justification_and_finalization/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processJustificationAndFinalization(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts index 9ea38a6b04d6..b9c6cd0c953a 100644 --- a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processJustificationAndFinalization} from "../../../../src/chain/stateTransition/epoch/justification"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/justification_and_finalization/justification_and_finalization_minimal.yaml"), - (state) => { +describeDirectorySpecTest( + "justification and finalization min", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/justification_and_finalization/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processJustificationAndFinalization(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts index a93b3e83eef8..54c4e5581842 100644 --- a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; -import {processRegistryUpdates} from "../../../../src/chain/stateTransition/epoch/registryUpdates"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; +import {processRegistryUpdates} from "../../../../src/chain/stateTransition/epoch/registryUpdates"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/registry_updates/registry_updates_mainnet.yaml"), - (state) => { +describeDirectorySpecTest( + "registry updates", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/registry_updates/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processRegistryUpdates(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts index 0ff8901f45b6..afaab5d7aa39 100644 --- a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processRegistryUpdates} from "../../../../src/chain/stateTransition/epoch/registryUpdates"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/registry_updates/registry_updates_minimal.yaml"), - (state) => { +describeDirectorySpecTest( + "registry updates min", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/registry_updates/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processRegistryUpdates(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts index 17d2833709c5..8ed9039f7b5b 100644 --- a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts @@ -1,32 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; -import {processSlashings} from "../../../../src/chain/stateTransition/epoch/slashings"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; +import {processSlashings} from "../../../../src/chain/stateTransition/epoch/slashings"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/slashings/slashings_mainnet.yaml"), - (state) => { +describeDirectorySpecTest( + "slashings", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/slashings/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processSlashings(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); diff --git a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts index 16130761b01d..01a30d6111ff 100644 --- a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts @@ -1,32 +1,35 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processSlashings} from "../../../../src/chain/stateTransition/epoch/slashings"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconStateComparisonCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/epoch_processing/slashings/slashings_minimal.yaml"), - (state) => { +describeDirectorySpecTest( + "slashings min", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/slashings/pyspec_tests"), + (testcase) => { + const state = testcase.pre; processSlashings(config, state); return state; }, - (input) => { - return [expandYamlValue(input.pre, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - () => false, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - }, - 0 + { + inputTypes: { + pre: InputType.SSZ, + post: InputType.SSZ, + }, + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); + diff --git a/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts b/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts new file mode 100644 index 000000000000..db17bafea2bc --- /dev/null +++ b/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts @@ -0,0 +1,4 @@ +export interface StateTestCase { + pre: BeaconState; + post: BeaconState; +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 9420e9666558..f9911484c6bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3801,10 +3801,6 @@ combined-stream@^1.0.6, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -commander@2.15.1: - version "2.15.1" - resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" - commander@^2.19.0, commander@^2.20.0, commander@^2.8.1, commander@~2.20.0: version "2.20.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422" @@ -5820,17 +5816,6 @@ glob-to-regexp@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab" -glob@7.1.2: - version "7.1.2" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" - dependencies: - fs.realpath "^1.0.0" - inflight "^1.0.4" - inherits "2" - minimatch "^3.0.4" - once "^1.3.0" - path-is-absolute "^1.0.0" - glob@7.1.3: version "7.1.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" @@ -6075,10 +6060,6 @@ hdkey@^1.1.0: safe-buffer "^5.1.1" secp256k1 "^3.0.1" -he@1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" - he@1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f" @@ -8045,22 +8026,6 @@ mkdirp@*, mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1: dependencies: minimist "0.0.8" -mocha@^5.2.0: - version "5.2.0" - resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.2.0.tgz#6d8ae508f59167f940f2b5b3c4a612ae50c90ae6" - dependencies: - browser-stdout "1.3.1" - commander "2.15.1" - debug "3.1.0" - diff "3.5.0" - escape-string-regexp "1.0.5" - glob "7.1.2" - growl "1.10.5" - he "1.1.1" - minimatch "3.0.4" - mkdirp "0.5.1" - supports-color "5.4.0" - mocha@^6.1.4, mocha@^6.2.0: version "6.2.0" resolved "https://registry.yarnpkg.com/mocha/-/mocha-6.2.0.tgz#f896b642843445d1bb8bca60eabd9206b8916e56" @@ -10911,12 +10876,6 @@ supertest@^4.0.2: methods "^1.1.2" superagent "^3.8.3" -supports-color@5.4.0: - version "5.4.0" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54" - dependencies: - has-flag "^3.0.0" - supports-color@6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.0.0.tgz#76cfe742cf1f41bb9b1c29ad03068c05b4c0e40a" From d380e7c96dc6126d089b0d03887f3444b7aeb1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 11:40:31 -0400 Subject: [PATCH 06/13] genesis spec tests upgraded to 0.8.3 --- packages/eth2.0-spec-test-util/src/single.ts | 4 +- .../genesis_initialization_minimal.test.ts | 78 +++++++++++-------- .../validity/genesis_validity_minimal.test.ts | 44 ++++++----- .../test/utils/specTestTypes/stateTestCase.ts | 2 + 4 files changed, 76 insertions(+), 52 deletions(-) diff --git a/packages/eth2.0-spec-test-util/src/single.ts b/packages/eth2.0-spec-test-util/src/single.ts index 041876ccfd48..139dd4103f51 100644 --- a/packages/eth2.0-spec-test-util/src/single.ts +++ b/packages/eth2.0-spec-test-util/src/single.ts @@ -21,9 +21,9 @@ export interface ISpecTestOptions { * If directory contains both ssz or yaml file version, * you can choose which one to use. Default is ssz. */ - inputTypes?: {[K in keyof TestCase]: InputType}; + inputTypes?: {[K in keyof NonNullable]: InputType}; - sszTypes?: {[K in keyof TestCase]: AnySSZType}; + sszTypes?: {[K in keyof NonNullable]: AnySSZType}; /** * Optionally diff --git a/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts b/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts index a0da023e38d0..7b9a670168ef 100644 --- a/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts +++ b/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts @@ -1,42 +1,58 @@ +/* eslint-disable @typescript-eslint/camelcase */ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; // @ts-ignore import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {BeaconState, Deposit, Hash, number64, uint64} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; import {initializeBeaconStateFromEth1} from "../../../../src/chain/genesis/genesis"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BeaconState} from "@chainsafe/eth2.0-types"; -import {GenesisInitCase} from "../../../utils/specTestTypes/genesis"; -import {DEPOSIT_CONTRACT_TREE_DEPTH} from "@chainsafe/eth2.0-params/lib/presets/minimal"; +import {IBeaconConfig} from "@chainsafe/eth2.0-config"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/genesis/initialization/genesis_initialization_minimal.yaml"), - (blockHash, timestamp, deposits) => { - return initializeBeaconStateFromEth1(config, blockHash, timestamp, deposits); - }, - (input) => { - return [ - expandYamlValue(input.eth1BlockHash, config.types.Hash), - expandYamlValue(input.eth1Timestamp, config.types.number64), - expandYamlValue(input.deposits, { - elementType: config.types.Deposit, - maxLength: 2 ^ DEPOSIT_CONTRACT_TREE_DEPTH, - }), - ]; - }, - (expected) => { - return expandYamlValue(expected.state, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.state; - }, - () => false, - (_1, _2, expected: BeaconState, actual: BeaconState) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; +interface GenesisInitSpecTest { + eth1_block_hash: Hash; + eth1_timestamp: uint64; + meta: { + depositsCount: uint64; + }; + state: BeaconState; + [k: string]: Deposit|unknown|null|undefined; +} + +describeDirectorySpecTest( + "genesis initialization", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/genesis/initialization/pyspec_tests"), + (testcase) => { + const deposits: Deposit[] = []; + for(let i= 0; i < testcase.meta.depositsCount.toNumber(); i++) { + deposits.push(testcase[`deposits_${i}`] as Deposit); + } + return initializeBeaconStateFromEth1(config, testcase.eth1_block_hash, testcase.eth1_timestamp.toNumber(), deposits); }, - 0 + { + // @ts-ignore + inputTypes: { + meta: InputType.YAML, + eth1_timestamp: InputType.YAML + }, + // @ts-ignore + sszTypes: { + eth1_block_hash: config.types.Hash, + state: config.types.BeaconState, + ...generateDepositSSZTypeMapping(64, config) + }, + getExpected: (testCase => testCase.state), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; + } + } ); +function generateDepositSSZTypeMapping(n: number, config: IBeaconConfig): object { + const depositMappings = {}; + for(let i = 0; i( - join(__dirname, "../../../../../spec-test-cases/tests/genesis/validity/genesis_validity_minimal.yaml"), - (state) => { - return isValidGenesisState(config, state); - }, - (input) => { - return [expandYamlValue(input.genesis, config.types.BeaconState)]; - }, - (expected) => { - return expandYamlValue(expected.isValid, config.types.bool); - }, - result => result, - (testCase) => false, - () => false, - (_1, _2, expected, actual) => { - expect(actual).to.equal(expected); +interface GenesisValidityTestCase { + is_valid: boolean; + genesis: BeaconState; +} + +describeDirectorySpecTest( + "genesis initialization", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/genesis/validity/pyspec_tests"), + (testcase) => { + return isValidGenesisState(config, testcase.genesis); }, - 0 + { + inputTypes: { + is_valid: InputType.YAML, + genesis: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + genesis: config.types.BeaconState + }, + getExpected: (testCase => testCase.is_valid) + } ); diff --git a/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts b/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts index db17bafea2bc..7b4885e84533 100644 --- a/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts +++ b/packages/lodestar/test/utils/specTestTypes/stateTestCase.ts @@ -1,3 +1,5 @@ +import {BeaconState} from "@chainsafe/eth2.0-types"; + export interface StateTestCase { pre: BeaconState; post: BeaconState; From 4b1143fad7d7ce17c9f50b3f6948afb07bc02024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 11:57:22 -0400 Subject: [PATCH 07/13] fix domain computing --- packages/lodestar/src/chain/stateTransition/util/domain.ts | 4 ++-- .../initialization/genesis_initialization_minimal.test.ts | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/lodestar/src/chain/stateTransition/util/domain.ts b/packages/lodestar/src/chain/stateTransition/util/domain.ts index 0178a73c7df3..4a54d61ccd83 100644 --- a/packages/lodestar/src/chain/stateTransition/util/domain.ts +++ b/packages/lodestar/src/chain/stateTransition/util/domain.ts @@ -17,10 +17,10 @@ import {getCurrentEpoch} from "./epoch"; * Return the domain for the [[domainType]] and [[forkVersion]]. */ export function computeDomain(domainType: DomainType, forkVersion: Version = Buffer.alloc(4)): Domain { - return bytesToBN(Buffer.concat([ + return Buffer.concat([ intToBytes(domainType, 4), forkVersion, - ])).toBuffer('be', 8); + ]); } /** diff --git a/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts b/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts index 7b9a670168ef..8df623ffd27a 100644 --- a/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts +++ b/packages/lodestar/test/spec/genesis/initialization/genesis_initialization_minimal.test.ts @@ -42,6 +42,7 @@ describeDirectorySpecTest( state: config.types.BeaconState, ...generateDepositSSZTypeMapping(64, config) }, + timeout: 60000, getExpected: (testCase => testCase.state), expectFunc: (testCase, expected, actual) => { expect(equals(actual, expected, config.types.BeaconState)).to.be.true; From 0739cab58b1b34103bd17a04e4f39d65248ddd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 12:42:22 -0400 Subject: [PATCH 08/13] block sanity tests updated to 0.8.3 --- .../stateTransition/epoch/finalUpdates.ts | 6 +- .../blocks/sanity_blocks_mainnet.test.ts | 70 +++++++++--------- .../blocks/sanity_blocks_minimal.test.ts | 73 ++++++++++--------- .../lodestar/test/spec/sanity/blocks/type.ts | 10 +++ 4 files changed, 88 insertions(+), 71 deletions(-) create mode 100644 packages/lodestar/test/spec/sanity/blocks/type.ts diff --git a/packages/lodestar/src/chain/stateTransition/epoch/finalUpdates.ts b/packages/lodestar/src/chain/stateTransition/epoch/finalUpdates.ts index 72e0eb39ed2c..87337a286fd2 100644 --- a/packages/lodestar/src/chain/stateTransition/epoch/finalUpdates.ts +++ b/packages/lodestar/src/chain/stateTransition/epoch/finalUpdates.ts @@ -33,9 +33,6 @@ export function processFinalUpdates(config: IBeaconConfig, state: BeaconState): config.params.MAX_EFFECTIVE_BALANCE); } }); - // Update start shard - state.startShard = - (state.startShard + getShardDelta(config, state, currentEpoch)) % config.params.SHARD_COUNT; // Set active index root const indexEpoch = nextEpoch + config.params.ACTIVATION_EXIT_DELAY; const indexRootPosition = indexEpoch % config.params.EPOCHS_PER_HISTORICAL_VECTOR; @@ -61,6 +58,9 @@ export function processFinalUpdates(config: IBeaconConfig, state: BeaconState): }; state.historicalRoots.push(hashTreeRoot(historicalBatch, config.types.HistoricalBatch)); } + // Update start shard + state.startShard = + (state.startShard + getShardDelta(config, state, currentEpoch)) % config.params.SHARD_COUNT; // Rotate current/previous epoch attestations state.previousEpochAttestations = state.currentEpochAttestations; state.currentEpochAttestations = []; diff --git a/packages/lodestar/test/spec/sanity/blocks/sanity_blocks_mainnet.test.ts b/packages/lodestar/test/spec/sanity/blocks/sanity_blocks_mainnet.test.ts index edcb17919e17..16cb37555420 100644 --- a/packages/lodestar/test/spec/sanity/blocks/sanity_blocks_mainnet.test.ts +++ b/packages/lodestar/test/spec/sanity/blocks/sanity_blocks_mainnet.test.ts @@ -1,47 +1,51 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; // @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {BeaconBlock, BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {stateTransition} from "../../../../src/chain/stateTransition"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BlockSanityCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {BlockSanityTestCase} from "./type"; +import {IBeaconConfig} from "@chainsafe/eth2.0-config"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/sanity/blocks/sanity_blocks_mainnet.yaml"), - (state: BeaconState, blocks: BeaconBlock[]) => { - blocks.forEach((block) => { - stateTransition(config, state, block, true); - }); +describeDirectorySpecTest( + "block sanity minimal", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/sanity/blocks/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + for(let i = 0; i < testcase.meta.blocksCount.toNumber(); i++) { + stateTransition(config, state, testcase[`blocks_${i}`] as BeaconBlock, true, true); + } return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + // @ts-ignore + inputTypes: { + meta: InputType.YAML + }, + // @ts-ignore + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + ...generateBlocksSZZTypeMapping(99, config) + }, + shouldError: (testCase => { + return !testCase.post; + }), + timeout: 10000000, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), input.blocks.map((b) => expandYamlValue(b, config.types.BeaconBlock))]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); +function generateBlocksSZZTypeMapping(n: number, config: IBeaconConfig): object { + const blocksMapping = {}; + for(let i = 0; i( - join(__dirname, "../../../../../spec-test-cases/tests/sanity/blocks/sanity_blocks_minimal.yaml"), - (state: BeaconState, blocks: BeaconBlock[]) => { - blocks.forEach((block) => { - stateTransition(config, state, block, true); - }); +describeDirectorySpecTest( + "block sanity minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/sanity/blocks/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + for(let i = 0; i < testcase.meta.blocksCount.toNumber(); i++) { + stateTransition(config, state, testcase[`blocks_${i}`] as BeaconBlock, true, true); + } return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + // @ts-ignore + inputTypes: { + meta: InputType.YAML + }, + // @ts-ignore + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + ...generateBlocksSZZTypeMapping(99, config) + }, + shouldError: (testCase => { + return !testCase.post; + }), + timeout: 60000, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), input.blocks.map((b) => expandYamlValue(b, config.types.BeaconBlock))]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); +function generateBlocksSZZTypeMapping(n: number, config: IBeaconConfig): object { + const blocksMapping = {}; + for(let i = 0; i Date: Fri, 6 Sep 2019 13:12:05 -0400 Subject: [PATCH 09/13] upgrade operations spec tests to 0.8.3 --- .../attestations/attestations_mainnet.test.ts | 58 +++++++------------ .../attestations/attestations_minimal.test.ts | 53 +++++++---------- .../test/spec/operations/attestations/type.ts | 9 +++ .../attester_slashing_mainnet.test.ts | 47 +++++++-------- .../attester_slashing_minimal.test.ts | 53 +++++++---------- .../spec/operations/attesterSlashing/type.ts | 9 +++ .../blockHeader/block_header_mainnet.test.ts | 45 +++++++------- .../blockHeader/block_header_minimal.test.ts | 52 +++++++---------- .../test/spec/operations/blockHeader/type.ts | 9 +++ .../deposit/deposit_mainnet.test.ts | 53 +++++++---------- .../deposit/deposit_minimal.test.ts | 51 +++++++--------- .../test/spec/operations/deposit/type.ts | 9 +++ .../proposer_slashing_mainnet.test.ts | 54 +++++++---------- .../proposer_slashing_minimal.test.ts | 53 +++++++---------- .../spec/operations/proposerSlashing/type.ts | 9 +++ .../transfer/transfer_minimal.test.ts | 51 +++++++--------- .../test/spec/operations/transfer/type.ts | 9 +++ .../spec/operations/voluntaryExit/type.ts | 9 +++ .../voluntary_exit_mainnet.test.ts | 51 +++++++--------- .../voluntary_exit_minimal.test.ts | 52 +++++++---------- 20 files changed, 334 insertions(+), 402 deletions(-) create mode 100644 packages/lodestar/test/spec/operations/attestations/type.ts create mode 100644 packages/lodestar/test/spec/operations/attesterSlashing/type.ts create mode 100644 packages/lodestar/test/spec/operations/blockHeader/type.ts create mode 100644 packages/lodestar/test/spec/operations/deposit/type.ts create mode 100644 packages/lodestar/test/spec/operations/proposerSlashing/type.ts create mode 100644 packages/lodestar/test/spec/operations/transfer/type.ts create mode 100644 packages/lodestar/test/spec/operations/voluntaryExit/type.ts diff --git a/packages/lodestar/test/spec/operations/attestations/attestations_mainnet.test.ts b/packages/lodestar/test/spec/operations/attestations/attestations_mainnet.test.ts index 470b4c6ad8b1..dd6336946c81 100644 --- a/packages/lodestar/test/spec/operations/attestations/attestations_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/attestations/attestations_mainnet.test.ts @@ -1,45 +1,31 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; - -import {Attestation, BeaconState} from "@chainsafe/eth2.0-types"; +import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processAttestation} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {AttestationCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessAttestationTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/attestation/attestation_mainnet.yaml"), - (state: BeaconState, attestation: Attestation): BeaconState => { - processAttestation(config, state, attestation); +describeDirectorySpecTest( + "process attestation mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/attestation/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processAttestation(config, state, testcase.attestation); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + attestation: config.types.Attestation, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.attestation, config.types.Attestation)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 -); - + } +); \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/attestations/attestations_minimal.test.ts b/packages/lodestar/test/spec/operations/attestations/attestations_minimal.test.ts index 93132b935f84..c465ce0463e7 100644 --- a/packages/lodestar/test/spec/operations/attestations/attestations_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/attestations/attestations_minimal.test.ts @@ -1,45 +1,34 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; // @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; -import {Attestation, BeaconState} from "@chainsafe/eth2.0-types"; +import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processAttestation} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {AttestationCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessAttestationTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/attestation/attestation_minimal.yaml"), - (state: BeaconState, attestation: Attestation): BeaconState => { - processAttestation(config, state, attestation); +describeDirectorySpecTest( + "process attestation minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/attestation/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processAttestation(config, state, testcase.attestation); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + attestation: config.types.Attestation, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.attestation, config.types.Attestation)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/attestations/type.ts b/packages/lodestar/test/spec/operations/attestations/type.ts new file mode 100644 index 000000000000..428501032cff --- /dev/null +++ b/packages/lodestar/test/spec/operations/attestations/type.ts @@ -0,0 +1,9 @@ +import {Attestation, BeaconState} from "@chainsafe/eth2.0-types"; + +export interface ProcessAttestationTestCase { + + attestation: Attestation; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_mainnet.test.ts b/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_mainnet.test.ts index 5e802bfa5206..c959e06b6be8 100644 --- a/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_mainnet.test.ts @@ -11,35 +11,30 @@ import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processAttesterSlashing} from "../../../../src/chain/stateTransition/block/operations"; import {expandYamlValue} from "../../../utils/expandYamlValue"; import {AttesterSlashingCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessAttesterSlashingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/attester_slashing/attester_slashing_mainnet.yaml"), - (state, attesterSlashing) => { - processAttesterSlashing(config, state, attesterSlashing); +describeDirectorySpecTest( + "process attester slashing mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/attester_slashing/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processAttesterSlashing(config, state, testcase.attester_slashing); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + attester_slashing: config.types.AttesterSlashing, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.attesterSlashing, config.types.AttesterSlashing)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_minimal.test.ts b/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_minimal.test.ts index df0ea42bf84e..cf1ffe120d13 100644 --- a/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/attesterSlashing/attester_slashing_minimal.test.ts @@ -1,44 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -import sinon from "sinon"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processAttesterSlashing} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {AttesterSlashingCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessAttesterSlashingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/attester_slashing/attester_slashing_minimal.yaml"), - (state, attesterSlashing) => { - processAttesterSlashing(config, state, attesterSlashing); +describeDirectorySpecTest( + "process attester slashing minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/attester_slashing/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processAttesterSlashing(config, state, testcase.attester_slashing); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true), - aggregatePubkeys: sinon.stub().returns(Buffer.alloc(48)) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + attester_slashing: config.types.AttesterSlashing, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.attesterSlashing, config.types.AttesterSlashing)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/attesterSlashing/type.ts b/packages/lodestar/test/spec/operations/attesterSlashing/type.ts new file mode 100644 index 000000000000..df3aa7fc7ce7 --- /dev/null +++ b/packages/lodestar/test/spec/operations/attesterSlashing/type.ts @@ -0,0 +1,9 @@ +import {AttesterSlashing, BeaconState} from "@chainsafe/eth2.0-types"; + +export interface ProcessAttesterSlashingTestCase { + + attester_slashing: AttesterSlashing; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/blockHeader/block_header_mainnet.test.ts b/packages/lodestar/test/spec/operations/blockHeader/block_header_mainnet.test.ts index baa38939795b..9b413a526dc7 100644 --- a/packages/lodestar/test/spec/operations/blockHeader/block_header_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/blockHeader/block_header_mainnet.test.ts @@ -11,34 +11,29 @@ import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processBlockHeader} from "../../../../src/chain/stateTransition/block/blockHeader"; import {expandYamlValue} from "../../../utils/expandYamlValue"; import {BlockHeaderCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessBlockHeader} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/block_header/block_header_mainnet.yaml"), - (state, block) => { - processBlockHeader(config, state, block); +describeDirectorySpecTest( + "process block header mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/block_header/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processBlockHeader(config, state, testcase.block); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + block: config.types.BeaconBlock, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.block, config.types.BeaconBlock)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/blockHeader/block_header_minimal.test.ts b/packages/lodestar/test/spec/operations/blockHeader/block_header_minimal.test.ts index ac7df17533a0..8409d9d2a78f 100644 --- a/packages/lodestar/test/spec/operations/blockHeader/block_header_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/blockHeader/block_header_minimal.test.ts @@ -1,43 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; // @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; -import {processBlockHeader} from "../../../../src/chain/stateTransition/block/blockHeader"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {BlockHeaderCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessBlockHeader} from "./type"; +import {processBlockHeader} from "../../../../src/chain/stateTransition/block/blockHeader"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/block_header/block_header_minimal.yaml"), - (state, block) => { - processBlockHeader(config, state, block); +describeDirectorySpecTest( + "process block header minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/block_header/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processBlockHeader(config, state, testcase.block); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + block: config.types.BeaconBlock, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.block, config.types.BeaconBlock)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/blockHeader/type.ts b/packages/lodestar/test/spec/operations/blockHeader/type.ts new file mode 100644 index 000000000000..cabda5434da8 --- /dev/null +++ b/packages/lodestar/test/spec/operations/blockHeader/type.ts @@ -0,0 +1,9 @@ +import {BeaconBlock, BeaconState} from "@chainsafe/eth2.0-types"; + +export interface ProcessBlockHeader { + + block: BeaconBlock; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/deposit/deposit_mainnet.test.ts b/packages/lodestar/test/spec/operations/deposit/deposit_mainnet.test.ts index 4cc59425fa18..9ab01781d9dc 100644 --- a/packages/lodestar/test/spec/operations/deposit/deposit_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/deposit/deposit_mainnet.test.ts @@ -1,44 +1,31 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; import {equals} from "@chainsafe/ssz"; -import sinon from "sinon"; - import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processDeposit} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {DepositCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessDepositTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/deposit/deposit_mainnet.yaml"), - (state, deposit) => { - processDeposit(config, state, deposit); +describeDirectorySpecTest( + "process deposit mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/deposit/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processDeposit(config, state, testcase.deposit); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + deposit: config.types.Deposit, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.deposit, config.types.Deposit)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); - diff --git a/packages/lodestar/test/spec/operations/deposit/deposit_minimal.test.ts b/packages/lodestar/test/spec/operations/deposit/deposit_minimal.test.ts index d425cdfbbf2e..be51a3d6ddfd 100644 --- a/packages/lodestar/test/spec/operations/deposit/deposit_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/deposit/deposit_minimal.test.ts @@ -1,43 +1,32 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; import {equals} from "@chainsafe/ssz"; -import sinon from "sinon"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processDeposit} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {DepositCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessDepositTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/deposit/deposit_minimal.yaml"), - (state, deposit) => { - processDeposit(config, state, deposit); +describeDirectorySpecTest( + "process deposit minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/deposit/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processDeposit(config, state, testcase.deposit); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + deposit: config.types.Deposit, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.deposit, config.types.Deposit)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/deposit/type.ts b/packages/lodestar/test/spec/operations/deposit/type.ts new file mode 100644 index 000000000000..955e9806d06f --- /dev/null +++ b/packages/lodestar/test/spec/operations/deposit/type.ts @@ -0,0 +1,9 @@ +import {BeaconState, Deposit} from "@chainsafe/eth2.0-types"; + +export interface ProcessDepositTestCase { + + deposit: Deposit; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_mainnet.test.ts b/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_mainnet.test.ts index 3ed1613c4a86..f5e72e9171a6 100644 --- a/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_mainnet.test.ts @@ -1,45 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; - import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processProposerSlashing} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {ProposerSlashingCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessProposerSlashingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/proposer_slashing/proposer_slashing_mainnet.yaml"), - (state, proposerSlashing) => { - processProposerSlashing(config, state, proposerSlashing); +describeDirectorySpecTest( + "process proposer slashing mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/proposer_slashing/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processProposerSlashing(config, state, testcase.proposer_slashing); return state; }, - (input) => { - restore(); - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + proposer_slashing: config.types.ProposerSlashing, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.proposerSlashing, config.types.ProposerSlashing)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_minimal.test.ts b/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_minimal.test.ts index 8ecefb3fa796..849dfe5f2262 100644 --- a/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/proposerSlashing/proposer_slashing_minimal.test.ts @@ -1,44 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processProposerSlashing} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {ProposerSlashingCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessProposerSlashingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/proposer_slashing/proposer_slashing_minimal.yaml"), - (state, proposerSlashing) => { - processProposerSlashing(config, state, proposerSlashing); +describeDirectorySpecTest( + "process proposer slashing minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/proposer_slashing/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processProposerSlashing(config, state, testcase.proposer_slashing); return state; }, - (input) => { - restore(); - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + proposer_slashing: config.types.ProposerSlashing, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.proposerSlashing, config.types.ProposerSlashing)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/proposerSlashing/type.ts b/packages/lodestar/test/spec/operations/proposerSlashing/type.ts new file mode 100644 index 000000000000..bf962625000b --- /dev/null +++ b/packages/lodestar/test/spec/operations/proposerSlashing/type.ts @@ -0,0 +1,9 @@ +import {BeaconState, ProposerSlashing} from "@chainsafe/eth2.0-types"; + +export interface ProcessProposerSlashingTestCase { + + proposer_slashing: ProposerSlashing; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/transfer/transfer_minimal.test.ts b/packages/lodestar/test/spec/operations/transfer/transfer_minimal.test.ts index a45af0a0e753..a973d3ea99e4 100644 --- a/packages/lodestar/test/spec/operations/transfer/transfer_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/transfer/transfer_minimal.test.ts @@ -1,44 +1,35 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; // @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processTransfer} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {TransferCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessTransferTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/transfer/transfer_minimal.yaml"), - (state, transfer) => { - processTransfer(config, state, transfer); +describeDirectorySpecTest( + "process transfer minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/transfer/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processTransfer(config, state, testcase.transfer); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + transfer: config.types.Transfer, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.transfer, config.types.Transfer)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); + diff --git a/packages/lodestar/test/spec/operations/transfer/type.ts b/packages/lodestar/test/spec/operations/transfer/type.ts new file mode 100644 index 000000000000..829aec419496 --- /dev/null +++ b/packages/lodestar/test/spec/operations/transfer/type.ts @@ -0,0 +1,9 @@ +import {BeaconState, Transfer} from "@chainsafe/eth2.0-types"; + +export interface ProcessTransferTestCase { + + transfer: Transfer; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/voluntaryExit/type.ts b/packages/lodestar/test/spec/operations/voluntaryExit/type.ts new file mode 100644 index 000000000000..5cb0c0354d74 --- /dev/null +++ b/packages/lodestar/test/spec/operations/voluntaryExit/type.ts @@ -0,0 +1,9 @@ +import {BeaconState, VoluntaryExit} from "@chainsafe/eth2.0-types"; + +export interface ProcessVoluntaryExitTestCase { + + voluntary_exit: VoluntaryExit; + pre: BeaconState; + post?: BeaconState; + +} \ No newline at end of file diff --git a/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_mainnet.test.ts b/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_mainnet.test.ts index af4a39ff83f6..b523a61db40a 100644 --- a/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_mainnet.test.ts +++ b/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_mainnet.test.ts @@ -1,44 +1,35 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; // @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; import {BeaconState} from "@chainsafe/eth2.0-types"; import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processVoluntaryExit} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {VoluntaryExitCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessVoluntaryExitTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/voluntary_exit/voluntary_exit_mainnet.yaml"), - (state, exit) => { - processVoluntaryExit(config, state, exit); +describeDirectorySpecTest( + "process voluntary exit mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/operations/voluntary_exit/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processVoluntaryExit(config, state, testcase.voluntary_exit); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + voluntary_exit: config.types.VoluntaryExit, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.voluntaryExit, config.types.VoluntaryExit)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_minimal.test.ts b/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_minimal.test.ts index 407b72b60e89..ce6646596ba1 100644 --- a/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_minimal.test.ts +++ b/packages/lodestar/test/spec/operations/voluntaryExit/voluntary_exit_minimal.test.ts @@ -1,43 +1,33 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; import {equals} from "@chainsafe/ssz"; import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; import {processVoluntaryExit} from "../../../../src/chain/stateTransition/block/operations"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {VoluntaryExitCase} from "../../../utils/specTestTypes/beaconStateComparison"; import {BeaconState} from "@chainsafe/eth2.0-types"; +import {describeDirectorySpecTest} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessVoluntaryExitTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/operations/voluntary_exit/voluntary_exit_minimal.yaml"), - (state, exit) => { - processVoluntaryExit(config, state, exit); +describeDirectorySpecTest( + "process voluntary exit minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/operations/voluntary_exit/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processVoluntaryExit(config, state, testcase.voluntary_exit); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + // eslint-disable-next-line @typescript-eslint/camelcase + voluntary_exit: config.types.VoluntaryExit, + }, + timeout: 100000000, + shouldError: testCase => !testCase.post, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), expandYamlValue(input.voluntaryExit, config.types.VoluntaryExit)]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); From 2aeff494192464a1c9f2be39a95ea3ce8f546562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 13:28:13 -0400 Subject: [PATCH 10/13] upgrade shuffling spec tests to 0.8.3 --- .../spec/shufling/shuffling_mainnet.test.ts | 41 ++++++++----------- .../spec/shufling/shuffling_minimal.test.ts | 41 +++++++------------ packages/lodestar/test/spec/shufling/type.ts | 9 ++++ 3 files changed, 40 insertions(+), 51 deletions(-) create mode 100644 packages/lodestar/test/spec/shufling/type.ts diff --git a/packages/lodestar/test/spec/shufling/shuffling_mainnet.test.ts b/packages/lodestar/test/spec/shufling/shuffling_mainnet.test.ts index d84a0361597b..f3c57c3d634f 100644 --- a/packages/lodestar/test/spec/shufling/shuffling_mainnet.test.ts +++ b/packages/lodestar/test/spec/shufling/shuffling_mainnet.test.ts @@ -1,40 +1,31 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; -import {expect} from "chai"; - import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {computeShuffledIndex} from "../../../src/chain/stateTransition/util"; -import {bytes32} from "@chainsafe/eth2.0-types"; -import {ShufflingCase} from "../../utils/specTestTypes/shufflingCase"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ShufflingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../spec-test-cases/tests/shuffling/core/shuffling_full.yaml"), - (seed: bytes32, count: number) => { +describeDirectorySpecTest( + "shuffling mainnet", + join(__dirname, "../../../../spec-test-cases/tests/mainnet/phase0/shuffling/core/shuffle"), + (testcase) => { const output = []; - for(let i = 0; i < count; i++) { + const seed = Buffer.from(testcase.mapping.seed.replace("0x", ""),"hex"); + for(let i = 0; i < testcase.mapping.count.toNumber(); i++) { output[i] = computeShuffledIndex( config, i, - count, + testcase.mapping.count.toNumber(), seed ); } return output; }, - (input) => { - return [Buffer.from(input.seed.slice(2), 'hex'), input.count.toNumber()]; - }, - (expected) => { - return expected.shuffled.map((value) => value.toNumber()); - }, - result => result, - (_) => { - return false; - }, - () => false, - (_1, _2, expected, actual) => { - expect(expected).to.be.deep.equal(actual); - }, - 0 + { + inputTypes: { + mapping: InputType.YAML + }, + timeout: 10000, + getExpected: (testCase => testCase.mapping.mapping.map((value) => value.toNumber())) + } ); diff --git a/packages/lodestar/test/spec/shufling/shuffling_minimal.test.ts b/packages/lodestar/test/spec/shufling/shuffling_minimal.test.ts index e6fbcb3e95ae..41e4f882889b 100644 --- a/packages/lodestar/test/spec/shufling/shuffling_minimal.test.ts +++ b/packages/lodestar/test/spec/shufling/shuffling_minimal.test.ts @@ -1,40 +1,29 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; -import {expect} from "chai"; - import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; import {computeShuffledIndex} from "../../../src/chain/stateTransition/util"; -import {bytes32} from "@chainsafe/eth2.0-types"; -import {ShufflingCase} from "../../utils/specTestTypes/shufflingCase"; +import {ShufflingTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../spec-test-cases/tests/shuffling/core/shuffling_minimal.yaml"), - (seed: bytes32, count: number) => { +describeDirectorySpecTest( + "shuffling minimal", + join(__dirname, "../../../../spec-test-cases/tests/minimal/phase0/shuffling/core/shuffle"), + (testcase) => { const output = []; - for(let i = 0; i < count; i++) { + const seed = Buffer.from(testcase.mapping.seed.replace("0x", ""),"hex"); + for(let i = 0; i < testcase.mapping.count.toNumber(); i++) { output[i] = computeShuffledIndex( config, i, - count, + testcase.mapping.count.toNumber(), seed ); } return output; }, - (input) => { - return [Buffer.from(input.seed.slice(2), 'hex'), input.count.toNumber()]; - }, - (expected) => { - return expected.shuffled.map((value) => value.toNumber()); - }, - result => result, - (_) => { - return false; - }, - () => false, - (_1, _2, expected, actual) => { - expect(expected).to.be.deep.equal(actual); - }, - 0 + { + inputTypes: { + mapping: InputType.YAML + }, + getExpected: (testCase => testCase.mapping.mapping.map((value) => value.toNumber())) + } ); - diff --git a/packages/lodestar/test/spec/shufling/type.ts b/packages/lodestar/test/spec/shufling/type.ts new file mode 100644 index 000000000000..82a5ea2309a8 --- /dev/null +++ b/packages/lodestar/test/spec/shufling/type.ts @@ -0,0 +1,9 @@ +import {uint64} from "@chainsafe/eth2.0-types"; + +export interface ShufflingTestCase { + mapping: { + seed: string; + count: uint64; + mapping: uint64[]; + }; +} \ No newline at end of file From a4c5830f2c824162c1d41dee93642ff758d32c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 16:04:49 -0400 Subject: [PATCH 11/13] fix epoch processing --- .../crosslinks/crosslinks_mainnet.test.ts | 6 +- .../crosslinks/crosslinks_minimal.test.ts | 2 +- .../final_updates_mainnet.test.ts | 4 +- .../final_updates_minimal.test.ts | 2 +- ...ification_and_finalization_mainnet.test.ts | 4 +- ...ification_and_finalization_minimal.test.ts | 2 +- .../registry_updates_mainnet.test.ts | 4 +- .../registry_updates_minimal.test.ts | 2 +- .../slashings/slashings_mainnet.test.ts | 4 +- .../slashings/slashings_minimal.test.ts | 2 +- .../sanity/slots/sanity_slots_mainnet.test.ts | 60 ++++++++---------- .../sanity/slots/sanity_slots_minimal.test.ts | 63 +++++++++---------- .../lodestar/test/spec/sanity/slots/type.ts | 7 +++ 13 files changed, 78 insertions(+), 84 deletions(-) create mode 100644 packages/lodestar/test/spec/sanity/slots/type.ts diff --git a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts index e3c12a233007..5d118f8e00a7 100644 --- a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_mainnet.test.ts @@ -1,5 +1,5 @@ import {join} from "path"; -import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; import {processCrosslinks} from "../../../../src/chain/stateTransition/epoch/crosslinks"; import {BeaconState} from "@chainsafe/eth2.0-types"; import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; @@ -8,8 +8,8 @@ import {expect} from "chai"; import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "epoch_croslinks", - join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/crosslinks/pyspec_tests"), + "epoch_croslinks mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/epoch_processing/crosslinks/pyspec_tests"), (testcase) => { const state = testcase.pre; processCrosslinks(config, state); diff --git a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts index a4a3a86c73ab..036a6ca16bf6 100644 --- a/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/crosslinks/crosslinks_minimal.test.ts @@ -9,7 +9,7 @@ import {expect} from "chai"; import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "epoch_croslinks", + "epoch_croslinks minimal", join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/crosslinks/pyspec_tests"), (testcase) => { const state = testcase.pre; diff --git a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts index afc2396cddd0..42be1efbd9b7 100644 --- a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_mainnet.test.ts @@ -9,8 +9,8 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "epoch_croslinks", - join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/final_updates/pyspec_tests"), + "epoch final updates mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/epoch_processing/final_updates/pyspec_tests"), (testcase) => { const state = testcase.pre; processFinalUpdates(config, state); diff --git a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts index 365ea2be3a61..6e2621ede042 100644 --- a/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/finalUpdates/final_updates_minimal.test.ts @@ -9,7 +9,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "epoch_croslinks_min", + "epoch final updates minimal", join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/final_updates/pyspec_tests"), (testcase) => { const state = testcase.pre; diff --git a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts index 1a82b13720c7..58af6650a7d0 100644 --- a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_mainnet.test.ts @@ -9,8 +9,8 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "justification and finalization", - join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/justification_and_finalization/pyspec_tests"), + "epoch justification and finalization mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/epoch_processing/justification_and_finalization/pyspec_tests"), (testcase) => { const state = testcase.pre; processJustificationAndFinalization(config, state); diff --git a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts index b9c6cd0c953a..14d57e6a1a1e 100644 --- a/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/justification/justification_and_finalization_minimal.test.ts @@ -9,7 +9,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "justification and finalization min", + "epoch justification and finalization minimal", join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/justification_and_finalization/pyspec_tests"), (testcase) => { const state = testcase.pre; diff --git a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts index 54c4e5581842..5120d152fabd 100644 --- a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_mainnet.test.ts @@ -9,8 +9,8 @@ import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; import {processRegistryUpdates} from "../../../../src/chain/stateTransition/epoch/registryUpdates"; describeDirectorySpecTest( - "registry updates", - join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/registry_updates/pyspec_tests"), + "epoch registry updates mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/epoch_processing/registry_updates/pyspec_tests"), (testcase) => { const state = testcase.pre; processRegistryUpdates(config, state); diff --git a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts index afaab5d7aa39..3cf355823af8 100644 --- a/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/registryUpdates/registry_updates_minimal.test.ts @@ -9,7 +9,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "registry updates min", + "epoch registry updates minimal", join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/registry_updates/pyspec_tests"), (testcase) => { const state = testcase.pre; diff --git a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts index 8ed9039f7b5b..6ebf6f870725 100644 --- a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_mainnet.test.ts @@ -9,8 +9,8 @@ import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; import {processSlashings} from "../../../../src/chain/stateTransition/epoch/slashings"; describeDirectorySpecTest( - "slashings", - join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/slashings/pyspec_tests"), + "epoch slashings mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/epoch_processing/slashings/pyspec_tests"), (testcase) => { const state = testcase.pre; processSlashings(config, state); diff --git a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts index 01a30d6111ff..df225e043c97 100644 --- a/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts +++ b/packages/lodestar/test/spec/epoch_processing/slashings/slashings_minimal.test.ts @@ -9,7 +9,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test- import {StateTestCase} from "../../../utils/specTestTypes/stateTestCase"; describeDirectorySpecTest( - "slashings min", + "epoch slashings minimal", join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/epoch_processing/slashings/pyspec_tests"), (testcase) => { const state = testcase.pre; diff --git a/packages/lodestar/test/spec/sanity/slots/sanity_slots_mainnet.test.ts b/packages/lodestar/test/spec/sanity/slots/sanity_slots_mainnet.test.ts index 98a8b621b6d0..185541336750 100644 --- a/packages/lodestar/test/spec/sanity/slots/sanity_slots_mainnet.test.ts +++ b/packages/lodestar/test/spec/sanity/slots/sanity_slots_mainnet.test.ts @@ -1,44 +1,38 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; - import {config} from "@chainsafe/eth2.0-config/lib/presets/mainnet"; -import {BeaconState, number64} from "@chainsafe/eth2.0-types"; +import {BeaconState} from "@chainsafe/eth2.0-types"; import {processSlots} from "../../../../src/chain/stateTransition"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {SlotSanityCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessSlotsTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/sanity/slots/sanity_slots_mainnet.yaml"), - (state: BeaconState, slots: number64) => { - processSlots(config, state, state.slot + slots); +describeDirectorySpecTest( + "slot sanity mainnet", + join(__dirname, "../../../../../spec-test-cases/tests/mainnet/phase0/sanity/slots/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processSlots(config, state, state.slot + testcase.slots.toNumber()); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + // @ts-ignore + inputTypes: { + slots: InputType.YAML + }, + // @ts-ignore + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + shouldError: (testCase => { + return !testCase.post; + }), + timeout: 10000000, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), input.slots.toNumber()]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 + } ); diff --git a/packages/lodestar/test/spec/sanity/slots/sanity_slots_minimal.test.ts b/packages/lodestar/test/spec/sanity/slots/sanity_slots_minimal.test.ts index 76191cdf58f1..608022198723 100644 --- a/packages/lodestar/test/spec/sanity/slots/sanity_slots_minimal.test.ts +++ b/packages/lodestar/test/spec/sanity/slots/sanity_slots_minimal.test.ts @@ -1,44 +1,37 @@ import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; import {expect} from "chai"; import {equals} from "@chainsafe/ssz"; -// @ts-ignore -import {restore, rewire} from "@chainsafe/bls"; -import sinon from "sinon"; - import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; -import {BeaconState, number64} from "@chainsafe/eth2.0-types"; +import {BeaconState} from "@chainsafe/eth2.0-types"; import {processSlots} from "../../../../src/chain/stateTransition"; -import {expandYamlValue} from "../../../utils/expandYamlValue"; -import {SlotSanityCase} from "../../../utils/specTestTypes/beaconStateComparison"; +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {ProcessSlotsTestCase} from "./type"; -describeMultiSpec( - join(__dirname, "../../../../../spec-test-cases/tests/sanity/slots/sanity_slots_minimal.yaml"), - (state: BeaconState, slots: number64) => { - processSlots(config, state, state.slot + slots); +describeDirectorySpecTest( + "slot sanity minimal", + join(__dirname, "../../../../../spec-test-cases/tests/minimal/phase0/sanity/slots/pyspec_tests"), + (testcase) => { + const state = testcase.pre; + processSlots(config, state, state.slot + testcase.slots.toNumber()); return state; }, - (input) => { - if(input.bls_setting && input.bls_setting.toNumber() === 2) { - rewire({ - verify: sinon.stub().returns(true), - verifyMultiple: sinon.stub().returns(true) - }); + { + // @ts-ignore + inputTypes: { + slots: InputType.YAML + }, + // @ts-ignore + sszTypes: { + pre: config.types.BeaconState, + post: config.types.BeaconState, + }, + shouldError: (testCase => { + return !testCase.post; + }), + timeout: 10000000, + getExpected: (testCase => testCase.post), + expectFunc: (testCase, expected, actual) => { + expect(equals(actual, expected, config.types.BeaconState)).to.be.true; } - return [expandYamlValue(input.pre, config.types.BeaconState), input.slots.toNumber()]; - }, - (expected) => { - return expandYamlValue(expected.post, config.types.BeaconState); - }, - result => result, - (testCase) => { - return !testCase.post; - }, - () => false, - (_1, _2, expected, actual) => { - expect(equals(expected, actual, config.types.BeaconState)).to.be.true; - restore(); - }, - 0 -); - + } +); \ No newline at end of file diff --git a/packages/lodestar/test/spec/sanity/slots/type.ts b/packages/lodestar/test/spec/sanity/slots/type.ts new file mode 100644 index 000000000000..9a0f923102e5 --- /dev/null +++ b/packages/lodestar/test/spec/sanity/slots/type.ts @@ -0,0 +1,7 @@ +import {BeaconState, uint64} from "@chainsafe/eth2.0-types"; + +export interface ProcessSlotsTestCase { + pre: BeaconState; + post?: BeaconState; + slots: uint64; +} \ No newline at end of file From 55f2435b7e4ca4f92a1165727362a57a40c7fdb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Fri, 6 Sep 2019 16:18:11 -0400 Subject: [PATCH 12/13] remove extra memory allocation --- packages/lodestar/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/lodestar/package.json b/packages/lodestar/package.json index 86139d8b16c9..bba2b477aa79 100644 --- a/packages/lodestar/package.json +++ b/packages/lodestar/package.json @@ -24,9 +24,9 @@ "pretest": "yarn run check-types", "test:unit": "nyc --cache-dir .nyc_output/.cache -e .ts mocha --colors -r ts-node/register 'test/unit/**/*.test.ts'", "test:e2e": "mocha --colors --timeout 5000 -r ts-node/register 'test/e2e/**/*.test.ts'", - "test:spec-main": "mocha --colors --max-old-space-size=7168 -r ./.babel-register 'test/spec/**/*mainnet.test.ts'", + "test:spec-main": "mocha --colors -r ./.babel-register 'test/spec/**/*mainnet.test.ts'", "test:spec-min": "mocha --colors -r ./.babel-register 'test/spec/**/*minimal.test.ts'", - "test:spec": "mocha --colors --max-old-space-size=7168 -r ./.babel-register 'test/spec/**/*.test.ts'", + "test:spec": "mocha --colors -r ./.babel-register 'test/spec/**/*.test.ts'", "test": "yarn test:unit && yarn test:spec && yarn test:e2e", "coverage": "codecov -F lodestar", "benchmark": "ts-node test/benchmarks" From 59fc9af1c72c2779317bc1bbb763699566503894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Sat, 7 Sep 2019 07:10:24 -0400 Subject: [PATCH 13/13] partitial ssz tests --- packages/eth2.0-spec-test-util/src/single.ts | 13 ++- packages/ssz/test/spec/core.test.ts | 94 ------------------- .../spec/minimal/static/attestation.test.ts | 56 +++++++++++ .../minimal/static/attestationData.test.ts | 51 ++++++++++ .../attestationDataAndCustodyBit.test.ts | 51 ++++++++++ .../minimal/static/attesterSlashing.test.ts | 51 ++++++++++ .../spec/minimal/static/beaconBlock.test.ts | 56 +++++++++++ .../minimal/static/beaconBlockBody.test.ts | 51 ++++++++++ .../minimal/static/beaconBlockHeader.test.ts | 51 ++++++++++ .../spec/minimal/static/beaconState.test.ts | 51 ++++++++++ .../spec/minimal/static/checkpoint.test.ts | 51 ++++++++++ .../minimal/static/compactCommitte.test.ts | 51 ++++++++++ .../spec/minimal/static/crosslink.test.ts | 51 ++++++++++ .../test/spec/minimal/static/deposit.test.ts | 51 ++++++++++ .../spec/minimal/static/depositData.test.ts | 56 +++++++++++ .../test/spec/minimal/static/eth1Data.test.ts | 51 ++++++++++ .../ssz/test/spec/minimal/static/fork.test.ts | 51 ++++++++++ packages/ssz/test/spec/type.ts | 8 ++ packages/ssz/test/spec/uint.test.ts | 59 ------------ packages/ssz/test/spec/util.ts | 65 ------------- 20 files changed, 798 insertions(+), 221 deletions(-) delete mode 100644 packages/ssz/test/spec/core.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/attestation.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/attestationData.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/attestationDataAndCustodyBit.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/attesterSlashing.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/beaconBlock.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/beaconBlockBody.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/beaconBlockHeader.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/beaconState.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/checkpoint.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/compactCommitte.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/crosslink.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/deposit.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/depositData.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/eth1Data.test.ts create mode 100644 packages/ssz/test/spec/minimal/static/fork.test.ts create mode 100644 packages/ssz/test/spec/type.ts delete mode 100644 packages/ssz/test/spec/uint.test.ts delete mode 100644 packages/ssz/test/spec/util.ts diff --git a/packages/eth2.0-spec-test-util/src/single.ts b/packages/eth2.0-spec-test-util/src/single.ts index 139dd4103f51..384a15187f56 100644 --- a/packages/eth2.0-spec-test-util/src/single.ts +++ b/packages/eth2.0-spec-test-util/src/single.ts @@ -47,6 +47,10 @@ export interface ISpecTestOptions { } +export interface ITestCaseMeta { + directoryName: string; +} + const defaultOptions: ISpecTestOptions = { inputTypes: {}, inputProcessing: {}, @@ -61,7 +65,7 @@ const defaultOptions: ISpecTestOptions = { export function describeDirectorySpecTest( name: string, testCaseDirectoryPath: string, - testFunction: (testCase: TestCase) => Result, + testFunction: (testCase: TestCase, directoryName: string) => Result, options: Partial> ): void { // @ts-ignore @@ -103,14 +107,14 @@ function generateTestCase( return this.skip(); } if(options.shouldError && options.shouldError(testCase)) { - expect(testFunction.bind(null, testCase)).to.throw; + expect(testFunction.bind(null, testCase, basename(testCaseDirectoryPath))).to.throw; } else { const profileId = `${name}-${Date.now()}.profile`; const profilingDirectory = process.env.GEN_PROFILE_DIR; if (profilingDirectory) { profiler.startProfiling(profileId); } - const result = testFunction(testCase); + const result = testFunction(testCase, basename(testCaseDirectoryPath)); if (profilingDirectory) { const profile = profiler.stopProfiling(profileId); @@ -138,6 +142,9 @@ function loadInputFiles( .forEach((file) => { const inputName = basename(file).replace(".ssz", "").replace(".yaml", ""); testCase[inputName] = deserializeTestCase(file, inputName, options); + if (file.endsWith(InputType.SSZ)) { + testCase[`${inputName}_raw`] = readFileSync(file); + } if(options.inputProcessing[inputName]) { testCase[inputName] = options.inputProcessing[inputName](testCase[inputName]); } diff --git a/packages/ssz/test/spec/core.test.ts b/packages/ssz/test/spec/core.test.ts deleted file mode 100644 index d0b6d900ebac..000000000000 --- a/packages/ssz/test/spec/core.test.ts +++ /dev/null @@ -1,94 +0,0 @@ -import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; -import {types} from "@chainsafe/eth2.0-ssz-types/lib/presets/mainnet"; - -import {deserialize, equals, hashTreeRoot, serialize, signingRoot} from "../../src"; - -import {getTestType, getTestValue, hydrateType, hydrateValue} from "./util"; - -// Serialize - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_static/core/ssz_mainnet_random.yaml"), - serialize, - (testCase) => { - const typeName = getTestType(testCase); - const value = getTestValue(testCase, 'value'); - const type = hydrateType((types as any)[typeName]); - const v = hydrateValue(value, type); - return [v, type]; - }, - (testCase) => { - const serialized = getTestValue(testCase, 'serialized'); - return serialized.slice(2); - }, - (result) => result.toString('hex'), -); - -// Deserialize - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_static/core/ssz_mainnet_random.yaml"), - deserialize, - (testCase) => { - const typeName = getTestType(testCase); - const serialized = getTestValue(testCase, 'serialized'); - const type = hydrateType((types as any)[typeName]); - return [Buffer.from(serialized.slice(2), 'hex'), type]; - }, - (testCase) => { - const typeName = getTestType(testCase); - const value = getTestValue(testCase, 'value'); - const type = hydrateType((types as any)[typeName]); - const v = hydrateValue(value, type); - return v; - }, - (result) => result, - () => false, - () => false, - (testCase, expect, expected, actual) => { - const typeName = getTestType(testCase); - const type = hydrateType((types as any)[typeName]); - expect(equals(expected, actual, type)).to.equal(true); - }, -); - -// hashTreeRoot - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_static/core/ssz_mainnet_random.yaml"), - hashTreeRoot, - (testCase) => { - const typeName = getTestType(testCase); - const value = getTestValue(testCase, 'value'); - const type = hydrateType((types as any)[typeName]); - const v = hydrateValue(value, type); - return [v, type]; - }, - (testCase) => { - const root = getTestValue(testCase, 'root'); - return root.slice(2); - }, - (result) => result.toString('hex'), -); - -// signingRoot - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_static/core/ssz_mainnet_random.yaml"), - signingRoot, - (testCase) => { - const typeName = getTestType(testCase); - const value = getTestValue(testCase, 'value'); - const type = hydrateType((types as any)[typeName]); - const v = hydrateValue(value, type); - return [v, type]; - }, - (testCase) => { - const signingRoot = getTestValue(testCase, 'signingRoot'); - return signingRoot.slice(2); - }, - (result) => result.toString('hex'), - () => false, - (testCase) => !getTestValue(testCase, 'signingRoot') -); diff --git a/packages/ssz/test/spec/minimal/static/attestation.test.ts b/packages/ssz/test/spec/minimal/static/attestation.test.ts new file mode 100644 index 000000000000..86fc56cfeedf --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/attestation.test.ts @@ -0,0 +1,56 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {Attestation, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize, signingRoot} from "../../../../src"; + +interface Result { + root: Hash; + signing: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `attestation ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Attestation/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Attestation); + const root = hashTreeRoot(testcase.serialized, config.types.Attestation); + const signing = signingRoot(testcase.serialized, config.types.Attestation); + return { + serialized, + root, + signing + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Attestation, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + signing: Buffer.from(testCase.roots.signingRoot.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + expect(expected.signing.equals(actual.signing)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/attestationData.test.ts b/packages/ssz/test/spec/minimal/static/attestationData.test.ts new file mode 100644 index 000000000000..413ae48b8bd3 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/attestationData.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {AttestationData, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize, signingRoot} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `attestation data ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/AttestationData/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.AttestationData); + const root = hashTreeRoot(testcase.serialized, config.types.AttestationData); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.AttestationData, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/attestationDataAndCustodyBit.test.ts b/packages/ssz/test/spec/minimal/static/attestationDataAndCustodyBit.test.ts new file mode 100644 index 000000000000..92add08e38a3 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/attestationDataAndCustodyBit.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {AttestationDataAndCustodyBit, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `attestation data and custody bit ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/AttestationDataAndCustodyBit/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.AttestationDataAndCustodyBit); + const root = hashTreeRoot(testcase.serialized, config.types.AttestationDataAndCustodyBit); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.AttestationDataAndCustodyBit, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/attesterSlashing.test.ts b/packages/ssz/test/spec/minimal/static/attesterSlashing.test.ts new file mode 100644 index 000000000000..bb41db0fa15f --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/attesterSlashing.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {AttesterSlashing, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `attester slashing ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/AttesterSlashing/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.AttesterSlashing); + const root = hashTreeRoot(testcase.serialized, config.types.AttesterSlashing); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.AttesterSlashing, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/beaconBlock.test.ts b/packages/ssz/test/spec/minimal/static/beaconBlock.test.ts new file mode 100644 index 000000000000..5760d4549982 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/beaconBlock.test.ts @@ -0,0 +1,56 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {AttesterSlashing, BeaconBlock, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize, signingRoot} from "../../../../src"; + +interface Result { + root: Hash; + signing: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `beacon block ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/BeaconBlock/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.BeaconBlock); + const root = hashTreeRoot(testcase.serialized, config.types.BeaconBlock); + const signing = signingRoot(testcase.serialized, config.types.BeaconBlock); + return { + serialized, + signing, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.BeaconBlock, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + signing: Buffer.from(testCase.roots.signingRoot.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + expect(expected.signing.equals(actual.signing)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/beaconBlockBody.test.ts b/packages/ssz/test/spec/minimal/static/beaconBlockBody.test.ts new file mode 100644 index 000000000000..8918127d033d --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/beaconBlockBody.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {BeaconBlockBody, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `beacon block body ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/BeaconBlockBody/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.BeaconBlockBody); + const root = hashTreeRoot(testcase.serialized, config.types.BeaconBlockBody); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.BeaconBlockBody, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/beaconBlockHeader.test.ts b/packages/ssz/test/spec/minimal/static/beaconBlockHeader.test.ts new file mode 100644 index 000000000000..dc18f2a0696f --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/beaconBlockHeader.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {BeaconBlockHeader, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `beacon block header ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/BeaconBlockHeader/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.BeaconBlockHeader); + const root = hashTreeRoot(testcase.serialized, config.types.BeaconBlockHeader); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.BeaconBlockHeader, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/beaconState.test.ts b/packages/ssz/test/spec/minimal/static/beaconState.test.ts new file mode 100644 index 000000000000..77453e5c2575 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/beaconState.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {BeaconState, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `beacon state ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/BeaconState/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.BeaconState); + const root = hashTreeRoot(testcase.serialized, config.types.BeaconState); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.BeaconState, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/checkpoint.test.ts b/packages/ssz/test/spec/minimal/static/checkpoint.test.ts new file mode 100644 index 000000000000..6e2cd516bb2e --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/checkpoint.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {Checkpoint, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `checkpoint ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Checkpoint/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Checkpoint); + const root = hashTreeRoot(testcase.serialized, config.types.Checkpoint); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Checkpoint, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/compactCommitte.test.ts b/packages/ssz/test/spec/minimal/static/compactCommitte.test.ts new file mode 100644 index 000000000000..644d1015a7fd --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/compactCommitte.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {Checkpoint, CompactCommittee, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `compact committee ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/CompactCommittee/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.CompactCommittee); + const root = hashTreeRoot(testcase.serialized, config.types.CompactCommittee); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.CompactCommittee, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/crosslink.test.ts b/packages/ssz/test/spec/minimal/static/crosslink.test.ts new file mode 100644 index 000000000000..866bd7737556 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/crosslink.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {CompactCommittee, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `crosslink ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Crosslink/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Crosslink); + const root = hashTreeRoot(testcase.serialized, config.types.Crosslink); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Crosslink, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/deposit.test.ts b/packages/ssz/test/spec/minimal/static/deposit.test.ts new file mode 100644 index 000000000000..e8fdb4916cb4 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/deposit.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {CompactCommittee, Deposit, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `deposit ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Deposit/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Deposit); + const root = hashTreeRoot(testcase.serialized, config.types.Deposit); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Deposit, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/depositData.test.ts b/packages/ssz/test/spec/minimal/static/depositData.test.ts new file mode 100644 index 000000000000..fb656fec625c --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/depositData.test.ts @@ -0,0 +1,56 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {CompactCommittee, Deposit, DepositData, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize, signingRoot} from "../../../../src"; + +interface Result { + root: Hash; + signing: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `deposit data ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/DepositData/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.DepositData); + const root = hashTreeRoot(testcase.serialized, config.types.DepositData); + const signing = signingRoot(testcase.serialized, config.types.DepositData); + return { + serialized, + signing, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.DepositData, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + signing: Buffer.from(testCase.roots.signingRoot.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + expect(expected.signing.equals(actual.signing)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/eth1Data.test.ts b/packages/ssz/test/spec/minimal/static/eth1Data.test.ts new file mode 100644 index 000000000000..7cf6753c9607 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/eth1Data.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {Eth1Data, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `eth1 data ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Eth1Data/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Eth1Data); + const root = hashTreeRoot(testcase.serialized, config.types.Eth1Data); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Eth1Data, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/minimal/static/fork.test.ts b/packages/ssz/test/spec/minimal/static/fork.test.ts new file mode 100644 index 000000000000..62131153b994 --- /dev/null +++ b/packages/ssz/test/spec/minimal/static/fork.test.ts @@ -0,0 +1,51 @@ +import {describeDirectorySpecTest, InputType} from "@chainsafe/eth2.0-spec-test-util/lib/single"; +import {Fork, Hash} from "@chainsafe/eth2.0-types"; +import {join} from "path"; +import {config} from "@chainsafe/eth2.0-config/lib/presets/minimal"; +import {expect} from "chai"; +import {BaseSSZStaticTestCase} from "../../type"; +import {hashTreeRoot, serialize} from "../../../../src"; + +interface Result { + root: Hash; + serialized: Buffer; +} + +["ssz_lengthy", "ssz_max", "ssz_nil", "ssz_one", "ssz_random", "ssz_random_chaos", "ssz_zero"].forEach((caseName) => { + + describeDirectorySpecTest, Result>( + `fork ${caseName} minimal`, + join(__dirname, `../../../../../spec-test-cases/tests/minimal/phase0/ssz_static/Fork/${caseName}`), + (testcase) => { + const serialized = serialize(testcase.serialized, config.types.Fork); + const root = hashTreeRoot(testcase.serialized, config.types.Fork); + return { + serialized, + root + }; + }, + { + // @ts-ignore + inputTypes: { + roots: InputType.YAML, + serialized: InputType.SSZ + }, + // @ts-ignore + sszTypes: { + serialized: config.types.Fork, + }, + getExpected: (testCase => { + return { + root: Buffer.from(testCase.roots.root.replace("0x", ""), "hex"), + // @ts-ignore + serialized: testCase.serialized_raw + }; + }), + expectFunc: (testCase, expected, actual) => { + expect(expected.serialized.equals(actual.serialized)).to.be.true; + expect(expected.root.equals(actual.root)).to.be.true; + } + } + ); + +}); diff --git a/packages/ssz/test/spec/type.ts b/packages/ssz/test/spec/type.ts new file mode 100644 index 000000000000..346bb16dc92c --- /dev/null +++ b/packages/ssz/test/spec/type.ts @@ -0,0 +1,8 @@ +export interface BaseSSZStaticTestCase { + roots: { + root: string; + signingRoot: string; + }; + serialized: T; + value: T; +} \ No newline at end of file diff --git a/packages/ssz/test/spec/uint.test.ts b/packages/ssz/test/spec/uint.test.ts deleted file mode 100644 index e10cc11c71a2..000000000000 --- a/packages/ssz/test/spec/uint.test.ts +++ /dev/null @@ -1,59 +0,0 @@ -import {join} from "path"; -import {describeMultiSpec} from "@chainsafe/eth2.0-spec-test-util"; -import BN from "bn.js"; - -import {deserialize, serialize} from "../../src"; -import {UintCase} from "../util/specTypes/uint"; - -// uint bounds - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_generic/uint/uint_bounds.yaml"), - serialize, - ({value, type}) => ([new BN(value), type]), - ({ssz}) => ssz.slice(2), - (result) => result.toString('hex'), - ({valid}) => !valid, -); - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_generic/uint/uint_bounds.yaml"), - deserialize, - ({ssz, type}) => ([Buffer.from(ssz.slice(2), 'hex'), type]), - ({value}) => (new BN(value)).toArrayLike(Buffer, 'le', 256).toString('hex'), - (result) => (new BN(result)).toArrayLike(Buffer, 'le', 256).toString('hex'), - () => false, - ({valid}) => !valid, -); - -// uint random - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_generic/uint/uint_random.yaml"), - serialize, - ({value, type}) => ([new BN(value), type]), - ({ssz}) => ssz.slice(2), - (result) => result.toString('hex'), - ({valid}) => !valid, -); - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_generic/uint/uint_random.yaml"), - deserialize, - ({ssz, type}) => ([Buffer.from(ssz.slice(2), 'hex'), type]), - ({value}) => (new BN(value)).toArrayLike(Buffer, 'le', 256).toString('hex'), - (result) => (new BN(result)).toArrayLike(Buffer, 'le', 256).toString('hex'), - () => false, - ({valid}) => !valid, -); - -// uint wrong length - -describeMultiSpec( - join(__dirname, "../../../spec-test-cases/tests/ssz_generic/uint/uint_wrong_length.yaml"), - deserialize, - ({ssz, type}) => ([Buffer.from(ssz.slice(2), 'hex'), type]), - ({value}) => (new BN(value)).toArrayLike(Buffer, 'le', 256).toString('hex'), - (result) => (new BN(result)).toArrayLike(Buffer, 'le', 256).toString('hex'), - ({valid}) => !valid, -); diff --git a/packages/ssz/test/spec/util.ts b/packages/ssz/test/spec/util.ts deleted file mode 100644 index 4711efa9c10c..000000000000 --- a/packages/ssz/test/spec/util.ts +++ /dev/null @@ -1,65 +0,0 @@ -import {BitList, BitVector} from "@chainsafe/bit-utils"; -// eslint-disable-next-line import/no-extraneous-dependencies -import camelCase from "camelcase"; -import { - FullSSZType, - parseType, - Type, -} from "../../src"; - -// remove all 'number' uint types, yaml spec tests convert all numbers to BN -export function hydrateType(type: any): FullSSZType { - return _hydrateType(parseType(type)); -} - -function _hydrateType(type: FullSSZType) { - switch (type.type) { - case Type.uint: - type.useNumber = false; - break; - case Type.list: - case Type.vector: - type.elementType = _hydrateType(type.elementType); - break; - case Type.container: - type.fields = type.fields.map(([fieldName, fieldType]): [string, FullSSZType] => ([fieldName, _hydrateType(fieldType)])); - break; - } - return type; -} - -export function hydrateValue(obj: any, type: any): any { - return _hydrateValue(obj, parseType(type)); -} - -function _hydrateValue(obj: any, type: FullSSZType): any { - switch (type.type) { - case Type.uint: - return obj; - case Type.bool: - return obj; - case Type.bitList: - return BitList.deserialize(Buffer.from(obj.slice(2), 'hex')); - case Type.bitVector: - return BitVector.fromBitfield(Buffer.from(obj.slice(2), 'hex'), type.length); - case Type.byteList: - case Type.byteVector: - return Buffer.from(obj.slice(2), 'hex'); - case Type.list: - case Type.vector: - return obj.map((element: any) => hydrateValue(element, type.elementType)); - case Type.container: - type.fields.forEach(([fieldName, fieldType]) => { - obj[fieldName] = hydrateValue(obj[fieldName], fieldType); - }); - return obj; - } -} - -export function getTestType(o: any): string { - return camelCase(Object.keys(o)[0], {pascalCase: true}); -} - -export function getTestValue(o: any, value: string) { - return Object.values(o)[0][value]; -}