-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVotingPortal.sol
172 lines (152 loc) · 5.19 KB
/
VotingPortal.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.8;
import {ICrossChainController} from 'aave-delivery-infrastructure/contracts/interfaces/ICrossChainController.sol';
import {IGovernanceCore} from '../interfaces/IGovernanceCore.sol';
import {IVotingPortal, IBaseReceiverPortal} from '../interfaces/IVotingPortal.sol';
import {Errors} from './libraries/Errors.sol';
import {IVotingMachineWithProofs} from './voting/interfaces/IVotingMachineWithProofs.sol';
import {Ownable} from 'solidity-utils/contracts/oz-common/Ownable.sol';
/**
* @title VotingPortal
* @author BGD Labs
* @notice Contract with the knowledge on how to initialize a proposal voting and get the votes results,
from a vote that happened on a different or same chain.
*/
contract VotingPortal is Ownable, IVotingPortal {
/// @inheritdoc IVotingPortal
address public immutable CROSS_CHAIN_CONTROLLER;
/// @inheritdoc IVotingPortal
address public immutable GOVERNANCE;
/// @inheritdoc IVotingPortal
address public immutable VOTING_MACHINE;
/// @inheritdoc IVotingPortal
uint256 public immutable VOTING_MACHINE_CHAIN_ID;
// stores the gas limit for start voting bridging tx
uint128 internal _startVotingGasLimit;
// proposalId => voter => has voted -> saves the voters of every proposal that used this voting portal to send the vote
mapping(uint256 => mapping(address => bool)) internal _proposalVoters;
/**
* @param crossChainController address of current network message controller (cross chain controller or same chain controller)
* @param governance address of the linked governance contract
* @param votingMachine address where the proposal votes will happen. Can be same or different chain
* @param startVotingGasLimit gas limit for "Start voting" bridging tx
*/
constructor(
address crossChainController,
address governance,
address votingMachine,
uint256 votingMachineChainId,
uint128 startVotingGasLimit,
address owner
) {
require(owner != address(0), Errors.INVALID_VOTING_PORTAL_OWNER);
require(
crossChainController != address(0),
Errors.INVALID_VOTING_PORTAL_CROSS_CHAIN_CONTROLLER
);
require(governance != address(0), Errors.INVALID_VOTING_PORTAL_GOVERNANCE);
require(
votingMachine != address(0),
Errors.INVALID_VOTING_PORTAL_VOTING_MACHINE
);
require(votingMachineChainId > 0, Errors.INVALID_VOTING_MACHINE_CHAIN_ID);
CROSS_CHAIN_CONTROLLER = crossChainController;
GOVERNANCE = governance;
VOTING_MACHINE = votingMachine;
VOTING_MACHINE_CHAIN_ID = votingMachineChainId;
_updateStartVotingGasLimit(startVotingGasLimit);
_transferOwnership(owner);
}
/// @inheritdoc IBaseReceiverPortal
/// @dev pushes the voting result and queues the proposal identified by proposalId
function receiveCrossChainMessage(
address originSender,
uint256 originChainId,
bytes memory message
) external {
require(
msg.sender == CROSS_CHAIN_CONTROLLER &&
originSender == VOTING_MACHINE &&
originChainId == VOTING_MACHINE_CHAIN_ID,
Errors.WRONG_MESSAGE_ORIGIN
);
try this.decodeMessage(message) returns (
uint256 proposalId,
uint128 forVotes,
uint128 againstVotes
) {
IGovernanceCore(GOVERNANCE).queueProposal(
proposalId,
forVotes,
againstVotes
);
bytes memory empty;
emit VoteMessageReceived(
originSender,
originChainId,
true,
message,
empty
);
} catch (bytes memory decodingError) {
emit VoteMessageReceived(
originSender,
originChainId,
false,
message,
decodingError
);
}
}
/// @inheritdoc IVotingPortal
function forwardStartVotingMessage(
uint256 proposalId,
bytes32 blockHash,
uint24 votingDuration
) external {
bytes memory message = abi.encode(proposalId, blockHash, votingDuration);
_sendMessage(
msg.sender,
MessageType.Proposal,
getStartVotingGasLimit(),
message
);
}
/// @inheritdoc IVotingPortal
function decodeMessage(
bytes memory message
) external pure returns (uint256, uint128, uint128) {
return abi.decode(message, (uint256, uint128, uint128));
}
/// @inheritdoc IVotingPortal
function setStartVotingGasLimit(uint128 gasLimit) external onlyOwner {
_updateStartVotingGasLimit(gasLimit);
}
/// @inheritdoc IVotingPortal
function getStartVotingGasLimit() public view returns (uint128) {
return _startVotingGasLimit;
}
function _sendMessage(
address caller,
MessageType messageType,
uint256 gasLimit,
bytes memory message
) internal {
require(caller == GOVERNANCE, Errors.CALLER_NOT_GOVERNANCE);
bytes memory messageWithType = abi.encode(messageType, message);
ICrossChainController(CROSS_CHAIN_CONTROLLER).forwardMessage(
VOTING_MACHINE_CHAIN_ID,
VOTING_MACHINE,
gasLimit,
messageWithType
);
}
/**
* @notice method to update the _startVotingGasLimit
* @param gasLimit the new gas limit
*/
function _updateStartVotingGasLimit(uint128 gasLimit) internal {
_startVotingGasLimit = gasLimit;
emit StartVotingGasLimitUpdated(gasLimit);
}
}