Skip to content

Commit

Permalink
feat: recover init functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cosinlink committed Aug 27, 2024
1 parent 73ca43d commit e600faa
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 69 deletions.
39 changes: 3 additions & 36 deletions contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
bool public isSystemRewardIncluded;

// BEP-294 BC-fusion
Validator[] private _tmpMigratedValidatorSet;
bytes[] private _tmpMigratedVoteAddrs;
Validator[] private _tmpMigratedValidatorSet; // @dev deprecated
bytes[] private _tmpMigratedVoteAddrs; // @dev deprecated

// BEP-341 Validators can produce consecutive blocks
uint256 public turnLength; // Consecutive number of blocks a validator receives priority for block production
Expand Down Expand Up @@ -228,35 +228,6 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
});
}

// if staking channel is not closed, store the migrated validator set and return
if (
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).registeredContractChannelMap(
VALIDATOR_CONTRACT_ADDR, STAKING_CHANNELID
)
) {
uint256 newLength = _validatorSet.length;
uint256 oldLength = _tmpMigratedValidatorSet.length;
if (oldLength > newLength) {
for (uint256 i = newLength; i < oldLength; ++i) {
_tmpMigratedValidatorSet.pop();
_tmpMigratedVoteAddrs.pop();
}
}

for (uint256 i; i < newLength; ++i) {
if (i >= oldLength) {
_tmpMigratedValidatorSet.push(_validatorSet[i]);
_tmpMigratedVoteAddrs.push(_voteAddrs[i]);
} else {
_tmpMigratedValidatorSet[i] = _validatorSet[i];
_tmpMigratedVoteAddrs[i] = _voteAddrs[i];
}
}

emit tmpValidatorSetUpdated(newLength);
return;
}

// step 0: force all maintaining validators to exit `Temporary Maintenance`
// - 1. validators exit maintenance
// - 2. clear all maintainInfo
Expand Down Expand Up @@ -1118,11 +1089,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
isFelony = false;
if (slashCount >= felonyThreshold) {
_felony(validator, index);
if (IStakeHub(STAKE_HUB_ADDR).consensusToOperator(validator) != address(0)) {
ISlashIndicator(SLASH_CONTRACT_ADDR).downtimeSlash(validator, slashCount, shouldRevert);
} else {
ISlashIndicator(SLASH_CONTRACT_ADDR).sendFelonyPackage(validator);
}
ISlashIndicator(SLASH_CONTRACT_ADDR).downtimeSlash(validator, slashCount, shouldRevert);
isFelony = true;
} else if (slashCount >= misdemeanorThreshold) {
_misdemeanor(validator);
Expand Down
28 changes: 3 additions & 25 deletions contracts/SlashIndicator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,7 @@ contract SlashIndicator is ISlashIndicator, System, IParamSubscriber, IApplicati
if (indicator.count % felonyThreshold == 0) {
indicator.count = 0;
IBSCValidatorSet(VALIDATOR_CONTRACT_ADDR).felony(validator);
if (IStakeHub(STAKE_HUB_ADDR).consensusToOperator(validator) != address(0)) {
_downtimeSlash(validator, indicator.count, false);
} else {
// send slash msg to bc if validator is not migrated
try ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendSynPackage(
SLASH_CHANNELID, encodeSlashPackage(validator), 0
) { } catch (bytes memory reason) {
emit failedFelony(validator, indicator.count, reason);
}
}
_downtimeSlash(validator, indicator.count, false);
} else if (indicator.count % misdemeanorThreshold == 0) {
IBSCValidatorSet(VALIDATOR_CONTRACT_ADDR).misdemeanor(validator);
}
Expand Down Expand Up @@ -277,17 +268,7 @@ contract SlashIndicator is ISlashIndicator, System, IParamSubscriber, IApplicati
}
}

if (IStakeHub(STAKE_HUB_ADDR).voteToOperator(_evidence.voteAddr) != address(0)) {
IStakeHub(STAKE_HUB_ADDR).maliciousVoteSlash(_evidence.voteAddr);
} else {
// send slash msg to bc if the validator not migrated
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendSynPackage(
SLASH_CHANNELID, encodeVoteSlashPackage(_evidence.voteAddr), 0
);

bytes32 voteAddrSlice = BytesLib.toBytes32(_evidence.voteAddr, 0);
emit maliciousVoteSlashed(voteAddrSlice);
}
IStakeHub(STAKE_HUB_ADDR).maliciousVoteSlash(_evidence.voteAddr);
}

