Skip to content

Rename assertJump to assertInvalidOpcode #1191

@barakman

Description

@barakman

🎉 Description

File assertJump name and contents are misleading.

It provides an infrastructure for asserting Invalid-Opcode exceptions, not Invalid-Jump exceptions.

I have suggested here several times in the past to merge the various exception-handling files into a single common utility, but if this is not planned in the near future and assertJump is here to stay, then perhaps it would be appropriate to replace the Jump suffix in the file name and exports.

I do realize that this is an API-breaking change though (for anyone using this file directly).

The code that I have suggested in the past (just in case):

module.exports.errTypes = {
    revert: "revert",
    outOfGas: "out of gas",
    invalidJump: "invalid JUMP",
    invalidOpcode: "invalid opcode",
    stackOverflow: "stack overflow",
    stackUnderflow: "stack underflow",
    staticStateChange: "static state change"
}

module.exports.tryCatch = async function(promise, errType) {
    try {
        await promise;
        throw null;
    }
    catch (error) {
        assert(error, "Expected an error but did not get one");
        assert(error.message.startsWith(PREFIX + errType), "Expected an error starting with '" + PREFIX + errType + "' but got '" + error.message + "' instead");
    }
};

const PREFIX = "VM Exception while processing transaction: ";

Usage example:

await tryCatch(myContract.func(arg1, arg2, arg3), errTypes.revert);

Here is a slightly modified version of the above, which IMO makes the usage a little easier (client code becoming easier to read and to maintain):

const PREFIX = "VM Exception while processing transaction: ";

async function tryCatch(promise, message) {
    try {
        await promise;
        throw null;
    }
    catch (error) {
        assert(error, "Expected an error but did not get one");
        assert(error.message.startsWith(PREFIX + message), "Expected an error starting with '" + PREFIX + message + "' but got '" + error.message + "' instead");
    }
};

module.exports = {
    catchRevert            : async function(promise) {await tryCatch(promise, "revert"             );},
    catchOutOfGas          : async function(promise) {await tryCatch(promise, "out of gas"         );},
    catchInvalidJump       : async function(promise) {await tryCatch(promise, "invalid JUMP"       );},
    catchInvalidOpcode     : async function(promise) {await tryCatch(promise, "invalid opcode"     );},
    catchStackOverflow     : async function(promise) {await tryCatch(promise, "stack overflow"     );},
    catchStackUnderflow    : async function(promise) {await tryCatch(promise, "stack underflow"    );},
    catchStaticStateChange : async function(promise) {await tryCatch(promise, "static state change");},
};

Usage example:

await catchRevert(myContract.func(arg1, arg2, arg3));

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeChanges that break backwards compatibility of the public API.testsTest suite and helpers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions