Skip to content

Commit

Permalink
fix: Serialization of contract artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Mar 22, 2024
1 parent 4aca2f5 commit 4f2563f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions yarn-project/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ scripts/tmp
noir-contracts.js/src
noir-contracts.js/artifacts/
noir-contracts.js/codegenCache.json
types/fixtures/*.json

.yarn/*
!.yarn/patches
Expand Down
9 changes: 6 additions & 3 deletions yarn-project/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
"./noir": "./dest/noir/index.js"
},
"scripts": {
"build": "yarn clean && tsc -b",
"build": "yarn clean && yarn generate && tsc -b",
"build:dev": "tsc -b --watch",
"clean": "rm -rf ./dest .tsbuildinfo",
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
"test:light": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests"
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests",
"generate": "yarn generate:noir-contracts",
"generate:noir-contracts": "./scripts/copy-contracts.sh"
},
"inherits": [
"../package.common.json"
"../package.common.json",
"./package.local.json"
],
"jest": {
"preset": "ts-jest/presets/default-esm",
Expand Down
7 changes: 7 additions & 0 deletions yarn-project/types/package.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"build": "yarn clean && yarn generate && tsc -b",
"generate": "yarn generate:noir-contracts",
"generate:noir-contracts": "./scripts/copy-contracts.sh"
}
}
5 changes: 5 additions & 0 deletions yarn-project/types/scripts/copy-contracts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/bash
set -euo pipefail
mkdir -p ./fixtures

cp "../../noir-projects/noir-contracts/target/benchmarking_contract-Benchmarking.json" ./fixtures/Benchmarking.test.json
12 changes: 12 additions & 0 deletions yarn-project/types/src/abi/contract_artifact.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getSampleContractArtifact } from '../test/fixtures.js';
import { contractArtifactFromBuffer, contractArtifactToBuffer } from './contract_artifact.js';

describe('contract_artifact', () => {
it('serializes and deserializes an instance', () => {
const artifact = getSampleContractArtifact();
delete artifact.aztecNrVersion;
const serialized = contractArtifactToBuffer(artifact);
const deserialized = contractArtifactFromBuffer(serialized);
expect(deserialized).toEqual(artifact);
});
});
25 changes: 21 additions & 4 deletions yarn-project/types/src/abi/contract_artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ import { mockVerificationKey } from './mocked_keys.js';
* @returns A buffer.
*/
export function contractArtifactToBuffer(artifact: ContractArtifact): Buffer {
// TODO(@spalladino): More efficient serialization
return Buffer.from(JSON.stringify(artifact), 'utf8');
return Buffer.from(
JSON.stringify(artifact, (key, value) => {
if (
key === 'bytecode' &&
value !== null &&
typeof value === 'object' &&
value.type === 'Buffer' &&
Array.isArray(value.data)
) {
return Buffer.from(value.data).toString('base64');
}
return value;
}),
'utf-8',
);
}

/**
Expand All @@ -33,8 +46,12 @@ export function contractArtifactToBuffer(artifact: ContractArtifact): Buffer {
* @returns Deserialized artifact.
*/
export function contractArtifactFromBuffer(buffer: Buffer): ContractArtifact {
// TODO(@spalladino): More efficient serialization
return JSON.parse(buffer.toString('utf8')) as ContractArtifact;
return JSON.parse(buffer.toString('utf-8'), (key, value) => {
if (key === 'bytecode' && typeof value === 'string') {
return Buffer.from(value, 'base64');
}
return value;
});
}

/**
Expand Down
19 changes: 19 additions & 0 deletions yarn-project/types/src/test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ContractArtifact } from '@aztec/foundation/abi';

import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

import { loadContractArtifact } from '../abi/contract_artifact.js';
import { NoirCompiledContract } from '../noir/index.js';

// Copied from the build output for the contract `Benchmarking` in noir-contracts
export function getSampleContractArtifact(): ContractArtifact {
const path = getPathToFixture('Benchmarking.test.json');
const content = JSON.parse(readFileSync(path).toString()) as NoirCompiledContract;
return loadContractArtifact(content);
}

function getPathToFixture(name: string) {
return resolve(dirname(fileURLToPath(import.meta.url)), `../../fixtures/${name}`);
}

0 comments on commit 4f2563f

Please sign in to comment.