From 7ceebf48d5cb772e1cfe95adc0d6b1c7a32b5ddf Mon Sep 17 00:00:00 2001 From: "g. nicholas d'andrea" Date: Wed, 29 Apr 2020 01:13:42 -0400 Subject: [PATCH] Generate Hasher artifact upon compile, not migrate To fix intermittent `truffle test` failures, use Truffle's external compiler system for generating a valid artifact for Hasher at compile-time instead of in migrations/2_deploy_hasher.js. - Define compileHelper.js script, which outputs temporary artifact data to `./build/Hasher.json`. - Configure `external` compiler in truffle-config to run compileHelper and process result - Remove usage of @truffle/artifactor in migration, since the artifact will now already exist --- compileHasher.js | 21 +++++++++++++++++++++ migrations/2_deploy_hasher.js | 21 +++------------------ package-lock.json | 20 -------------------- package.json | 1 - truffle-config.js | 6 ++++++ 5 files changed, 30 insertions(+), 39 deletions(-) create mode 100644 compileHasher.js diff --git a/compileHasher.js b/compileHasher.js new file mode 100644 index 0000000..aa61e2c --- /dev/null +++ b/compileHasher.js @@ -0,0 +1,21 @@ +// Generates Hasher artifact at compile-time using Truffle's external compiler +// mechanism +const path = require('path') +const fs = require('fs') +const genContract = require('circomlib/src/mimcsponge_gencontract.js') + +// where Truffle will expect to find the results of the external compiler +// command +const outputPath = path.join(__dirname, 'build', 'Hasher.json') + +function main () { + const contract = { + contractName: 'Hasher', + abi: genContract.abi, + bytecode: genContract.createCode('mimcsponge', 220) + } + + fs.writeFileSync(outputPath, JSON.stringify(contract)) +} + +main() diff --git a/migrations/2_deploy_hasher.js b/migrations/2_deploy_hasher.js index efb9ae5..50b12c5 100644 --- a/migrations/2_deploy_hasher.js +++ b/migrations/2_deploy_hasher.js @@ -1,21 +1,6 @@ /* global artifacts */ -const path = require('path') +const Hasher = artifacts.require('Hasher') -const genContract = require('circomlib/src/mimcsponge_gencontract.js') -const Artifactor = require('@truffle/artifactor') - -module.exports = function(deployer) { - return deployer.then( async () => { - const contractsDir = path.join(__dirname, '..', 'build/contracts') - let artifactor = new Artifactor(contractsDir) - let contractName = 'Hasher' - await artifactor.save({ - contractName, - abi: genContract.abi, - unlinked_binary: genContract.createCode('mimcsponge', 220), - }).then(async () => { - const hasherContract = artifacts.require(contractName) - await deployer.deploy(hasherContract) - }) - }) +module.exports = async function(deployer) { + await deployer.deploy(Hasher) } diff --git a/package-lock.json b/package-lock.json index b2abaac..f2622d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -151,16 +151,6 @@ "defer-to-connect": "^1.0.1" } }, - "@truffle/artifactor": { - "version": "4.0.38", - "resolved": "https://registry.npmjs.org/@truffle/artifactor/-/artifactor-4.0.38.tgz", - "integrity": "sha512-blkKmw0CdiJ4V2Xz9s8aHhcHHE3nr5jgQrgyI2fQhW3gfWLUUl9TQ+TLgNBDd90+aUqhPyTQRzy8lH1Z4xxmmg==", - "requires": { - "@truffle/contract-schema": "^3.0.18", - "fs-extra": "6.0.1", - "lodash": "^4.17.13" - } - }, "@truffle/blockchain-utils": { "version": "0.0.14", "resolved": "https://registry.npmjs.org/@truffle/blockchain-utils/-/blockchain-utils-0.0.14.tgz", @@ -3683,16 +3673,6 @@ "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" }, - "fs-extra": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-6.0.1.tgz", - "integrity": "sha512-GnyIkKhhzXZUWFCaJzvyDLEEgDkPfb4/TPvJCJVuS8MWZgoSsErf++QpiAlDnKFcqhRlm+tIOcencCjyJE6ZCA==", - "requires": { - "graceful-fs": "^4.1.2", - "jsonfile": "^4.0.0", - "universalify": "^0.1.0" - } - }, "fs-minipass": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", diff --git a/package.json b/package.json index 314e89d..2c1c9af 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "license": "ISC", "dependencies": { "@openzeppelin/contracts": "^2.4.0", - "@truffle/artifactor": "^4.0.38", "@truffle/contract": "^4.0.39", "@truffle/hdwallet-provider": "^1.0.24", "axios": "^0.19.0", diff --git a/truffle-config.js b/truffle-config.js index 04451ba..4a5abd8 100644 --- a/truffle-config.js +++ b/truffle-config.js @@ -95,6 +95,12 @@ module.exports = { }, // evmVersion: "byzantium" } + }, + external: { + command: 'node ./compileHasher.js', + targets: [{ + path: './build/Hasher.json' + }] } } }