Skip to content

Commit fbcd265

Browse files
committed
nits
1 parent b850bc7 commit fbcd265

File tree

5 files changed

+209
-313
lines changed

5 files changed

+209
-313
lines changed

crates/common/src/mapping_slots.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ impl MappingSlots {
2424
pub fn insert(&mut self, slot: B256) -> bool {
2525
match self.seen_sha3.get(&slot).copied() {
2626
Some((key, parent)) => {
27-
if self.keys.contains_key(&slot) {
27+
if self.keys.insert(slot, key).is_some() {
2828
return false;
2929
}
30-
self.keys.insert(slot, key);
3130
self.parent_slots.insert(slot, parent);
3231
self.children.entry(parent).or_default().push(slot);
3332
self.insert(parent);

testdata/default/cheats/StateDiffMappings.t.sol

Lines changed: 75 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pragma solidity ^0.8.18;
33

44
import "ds-test/test.sol";
55
import "cheats/Vm.sol";
6-
import "./StateDiffTestUtils.sol";
76

87
contract MappingStorage {
98
// Simple mappings only
@@ -30,7 +29,7 @@ contract MappingStorage {
3029
}
3130
}
3231

33-
contract StateDiffMappingsTest is StateDiffTestUtils {
32+
contract StateDiffMappingsTest is DSTest {
3433
Vm constant vm = Vm(HEVM_ADDRESS);
3534
MappingStorage public mappingStorage;
3635

@@ -52,53 +51,35 @@ contract StateDiffMappingsTest is StateDiffTestUtils {
5251
emit log_string(stateDiffText);
5352

5453
// Verify text format contains the mapping label
55-
assertContains(
56-
stateDiffText,
57-
"balances[0x0000000000000000000000000000000000001234]",
58-
"Text format should contain mapping label"
59-
);
54+
assertTrue(vm.contains(stateDiffText, "balances[0x0000000000000000000000000000000000001234]"));
6055

6156
// Verify text format contains the value type
62-
assertContains(stateDiffText, "uint256", "Text format should contain value type");
57+
assertTrue(vm.contains(stateDiffText, "uint256"));
6358

6459
// Verify text format contains decoded values (shown with arrow)
65-
assertContains(stateDiffText, ": 0", "Text format should contain initial value");
66-
assertContains(
67-
stateDiffText, "1000000000000000000000", "Text format should contain new value (1000 ether in wei)"
68-
);
60+
assertTrue(vm.contains(stateDiffText, ": 0"));
61+
assertTrue(vm.contains(stateDiffText, "1000000000000000000000"));
6962

7063
// Test JSON format output
7164
string memory json = vm.getStateDiffJson();
7265
emit log_string("State diff JSON (simple mapping):");
7366
emit log_string(json);
7467

7568
// The JSON should contain the decoded mapping slot with proper label
76-
assertContains(
77-
json,
78-
'"label":"balances[0x0000000000000000000000000000000000001234]"',
79-
"JSON should contain 'balances[0x0000...1234]' label"
80-
);
69+
assertTrue(vm.contains(json, '"label":"balances[0x0000000000000000000000000000000000001234]"'));
8170

8271
// Check the type is correctly identified
83-
assertContains(json, '"type":"mapping(address => uint256)"', "JSON should contain mapping type");
72+
assertTrue(vm.contains(json, '"type":"mapping(address => uint256)"'));
8473

8574
// Check decoded values
86-
assertContains(
87-
json,
88-
'"decoded":{"previousValue":"0","newValue":"1000000000000000000000"}',
89-
"JSON should decode balance value correctly (1000 ether = 1000000000000000000000 wei)"
90-
);
75+
assertTrue(vm.contains(json, '"decoded":{"previousValue":"0","newValue":"1000000000000000000000"}'));
9176

9277
// Check that the key field is present for simple mapping
93-
assertContains(
94-
json,
95-
'"key":"0x0000000000000000000000000000000000001234"',
96-
"JSON should contain decoded key field for simple mapping"
97-
);
78+
assertTrue(vm.contains(json, '"key":"0x0000000000000000000000000000000000001234"'));
9879

9980
// Stop recording and verify we have account accesses
10081
Vm.AccountAccess[] memory accesses = vm.stopAndReturnStateDiff();
101-
assertTrue(accesses.length > 0, "Should have account accesses");
82+
assertTrue(accesses.length > 0);
10283

10384
// The AccountAccess structure contains information about storage changes
10485
// but the label and decoded values are only available in the string/JSON outputs
@@ -122,19 +103,13 @@ contract StateDiffMappingsTest is StateDiffTestUtils {
122103
emit log_string(stateDiffText);
123104

124105
// Verify text format contains decoded values for uint256 key
125-
assertContains(stateDiffText, "owners[12345]", "Text format should contain owners mapping with decimal key");
126-
assertContains(
127-
stateDiffText,
128-
"address): 0x0000000000000000000000000000000000000000",
129-
"Text format should contain initial address value"
130-
);
131-
assertContains(
132-
stateDiffText, "0x0000000000000000000000000000000000007777", "Text format should contain new address value"
133-
);
106+
assertTrue(vm.contains(stateDiffText, "owners[12345]"));
107+
assertTrue(vm.contains(stateDiffText, "address): 0x0000000000000000000000000000000000000000"));
108+
assertTrue(vm.contains(stateDiffText, "0x0000000000000000000000000000000000007777"));
134109

135110
// Verify text format contains decoded values for bytes32 key
136-
assertContains(stateDiffText, "bool): false", "Text format should contain initial bool value");
137-
assertContains(stateDiffText, "true", "Text format should contain new bool value");
111+
assertTrue(vm.contains(stateDiffText, "bool): false"));
112+
assertTrue(vm.contains(stateDiffText, "true"));
138113

139114
// Get state diff JSON
140115
string memory json = vm.getStateDiffJson();
@@ -144,23 +119,22 @@ contract StateDiffMappingsTest is StateDiffTestUtils {
144119
emit log_string(json);
145120

146121
// Check uint256 key mapping
147-
assertContains(json, '"label":"owners[12345]"', "Should contain owners mapping with uint256 key");
148-
assertContains(json, '"type":"mapping(uint256 => address)"', "Should contain uint256=>address mapping type");
149-
assertContains(
150-
json,
151-
'"decoded":{"previousValue":"0x0000000000000000000000000000000000000000","newValue":"0x0000000000000000000000000000000000007777"}',
152-
"Should decode owner address correctly"
122+
assertTrue(vm.contains(json, '"label":"owners[12345]"'));
123+
assertTrue(vm.contains(json, '"type":"mapping(uint256 => address)"'));
124+
assertTrue(
125+
vm.contains(
126+
json,
127+
'"decoded":{"previousValue":"0x0000000000000000000000000000000000000000","newValue":"0x0000000000000000000000000000000000007777"}'
128+
)
153129
);
154130

155131
// Check bytes32 key mapping - the key will be shown as hex
156-
assertContains(json, '"label":"flags[', "Should contain flags mapping label");
157-
assertContains(json, '"type":"mapping(bytes32 => bool)"', "Should contain bytes32=>bool mapping type");
158-
assertContains(
159-
json, '"decoded":{"previousValue":"false","newValue":"true"}', "Should decode flag bool value correctly"
160-
);
132+
assertTrue(vm.contains(json, '"label":"flags['));
133+
assertTrue(vm.contains(json, '"type":"mapping(bytes32 => bool)"'));
134+
assertTrue(vm.contains(json, '"decoded":{"previousValue":"false","newValue":"true"}'));
161135

162136
// Check that the key field is present for uint256 key mapping
163-
assertContains(json, '"key":"12345"', "JSON should contain decoded key field for uint256 mapping");
137+
assertTrue(vm.contains(json, '"key":"12345"'));
164138

165139
// Stop recording
166140
vm.stopAndReturnStateDiff();
@@ -190,35 +164,25 @@ contract StateDiffMappingsTest is StateDiffTestUtils {
190164
emit log_string(stateDiffText);
191165

192166
// Verify text format contains nested mapping labels
193-
assertContains(
194-
stateDiffText,
195-
"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000002222]",
196-
"Text format should contain first nested mapping label"
197-
);
198-
assertContains(
199-
stateDiffText,
200-
"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000003333]",
201-
"Text format should contain second nested mapping label"
167+
assertTrue(
168+
vm.contains(
169+
stateDiffText,
170+
"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000002222]"
171+
)
172+
);
173+
assertTrue(
174+
vm.contains(
175+
stateDiffText,
176+
"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000003333]"
177+
)
202178
);
203179
// The text format shows the value type (uint256) not the full mapping type
204-
assertContains(stateDiffText, "uint256): 0", "Text format should contain value type");
180+
assertTrue(vm.contains(stateDiffText, "uint256): 0"));
205181

206182
// Verify text format contains decoded values for nested mappings
207-
assertContains(
208-
stateDiffText,
209-
"500000000000000000000",
210-
"Text format should contain decoded value for owner1->spender1 (500 ether)"
211-
);
212-
assertContains(
213-
stateDiffText,
214-
"750000000000000000000",
215-
"Text format should contain decoded value for owner1->spender2 (750 ether)"
216-
);
217-
assertContains(
218-
stateDiffText,
219-
"1000000000000000000000",
220-
"Text format should contain decoded value for owner2->spender3 (1000 ether)"
221-
);
183+
assertTrue(vm.contains(stateDiffText, "500000000000000000000"));
184+
assertTrue(vm.contains(stateDiffText, "750000000000000000000"));
185+
assertTrue(vm.contains(stateDiffText, "1000000000000000000000"));
222186

223187
// Test JSON format output
224188
string memory json = vm.getStateDiffJson();
@@ -228,57 +192,55 @@ contract StateDiffMappingsTest is StateDiffTestUtils {
228192
// Check that all three nested mapping entries are correctly decoded
229193

230194
// Entry 1: owner1 -> spender1
231-
assertContains(
232-
json,
233-
'"label":"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000002222]"',
234-
"Should contain first nested mapping label (owner1 -> spender1)"
235-
);
236-
assertContains(
237-
json, '"newValue":"500000000000000000000"', "Should have correct value for owner1 -> spender1 (500 ether)"
195+
assertTrue(
196+
vm.contains(
197+
json,
198+
'"label":"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000002222]"'
199+
)
238200
);
201+
assertTrue(vm.contains(json, '"newValue":"500000000000000000000"'));
239202

240203
// Entry 2: owner1 -> spender2 (same owner, different spender)
241-
assertContains(
242-
json,
243-
'"label":"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000003333]"',
244-
"Should contain second nested mapping label (owner1 -> spender2)"
245-
);
246-
assertContains(
247-
json, '"newValue":"750000000000000000000"', "Should have correct value for owner1 -> spender2 (750 ether)"
204+
assertTrue(
205+
vm.contains(
206+
json,
207+
'"label":"allowances[0x0000000000000000000000000000000000001111][0x0000000000000000000000000000000000003333]"'
208+
)
248209
);
210+
assertTrue(vm.contains(json, '"newValue":"750000000000000000000"'));
249211

250212
// Entry 3: owner2 -> spender3 (different owner)
251-
assertContains(
252-
json,
253-
'"label":"allowances[0x0000000000000000000000000000000000004444][0x0000000000000000000000000000000000005555]"',
254-
"Should contain third nested mapping label (owner2 -> spender3)"
255-
);
256-
assertContains(
257-
json, '"newValue":"1000000000000000000000"', "Should have correct value for owner2 -> spender3 (1000 ether)"
213+
assertTrue(
214+
vm.contains(
215+
json,
216+
'"label":"allowances[0x0000000000000000000000000000000000004444][0x0000000000000000000000000000000000005555]"'
217+
)
258218
);
219+
assertTrue(vm.contains(json, '"newValue":"1000000000000000000000"'));
259220

260221
// Check the type is correctly identified for all entries
261-
assertContains(
262-
json, '"type":"mapping(address => mapping(address => uint256))"', "Should contain nested mapping type"
263-
);
222+
assertTrue(vm.contains(json, '"type":"mapping(address => mapping(address => uint256))"'));
264223

265224
// Check that the keys field is present for nested mappings
266-
assertContains(
267-
json,
268-
'"keys":["0x0000000000000000000000000000000000001111","0x0000000000000000000000000000000000002222"]',
269-
"JSON should contain decoded keys array for owner1->spender1 nested mapping"
225+
assertTrue(
226+
vm.contains(
227+
json,
228+
'"keys":["0x0000000000000000000000000000000000001111","0x0000000000000000000000000000000000002222"]'
229+
)
270230
);
271231

272-
assertContains(
273-
json,
274-
'"keys":["0x0000000000000000000000000000000000001111","0x0000000000000000000000000000000000003333"]',
275-
"JSON should contain decoded keys array for owner1->spender2 nested mapping"
232+
assertTrue(
233+
vm.contains(
234+
json,
235+
'"keys":["0x0000000000000000000000000000000000001111","0x0000000000000000000000000000000000003333"]'
236+
)
276237
);
277238

278-
assertContains(
279-
json,
280-
'"keys":["0x0000000000000000000000000000000000004444","0x0000000000000000000000000000000000005555"]',
281-
"JSON should contain decoded keys array for owner2->spender3 nested mapping"
239+
assertTrue(
240+
vm.contains(
241+
json,
242+
'"keys":["0x0000000000000000000000000000000000004444","0x0000000000000000000000000000000000005555"]'
243+
)
282244
);
283245

284246
// Stop recording

0 commit comments

Comments
 (0)