Skip to content

Commit

Permalink
Merge pull request #37 from ethpm/validator-revert
Browse files Browse the repository at this point in the history
Revert with error message when release validation fails
  • Loading branch information
cgewecke authored Jul 24, 2018
2 parents 6c7799d + 61e356a commit 3dc3309
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 77 deletions.
26 changes: 12 additions & 14 deletions contracts/PackageIndex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,18 @@ contract PackageIndex is Authorized, PackageIndexInterface {
);
}

if (!releaseValidator.validateRelease(
packageDb,
releaseDb,
msg.sender,
name,
majorMinorPatch,
preRelease,
build,
releaseLockfileURI)
)
{
// Release is invalid
return false;
}
// Run release validator. This method reverts with an error message string
// on failure.
releaseValidator.validateRelease(
packageDb,
releaseDb,
msg.sender,
name,
majorMinorPatch,
preRelease,
build,
releaseLockfileURI
);

// Compute hashes
bool _packageExists = packageExists(name);
Expand Down
21 changes: 12 additions & 9 deletions contracts/ReleaseValidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,27 @@ contract ReleaseValidator {
view
returns (bool)
{
require(address(packageDb) != 0x0, "escape:ReleaseValidator:package-db-not-set");
require(address(releaseDb) != 0x0, "escape:ReleaseValidator:release-db-not-set");

if (!validateAuthorization(packageDb, callerAddress, name)) {
if (address(packageDb) == 0x0){
// packageDb address is null
revert("escape:ReleaseValidator:package-db-not-set");
} else if (address(releaseDb) == 0x0){
// releaseDb address is null
revert("escape:ReleaseValidator:release-db-not-set");
} else if (!validateAuthorization(packageDb, callerAddress, name)) {
// package exists and msg.sender is not the owner not the package owner.
return false;
revert("escape:ReleaseValidator:caller-not-authorized");
} else if (!validateIsNewRelease(packageDb, releaseDb, name, majorMinorPatch, preRelease, build)) {
// this version has already been released.
return false;
revert("escape:ReleaseValidator:version-exists");
} else if (!validatePackageName(packageDb, name)) {
// invalid package name.
return false;
revert("escape:ReleaseValidator:invalid-package-name");
} else if (!validateReleaseLockfileURI(releaseLockfileURI)) {
// disallow empty release lockfile URI
return false;
revert("escape:ReleaseValidator:invalid-lockfile-uri");
} else if (!validateReleaseVersion(majorMinorPatch)) {
// disallow version 0.0.0
return false;
revert("escape:ReleaseValidator:invalid-release-version");
}
return true;
}
Expand Down
18 changes: 8 additions & 10 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,24 @@ module.exports = {
},

// There's variant behavior across three clients when a require gate fails during a `.call`
// + ganache-cli > 6.1.3: if require contains reason string, there's no failure.
// + ganache-cli > 6.1.3 / geth : if require contains reason string - no failure, null result.
// + testrpc-sc (coverage) : errors with a revert message
// + geth: response that web3 doesn't handle correctly.
assertCallFailure: async (promise) => {
// + geth (no reason string): response that web3 doesn't handle correctly.
assertCallFailure: async (promise, expectedResult) => {
let result;
try {
await promise;
result = await promise;
assert.fail();
} catch(err){

// testrpc-sc (ganache 6.1.0)
if (process.env.SOLIDITY_COVERAGE) {

assert(err.message.includes('revert'))

// geth: weird web3 error
// we'd also see this with newer ganache if there is no reason string
} else if (process.env.GETH) {

assert(err.message.includes('0x'));
// ganache 6.1.4 & geth (should) return a null, false, zero result
// for all the return args
} else if (process.env.NETWORK === 'ganache' || process.env.NETWORK === 'geth') {
assert(result === expectedResult || err.message.includes('0x'));
}
}
},
Expand Down
10 changes: 8 additions & 2 deletions test/packageDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ contract('PackageDB', function(accounts){
assert(!exists);
assert(num === (0).toString());

// There is a bug somewhere causing reverts
// in `view` fns to be ignored. In this case it seems like
// the modifier just falls through and the call executes.
/*
await assertCallFailure(
packageDB.getPackageData(nameHash)
);
packageDB.getPackageData(nameHash),
false
);*/

});

it('should emit `PackageDelete`', async function(){
Expand Down
3 changes: 2 additions & 1 deletion test/releaseDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ contract('ReleaseDB', function(accounts){
assert( await releaseDB.isLatestMajorTree(nameHash, trueVersionHash) );

await assertCallFailure(
releaseDB.isLatestMajorTree(nameHash, falseVersionHash)
releaseDB.isLatestMajorTree(nameHash, falseVersionHash),
false
);
})
});
Expand Down
Loading

0 comments on commit 3dc3309

Please sign in to comment.