function submitDoubleSignEvidence(bytes memory header1, bytes memory header2) public onlyInit {
Expand Down Expand Up @@ -336,10 +317,7 @@ contract SlashIndicator is ISlashIndicator, System, IParamSubscriber, IApplicati
* @param validator Who will be jailed
*/
function sendFelonyPackage(address validator) external override(ISlashIndicator) onlyValidatorContract onlyInit {
try ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendSynPackage(SLASH_CHANNELID, encodeSlashPackage(validator), 0) { }
catch (bytes memory reason) {
emit failedFelony(validator, 0, reason);
}
emit failedFelony(validator, 0, "deprecated");
}

function _verifyBLSSignature(VoteData memory vote, bytes memory voteAddr) internal view returns (bool) {
Expand Down
8 changes: 7 additions & 1 deletion contracts/deprecated/CrossChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ contract CrossChain is System, ICrossChain, IParamSubscriber {
}

function init() external onlyNotInit {
revert("deprecated");
batchSizeForOracle = INIT_BATCH_SIZE;

oracleSequence = -1;
previousTxHeight = 0;
txCounter = 0;

alreadyInit = true;
}

function encodePayload(
Expand Down
4 changes: 3 additions & 1 deletion contracts/deprecated/RelayerHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ contract RelayerHub is IRelayerHub, System, IParamSubscriber {
event relayerAddedProvisionally(address _relayer);

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

function unregister() external exist onlyInit {
Expand Down
19 changes: 17 additions & 2 deletions contracts/deprecated/RelayerIncentivize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ contract RelayerIncentivize is IRelayerIncentivize, System, IParamSubscriber {
event rewardToRelayer(address relayer, uint256 amount);

function init() external onlyNotInit {
revert("deprecated");
require(!alreadyInit, "already initialized");
headerRelayerRewardRateMolecule = HEADER_RELAYER_REWARD_RATE_MOLECULE;
headerRelayerRewardRateDenominator = HEADER_RELAYER_REWARD_RATE_DENOMINATOR;
callerCompensationMolecule = CALLER_COMPENSATION_MOLECULE;
callerCompensationDenominator = CALLER_COMPENSATION_DENOMINATOR;
alreadyInit = true;
}

receive() external payable {
Expand All @@ -64,7 +69,17 @@ contract RelayerIncentivize is IRelayerIncentivize, System, IParamSubscriber {
}

function claimRelayerReward(address relayerAddr) external {
revert("deprecated");
uint256 reward = relayerRewardVault[relayerAddr];
require(reward > 0, "no relayer reward");
relayerRewardVault[relayerAddr] = 0;
address payable recipient = address(uint160(relayerAddr));
if (!recipient.send(reward)) {
address payable systemPayable = address(uint160(SYSTEM_REWARD_ADDR));
systemPayable.transfer(reward);
emit rewardToRelayer(SYSTEM_REWARD_ADDR, reward);
return;
}
emit rewardToRelayer(relayerAddr, reward);
}

function calculateTransferRelayerWeight(uint256 count) public pure returns (uint256) {
Expand Down
4 changes: 1 addition & 3 deletions contracts/deprecated/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ contract Staking is IStaking, System, IParamSubscriber, IApplication {
event failedSynPackage(uint8 indexed eventType, uint256 errCode);
event crashResponse(uint8 indexed eventType);

receive() external payable {
revert("deprecated");
}
receive() external payable { }

/*----------------- Implement cross chain app -----------------*/
function handleSynPackage(
Expand Down
67 changes: 66 additions & 1 deletion contracts/deprecated/TendermintLightClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,27 @@ contract TendermintLightClient is ILightClient, System, IParamSubscriber {
event paramChange(string key, bytes value);

function init() external onlyNotInit {
revert("deprecated");
uint256 pointer;
uint256 length;
(pointer, length) = Memory.fromBytes(INIT_CONSENSUS_STATE_BYTES);

/* solium-disable-next-line */
assembly {
sstore(chainID_slot, mload(pointer))
}

ConsensusState memory cs;
uint64 height;
(cs, height) = decodeConsensusState(pointer, length, false);
cs.preValidatorSetChangeHeight = 0;
lightClientConsensusStates[height] = cs;

initialHeight = height;
latestHeight = height;
alreadyInit = true;
rewardForValidatorSetChange = INIT_REWARD_FOR_VALIDATOR_SER_CHANGE;

emit initConsensusState(initialHeight, cs.appHash);
}

function syncTendermintHeader(bytes calldata header, uint64 height) external onlyRelayer returns (bool) {
Expand Down Expand Up @@ -73,6 +93,51 @@ contract TendermintLightClient is ILightClient, System, IParamSubscriber {
return string(chainIDStr);
}

// | chainID | height | appHash | curValidatorSetHash | [{validator pubkey, voting power}] |
// | 32 bytes | 8 bytes | 32 bytes | 32 bytes | [{32 bytes, 8 bytes}] |
/* solium-disable-next-line */
function decodeConsensusState(
uint256 ptr,
uint256 size,
bool leaveOutValidatorSet
) internal pure returns (ConsensusState memory, uint64) {
ptr = ptr + 8;
uint64 height;
/* solium-disable-next-line */
assembly {
height := mload(ptr)
}

ptr = ptr + 32;
bytes32 appHash;
/* solium-disable-next-line */
assembly {
appHash := mload(ptr)
}

ptr = ptr + 32;
bytes32 curValidatorSetHash;
/* solium-disable-next-line */
assembly {
curValidatorSetHash := mload(ptr)
}

ConsensusState memory cs;
cs.appHash = appHash;
cs.curValidatorSetHash = curValidatorSetHash;

if (!leaveOutValidatorSet) {
uint256 dest;
uint256 length;
cs.nextValidatorSet = new bytes(size - 104);
(dest, length) = Memory.fromBytes(cs.nextValidatorSet);

Memory.copy(ptr + 32, dest, length);
}

return (cs, height);
}

function updateParam(string calldata key, bytes calldata value) external override onlyInit onlyGov {
revert("deprecated");
}
Expand Down

0 comments on commit e600faa

Please sign in to comment.