Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEP-174: Cross Chain Relayer Management #205

Merged
merged 96 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
1d9c1e2
Add new proposed functions skeleton
emailtovamos Nov 15, 2022
9c9423a
admin struct & removeAdminAddress code
emailtovamos Nov 15, 2022
646ba57
remove, register, add admin + onlyAdmin modifier
emailtovamos Nov 15, 2022
2747fcb
add relayer functions
emailtovamos Nov 15, 2022
0065836
helper function to avoid repeat of code
emailtovamos Nov 16, 2022
981c225
verifyRelayer and relayerExistsMap
emailtovamos Nov 16, 2022
d31bc5d
updateParam changes
emailtovamos Nov 16, 2022
7513b3f
Remove unwanted code and variables
emailtovamos Nov 16, 2022
38224e5
Remove old code
emailtovamos Nov 16, 2022
5a5bb13
Formatting + efficient return for verify function
emailtovamos Nov 17, 2022
a40d34c
decouple registered and nonregistered admins, remove relayer before d…
emailtovamos Nov 17, 2022
744819c
add old modifier checks before adding relayer
emailtovamos Nov 17, 2022
0994f27
seed with current whitelists.todo: add relayers
emailtovamos Nov 18, 2022
1f1135c
Refactor: replace admin with manager
emailtovamos Nov 21, 2022
3efc715
contract and exist checks inside function
emailtovamos Nov 21, 2022
e808819
remove onlyWhitelabelRelayer for the most part.
emailtovamos Nov 22, 2022
0fc3985
Remove register unregister from template file
emailtovamos Nov 23, 2022
ef4e086
isRelayer, not verifyRelayer
emailtovamos Nov 23, 2022
9304110
isContract check for manager
emailtovamos Nov 23, 2022
7f5751e
override in isRelayer
emailtovamos Nov 23, 2022
5b757c7
refactor naming
emailtovamos Nov 24, 2022
ea38c55
addRelayer -> editRelayer
emailtovamos Nov 24, 2022
9a6385c
remove most compilation errors apart from transfer
emailtovamos Nov 25, 2022
ba4c474
compilation error fix: payable+map
emailtovamos Nov 25, 2022
c3cdc8d
extra payables
emailtovamos Nov 25, 2022
5d67e91
modify templates
MatusKysel Dec 9, 2022
5ee2dc0
fix bugs in contract
MatusKysel Dec 13, 2022
a32ce75
Merge pull request #219 from bnb-chain/bep-174
emailtovamos Dec 16, 2022
0bf58d1
Merge develop into relayer-admin-add-foundry
emailtovamos Dec 16, 2022
711e76e
Merge pull request #220 from bnb-chain/relayer-admin-add-foundry
emailtovamos Dec 16, 2022
5c501c4
Remove unneeded modifier + properly remove relayer
emailtovamos Dec 20, 2022
ce2ea8f
updateRelayer event and no need for removeRelayer
emailtovamos Dec 20, 2022
6dfa169
For smooth transition and let old relayers exit
emailtovamos Dec 20, 2022
21fd9d6
Only use updateRelayerEvent + Refacoring functions
emailtovamos Dec 21, 2022
b570b67
Better naming of some maps
emailtovamos Dec 21, 2022
dacd6ec
update only once
emailtovamos Dec 21, 2022
744320e
Remove repeated event
emailtovamos Dec 21, 2022
2820ea4
Remove onlyAllowedParty + correct var order
emailtovamos Dec 23, 2022
627bb00
remove deposit + bring back old withdraw
emailtovamos Jan 2, 2023
58a2a09
Merge pull request #225 from bnb-chain/relayer-admin-remove-100bnbdep…
emailtovamos Jan 2, 2023
14f9aad
test for adding new param which doesn't work
emailtovamos Jan 5, 2023
0ad173a
correct the test
emailtovamos Jan 5, 2023
e3c3829
Merge pull request #226 from bnb-chain/relayer-admin-foundrytest
emailtovamos Jan 5, 2023
af5b457
testunregister
emailtovamos Jan 9, 2023
f0b4590
Check emit event
emailtovamos Jan 9, 2023
96d1b63
remove manager and ismanger, isrelayer test
emailtovamos Jan 9, 2023
7c5f2a0
removeManagerByHimself test + comments
emailtovamos Jan 10, 2023
466688d
testCurrentRelayerTransition() and update()
emailtovamos Jan 10, 2023
61cc96c
Merge pull request #227 from bnb-chain/relayer-admin-foundrytest
emailtovamos Jan 10, 2023
d0b9e2a
Remove manager struct
emailtovamos Jan 10, 2023
58f82eb
Refactor + remove unwanted functions
emailtovamos Jan 11, 2023
673f929
Remove check that is not needed
emailtovamos Jan 11, 2023
001e9b3
Refactor to less confusing name
emailtovamos Jan 11, 2023
286f4ce
Ensure manager isn't already registered
emailtovamos Jan 12, 2023
ee74731
bug info
emailtovamos Jan 13, 2023
e941958
Test for relayer address 0 which had a bug
emailtovamos Jan 13, 2023
07efff4
Remove todo which is already done now
emailtovamos Jan 13, 2023
77c89cc
Merge pull request #228 from bnb-chain/relayer-admin-bugfix
emailtovamos Jan 13, 2023
1e4ab68
Combine to one manager struct
emailtovamos Jan 16, 2023
876f9ef
Remove dues
emailtovamos Jan 16, 2023
3d05cbf
remove unwanted code
emailtovamos Jan 16, 2023
e03d020
Merge pull request #230 from bnb-chain/relayer-admin-removedues
emailtovamos Jan 17, 2023
6186657
ensure 0 address is not a relayer
emailtovamos Jan 17, 2023
164c20a
remove deposit and dues code in updateParam
emailtovamos Jan 17, 2023
d1c4947
template file update + correct whitelist mainnet addr
emailtovamos Jan 17, 2023
1ebc8fd
check contract first
emailtovamos Jan 17, 2023
919c2bb
refactor
emailtovamos Jan 17, 2023
ae24743
Refactor even names and regenerate interface
emailtovamos Jan 20, 2023
8055814
payable manager not required
emailtovamos Jan 23, 2023
195eccb
bug fix of twice adding of same relayer + test
emailtovamos Jan 23, 2023
8ad0ad7
emit events in initialisation as well
emailtovamos Jan 23, 2023
ba6aac5
update the template file based on recent changes
emailtovamos Jan 26, 2023
06927b4
public to internal + remove unused event
emailtovamos Feb 3, 2023
0785d1d
Test to ensure a contract can't be a relayer
emailtovamos Feb 9, 2023
c9298af
2 step relayer registration. todo: test update
emailtovamos Feb 10, 2023
15380dc
Fix testAddManager
emailtovamos Feb 13, 2023
e4afcdb
Fix the rest of the tests
emailtovamos Feb 13, 2023
0f4ff88
Remove unused comments
emailtovamos Feb 13, 2023
e106889
check for code and proxy relayer + test
emailtovamos Feb 14, 2023
d2f9643
edge case where manager gets removed before accept
emailtovamos Feb 15, 2023
979cd80
name change to be consistent
emailtovamos Feb 16, 2023
e094a54
delete provisional relayer when relayer is deleted
emailtovamos Feb 20, 2023
1ecf7d0
Check for correct manager
emailtovamos Feb 20, 2023
f79183d
update comment
emailtovamos Feb 20, 2023
eb06f38
dlt managerToProvisionalRelayer when dltng relayer
emailtovamos Feb 20, 2023
e326cd0
address pr comments
emailtovamos Feb 22, 2023
9fcc64b
Merge pull request #240 from bnb-chain/relayer-admin-2steprelayer
emailtovamos Feb 22, 2023
1099e1b
update template
emailtovamos Feb 27, 2023
f012461
remove check for contracts on relayer manager
MatusKysel Mar 1, 2023
f21b86f
Merge branch 'develop' into relayer-admin
emailtovamos Mar 1, 2023
ac8698d
remove unused onlyWhitelabelRelayer
emailtovamos Mar 1, 2023
0acc058
relayer: update template
MatusKysel Mar 1, 2023
df9bd2c
extract helper function addNewManager in tests
Feb 27, 2023
1ce7f9f
Merge pull request #247 from bnb-chain/relayer-admin-relayerhub-test-…
Mister-EA Mar 2, 2023
b13d055
relayer: fix generation scripts
MatusKysel Mar 7, 2023
982ab60
Merge branch 'develop' into relayer-admin
MatusKysel Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/CrossChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ contract CrossChain is System, ICrossChain, IParamSubscriber{
return (true, packageType, relayFee, msgBytes);
}

