Skip to content

Commit

Permalink
test: include test vector submodule and runner for execution proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell committed May 8, 2023
1 parent 4a1f76c commit 4d69211
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/contracts-eth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
- name: Clone the repository
uses: actions/checkout@v3
with:
submodules: 'true'
lfs: 'true'

- name: Execute
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "near-light-client-tests"]
path = near-light-client-tests
url = https://github.com/austinabell/near-light-client-tests.git
3 changes: 2 additions & 1 deletion contracts/eth/nearprover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"readline-sync": "^1.4.10",
"solc": "0.8.3",
"solidity-coverage": "^0.7.16",
"solium": "^1.2.5"
"solium": "^1.2.5",
"bs58": "^4.0.1"
},
"scripts": {
"build": "hardhat compile",
Expand Down
45 changes: 45 additions & 0 deletions contracts/eth/nearprover/test/NearProver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { ethers } = require('hardhat');
const { borshifyOutcomeProof } = require(`rainbow-bridge-utils`);
const fs = require('fs').promises;
const { computeMerkleRoot } = require('../utils/utils');
const bs58 = require('bs58');
const { getAllJsonFilesRecursive } = require('../../../../utils/proof-vector-utils');

let NearProver, NearBridgeMock;

Expand All @@ -15,6 +17,49 @@ beforeEach(async function () {
);
});

async function runTestVectors(testVectors) {
for (const test of testVectors) {
const {
description,
params: { proof, block_merkle_root },
expected: { is_valid, error },
} = test;
let wasValid;
let executionError;
try {
const blockMerkleRoot = bs58.decode(block_merkle_root).toString('hex');
// Note, the height shouldn't matter here, defaulting to 100
const height = 100;
expect(blockMerkleRoot === computeMerkleRoot(proof).toString('hex')).to.be.true;
await NearBridgeMock.setBlockMerkleRoot(height, "0x" + blockMerkleRoot);
wasValid = await NearProver.proveOutcome(borshifyOutcomeProof(proof), height);
} catch (error) {
wasValid = false;
executionError = error;
}
if (wasValid !== is_valid) {
const prefix = `Test Case "${description}": FAILED - expected`;
throw new Error(
`${prefix} ${is_valid
? `valid, got error ${executionError}`
: `invalid result${error ? ` with error "${error}"` : ""}`
}`
);
}
}
}

describe('execution outcome proof vectors', async function () {
const files = getAllJsonFilesRecursive("../../../near-light-client-tests/test-vectors/executions");

for (const file of files) {
const fileName = file.split('\\').pop().split('/').pop();
it(`execution vector file "${fileName}"`, async function () {
await runTestVectors(require("../" + file));
});
}
});

async function testProof(merkleRoot, height, proofPath) {
let proof = require(proofPath);
console.log(computeMerkleRoot(proof).toString('hex'));
Expand Down
1 change: 1 addition & 0 deletions near-light-client-tests
22 changes: 22 additions & 0 deletions utils/proof-vector-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { join } = require("path");
const { readdirSync, statSync } = require("fs");

function getAllJsonFilesRecursive(dirPath) {
let arrayOfFiles = [];
const files = readdirSync(dirPath);

files.forEach((file) => {
if (statSync(join(dirPath, file)).isDirectory()) {
const subDirFiles = getAllJsonFilesRecursive(join(dirPath, file));
arrayOfFiles = arrayOfFiles.concat(subDirFiles);
} else if (file.endsWith(".json")) {
arrayOfFiles.push(join(dirPath, file));
}
});

return arrayOfFiles;
}

module.exports = {
getAllJsonFilesRecursive,
}

0 comments on commit 4d69211

Please sign in to comment.