|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.12; |
| 3 | + |
| 4 | +import "../pyth/PythGovernanceInstructions.sol"; |
| 5 | +import "../wormhole/interfaces/IWormhole.sol"; |
| 6 | +import "./ExecutorErrors.sol"; |
| 7 | + |
| 8 | +contract Executor { |
| 9 | + using BytesLib for bytes; |
| 10 | + |
| 11 | + // Magic is `PTGM` encoded as a 4 byte data: Pyth Governance Message |
| 12 | + // TODO: it's annoying that we can't import this from PythGovernanceInstructions |
| 13 | + uint32 constant MAGIC = 0x5054474d; |
| 14 | + |
| 15 | + PythGovernanceInstructions.GovernanceModule constant MODULE = |
| 16 | + PythGovernanceInstructions.GovernanceModule.EvmExecutor; |
| 17 | + |
| 18 | + // Instruction indicating that the executor contract on |
| 19 | + // targetChainId at executorAddress should call the contract at callAddress |
| 20 | + // with the provided callData |
| 21 | + struct GovernanceInstruction { |
| 22 | + PythGovernanceInstructions.GovernanceModule module; |
| 23 | + ExecutorAction action; |
| 24 | + uint16 targetChainId; |
| 25 | + // The address of the specific executor that should perform the call. |
| 26 | + // This argument is included to support multiple Executors on the same blockchain. |
| 27 | + address executorAddress; |
| 28 | + address callAddress; |
| 29 | + bytes callData; |
| 30 | + } |
| 31 | + |
| 32 | + // We have different actions here for potential future extensibility |
| 33 | + enum ExecutorAction { |
| 34 | + // TODO: add an instruction to change the governance data source. |
| 35 | + Execute // 0 |
| 36 | + } |
| 37 | + |
| 38 | + IWormhole private wormhole; |
| 39 | + uint64 private lastExecutedSequence; |
| 40 | + uint16 private chainId; |
| 41 | + |
| 42 | + uint16 private ownerEmitterChainId; |
| 43 | + bytes32 private ownerEmitterAddress; |
| 44 | + |
| 45 | + constructor( |
| 46 | + address _wormhole, |
| 47 | + uint64 _lastExecutedSequence, |
| 48 | + uint16 _chainId, |
| 49 | + uint16 _ownerEmitterChainId, |
| 50 | + bytes32 _ownerEmitterAddress |
| 51 | + ) { |
| 52 | + wormhole = IWormhole(_wormhole); |
| 53 | + lastExecutedSequence = _lastExecutedSequence; |
| 54 | + chainId = _chainId; |
| 55 | + ownerEmitterChainId = _ownerEmitterChainId; |
| 56 | + ownerEmitterAddress = _ownerEmitterAddress; |
| 57 | + } |
| 58 | + |
| 59 | + // Execute the contract call in the provided wormhole message. |
| 60 | + // The argument should be the bytes of a valid wormhole message |
| 61 | + // whose payload is a serialized GovernanceInstruction. |
| 62 | + function execute( |
| 63 | + bytes memory encodedVm |
| 64 | + ) public returns (bytes memory response) { |
| 65 | + IWormhole.VM memory vm = verifyGovernanceVM(encodedVm); |
| 66 | + |
| 67 | + GovernanceInstruction memory gi = parseGovernanceInstruction( |
| 68 | + vm.payload |
| 69 | + ); |
| 70 | + |
| 71 | + if (gi.targetChainId != chainId && gi.targetChainId != 0) |
| 72 | + revert ExecutorErrors.InvalidGovernanceTarget(); |
| 73 | + |
| 74 | + if ( |
| 75 | + gi.action != ExecutorAction.Execute || |
| 76 | + gi.executorAddress != address(this) |
| 77 | + ) revert ExecutorErrors.DeserializationError(); |
| 78 | + |
| 79 | + bool success; |
| 80 | + (success, response) = address(gi.callAddress).call(gi.callData); |
| 81 | + |
| 82 | + // Check if the call was successful or not. |
| 83 | + if (!success) { |
| 84 | + // If there is return data, the delegate call reverted with a reason or a custom error, which we bubble up. |
| 85 | + if (response.length > 0) { |
| 86 | + // The first word of response is the length, so when we call revert we add 1 word (32 bytes) |
| 87 | + // to give the pointer to the beginning of the revert data and pass the size as the second argument. |
| 88 | + assembly { |
| 89 | + let returndata_size := mload(response) |
| 90 | + revert(add(32, response), returndata_size) |
| 91 | + } |
| 92 | + } else { |
| 93 | + revert ExecutorErrors.ExecutionReverted(); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// @dev Called when `msg.value` is not zero and the call data is empty. |
| 99 | + receive() external payable {} |
| 100 | + |
| 101 | + // Check that the encoded VM is a valid wormhole VAA from the correct emitter |
| 102 | + // and with a sufficiently recent sequence number. |
| 103 | + function verifyGovernanceVM( |
| 104 | + bytes memory encodedVM |
| 105 | + ) internal returns (IWormhole.VM memory parsedVM) { |
| 106 | + (IWormhole.VM memory vm, bool valid, ) = wormhole.parseAndVerifyVM( |
| 107 | + encodedVM |
| 108 | + ); |
| 109 | + |
| 110 | + if (!valid) revert ExecutorErrors.InvalidWormholeVaa(); |
| 111 | + |
| 112 | + if ( |
| 113 | + vm.emitterChainId != ownerEmitterChainId || |
| 114 | + vm.emitterAddress != ownerEmitterAddress |
| 115 | + ) revert ExecutorErrors.UnauthorizedEmitter(); |
| 116 | + |
| 117 | + if (vm.sequence <= lastExecutedSequence) |
| 118 | + revert ExecutorErrors.MessageOutOfOrder(); |
| 119 | + |
| 120 | + lastExecutedSequence = vm.sequence; |
| 121 | + |
| 122 | + return vm; |
| 123 | + } |
| 124 | + |
| 125 | + /// @dev Parse a GovernanceInstruction |
| 126 | + function parseGovernanceInstruction( |
| 127 | + bytes memory encodedInstruction |
| 128 | + ) public pure returns (GovernanceInstruction memory gi) { |
| 129 | + uint index = 0; |
| 130 | + |
| 131 | + uint32 magic = encodedInstruction.toUint32(index); |
| 132 | + |
| 133 | + if (magic != MAGIC) revert ExecutorErrors.DeserializationError(); |
| 134 | + |
| 135 | + index += 4; |
| 136 | + |
| 137 | + uint8 modNumber = encodedInstruction.toUint8(index); |
| 138 | + gi.module = PythGovernanceInstructions.GovernanceModule(modNumber); |
| 139 | + index += 1; |
| 140 | + |
| 141 | + if (gi.module != MODULE) revert PythErrors.InvalidGovernanceTarget(); |
| 142 | + |
| 143 | + uint8 actionNumber = encodedInstruction.toUint8(index); |
| 144 | + gi.action = ExecutorAction(actionNumber); |
| 145 | + index += 1; |
| 146 | + |
| 147 | + gi.targetChainId = encodedInstruction.toUint16(index); |
| 148 | + index += 2; |
| 149 | + |
| 150 | + gi.executorAddress = encodedInstruction.toAddress(index); |
| 151 | + index += 20; |
| 152 | + |
| 153 | + gi.callAddress = encodedInstruction.toAddress(index); |
| 154 | + index += 20; |
| 155 | + |
| 156 | + // As solidity performs math operations in a checked mode |
| 157 | + // if the length of the encoded instruction be smaller than index |
| 158 | + // it will revert. So we don't need any extra check. |
| 159 | + gi.callData = encodedInstruction.slice( |
| 160 | + index, |
| 161 | + encodedInstruction.length - index |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments