forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCrosschainExecutor.test.js
More file actions
144 lines (118 loc) · 5.6 KB
/
CrosschainExecutor.test.js
File metadata and controls
144 lines (118 loc) · 5.6 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
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { getLocalChain } = require('../helpers/chains');
const {
CALL_TYPE_SINGLE,
CALL_TYPE_BATCH,
CALL_TYPE_DELEGATE,
encodeMode,
encodeSingle,
encodeBatch,
encodeDelegate,
} = require('../helpers/erc7579');
async function fixture() {
const chain = await getLocalChain();
const [admin, other] = await ethers.getSigners();
const gateway = await ethers.deployContract('$ERC7786GatewayMock');
const target = await ethers.deployContract('CallReceiverMock');
// Deploy executor
const executor = await ethers.deployContract('$CrosschainRemoteExecutor', [gateway, chain.toErc7930(admin)]);
const remoteExecute = (from, target, mode, data) =>
gateway.connect(from).sendMessage(target, ethers.concat([mode, data]), []);
return { chain, gateway, target, executor, admin, other, remoteExecute };
}
describe('CrosschainRemoteController & CrosschainRemoteExecutor', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('setup', async function () {
await expect(this.executor.gateway()).to.eventually.equal(this.gateway);
await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.admin));
});
describe('crosschain operation', function () {
it('support single mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_SINGLE });
const data = encodeSingle(this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra'));
await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data))
.to.emit(this.target, 'MockFunctionCalledExtra')
.withArgs(this.executor, 0n);
});
it('support batch mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_BATCH });
const data = encodeBatch(
[this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234'])],
[this.target, 0n, this.target.interface.encodeFunctionData('mockFunctionExtra')],
);
await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data))
.to.emit(this.target, 'MockFunctionCalledWithArgs')
.withArgs(42, '0x1234')
.to.emit(this.target, 'MockFunctionCalledExtra')
.withArgs(this.executor, 0n);
});
it('support delegate mode', async function () {
const mode = encodeMode({ callType: CALL_TYPE_DELEGATE });
const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunctionExtra'));
await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data))
.to.emit(this.target.attach(this.executor.target), 'MockFunctionCalledExtra')
.withArgs(this.gateway, 0n);
});
it('revert when mode is invalid', async function () {
const mode = encodeMode({ callType: '0x42' });
const data = '0x';
await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data))
.to.be.revertedWithCustomError(this.executor, 'ERC7579UnsupportedCallType')
.withArgs('0x42');
});
});
describe('reconfigure', function () {
beforeEach(async function () {
this.newGateway = await ethers.deployContract('$ERC7786GatewayMock');
});
it('through a crosschain call: success', async function () {
const mode = encodeMode({ callType: CALL_TYPE_SINGLE });
const data = encodeSingle(
this.executor,
0n,
this.executor.interface.encodeFunctionData('reconfigure', [
this.newGateway.target,
this.chain.toErc7930(this.other),
]),
);
await expect(this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data))
.to.emit(this.executor, 'CrosschainControllerSet')
.withArgs(this.newGateway, this.chain.toErc7930(this.other));
await expect(this.executor.gateway()).to.eventually.equal(this.newGateway);
await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.other));
});
it('through the internal setter: success', async function () {
await expect(this.executor.$_setup(this.newGateway, this.chain.toErc7930(this.other)))
.to.emit(this.executor, 'CrosschainControllerSet')
.withArgs(this.newGateway, this.chain.toErc7930(this.other));
await expect(this.executor.gateway()).to.eventually.equal(this.newGateway);
await expect(this.executor.controller()).to.eventually.equal(this.chain.toErc7930(this.other));
});
it('with an invalid new gateway: revert', async function () {
// directly using the internal setter
await expect(this.executor.$_setup(this.other, this.chain.toErc7930(this.other))).to.be.reverted;
// through a crosschain call
const mode = encodeMode({ callType: CALL_TYPE_SINGLE });
const data = encodeSingle(
this.executor,
0n,
this.executor.interface.encodeFunctionData('reconfigure', [
this.other.address,
this.chain.toErc7930(this.other),
]),
);
await expect(
this.remoteExecute(this.admin, this.chain.toErc7930(this.executor), mode, data),
).to.be.revertedWithCustomError(this.executor, 'FailedCall');
});
it('is access controlled', async function () {
await expect(
this.executor.reconfigure(this.newGateway, this.chain.toErc7930(this.other)),
).to.be.revertedWithCustomError(this.executor, 'AccessRestricted');
});
});
});