Skip to content

Commit

Permalink
gas: increment by one tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbale-altoros committed Jan 23, 2024
1 parent 4d99314 commit bda8bca
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions conf/rulesets/solhint-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = Object.freeze({
],
'constructor-syntax': 'warn',
'gas-calldata-parameters': 'warn',
'gas-increment-by-one': 'warn',
'gas-indexed-events': 'warn',
'gas-multitoken1155': 'warn',
'gas-small-strings': 'warn',
Expand Down
4 changes: 0 additions & 4 deletions lib/rules/gas-consumption/gas-increment-by-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ class GasIncrementByOne extends BaseChecker {

module.exports = GasIncrementByOne

// unary operations
// if (node.expression.type === 'UnaryOperation') {
// }

// SourceUnit(node) {
// this.findVariableUpdates(node)
// }
Expand Down
108 changes: 108 additions & 0 deletions test/rules/gas-consumption/gas-increment-by-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const assert = require('assert')
const linter = require('../../../lib/index')
const { funcWith } = require('../../common/contract-builder')

const replaceErrorMsg = (variableName) => {
const errMsg = `GC: For [ ${variableName} ] variable, increment/decrement by 1 using: [ ++variable ] to save gas`
return errMsg
}

const RAISE_ERRORS = [
{
statement: 'myStringArray[0] = myStringArray[0] - 1;',
varName: 'myStringArray',
},
{
statement: 'customStruct.value = customStruct.value + 1;',
varName: 'customStruct',
},
{
statement: 'testMapping2[account].value = testMapping2[account].value + 1;',
varName: 'testMapping2',
},
{
statement: 'testMapping3[account][token].value = testMapping3[account][token].value + 1;',
varName: 'testMapping3',
},
{
statement: 'someArray[index]--;',
varName: 'someArray',
},
{
statement: 'anotherCounter++;',
varName: 'anotherCounter',
},
{
statement: 'balance1 += 1;',
varName: 'balance1',
},
{
statement: 'anotherVar -= 1;',
varName: 'anotherVar',
},
// {
// statement:
// 'complexMapping[user].balances[asset] = complexMapping[user].balances[asset] + 1 + someFunctionCall();',
// varName: 'complexMapping',
// },
// {
// statement: 'anotherMapping[user][token].count = anotherMapping[user][token].count - 1 + 5;',
// varName: 'anotherMapping',
// },
]

const NOT_RAISE_ERRORS = [
{
statement: 'edgeCaseVar = edgeCaseVar * 2 + 1;',
varName: 'edgeCaseVar',
},
{
statement: 'bytes32 byteArray = byteArray + bytes32(1);',
varName: 'byteArray',
},
{
statement: '--balance1;',
varName: 'balance1',
},
{
statement: '++balance2;',
varName: 'balance2',
},
{
statement: 'someVar = someVar + 2;',
varName: 'someVar',
},
{
statement: 'unrelatedVar = someVar + 1;',
varName: 'unrelatedVar',
},
{
statement: 'uint256 result = someVar + 1;',
varName: 'result',
},
]

describe('Linter - gas-increment-by-one', () => {
for (const result of RAISE_ERRORS) {
it(`should raise error for ${result.varName}`, () => {
const code = funcWith(result.statement)
const report = linter.processStr(code, {
rules: { 'gas-increment-by-one': 'error' },
})

assert.equal(report.errorCount, 1)
assert.equal(report.messages[0].message, replaceErrorMsg(result.varName))
})
}

for (const result of NOT_RAISE_ERRORS) {
it(`should NOT raise error for ${result.varName}`, () => {
const code = funcWith(result.statement)
const report = linter.processStr(code, {
rules: { 'gas-increment-by-one': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
1 change: 0 additions & 1 deletion test/rules/naming/foundry-test-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('Linter - foundry-test-functions', () => {
const functionDefinition = composeFunctionName(prefix, 'FunctionName()', 'public')
const code = contractWith(functionDefinition)

console.log('`code` :>> ', code)
const report = linter.processStr(code, {
rules: { 'foundry-test-functions': ['error', ['setUp', 'finish']] },
})
Expand Down

0 comments on commit bda8bca

Please sign in to comment.