forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRelayedCall.test.js
More file actions
223 lines (180 loc) · 8.18 KB
/
RelayedCall.test.js
File metadata and controls
223 lines (180 loc) · 8.18 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { impersonate } = require('../helpers/account');
async function fixture() {
const [admin, receiver, other] = await ethers.getSigners();
const mock = await ethers.deployContract('$RelayedCall');
const computeRelayerAddress = (salt = ethers.ZeroHash) =>
ethers.getCreate2Address(
mock.target,
salt,
ethers.keccak256(
ethers.concat([
'0x60475f8160095f39f373',
mock.target,
'0x331460133611166022575f5ffd5b6014360360145f375f5f601436035f345f3560601c5af13d5f5f3e5f3d91604557fd5bf3',
]),
),
);
const authority = await ethers.deployContract('$AccessManager', [admin]);
const target = await ethers.deployContract('$AccessManagedTarget', [authority]);
return { mock, target, receiver, other, computeRelayerAddress };
}
describe('RelayedCall', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('default (zero) salt', function () {
beforeEach(async function () {
this.relayer = await this.computeRelayerAddress();
});
it('automatic relayer deployment', async function () {
await expect(ethers.provider.getCode(this.relayer)).to.eventually.equal('0x');
// First call performs deployment
await expect(this.mock.$getRelayer()).to.emit(this.mock, 'return$getRelayer').withArgs(this.relayer);
await expect(ethers.provider.getCode(this.relayer)).to.eventually.not.equal('0x');
// Following calls use the same relayer
await expect(this.mock.$getRelayer()).to.emit(this.mock, 'return$getRelayer').withArgs(this.relayer);
});
describe('relayed call', function () {
it('target success', async function () {
const tx = this.mock.$relayCall(
ethers.Typed.address(this.target),
ethers.Typed.bytes(this.target.interface.encodeFunctionData('fnUnrestricted', [])),
);
await expect(tx)
.to.emit(this.target, 'CalledUnrestricted')
.withArgs(this.relayer)
.to.emit(this.mock, 'return$relayCall_address_bytes')
.withArgs(true, '0x');
});
it('target success (with value)', async function () {
const value = 42n;
// fund the mock
await this.other.sendTransaction({ to: this.mock.target, value });
// perform relayed call
const tx = this.mock.$relayCall(
ethers.Typed.address(this.receiver),
ethers.Typed.uint256(value),
ethers.Typed.bytes('0x'),
);
await expect(tx).to.changeEtherBalances([this.mock, this.relayer, this.receiver], [-value, 0n, value]);
await expect(tx).to.emit(this.mock, 'return$relayCall_address_uint256_bytes').withArgs(true, '0x');
});
it('target revert', async function () {
const tx = this.mock.$relayCall(
ethers.Typed.address(this.target),
ethers.Typed.bytes(this.target.interface.encodeFunctionData('fnRestricted', [])),
);
await expect(tx)
.to.emit(this.mock, 'return$relayCall_address_bytes')
.withArgs(false, this.target.interface.encodeErrorResult('AccessManagedUnauthorized', [this.relayer]));
});
});
it('direct call to the relayer', async function () {
// deploy relayer
await this.mock.$getRelayer();
// unauthorized caller
await expect(
this.other.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce525' }),
).to.be.revertedWithoutReason();
});
it('relayer input format', async function () {
// deploy relayer
await this.mock.$getRelayer();
// impersonate mock to pass caller checks
const mockAsWallet = await impersonate(this.mock.target);
// 20 bytes (address + empty data) - OK
await expect(
mockAsWallet.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce525' }),
).to.not.be.reverted;
// 19 bytes (not enough for an address) - REVERT
await expect(
mockAsWallet.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce5' }),
).to.be.revertedWithoutReason();
// 0 bytes (not enough for an address) - REVERT
await expect(mockAsWallet.sendTransaction({ to: this.relayer, data: '0x' })).to.be.revertedWithoutReason();
});
});
describe('random salt', function () {
beforeEach(async function () {
this.salt = ethers.hexlify(ethers.randomBytes(32));
this.relayer = await this.computeRelayerAddress(this.salt);
});
it('automatic relayer deployment', async function () {
await expect(ethers.provider.getCode(this.relayer)).to.eventually.equal('0x');
// First call performs deployment
await expect(this.mock.$getRelayer(ethers.Typed.bytes32(this.salt)))
.to.emit(this.mock, 'return$getRelayer_bytes32')
.withArgs(this.relayer);
await expect(ethers.provider.getCode(this.relayer)).to.eventually.not.equal('0x');
// Following calls use the same relayer
await expect(this.mock.$getRelayer(ethers.Typed.bytes32(this.salt)))
.to.emit(this.mock, 'return$getRelayer_bytes32')
.withArgs(this.relayer);
});
describe('relayed call', function () {
it('target success', async function () {
const tx = this.mock.$relayCall(
ethers.Typed.address(this.target),
ethers.Typed.bytes(this.target.interface.encodeFunctionData('fnUnrestricted', [])),
ethers.Typed.bytes32(this.salt),
);
await expect(tx)
.to.emit(this.target, 'CalledUnrestricted')
.withArgs(this.relayer)
.to.emit(this.mock, 'return$relayCall_address_bytes_bytes32')
.withArgs(true, '0x');
});
it('target success (with value)', async function () {
const value = 42n;
// fund the mock
await this.other.sendTransaction({ to: this.mock.target, value });
// perform relayed call
const tx = this.mock.$relayCall(
ethers.Typed.address(this.receiver),
ethers.Typed.uint256(value),
ethers.Typed.bytes('0x'),
ethers.Typed.bytes32(this.salt),
);
await expect(tx).to.changeEtherBalances([this.mock, this.relayer, this.receiver], [-value, 0n, value]);
await expect(tx).to.emit(this.mock, 'return$relayCall_address_uint256_bytes_bytes32').withArgs(true, '0x');
});
it('target revert', async function () {
const tx = this.mock.$relayCall(
ethers.Typed.address(this.target),
ethers.Typed.bytes(this.target.interface.encodeFunctionData('fnRestricted', [])),
ethers.Typed.bytes32(this.salt),
);
await expect(tx)
.to.emit(this.mock, 'return$relayCall_address_bytes_bytes32')
.withArgs(false, this.target.interface.encodeErrorResult('AccessManagedUnauthorized', [this.relayer]));
});
});
it('direct call to the relayer', async function () {
// deploy relayer
await this.mock.$getRelayer(ethers.Typed.bytes32(this.salt));
// unauthorized caller
await expect(
this.other.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce525' }),
).to.be.revertedWithoutReason();
});
it('input format', async function () {
// deploy relayer
await this.mock.$getRelayer(ethers.Typed.bytes32(this.salt));
// impersonate mock to pass caller checks
const mockAsWallet = await impersonate(this.mock.target);
// 20 bytes (address + empty data) - OK
await expect(
mockAsWallet.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce525' }),
).to.not.be.reverted;
// 19 bytes (not enough for an address) - REVERT
await expect(
mockAsWallet.sendTransaction({ to: this.relayer, data: '0x7859821024E633C5dC8a4FcF86fC52e7720Ce5' }),
).to.be.revertedWithoutReason();
// 0 bytes (not enough for an address) - REVERT
await expect(mockAsWallet.sendTransaction({ to: this.relayer, data: '0x' })).to.be.revertedWithoutReason();
});
});
});