-
Notifications
You must be signed in to change notification settings - Fork 16
/
RelayerHub.sol
98 lines (83 loc) · 3.15 KB
/
RelayerHub.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
// SPDX-License-Identifier: Apache2.0
pragma solidity 0.8.4;
import "./lib/BytesToTypes.sol";
import "./lib/Memory.sol";
import "./interface/IRelayerHub.sol";
import "./interface/IParamSubscriber.sol";
import "./System.sol";
/// This contract manages BTC relayers on Core blockchain
contract RelayerHub is IRelayerHub, System, IParamSubscriber{
uint256 public constant INIT_REQUIRED_DEPOSIT = 1e20;
uint256 public constant INIT_DUES = 1e18;
// the refundable deposit
uint256 public requiredDeposit;
// the unregister fee
uint256 public dues;
mapping(address =>Relayer) relayers;
mapping(address =>bool) relayersExistMap;
struct Relayer{
uint256 deposit;
uint256 dues;
}
modifier noExist() {
require(!relayersExistMap[msg.sender], "relayer already exists");
_;
}
modifier exist() {
require(relayersExistMap[msg.sender], "relayer does not exist");
_;
}
modifier noProxy() {
require(msg.sender == tx.origin, "no proxy is allowed");
_;
}
event relayerRegister(address indexed relayer);
event relayerUnRegister(address indexed relayer);
function init() external onlyNotInit{
requiredDeposit = INIT_REQUIRED_DEPOSIT;
dues = INIT_DUES;
alreadyInit = true;
}
/// Register as a BTC relayer on Core blockchain
function register() external payable noExist onlyInit noProxy{
require(msg.value == requiredDeposit, "deposit value does not match requirement");
relayers[msg.sender] = Relayer(requiredDeposit, dues);
relayersExistMap[msg.sender] = true;
emit relayerRegister(msg.sender);
}
/// Unregister the BTC relayer role on Core blockchain
function unregister() external exist onlyInit{
Relayer memory r = relayers[msg.sender];
delete relayersExistMap[msg.sender];
delete relayers[msg.sender];
payable(msg.sender).transfer(r.deposit - r.dues);
payable(SYSTEM_REWARD_ADDR).transfer(r.dues);
emit relayerUnRegister(msg.sender);
}
/*********************** Param update ********************************/
/// Update parameters through governance vote
/// @param key The name of the parameter
/// @param value the new value set to the parameter
function updateParam(string calldata key, bytes calldata value) external override onlyInit onlyGov{
if (Memory.compareStrings(key,"requiredDeposit")) {
require(value.length == 32, "length of requiredDeposit mismatch");
uint256 newRequiredDeposit = BytesToTypes.bytesToUint256(32, value);
require(newRequiredDeposit > dues, "the requiredDeposit out of range");
requiredDeposit = newRequiredDeposit;
} else if (Memory.compareStrings(key,"dues")) {
require(value.length == 32, "length of dues mismatch");
uint256 newDues = BytesToTypes.bytesToUint256(32, value);
require(newDues > 0 && newDues < requiredDeposit, "the dues out of range");
dues = newDues;
} else {
revert UnsupportedGovParam(key);
}
emit paramChange(key, value);
}
/// Whether the input address is a relayer
/// @param sender The address to check
/// @return true/false
function isRelayer(address sender) external override view returns (bool) {
return relayersExistMap[sender];
}
}