@@ -7,11 +7,17 @@ import "openzeppelin-contracts/contracts/access/Ownable.sol";
7
7
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol " ;
8
8
9
9
contract GovernedContract is Ownable {
10
+ error RandomError ();
11
+
10
12
bool public governanceStuffCalled;
11
13
12
14
function governanceStuff () public onlyOwner {
13
15
governanceStuffCalled = true ;
14
16
}
17
+
18
+ function governanceRevert () public view onlyOwner {
19
+ revert RandomError ();
20
+ }
15
21
}
16
22
17
23
contract GovernanceTest is Test {
@@ -34,14 +40,22 @@ contract GovernanceTest is Test {
34
40
myContract.transferOwnership (address (governance));
35
41
}
36
42
37
- function test_governance () public {
38
- uint16 thisChain = wormhole.chainId ();
39
-
40
- Governance.GovernanceMessage memory message = Governance.GovernanceMessage ({
41
- chainId: thisChain,
42
- governanceContract: address (governance),
43
- governedContract: address (myContract),
44
- callData: abi.encodeWithSignature ("governanceStuff() " )
43
+ function buildGovernanceVaa (
44
+ bytes32 module ,
45
+ uint8 action ,
46
+ uint16 chainId ,
47
+ address governanceContract ,
48
+ address governedContract ,
49
+ bytes memory callData
50
+ ) public view returns (bytes memory ) {
51
+ Governance.GeneralPurposeGovernanceMessage memory message = Governance
52
+ .GeneralPurposeGovernanceMessage ({
53
+ module: module,
54
+ action: action,
55
+ chain: chainId,
56
+ governanceContract: governanceContract,
57
+ governedContract: governedContract,
58
+ callData: callData
45
59
});
46
60
47
61
IWormhole.VM memory vaa = IWormhole.VM ({
@@ -52,13 +66,110 @@ contract GovernanceTest is Test {
52
66
emitterAddress: wormhole.governanceContract (),
53
67
sequence: 0 ,
54
68
consistencyLevel: 200 ,
55
- payload: abi.encode (message),
69
+ payload: governance. encodeGeneralPurposeGovernanceMessage (message),
56
70
guardianSetIndex: 0 ,
57
71
signatures: new IWormhole.Signature [](0 ),
58
72
hash: bytes32 ("" )
59
73
});
60
74
61
- bytes memory signed = guardian.encodeAndSignMessage (vaa);
75
+ return guardian.encodeAndSignMessage (vaa);
76
+ }
77
+
78
+ function test_invalidModule () public {
79
+ uint16 thisChain = wormhole.chainId ();
80
+ bytes32 coreBridgeModule =
81
+ 0x00000000000000000000000000000000000000000000000000000000436f7265 ;
82
+
83
+ bytes memory signed = buildGovernanceVaa (
84
+ coreBridgeModule,
85
+ uint8 (Governance.GovernanceAction.EVM_CALL),
86
+ thisChain,
87
+ address (governance),
88
+ address (myContract),
89
+ abi.encodeWithSignature ("governanceStuff() " )
90
+ );
91
+
92
+ vm.expectRevert (abi.encodeWithSignature ("InvalidModule(bytes32) " , coreBridgeModule));
93
+ governance.performGovernance (signed);
94
+ }
95
+
96
+ // TODO: this should ideally test all actions that != 1
97
+ function test_invalidAction () public {
98
+ uint16 thisChain = wormhole.chainId ();
99
+
100
+ bytes memory signed = buildGovernanceVaa (
101
+ governance.MODULE (),
102
+ uint8 (Governance.GovernanceAction.UNDEFINED),
103
+ thisChain,
104
+ address (governance),
105
+ address (myContract),
106
+ abi.encodeWithSignature ("governanceStuff() " )
107
+ );
108
+
109
+ vm.expectRevert (abi.encodeWithSignature ("InvalidAction(uint8) " , 0 ));
110
+ governance.performGovernance (signed);
111
+ }
112
+
113
+ // TODO: this should ideally test all chainIds that != wormhole.chainId()
114
+ function test_invalidChain () public {
115
+ bytes memory signed = buildGovernanceVaa (
116
+ governance.MODULE (),
117
+ uint8 (Governance.GovernanceAction.EVM_CALL),
118
+ 0 ,
119
+ address (governance),
120
+ address (myContract),
121
+ abi.encodeWithSignature ("governanceStuff() " )
122
+ );
123
+
124
+ vm.expectRevert (abi.encodeWithSignature ("NotRecipientChain(uint16) " , 0 ));
125
+ governance.performGovernance (signed);
126
+ }
127
+
128
+ // TODO: this should ideally test lots of address possibilities
129
+ function test_invalidRecipientContract () public {
130
+ uint16 thisChain = wormhole.chainId ();
131
+ address random = address (0x1234 );
132
+
133
+ bytes memory signed = buildGovernanceVaa (
134
+ governance.MODULE (),
135
+ uint8 (Governance.GovernanceAction.EVM_CALL),
136
+ thisChain,
137
+ random,
138
+ address (myContract),
139
+ abi.encodeWithSignature ("governanceStuff() " )
140
+ );
141
+
142
+ vm.expectRevert (abi.encodeWithSignature ("NotRecipientContract(address) " , random));
143
+ governance.performGovernance (signed);
144
+ }
145
+
146
+ function test_governanceCallFailure () public {
147
+ uint16 thisChain = wormhole.chainId ();
148
+
149
+ bytes memory signed = buildGovernanceVaa (
150
+ governance.MODULE (),
151
+ uint8 (Governance.GovernanceAction.EVM_CALL),
152
+ thisChain,
153
+ address (governance),
154
+ address (myContract),
155
+ abi.encodeWithSignature ("governanceRevert() " )
156
+ );
157
+
158
+ vm.expectRevert (abi.encodeWithSignature ("RandomError() " ));
159
+ governance.performGovernance (signed);
160
+ }
161
+
162
+ function test_governance () public {
163
+ uint16 thisChain = wormhole.chainId ();
164
+
165
+ bytes memory signed = buildGovernanceVaa (
166
+ governance.MODULE (),
167
+ uint8 (Governance.GovernanceAction.EVM_CALL),
168
+ thisChain,
169
+ address (governance),
170
+ address (myContract),
171
+ abi.encodeWithSignature ("governanceStuff() " )
172
+ );
62
173
63
174
governance.performGovernance (signed);
64
175
@@ -69,28 +180,14 @@ contract GovernanceTest is Test {
69
180
address newOwner = address (0x1234 );
70
181
uint16 thisChain = wormhole.chainId ();
71
182
72
- Governance.GovernanceMessage memory message = Governance.GovernanceMessage ({
73
- chainId: thisChain,
74
- governanceContract: address (governance),
75
- governedContract: address (myContract),
76
- callData: abi.encodeWithSignature ("transferOwnership(address) " , newOwner)
77
- });
78
-
79
- IWormhole.VM memory vaa = IWormhole.VM ({
80
- version: 1 ,
81
- timestamp: uint32 (block .timestamp ),
82
- nonce: 0 ,
83
- emitterChainId: wormhole.governanceChainId (),
84
- emitterAddress: wormhole.governanceContract (),
85
- sequence: 0 ,
86
- consistencyLevel: 200 ,
87
- payload: abi.encode (message),
88
- guardianSetIndex: 0 ,
89
- signatures: new IWormhole.Signature [](0 ),
90
- hash: bytes32 ("" )
91
- });
92
-
93
- bytes memory signed = guardian.encodeAndSignMessage (vaa);
183
+ bytes memory signed = buildGovernanceVaa (
184
+ governance.MODULE (),
185
+ uint8 (Governance.GovernanceAction.EVM_CALL),
186
+ thisChain,
187
+ address (governance),
188
+ address (myContract),
189
+ abi.encodeWithSignature ("transferOwnership(address) " , newOwner)
190
+ );
94
191
95
192
governance.performGovernance (signed);
96
193
@@ -99,6 +196,4 @@ contract GovernanceTest is Test {
99
196
100
197
assert (myContract.governanceStuffCalled ());
101
198
}
102
-
103
- // TODO: tests triggering the error conditions
104
199
}
0 commit comments