Skip to content

Commit b547cf5

Browse files
committed
fix testing logic
1 parent 0a3b2cc commit b547cf5

File tree

3 files changed

+54
-76
lines changed

3 files changed

+54
-76
lines changed

contracts/mocks/CallReceiverMock.sol

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,56 @@ contract CallReceiverMock {
77
event MockFunctionCalledWithArgs(uint256 a, uint256 b);
88
event MockFunctionCalledExtra(address caller, uint256 value);
99

10-
uint256 public nbCalls;
1110
uint256[] private _array;
1211

13-
modifier countCall() {
14-
++nbCalls;
15-
_;
16-
}
17-
18-
function mockFunction() public payable countCall returns (string memory) {
12+
function mockFunction() public payable returns (string memory) {
1913
emit MockFunctionCalled();
14+
return "0x1234";
15+
}
2016

17+
function mockFunctionWritesStorage(bytes32 slot, bytes32 value) public returns (string memory) {
18+
assembly ("memory-safe") {
19+
sstore(slot, value)
20+
}
2121
return "0x1234";
2222
}
2323

24-
function mockFunctionEmptyReturn() public payable countCall {
24+
function mockFunctionEmptyReturn() public payable {
25+
emit MockFunctionCalled();
26+
}
27+
28+
function mockFunctionEmptyReturnWritesStorage(bytes32 slot, bytes32 value) public payable {
29+
assembly ("memory-safe") {
30+
sstore(slot, value)
31+
}
2532
emit MockFunctionCalled();
2633
}
2734

28-
function mockFunctionWithArgs(uint256 a, uint256 b) public payable countCall returns (string memory) {
35+
function mockFunctionWithArgs(uint256 a, uint256 b) public payable returns (string memory) {
2936
emit MockFunctionCalledWithArgs(a, b);
3037

3138
return "0x1234";
3239
}
3340

34-
function mockFunctionWithArgsReturn(uint256 a, uint256 b) public payable countCall returns (uint256, uint256) {
41+
function mockFunctionWithArgsReturn(uint256 a, uint256 b) public payable returns (uint256, uint256) {
3542
emit MockFunctionCalledWithArgs(a, b);
43+
return (a, b);
44+
}
3645

46+
function mockFunctionWithArgsReturnWritesStorage(
47+
bytes32 slot,
48+
bytes32 value,
49+
uint256 a,
50+
uint256 b
51+
) public payable returns (uint256, uint256) {
52+
assembly ("memory-safe") {
53+
sstore(slot, value)
54+
}
55+
emit MockFunctionCalledWithArgs(a, b);
3756
return (a, b);
3857
}
3958

40-
function mockFunctionNonPayable() public countCall returns (string memory) {
59+
function mockFunctionNonPayable() public returns (string memory) {
4160
emit MockFunctionCalled();
4261

4362
return "0x1234";
@@ -51,32 +70,25 @@ contract CallReceiverMock {
5170
return (a, b);
5271
}
5372

54-
function mockFunctionRevertsNoReason() public payable countCall {
73+
function mockFunctionRevertsNoReason() public payable {
5574
revert();
5675
}
5776

58-
function mockFunctionRevertsReason() public payable countCall {
77+
function mockFunctionRevertsReason() public payable {
5978
revert("CallReceiverMock: reverting");
6079
}
6180

62-
function mockFunctionThrows() public payable countCall {
81+
function mockFunctionThrows() public payable {
6382
assert(false);
6483
}
6584

66-
function mockFunctionOutOfGas() public payable countCall {
85+
function mockFunctionOutOfGas() public payable {
6786
for (uint256 i = 0; ; ++i) {
6887
_array.push(i);
6988
}
7089
}
7190

72-
function mockFunctionWritesStorage(bytes32 slot, bytes32 value) public countCall returns (string memory) {
73-
assembly {
74-
sstore(slot, value)
75-
}
76-
return "0x1234";
77-
}
78-
79-
function mockFunctionExtra() public payable countCall {
91+
function mockFunctionExtra() public payable {
8092
emit MockFunctionCalledExtra(msg.sender, msg.value);
8193
}
8294
}

contracts/utils/Create2.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ library Create2 {
5050
revert Errors.FailedDeployment();
5151
} else {
5252
LowLevelCall.bubbleRevert();
53-
}
53+
}
5454
}
5555
}
5656

test/utils/LowLevelCall.test.js

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
55
const value = ethers.parseEther('1');
66
const returnValue1 = ethers.id('hello');
77
const returnValue2 = ethers.id('world');
8+
const storageSlot = ethers.id('location');
9+
const storageValue = ethers.id('data');
810

911
async function fixture() {
1012
const [account] = await ethers.getSigners();
@@ -23,65 +25,45 @@ describe('LowLevelCall', function () {
2325
describe('call', function () {
2426
describe('without any return', function () {
2527
it('calls the requested function and returns true', async function () {
26-
await expect(this.target.nbCalls()).to.eventually.equal(0);
27-
2828
await expect(this.mock.$callNoReturn(this.target, this.target.interface.encodeFunctionData('mockFunction')))
2929
.to.emit(this.target, 'MockFunctionCalled')
3030
.to.emit(this.mock, 'return$callNoReturn_address_bytes')
3131
.withArgs(true);
32-
33-
await expect(this.target.nbCalls()).to.eventually.equal(1);
3432
});
3533

3634
it('calls the requested function with value and returns true', async function () {
3735
await this.account.sendTransaction({ to: this.mock, value });
3836

39-
await expect(this.target.nbCalls()).to.eventually.equal(0);
40-
4137
const tx = this.mock.$callNoReturn(
4238
this.target,
4339
ethers.Typed.uint256(value),
4440
this.target.interface.encodeFunctionData('mockFunction'),
4541
);
4642
await expect(tx).to.changeEtherBalances([this.mock, this.target], [-value, value]);
4743
await expect(tx).to.emit(this.mock, 'return$callNoReturn_address_uint256_bytes').withArgs(true);
48-
49-
await expect(this.target.nbCalls()).to.eventually.equal(1);
5044
});
5145

5246
it("calls the requested function and returns false if the caller doesn't have enough balance", async function () {
53-
await expect(this.target.nbCalls()).to.eventually.equal(0);
54-
5547
const tx = this.mock.$callNoReturn(
5648
this.target,
5749
ethers.Typed.uint256(value),
5850
this.target.interface.encodeFunctionData('mockFunction'),
5951
);
6052
await expect(tx).to.changeEtherBalances([this.mock, this.target], [0n, 0n]);
6153
await expect(tx).to.emit(this.mock, 'return$callNoReturn_address_uint256_bytes').withArgs(false);
62-
63-
// reverted call do not count
64-
await expect(this.target.nbCalls()).to.eventually.equal(0);
6554
});
6655

6756
it('calls the requested function and returns false if the subcall reverts', async function () {
68-
await expect(this.target.nbCalls()).to.eventually.equal(0);
69-
7057
const tx = this.mock.$callNoReturn(
7158
this.target,
7259
this.target.interface.encodeFunctionData('mockFunctionRevertsNoReason'),
7360
);
7461
await expect(tx).to.emit(this.mock, 'return$callNoReturn_address_bytes').withArgs(false);
75-
76-
// reverted call do not count
77-
await expect(this.target.nbCalls()).to.eventually.equal(0);
7862
});
7963
});
8064

8165
describe('with 64 bytes return in the scratch space', function () {
8266
it('calls the requested function and returns true', async function () {
83-
await expect(this.target.nbCalls()).to.eventually.equal(0);
84-
8567
await expect(
8668
this.mock.$callReturn64Bytes(
8769
this.target,
@@ -92,15 +74,11 @@ describe('LowLevelCall', function () {
9274
.withArgs(returnValue1, returnValue2)
9375
.to.emit(this.mock, 'return$callReturn64Bytes_address_bytes')
9476
.withArgs(true, returnValue1, returnValue2);
95-
96-
await expect(this.target.nbCalls()).to.eventually.equal(1);
9777
});
9878

9979
it('calls the requested function with value and returns true', async function () {
10080
await this.account.sendTransaction({ to: this.mock, value });
10181

102-
await expect(this.target.nbCalls()).to.eventually.equal(0);
103-
10482
const tx = this.mock.$callReturn64Bytes(
10583
this.target,
10684
ethers.Typed.uint256(value),
@@ -110,13 +88,9 @@ describe('LowLevelCall', function () {
11088
await expect(tx)
11189
.to.emit(this.mock, 'return$callReturn64Bytes_address_uint256_bytes')
11290
.withArgs(true, returnValue1, returnValue2);
113-
114-
await expect(this.target.nbCalls()).to.eventually.equal(1);
11591
});
11692

11793
it("calls the requested function and returns false if the caller doesn't have enough balance", async function () {
118-
await expect(this.target.nbCalls()).to.eventually.equal(0);
119-
12094
const tx = this.mock.$callReturn64Bytes(
12195
this.target,
12296
ethers.Typed.uint256(value),
@@ -126,24 +100,16 @@ describe('LowLevelCall', function () {
126100
await expect(tx)
127101
.to.emit(this.mock, 'return$callReturn64Bytes_address_uint256_bytes')
128102
.withArgs(false, ethers.ZeroHash, ethers.ZeroHash);
129-
130-
// reverted call do not count
131-
await expect(this.target.nbCalls()).to.eventually.equal(0);
132103
});
133104

134105
it('calls the requested function and returns false if the subcall reverts', async function () {
135-
await expect(this.target.nbCalls()).to.eventually.equal(0);
136-
137106
const tx = this.mock.$callReturn64Bytes(
138107
this.target,
139108
this.target.interface.encodeFunctionData('mockFunctionRevertsNoReason'),
140109
);
141110
await expect(tx)
142111
.to.emit(this.mock, 'return$callReturn64Bytes_address_bytes')
143112
.withArgs(false, ethers.ZeroHash, ethers.ZeroHash);
144-
145-
// reverted call do not count
146-
await expect(this.target.nbCalls()).to.eventually.equal(0);
147113
});
148114
});
149115
});
@@ -190,20 +156,22 @@ describe('LowLevelCall', function () {
190156
describe('delegate', function () {
191157
describe('without any return', function () {
192158
it('calls the requested function and returns true', async function () {
193-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
159+
await expect(ethers.provider.getStorage(this.mock, storageSlot)).to.eventually.equal(ethers.ZeroHash);
194160

195161
await expect(
196-
this.mock.$delegatecallNoReturn(this.target, this.target.interface.encodeFunctionData('mockFunction')),
162+
this.mock.$delegatecallNoReturn(
163+
this.target,
164+
this.target.interface.encodeFunctionData('mockFunctionWritesStorage', [storageSlot, storageValue]),
165+
),
197166
)
198167
.to.emit(this.mock, 'return$delegatecallNoReturn')
199168
.withArgs(true);
200169

201-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(1, 32));
170+
// check delegate call set storage correctly
171+
await expect(ethers.provider.getStorage(this.mock, storageSlot)).to.eventually.equal(storageValue);
202172
});
203173

204174
it('calls the requested function and returns false if the subcall reverts', async function () {
205-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
206-
207175
await expect(
208176
this.mock.$delegatecallNoReturn(
209177
this.target,
@@ -212,31 +180,32 @@ describe('LowLevelCall', function () {
212180
)
213181
.to.emit(this.mock, 'return$delegatecallNoReturn')
214182
.withArgs(false);
215-
216-
// reverted call do not count
217-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
218183
});
219184
});
220185

221186
describe('with 64 bytes return in the scratch space', function () {
222187
it('calls the requested function and returns true', async function () {
223-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
188+
await expect(ethers.provider.getStorage(this.mock, storageSlot)).to.eventually.equal(ethers.ZeroHash);
224189

225190
await expect(
226191
this.mock.$delegatecallReturn64Bytes(
227192
this.target,
228-
this.target.interface.encodeFunctionData('mockFunctionWithArgsReturn', [returnValue1, returnValue2]),
193+
this.target.interface.encodeFunctionData('mockFunctionWithArgsReturnWritesStorage', [
194+
storageSlot,
195+
storageValue,
196+
returnValue1,
197+
returnValue2,
198+
]),
229199
),
230200
)
231201
.to.emit(this.mock, 'return$delegatecallReturn64Bytes')
232202
.withArgs(true, returnValue1, returnValue2);
233203

234-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(1, 32));
204+
// check delegate call set storage correctly
205+
await expect(ethers.provider.getStorage(this.mock, storageSlot)).to.eventually.equal(storageValue);
235206
});
236207

237208
it('calls the requested function and returns false if the subcall reverts', async function () {
238-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
239-
240209
await expect(
241210
this.mock.$delegatecallReturn64Bytes(
242211
this.target,
@@ -245,9 +214,6 @@ describe('LowLevelCall', function () {
245214
)
246215
.to.emit(this.mock, 'return$delegatecallReturn64Bytes')
247216
.withArgs(false, ethers.ZeroHash, ethers.ZeroHash);
248-
249-
// reverted call do not count
250-
await expect(ethers.provider.getStorage(this.mock, 0)).to.eventually.equal(ethers.toBeHex(0, 32));
251217
});
252218
});
253219
});

0 commit comments

Comments
 (0)