-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathgas-small-strings.js
159 lines (133 loc) · 3.97 KB
/
gas-small-strings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const BaseChecker = require('../base-checker')
const ruleId = 'gas-small-strings'
const meta = {
type: 'gas-consumption',
docs: {
description: 'Keep strings smaller than 32 bytes',
category: 'Gas Consumption Rules',
notes: [
{
note: '[source](https://www.rareskills.io/post/gas-optimization?postId=c9db474a-ff97-4fa3-a51d-fe13ccb8fe3b#viewer-ck1vq) of the rule initiative',
},
],
},
isDefault: false,
recommended: false,
defaultSetup: 'warn',
schema: null,
}
class GasSmallStrings extends BaseChecker {
constructor(reporter) {
super(reporter, ruleId, meta)
}
FileLevelConstant(node) {
let isLarger = false
if (node.typeName.name === 'string') {
isLarger = this.isStringLargerThan32Bytes(node.initialValue.value)
}
if (isLarger) {
this.reportError(node)
}
}
VariableDeclaration(node) {
let isLarger = false
if (node.parent.type === 'StateVariableDeclaration' && node.typeName.name === 'string') {
isLarger = this.isStringLargerThan32Bytes(node.expression.value)
}
if (
node.parent.type === 'StateVariableDeclaration' &&
node.typeName.type === 'ArrayTypeName' &&
node.typeName.baseTypeName.name === 'string'
) {
let isComponentLarger = false
const componentsSize = node.expression.components.length
let i = 0
while (i < componentsSize) {
isComponentLarger = this.isStringLargerThan32Bytes(node.expression.components[i].value)
i++
if (isComponentLarger) {
// update the result
isLarger = true
// if there is one larger this loop can be exited
i = componentsSize + 1
}
}
}
if (node.parent.type === 'VariableDeclarationStatement' && node.typeName.name === 'string') {
isLarger = this.isStringLargerThan32Bytes(node.parent.initialValue.value)
}
if (isLarger) {
this.reportError(node)
}
}
BinaryOperation(node) {
let isLarger = false
if (node.right.type === 'StringLiteral') {
isLarger = this.isStringLargerThan32Bytes(node.right.value)
}
if (isLarger) {
this.reportError(node)
}
}
FunctionCall(node) {
let isLarger = false
let isArgumentLarger = false
const argumentsSize = node.arguments.length
let i = 0
while (i < argumentsSize) {
if (node.arguments[i].type === 'StringLiteral') {
isArgumentLarger = this.isStringLargerThan32Bytes(node.arguments[i].value)
if (isArgumentLarger) {
// update the result
isLarger = true
// if there is one larger this loop can be exited
i = argumentsSize
}
}
i++
}
if (isLarger) {
this.reportError(node)
}
}
ReturnStatement(node) {
let isLarger = false
let isComponentLarger = false
const componentsSize = node.expression.components.length
let i = 0
while (i < componentsSize) {
if (
node.expression.components[i].type === 'StringLiteral'
// &&
// node.parent.parent.returnParameters[i].typeName.name === 'string'
) {
isComponentLarger = this.isStringLargerThan32Bytes(node.expression.components[i].value)
if (isComponentLarger) {
// update the result
isLarger = true
// if there is one larger this loop can be exited
i = componentsSize
}
}
i++
}
if (isLarger) {
this.reportError(node)
}
}
isStringLargerThan32Bytes(inputString) {
if (inputString.startsWith('0x') && inputString.length === 42) {
// Check if the input is a valid Ethereum address
return false
} else {
// Convert the string to Buffer and get its byte length
const byteLength = Buffer.from(inputString, 'utf8').length
// Check if the byte length is greater than 32
return byteLength > 32
}
}
reportError(node) {
this.error(node, 'GC: String exceeds 32 bytes')
}
}
module.exports = GasSmallStrings