Consensys / surya

A set of utilities for exploring Solidity contracts
Apache License 2.0
1.06k stars 118 forks source link

Negative Modifiers Functionality for `mdreport` #162

Closed Rhynorater closed 2 years ago

Rhynorater commented 2 years ago

I created a feature in my private fork of surya that adds the -n parameter to the mdreport command. When this parameter is defined, it also lists the modifiers that are NOT present. This may help catch issues relating to missing modifiers.

image

In the above scenario, onlyMinters, onlyMasterMinter, checkWhitelist, are shown to be NOT present.

Note that the way this is implemented is by running:

    parser.visit(ast, {
      ModifierInvocation: function ModifierInvocation(node){
        if (modifiers.indexOf(node.name) == -1){
          modifiers.push(node.name);
        }
      },
      ModifierDefinition: function ModifierDefinition(node){
        if (modifiers.indexOf(node.name) == -1){
          modifiers.push(node.name);
        }
      }
    });

before the main call to parser.visit to enumerate all of the modifiers called in this file. All modifiers that are invoked AND all modifiers that are defined are evaluated and pushed into an array. That array is used to determine which modifiers are missing. If there is a better implementation for this, I'd love to know.

Thanks, hope others will find this useful as well. Rhynorater