function handlePackage(bytes calldata payload, bytes calldata proof, uint64 height, uint64 packageSequence, uint8 channelId) onlyInit onlyRelayer onlyWhitelabelRelayer
function handlePackage(bytes calldata payload, bytes calldata proof, uint64 height, uint64 packageSequence, uint8 channelId) onlyInit onlyRelayer
sequenceInOrder(packageSequence, channelId) blockSynced(height) channelSupported(channelId) headerInOrder(height, channelId) external {
bytes memory payloadLocal = payload; // fix error: stack too deep, try removing local variables
bytes memory proofLocal = proof; // fix error: stack too deep, try removing local variables
Expand Down
2 changes: 1 addition & 1 deletion contracts/CrossChain.template
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ contract CrossChain is System, ICrossChain, IParamSubscriber{
return (true, packageType, relayFee, msgBytes);
}

function handlePackage(bytes calldata payload, bytes calldata proof, uint64 height, uint64 packageSequence, uint8 channelId) onlyInit onlyRelayer onlyWhitelabelRelayer
function handlePackage(bytes calldata payload, bytes calldata proof, uint64 height, uint64 packageSequence, uint8 channelId) onlyInit onlyRelayer
sequenceInOrder(packageSequence, channelId) blockSynced(height) channelSupported(channelId) headerInOrder(height, channelId) external {
bytes memory payloadLocal = payload; // fix error: stack too deep, try removing local variables
bytes memory proofLocal = proof; // fix error: stack too deep, try removing local variables
Expand Down
227 changes: 145 additions & 82 deletions contracts/RelayerHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,151 @@ import "./System.sol";
import "./lib/SafeMath.sol";


contract RelayerHub is IRelayerHub, System, IParamSubscriber{
using SafeMath for uint256;

uint256 public constant INIT_REQUIRED_DEPOSIT = 1e20;
uint256 public constant INIT_DUES = 1e17;

uint256 public requiredDeposit;
uint256 public dues;

mapping(address =>relayer) relayers;
mapping(address =>bool) relayersExistMap;

struct relayer{
uint256 deposit;
uint256 dues;
}

modifier notContract() {
require(!isContract(msg.sender), "contract is not allowed to be a relayer");
_;
}

modifier noProxy() {
require(msg.sender == tx.origin, "no proxy is allowed");
_;
}

modifier noExist() {
require(!relayersExistMap[msg.sender], "relayer already exist");
_;
}
MatusKysel marked this conversation as resolved.
Show resolved Hide resolved

modifier exist() {
require(relayersExistMap[msg.sender], "relayer do not exist");
_;
}

event relayerRegister(address _relayer);
event relayerUnRegister(address _relayer);
event paramChange(string key, bytes value);


function init() external onlyNotInit{
requiredDeposit = INIT_REQUIRED_DEPOSIT;
dues = INIT_DUES;
alreadyInit = true;
}

function register() external payable noExist onlyInit notContract noProxy{
revert("register suspended");
}


function unregister() external exist onlyInit{
relayer memory r = relayers[msg.sender];
msg.sender.transfer(r.deposit.sub(r.dues));
address payable systemPayable = address(uint160(SYSTEM_REWARD_ADDR));
systemPayable.transfer(r.dues);
delete relayersExistMap[msg.sender];
delete relayers[msg.sender];
emit relayerUnRegister(msg.sender);
}

/*********************** Param update ********************************/
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 > 1 && newRequiredDeposit <= 1e21 && 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 {
require(false, "unknown param");
contract RelayerHub is IRelayerHub, System, IParamSubscriber {
using SafeMath for uint256;

uint256 public constant INIT_REQUIRED_DEPOSIT = 1e20;
uint256 public constant INIT_DUES = 1e17;
address public constant WHITELIST_1 = 0xb005741528b86F5952469d80A8614591E3c5B632;
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
address public constant WHITELIST_2 = 0x446AA6E0DC65690403dF3F127750da1322941F3e;

uint256 public requiredDeposit; // have to keep it to not break the storage layout
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
uint256 public dues;

mapping(address => relayer) relayers; // old map holding the relayers which are to be allowed safe exit
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
mapping(address => bool) relayersExistMap;

struct relayer {
uint256 deposit;
uint256 dues;
}
emit paramChange(key, value);
}

function isRelayer(address sender) external override view returns (bool) {
return relayersExistMap[sender];
}
mapping(address => bool) relayManagersExistMap;
MatusKysel marked this conversation as resolved.
Show resolved Hide resolved
mapping(address => address) managerToRelayer;
mapping(address => bool) currentRelayers;

bool public whitelistInitDone;

modifier onlyManager(){
require(relayManagersExistMap[msg.sender], "manager does not exist");
_;
}

modifier exist() {
require(relayersExistMap[msg.sender], "relayer do not exist");
_;
}

event relayerRegister(address _relayer);
event relayerUnRegister(address _relayer);
event paramChange(string key, bytes value);

event managerRemoved(address _removedManager);
event managerAdded(address _addedManager);
event relayerUpdated(address _from, address _to);

function init() external onlyNotInit {
requiredDeposit = INIT_REQUIRED_DEPOSIT;
dues = INIT_DUES;
alreadyInit = true;
}

function unregister() external exist onlyInit {
relayer memory r = relayers[msg.sender];
msg.sender.transfer(r.deposit.sub(r.dues));
address payable systemPayable = address(uint160(SYSTEM_REWARD_ADDR));
systemPayable.transfer(r.dues);
delete relayersExistMap[msg.sender];
delete relayers[msg.sender];
emit relayerUnRegister(msg.sender);
}

function whitelistInit() external {
require(!whitelistInitDone, "the whitelists already updated");
addInitRelayer(WHITELIST_1);
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
addInitRelayer(WHITELIST_2);
whitelistInitDone = true;
}

function addInitRelayer(address addr) internal {
relayManagersExistMap[addr] = true;
managerToRelayer[addr] = addr; // for the current whitelisted relayers we are keeping manager and relayer address the same
currentRelayers[addr] = true;
cosinlink marked this conversation as resolved.
Show resolved Hide resolved
emit managerAdded(addr);
emit relayerUpdated(address(0), addr);
}

/*********************** Param update ********************************/
function updateParam(string calldata key, bytes calldata value) external override onlyInit onlyGov {
if (Memory.compareStrings(key, "addManager")) {
require(value.length == 20, "length of manager address mismatch");
address newManager = BytesToTypes.bytesToAddress(20, value);
addManagerByGov(newManager);
} else if (Memory.compareStrings(key, "removeManager")) {
require(value.length == 20, "length of manager address mismatch");
address managerAddress = BytesToTypes.bytesToAddress(20, value);
removeManager(managerAddress);
} else {
require(false, "unknown param");
}
emit paramChange(key, value);
}

function removeManagerByHimself() external {
// here the manager removes himself
removeManager(msg.sender);
}

function removeManager(address managerAddress) internal {
// check if the manager address already exists
require(relayManagersExistMap[managerAddress], "manager doesn't exist");
MatusKysel marked this conversation as resolved.
Show resolved Hide resolved

address relayerAddress = managerToRelayer[managerAddress];

delete (relayManagersExistMap[managerAddress]);
delete (managerToRelayer[managerAddress]);

// emit success event
emit managerRemoved(managerAddress);
if (relayerAddress != address(0)) {
delete (currentRelayers[relayerAddress]);
emit relayerUpdated(relayerAddress, address(0));
}
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
}

function addManagerByGov(address managerToBeAdded) internal {
require(!relayManagersExistMap[managerToBeAdded], "manager already exists");
require(!isContract(managerToBeAdded), "contract is not allowed to be a manager");

MatusKysel marked this conversation as resolved.
Show resolved Hide resolved
relayManagersExistMap[managerToBeAdded] = true;

emit managerAdded(managerToBeAdded);
}

// updateRelayer() can be used to add relayer for the first time, update it in future and remove it
// in case of removal we can simply update it to a non-existing account
function updateRelayer(address relayerToBeAdded) public onlyManager {
require(!isContract(relayerToBeAdded), "contract is not allowed to be a relayer");

address oldRelayer = managerToRelayer[msg.sender];

if (relayerToBeAdded != address(0)) {
require(!currentRelayers[relayerToBeAdded], "relayer already exists");
currentRelayers[relayerToBeAdded] = true;
managerToRelayer[msg.sender] = relayerToBeAdded;
} else {
delete managerToRelayer[msg.sender];
}

delete currentRelayers[oldRelayer];
emit relayerUpdated(oldRelayer, relayerToBeAdded);
}

function isRelayer(address relayerAddress) external override view returns (bool){
emailtovamos marked this conversation as resolved.
Show resolved Hide resolved
return currentRelayers[relayerAddress];
}

function isManager(address relayerAddress) external view returns (bool){
return relayManagersExistMap[relayerAddress];
}
}
Loading