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
This commit is contained in:
g. nicholas d'andrea 2020-04-29 01:13:42 -04:00
parent a533ad9ffb
commit 7ceebf48d5
5 changed files with 30 additions and 39 deletions

21
compileHasher.js Normal file
View File

@ -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()

View File

@ -1,21 +1,6 @@
/* global artifacts */ /* global artifacts */
const path = require('path') const Hasher = artifacts.require('Hasher')
const genContract = require('circomlib/src/mimcsponge_gencontract.js') module.exports = async function(deployer) {
const Artifactor = require('@truffle/artifactor') await deployer.deploy(Hasher)
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)
})
})
} }

20
package-lock.json generated
View File

@ -151,16 +151,6 @@
"defer-to-connect": "^1.0.1" "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": { "@truffle/blockchain-utils": {
"version": "0.0.14", "version": "0.0.14",
"resolved": "https://registry.npmjs.org/@truffle/blockchain-utils/-/blockchain-utils-0.0.14.tgz", "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", "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" "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": { "fs-minipass": {
"version": "1.2.7", "version": "1.2.7",
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz",

View File

@ -28,7 +28,6 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@openzeppelin/contracts": "^2.4.0", "@openzeppelin/contracts": "^2.4.0",
"@truffle/artifactor": "^4.0.38",
"@truffle/contract": "^4.0.39", "@truffle/contract": "^4.0.39",
"@truffle/hdwallet-provider": "^1.0.24", "@truffle/hdwallet-provider": "^1.0.24",
"axios": "^0.19.0", "axios": "^0.19.0",

View File

@ -95,6 +95,12 @@ module.exports = {
}, },
// evmVersion: "byzantium" // evmVersion: "byzantium"
} }
},
external: {
command: 'node ./compileHasher.js',
targets: [{
path: './build/Hasher.json'
}]
} }
} }
} }