-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bridge.sol
223 lines (191 loc) · 9.58 KB
/
Bridge.sol
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
pragma solidity 0.6.4;
pragma experimental ABIEncoderV2;
import "./helpers/SafeMath.sol";
import "./interfaces/IRelayer.sol";
import "./interfaces/IDepositHandler.sol";
import "./interfaces/IBridge.sol";
contract Bridge {
using SafeMath for uint;
uint8 public _chainID;
IRelayer public _relayerContract;
uint256 public _relayerThreshold;
RelayerThresholdProposal public _currentRelayerThresholdProposal;
uint256 public _totalDepositProposals;
enum Vote {No, Yes}
enum RelayerThresholdProposalStatus {Inactive, Active}
enum DepositProposalStatus {Inactive, Active, Passed, Transferred}
struct DepositProposal {
bytes32 _dataHash;
address[] _yesVotes;
address[] _noVotes;
DepositProposalStatus _status;
}
struct RelayerThresholdProposal {
uint256 _proposedValue;
mapping(address => bool) _hasVoted;
address[] _yesVotes;
address[] _noVotes;
RelayerThresholdProposalStatus _status;
}
// destinationChainID => number of deposits
mapping(uint8 => uint256) public _depositCounts;
// destinationChainID => depositNonce => bytes
mapping(uint8 => mapping(uint256 => bytes)) public _depositRecords;
// destinationChainID => depositNonce => depositProposal
mapping(uint8 => mapping(uint256 => DepositProposal)) public _depositProposals;
// destinationChainID => depositNonce => relayerAddress => bool
mapping(uint8 => mapping(uint256 => mapping(address => bool))) public _hasVotedOnDepositProposal;
event RelayerThresholdProposalCreated(uint indexed proposedValue);
event RelayerThresholdProposalVote(Vote vote);
event RelayerThresholdChanged(uint indexed newThreshold);
event Deposit(
uint8 indexed destinationChainID,
address indexed originChainHandlerAddress,
uint256 indexed depositNonce
);
event DepositProposalCreated(
uint8 indexed originChainID,
uint8 indexed destinationChainID,
uint256 indexed depositNonce,
bytes32 dataHash
);
event DepositProposalVote(
uint8 indexed originChainID,
uint8 indexed destinationChainID,
uint256 indexed depositNonce,
DepositProposalStatus status
);
event DepositProposalFinalized(
uint8 indexed originChainID,
uint8 indexed destinationChainID,
uint256 indexed depositNonce
);
event DepositProposalExecuted(
uint8 indexed originChainID,
uint8 indexed destinationChainID,
uint256 indexed depositNonce
);
modifier _onlyRelayers() {
IRelayer relayerContract = IRelayer(_relayerContract);
require(relayerContract.isRelayer(msg.sender), "sender must be a relayer");
_;
}
constructor (uint8 chainID, address relayerContract, uint initialRelayerThreshold) public {
_chainID = chainID;
_relayerContract = IRelayer(relayerContract);
_relayerThreshold = initialRelayerThreshold;
}
function getCurrentRelayerThresholdProposal() public view returns (
uint256, address[] memory, address[] memory, RelayerThresholdProposalStatus) {
return (
_currentRelayerThresholdProposal._proposedValue,
_currentRelayerThresholdProposal._yesVotes,
_currentRelayerThresholdProposal._noVotes,
_currentRelayerThresholdProposal._status
);
}
function getDepositProposal(
uint8 destinationChainID,
uint256 depositNonce
) public view returns (DepositProposal memory) {
return _depositProposals[destinationChainID][depositNonce];
}
function deposit(
uint8 destinationChainID,
address originChainHandlerAddress,
bytes memory data
) public {
uint256 depositNonce = ++_depositCounts[destinationChainID];
_depositRecords[destinationChainID][depositNonce] = data;
IDepositHandler depositHandler = IDepositHandler(originChainHandlerAddress);
depositHandler.deposit(destinationChainID, depositNonce, msg.sender, data);
emit Deposit(destinationChainID, originChainHandlerAddress, depositNonce);
}
function voteDepositProposal(
uint8 originChainID,
uint256 depositNonce,
bytes32 dataHash
) public _onlyRelayers {
DepositProposal storage depositProposal = _depositProposals[uint8(originChainID)][depositNonce];
require(uint(depositProposal._status) <= 1, "proposal has already been passed or transferred");
require(!_hasVotedOnDepositProposal[originChainID][depositNonce][msg.sender], "relayer has already voted on proposal");
if (uint(depositProposal._status) == 0) {
++_totalDepositProposals;
_depositProposals[originChainID][depositNonce] = DepositProposal({
_dataHash: dataHash,
_yesVotes: new address[](1),
_noVotes: new address[](0),
_status: DepositProposalStatus.Active
});
depositProposal._yesVotes[0] = msg.sender;
emit DepositProposalCreated(originChainID, _chainID, depositNonce, dataHash);
} else {
depositProposal._yesVotes.push(msg.sender);
}
_hasVotedOnDepositProposal[originChainID][depositNonce][msg.sender] = true;
emit DepositProposalVote(originChainID, _chainID, depositNonce, depositProposal._status);
// If _depositThreshold is set to 1, then auto finalize
// or if _relayerThreshold has been exceeded
if (_relayerThreshold <= 1 || depositProposal._yesVotes.length >= _relayerThreshold) {
depositProposal._status = DepositProposalStatus.Passed;
emit DepositProposalFinalized(originChainID, _chainID, depositNonce);
}
}
function executeDepositProposal(
uint8 originChainID,
uint256 depositNonce,
address destinationChainHandlerAddress,
bytes memory data
) public {
DepositProposal storage depositProposal = _depositProposals[uint8(originChainID)][depositNonce];
require(depositProposal._status != DepositProposalStatus.Inactive, "proposal is not active");
require(depositProposal._status == DepositProposalStatus.Passed, "proposal was not passed or has already been transferred");
require(keccak256(abi.encodePacked(destinationChainHandlerAddress, data)) == depositProposal._dataHash,
"provided data does not match proposal's data hash");
IDepositHandler depositHandler = IDepositHandler(destinationChainHandlerAddress);
depositHandler.executeDeposit(data);
depositProposal._status = DepositProposalStatus.Transferred;
emit DepositProposalExecuted(originChainID, _chainID, depositNonce);
}
function createRelayerThresholdProposal(uint proposedValue) public _onlyRelayers {
require(_currentRelayerThresholdProposal._status == RelayerThresholdProposalStatus.Inactive, "a proposal is currently active");
require(proposedValue <= _relayerContract.getTotalRelayers(), "proposed value cannot be greater than the total number of relayers");
_currentRelayerThresholdProposal = RelayerThresholdProposal({
_proposedValue: proposedValue,
_yesVotes: new address[](1),
_noVotes: new address[](0),
_status: RelayerThresholdProposalStatus.Active
});
if (_relayerThreshold <= 1) {
_relayerThreshold = _currentRelayerThresholdProposal._proposedValue;
_currentRelayerThresholdProposal._status = RelayerThresholdProposalStatus.Inactive;
emit RelayerThresholdChanged(proposedValue);
}
// Record vote
_currentRelayerThresholdProposal._yesVotes[0] = msg.sender;
_currentRelayerThresholdProposal._hasVoted[msg.sender] = true;
emit RelayerThresholdProposalCreated(proposedValue);
}
function voteRelayerThresholdProposal(Vote vote) public _onlyRelayers {
require(_currentRelayerThresholdProposal._status == RelayerThresholdProposalStatus.Active, "no proposal is currently active");
require(!_currentRelayerThresholdProposal._hasVoted[msg.sender], "relayer has already voted");
require(uint8(vote) <= 1, "vote out of the vote enum range");
// Cast vote
if (vote == Vote.Yes) {
_currentRelayerThresholdProposal._yesVotes.push(msg.sender);
} else {
_currentRelayerThresholdProposal._noVotes.push(msg.sender);
}
_currentRelayerThresholdProposal._hasVoted[msg.sender] = true;
emit RelayerThresholdProposalVote(vote);
// Todo: Edge case if relayer threshold changes?
// Todo: For a proposal to pass does the number of yes votes just need to be higher than the threshold, or does it also have to be greater than the number of no votes?
if (_currentRelayerThresholdProposal._yesVotes.length >= _relayerThreshold) {
_relayerThreshold = _currentRelayerThresholdProposal._proposedValue;
_currentRelayerThresholdProposal._status = RelayerThresholdProposalStatus.Inactive;
emit RelayerThresholdChanged(_currentRelayerThresholdProposal._proposedValue);
} else if (_relayerContract.getTotalRelayers().sub(_currentRelayerThresholdProposal._noVotes.length) < _relayerThreshold) {
_currentRelayerThresholdProposal._status = RelayerThresholdProposalStatus.Inactive;
}
}
}