Skip to content

Commit cc3d285

Browse files
authored
evm: cleanup: remove module from governance struct (#150)
1 parent ee4fc17 commit cc3d285

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

evm/src/wormhole/Governance.sol

+9-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ contract Governance {
4646
* @dev General purpose governance message to call arbitrary methods on a governed smart contract.
4747
* This message adheres to the Wormhole governance packet standard: https://github.com/wormhole-foundation/wormhole/blob/main/whitepapers/0002_governance_messaging.md
4848
* The wire format for this message is:
49-
* - module - 32 bytes
49+
* - MODULE - 32 bytes
5050
* - action - 1 byte
5151
* - chain - 2 bytes
5252
* - governanceContract - 20 bytes
@@ -55,7 +55,6 @@ contract Governance {
5555
* - callData - `callDataLength` bytes
5656
*/
5757
struct GeneralPurposeGovernanceMessage {
58-
bytes32 module;
5958
uint8 action;
6059
uint16 chain;
6160
address governanceContract;
@@ -72,10 +71,6 @@ contract Governance {
7271
GeneralPurposeGovernanceMessage memory message =
7372
parseGeneralPurposeGovernanceMessage(verified.payload);
7473

75-
if (message.module != MODULE) {
76-
revert InvalidModule(message.module);
77-
}
78-
7974
if (message.action != uint8(GovernanceAction.EVM_CALL)) {
8075
revert InvalidAction(message.action);
8176
}
@@ -138,7 +133,7 @@ contract Governance {
138133
}
139134
uint16 callDataLength = uint16(m.callData.length);
140135
return abi.encodePacked(
141-
m.module,
136+
MODULE,
142137
m.action,
143138
m.chain,
144139
m.governanceContract,
@@ -154,7 +149,13 @@ contract Governance {
154149
returns (GeneralPurposeGovernanceMessage memory message)
155150
{
156151
uint256 offset = 0;
157-
(message.module, offset) = encoded.asBytes32Unchecked(offset);
152+
153+
bytes32 module;
154+
(module, offset) = encoded.asBytes32Unchecked(offset);
155+
if (module != MODULE) {
156+
revert InvalidModule(module);
157+
}
158+
158159
(message.action, offset) = encoded.asUint8Unchecked(offset);
159160
(message.chain, offset) = encoded.asUint16Unchecked(offset);
160161
(message.governanceContract, offset) = encoded.asAddressUnchecked(offset);

evm/test/wormhole/Governance.t.sol

+14-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pragma solidity >=0.8.8 <0.9.0;
33

44
import "../../src/wormhole/Governance.sol";
5+
import "forge-std/console.sol";
56
import "forge-std/Test.sol";
67
import "openzeppelin-contracts/contracts/access/Ownable.sol";
78
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
@@ -41,7 +42,6 @@ contract GovernanceTest is Test {
4142
}
4243

4344
function buildGovernanceVaa(
44-
bytes32 module,
4545
uint8 action,
4646
uint16 chainId,
4747
address governanceContract,
@@ -50,55 +50,51 @@ contract GovernanceTest is Test {
5050
) public view returns (bytes memory) {
5151
Governance.GeneralPurposeGovernanceMessage memory message = Governance
5252
.GeneralPurposeGovernanceMessage({
53-
module: module,
5453
action: action,
5554
chain: chainId,
5655
governanceContract: governanceContract,
5756
governedContract: governedContract,
5857
callData: callData
5958
});
6059

61-
IWormhole.VM memory vaa = IWormhole.VM({
60+
IWormhole.VM memory vaa =
61+
buildVaa(governance.encodeGeneralPurposeGovernanceMessage(message));
62+
63+
return guardian.encodeAndSignMessage(vaa);
64+
}
65+
66+
function buildVaa(bytes memory payload) public view returns (IWormhole.VM memory) {
67+
return IWormhole.VM({
6268
version: 1,
6369
timestamp: uint32(block.timestamp),
6470
nonce: 0,
6571
emitterChainId: wormhole.governanceChainId(),
6672
emitterAddress: wormhole.governanceContract(),
6773
sequence: 0,
6874
consistencyLevel: 200,
69-
payload: governance.encodeGeneralPurposeGovernanceMessage(message),
75+
payload: payload,
7076
guardianSetIndex: 0,
7177
signatures: new IWormhole.Signature[](0),
7278
hash: bytes32("")
7379
});
74-
75-
return guardian.encodeAndSignMessage(vaa);
7680
}
7781

7882
function test_invalidModule() public {
79-
uint16 thisChain = wormhole.chainId();
8083
bytes32 coreBridgeModule =
8184
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-
);
85+
bytes memory restOfPayload =
86+
"0x0100022e234dae75c793f67a35089c9d99245e1c58470bf62849f9a0b5bf2913b396098f7c7019b51a820a000471cd25f9";
87+
bytes memory badModulePayload = abi.encodePacked(coreBridgeModule, restOfPayload);
9188

9289
vm.expectRevert(abi.encodeWithSignature("InvalidModule(bytes32)", coreBridgeModule));
93-
governance.performGovernance(signed);
90+
governance.parseGeneralPurposeGovernanceMessage(badModulePayload);
9491
}
9592

9693
// TODO: this should ideally test all actions that != 1
9794
function test_invalidAction() public {
9895
uint16 thisChain = wormhole.chainId();
9996

10097
bytes memory signed = buildGovernanceVaa(
101-
governance.MODULE(),
10298
uint8(Governance.GovernanceAction.UNDEFINED),
10399
thisChain,
104100
address(governance),
@@ -113,7 +109,6 @@ contract GovernanceTest is Test {
113109
// TODO: this should ideally test all chainIds that != wormhole.chainId()
114110
function test_invalidChain() public {
115111
bytes memory signed = buildGovernanceVaa(
116-
governance.MODULE(),
117112
uint8(Governance.GovernanceAction.EVM_CALL),
118113
0,
119114
address(governance),
@@ -131,7 +126,6 @@ contract GovernanceTest is Test {
131126
address random = address(0x1234);
132127

133128
bytes memory signed = buildGovernanceVaa(
134-
governance.MODULE(),
135129
uint8(Governance.GovernanceAction.EVM_CALL),
136130
thisChain,
137131
random,
@@ -147,7 +141,6 @@ contract GovernanceTest is Test {
147141
uint16 thisChain = wormhole.chainId();
148142

149143
bytes memory signed = buildGovernanceVaa(
150-
governance.MODULE(),
151144
uint8(Governance.GovernanceAction.EVM_CALL),
152145
thisChain,
153146
address(governance),
@@ -163,7 +156,6 @@ contract GovernanceTest is Test {
163156
uint16 thisChain = wormhole.chainId();
164157

165158
bytes memory signed = buildGovernanceVaa(
166-
governance.MODULE(),
167159
uint8(Governance.GovernanceAction.EVM_CALL),
168160
thisChain,
169161
address(governance),
@@ -181,7 +173,6 @@ contract GovernanceTest is Test {
181173
uint16 thisChain = wormhole.chainId();
182174

183175
bytes memory signed = buildGovernanceVaa(
184-
governance.MODULE(),
185176
uint8(Governance.GovernanceAction.EVM_CALL),
186177
thisChain,
187178
address(governance),

0 commit comments

Comments
 (0)