Skip to content

Commit

Permalink
regenerate deploy info when it's the same address but changed (for lo…
Browse files Browse the repository at this point in the history
…cal chains)
  • Loading branch information
szerintedmi committed Apr 24, 2018
1 parent 0ce7b1c commit 9fd0ef3
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/deploymentsExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
const contractName = truffleContractFileContent.contractName;
const deploymentsFileName = contractName + '_DEPLOYS.json';
const generatedAt = new Date().toISOString();
const bytecodeHash = md5(truffleContractFileContent.bytecode);
const deployedBytecodeHash = md5(truffleContractFileContent.deployedBytecode);
const sourceHash = md5(truffleContractFileContent.source);

/**************************************
* Parse each network in truffle contract file, create <networkId> folder and update deployments file
Expand All @@ -17,6 +20,7 @@ module.exports = {
.filter(([network, entry]) => onlyNetworkId === null || network === onlyNetworkId)
.forEach(([network, entry]) => {
const deploymentsFilePath = outputDirectory + '/' + network;

if (!filesLib.directoryExists(deploymentsFilePath)) {
try {
filesLib.mkDirByPathSync(deploymentsFilePath);
Expand All @@ -36,11 +40,24 @@ module.exports = {
deploys = filesLib.readJsonFile(deploysFile);
}

const deploymentInfoExists =
const deploymentAddressExists =
deploys[abiHash] && deploys[abiHash].deployments && deploys[abiHash].deployments[entry.address]
? true
: false;
if (deploymentInfoExists && !regenerate) {

// handle special case for ganache or private network deploys where the same address possible with different code
// in this case we will update the deployment info even if regenerate is not set
let infoChangedForDeployedAddress = false;
if (deploymentAddressExists) {
const existingEntry = deploys[abiHash].deployments[entry.address];

infoChangedForDeployedAddress =
existingEntry.bytecodeHash !== bytecodeHash ||
existingEntry.deployedBytecodeHash !== deployedBytecodeHash ||
existingEntry.sourceHash !== sourceHash;
}

if (deploymentAddressExists && !regenerate && !infoChangedForDeployedAddress) {
console.log(
`\tContract ${contractName} already on network: ${network} with abi hash: ${abiHash} at address ${
entry.address
Expand All @@ -66,9 +83,9 @@ module.exports = {
truffleContractFileUpdatedAt: truffleContractFileContent.updatedAt,
deployTransactionHash: entry.transactionHash,
compiler: truffleContractFileContent.compiler,
bytecodeHash: md5(truffleContractFileContent.bytecode),
deployedBytecodeHash: md5(truffleContractFileContent.deployedBytecode),
sourceHash: md5(truffleContractFileContent.source),
bytecodeHash,
deployedBytecodeHash,
sourceHash,
source: truffleContractFileContent.source
};
deploys[abiHash].deployments[entry.address] = newEntry;
Expand All @@ -78,8 +95,8 @@ module.exports = {
.writeFile(deploysFile, deploysFileNewContent)
.then(() => {
console.log(
`\t${
deploymentInfoExists ? 'Updated' : 'Created'
`${
deploymentAddressExists ? 'Updated' : 'Created'
} deployment info for abi hash ${abiHash} with network id ${network} in: ${deploysFile}`
);
})
Expand Down

0 comments on commit 9fd0ef3

Please sign in to comment.