-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathHandlerV2.sol
More file actions
115 lines (101 loc) · 4.92 KB
/
Copy pathHandlerV2.sol
File metadata and controls
115 lines (101 loc) · 4.92 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
// Copyright (C) Polytope Labs Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pragma solidity ^0.8.17;
import {HandlerV1} from "./HandlerV1.sol";
import {IHandlerV2} from "@hyperbridge/core/interfaces/IHandlerV2.sol";
import {IConsensusV2} from "@hyperbridge/core/interfaces/IConsensusV2.sol";
import {IntermediateState, StateMachineHeight, StateCommitment} from "@hyperbridge/core/interfaces/IConsensus.sol";
import {IHandler} from "@hyperbridge/core/interfaces/IHandler.sol";
import {IHost} from "@hyperbridge/core/interfaces/IHost.sol";
/**
* @title The ISMP Message Handler V2.
* @author Polytope Labs (hello@polytope.technology)
*
* @notice Extends HandlerV1 with batch call support and IConsensusV2 integration.
* Relayers can bundle multiple handler operations into a single transaction via batchCall.
* Also tracks which relayer submitted the consensus proof for each authority set epoch.
*/
contract HandlerV2 is HandlerV1, IHandlerV2 {
// Maps authority set ID to the relayer that submitted the consensus proof for that epoch
mapping(uint256 => address) private _epochs;
// The current authority set epoch
uint256 private _currentEpoch;
// A call in the batch failed
error BatchCallFailed(uint256 index, bytes reason);
event NewEpoch(uint256 indexed authoritySetId, address indexed relayer);
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IHandlerV2).interfaceId || super.supportsInterface(interfaceId);
}
/**
* @dev Process a batch of encoded handler calls in a single transaction.
* Uses delegatecall to self so msg.sender is preserved and storage writes
* happen in this contract's context. Atomic, any failure reverts the entire batch.
* @param calls - array of ABI-encoded handler function calls
*/
function batchCall(bytes[] memory calls) external {
uint256 len = calls.length;
for (uint256 i = 0; i < len; ++i) {
(bool success, bytes memory returnData) = address(this).delegatecall(calls[i]);
if (!success) {
revert BatchCallFailed(i, returnData);
}
}
}
/**
* @dev Handle incoming consensus messages using IConsensusV2.
* Verifies the proof, stores the new consensus state and intermediate states,
* and records the relayer for the new authority set epoch if one occurred.
* @param host - `IsmpHost`
* @param proof - consensus proof
*/
function handleConsensus(IHost host, bytes calldata proof) external override(HandlerV1, IHandler) notFrozen(host) {
uint256 delay = block.timestamp - host.consensusUpdateTime();
if (delay >= host.unStakingPeriod()) revert ConsensusClientExpired();
(bytes memory verifiedState, IntermediateState[] memory intermediates, uint256 nextAuthoritySetId) =
IConsensusV2(host.consensusClient()).verify(host.consensusState(), proof);
host.storeConsensusState(verifiedState);
uint256 intermediatesLen = intermediates.length;
for (uint256 i = 0; i < intermediatesLen; i++) {
IntermediateState memory intermediate = intermediates[i];
uint256 latestHeight = host.latestStateMachineHeight(intermediate.stateMachineId);
if (latestHeight != 0 && intermediate.height > latestHeight) {
StateMachineHeight memory stateMachineHeight =
StateMachineHeight({stateMachineId: intermediate.stateMachineId, height: intermediate.height});
host.storeStateMachineCommitment(stateMachineHeight, intermediate.commitment);
}
}
if (nextAuthoritySetId > _currentEpoch) {
_currentEpoch = nextAuthoritySetId;
_epochs[nextAuthoritySetId] = msg.sender;
emit NewEpoch(nextAuthoritySetId, msg.sender);
}
}
/**
* @dev Returns the relayer address for a given authority set ID.
* @param authoritySetId - the authority set / epoch ID
* @return the relayer address, or address(0) if not set
*/
function relayerOf(uint256 authoritySetId) external view returns (address) {
return _epochs[authoritySetId];
}
/**
* @dev Returns the current authority set epoch.
*/
function currentEpoch() external view returns (uint256) {
return _currentEpoch;
}
}