Skip to content

Commit 5db2907

Browse files
authored
Merge pull request #12653 from nishant-sachdeva/selector_for_custom_errors
Added errorSelector field for custom errors
2 parents daad9a4 + 195f72e commit 5db2907

10 files changed

+124
-25
lines changed

Changelog.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ Language Features:
44

55

66
Compiler Features:
7-
7+
* JSON-AST: Added selector field for errors and events.
88

99
Bugfixes:
1010

1111

12-
1312
### 0.8.12 (2022-02-16)
1413

1514
Language Features:

libsolidity/ast/ASTJsonConverter.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <libsolutil/JSON.h>
3333
#include <libsolutil/UTF8.h>
3434
#include <libsolutil/CommonData.h>
35+
#include <libsolutil/Keccak256.h>
3536

3637
#include <boost/algorithm/string/join.hpp>
3738

@@ -493,24 +494,36 @@ bool ASTJsonConverter::visit(ModifierInvocation const& _node)
493494
bool ASTJsonConverter::visit(EventDefinition const& _node)
494495
{
495496
m_inEvent = true;
496-
setJsonNode(_node, "EventDefinition", {
497+
std::vector<pair<string, Json::Value>> _attributes = {
497498
make_pair("name", _node.name()),
498499
make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
499500
make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue),
500501
make_pair("parameters", toJson(_node.parameterList())),
501502
make_pair("anonymous", _node.isAnonymous())
502-
});
503+
};
504+
if (m_stackState >= CompilerStack::State::AnalysisPerformed)
505+
_attributes.emplace_back(
506+
make_pair(
507+
"eventSelector",
508+
toHex(u256(h256::Arith(util::keccak256(_node.functionType(true)->externalSignature()))))
509+
));
510+
511+
setJsonNode(_node, "EventDefinition", std::move(_attributes));
503512
return false;
504513
}
505514

506515
bool ASTJsonConverter::visit(ErrorDefinition const& _node)
507516
{
508-
setJsonNode(_node, "ErrorDefinition", {
517+
std::vector<pair<string, Json::Value>> _attributes = {
509518
make_pair("name", _node.name()),
510519
make_pair("nameLocation", sourceLocationToString(_node.nameLocation())),
511520
make_pair("documentation", _node.documentation() ? toJson(*_node.documentation()) : Json::nullValue),
512521
make_pair("parameters", toJson(_node.parameterList()))
513-
});
522+
};
523+
if (m_stackState >= CompilerStack::State::AnalysisPerformed)
524+
_attributes.emplace_back(make_pair("errorSelector", _node.functionType(true)->externalIdentifierHex()));
525+
526+
setJsonNode(_node, "ErrorDefinition", std::move(_attributes));
514527
return false;
515528
}
516529

test/libsolidity/ASTJSON/documentation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
"src": "69:26:3",
163163
"text": "Some comment on Evt."
164164
},
165+
"eventSelector": "a69007916fc1145953e5a7032d7c3eab4b8e2f33ec59b0f71e732904eeede3a4",
165166
"id": 12,
166167
"name": "Evt",
167168
"nameLocation": "102:3:3",

test/libsolidity/ASTJSON/event_definition.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
[
3131
{
3232
"anonymous": false,
33+
"eventSelector": "92bbf6e823a631f3c8e09b1c8df90f378fb56f7fbc9701827e1ff8aad7f6a028",
3334
"id": 2,
3435
"name": "E",
3536
"nameLocation": "19:1:1",

test/libsolidity/ASTJSON/event_with_variables_of_internal_types.json

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
contract C {
2+
event E(function() internal);
3+
}
4+
5+
// ----
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"absolutePath": "a",
3+
"id": 8,
4+
"nodeType": "SourceUnit",
5+
"nodes":
6+
[
7+
{
8+
"abstract": false,
9+
"baseContracts": [],
10+
"contractDependencies": [],
11+
"contractKind": "contract",
12+
"id": 7,
13+
"name": "C",
14+
"nameLocation": "9:1:1",
15+
"nodeType": "ContractDefinition",
16+
"nodes":
17+
[
18+
{
19+
"anonymous": false,
20+
"id": 6,
21+
"name": "E",
22+
"nameLocation": "23:1:1",
23+
"nodeType": "EventDefinition",
24+
"parameters":
25+
{
26+
"id": 5,
27+
"nodeType": "ParameterList",
28+
"parameters":
29+
[
30+
{
31+
"constant": false,
32+
"id": 4,
33+
"indexed": false,
34+
"mutability": "mutable",
35+
"name": "",
36+
"nameLocation": "-1:-1:-1",
37+
"nodeType": "VariableDeclaration",
38+
"src": "25:20:1",
39+
"stateVariable": false,
40+
"storageLocation": "default",
41+
"typeDescriptions": {},
42+
"typeName":
43+
{
44+
"id": 3,
45+
"nodeType": "FunctionTypeName",
46+
"parameterTypes":
47+
{
48+
"id": 1,
49+
"nodeType": "ParameterList",
50+
"parameters": [],
51+
"src": "33:2:1"
52+
},
53+
"returnParameterTypes":
54+
{
55+
"id": 2,
56+
"nodeType": "ParameterList",
57+
"parameters": [],
58+
"src": "44:0:1"
59+
},
60+
"src": "25:20:1",
61+
"stateMutability": "nonpayable",
62+
"typeDescriptions": {},
63+
"visibility": "internal"
64+
},
65+
"visibility": "internal"
66+
}
67+
],
68+
"src": "24:21:1"
69+
},
70+
"src": "17:29:1"
71+
}
72+
],
73+
"src": "0:48:1",
74+
"usedErrors": []
75+
}
76+
],
77+
"src": "0:49:1"
78+
}

test/libsolidity/ASTJSON/used_errors.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"nodes":
2121
[
2222
{
23+
"errorSelector": "c1599bd9",
2324
"id": 2,
2425
"name": "X",
2526
"nameLocation": "6:1:1",
@@ -127,6 +128,7 @@
127128
"nodes":
128129
[
129130
{
131+
"errorSelector": "2bc80f3a",
130132
"id": 11,
131133
"name": "T",
132134
"nameLocation": "63:1:1",
@@ -145,7 +147,7 @@
145147
{
146148
"id": 17,
147149
"nodeType": "Block",
148-
"src": "97:8:1",
150+
"src": "92:8:1",
149151
"statements":
150152
[
151153
{
@@ -160,7 +162,7 @@
160162
"nodeType": "Identifier",
161163
"overloadedDeclarations": [],
162164
"referencedDeclaration": 9,
163-
"src": "99:1:1",
165+
"src": "94:1:1",
164166
"typeDescriptions":
165167
{
166168
"typeIdentifier": "t_function_internal_nonpayable$__$returns$__$",
@@ -175,7 +177,7 @@
175177
"lValueRequested": false,
176178
"names": [],
177179
"nodeType": "FunctionCall",
178-
"src": "99:3:1",
180+
"src": "94:3:1",
179181
"tryCall": false,
180182
"typeDescriptions":
181183
{
@@ -185,7 +187,7 @@
185187
},
186188
"id": 16,
187189
"nodeType": "ExpressionStatement",
188-
"src": "99:3:1"
190+
"src": "94:3:1"
189191
}
190192
]
191193
},
@@ -209,23 +211,23 @@
209211
"id": 13,
210212
"nodeType": "ParameterList",
211213
"parameters": [],
212-
"src": "97:0:1"
214+
"src": "92:0:1"
213215
},
214216
"scope": 19,
215-
"src": "72:33:1",
216-
"stateMutability": "pure",
217+
"src": "72:28:1",
218+
"stateMutability": "nonpayable",
217219
"virtual": false,
218220
"visibility": "public"
219221
}
220222
],
221223
"scope": 20,
222-
"src": "40:67:1",
224+
"src": "40:62:1",
223225
"usedErrors":
224226
[
225227
2,
226228
11
227229
]
228230
}
229231
],
230-
"src": "0:108:1"
232+
"src": "0:103:1"
231233
}

test/libsolidity/ASTJSON/used_errors.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error X();
22
function f() { revert X(); }
33
contract C {
44
error T();
5-
function h() public pure { f(); }
5+
function h() public { f(); }
66
}
77

88
// ----

test/libsolidity/ASTJSON/used_errors_parseOnly.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
{
109109
"id": 17,
110110
"nodeType": "Block",
111-
"src": "97:8:1",
111+
"src": "92:8:1",
112112
"statements":
113113
[
114114
{
@@ -121,19 +121,19 @@
121121
"name": "f",
122122
"nodeType": "Identifier",
123123
"overloadedDeclarations": [],
124-
"src": "99:1:1",
124+
"src": "94:1:1",
125125
"typeDescriptions": {}
126126
},
127127
"id": 15,
128128
"names": [],
129129
"nodeType": "FunctionCall",
130-
"src": "99:3:1",
130+
"src": "94:3:1",
131131
"tryCall": false,
132132
"typeDescriptions": {}
133133
},
134134
"id": 16,
135135
"nodeType": "ExpressionStatement",
136-
"src": "99:3:1"
136+
"src": "94:3:1"
137137
}
138138
]
139139
},
@@ -156,17 +156,17 @@
156156
"id": 13,
157157
"nodeType": "ParameterList",
158158
"parameters": [],
159-
"src": "97:0:1"
159+
"src": "92:0:1"
160160
},
161-
"src": "72:33:1",
162-
"stateMutability": "pure",
161+
"src": "72:28:1",
162+
"stateMutability": "nonpayable",
163163
"virtual": false,
164164
"visibility": "public"
165165
}
166166
],
167-
"src": "40:67:1",
167+
"src": "40:62:1",
168168
"usedErrors": []
169169
}
170170
],
171-
"src": "0:108:1"
171+
"src": "0:103:1"
172172
}

0 commit comments

Comments
 (0)