Skip to content

Commit

Permalink
Merge pull request #50 from coredao-org/btc_stake_v2
Browse files Browse the repository at this point in the history
Support Btc staking
  • Loading branch information
charles2023wood committed Mar 29, 2024
2 parents 367adc3 + 98e2f59 commit 5e846bf
Show file tree
Hide file tree
Showing 33 changed files with 6,089 additions and 194 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/main.yaml
Expand Up @@ -44,6 +44,14 @@ jobs:
run: |
npm install
npm install -g ganache
- name: modify openssl.cnf
run: |
sudo chmod -R 777 /usr/lib/ssl/openssl.cnf
sudo sed -i 's/^#openssl_conf = openssl_init/openssl_conf = openssl_init/' /usr/lib/ssl/openssl.cnf
sudo sed -i '/^\[provider_sect\]$/,/^\[/ s/^default = default_sect$/&\nlegacy = legacy_sect/' /usr/lib/ssl/openssl.cnf
sudo sed -i 's/^# activate = 1/activate = 1/' /usr/lib/ssl/openssl.cnf
sudo echo -e "\n[legacy_sect]\nactivate = 1" >> /usr/lib/ssl/openssl.cnf
- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion abi/PledgeAgent.abi

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions brownie-config.yaml
Expand Up @@ -16,6 +16,7 @@ networks:
mnemonic: clock radar mass judge dismiss just intact mind resemble fringe diary casino
evm_version: berlin
port: 8546
unlimited_contract_size: true

compiler:
solc:
Expand Down
5 changes: 4 additions & 1 deletion contracts/BtcLightClient.sol
Expand Up @@ -291,8 +291,9 @@ contract BtcLightClient is ILightClient, System, IParamSubscriber{
/// @param nodes Part of the Merkle tree from the tx to the root in LE form (called Merkle proof)
/// @param index of the tx in Merkle tree
/// @return True if the provided tx is confirmed on Bitcoin
function checkTxProof(bytes32 txid, uint32 blockHeight, uint32 confirmBlock, bytes32[] calldata nodes, uint256 index) external view returns (bool) {
function checkTxProof(bytes32 txid, uint32 blockHeight, uint32 confirmBlock, bytes32[] calldata nodes, uint256 index) public view override returns (bool) {
bytes32 blockHash = height2HashMap[blockHeight];

if (blockHeight + confirmBlock > getChainTipHeight() || txid == bytes32(0) || blockHash == bytes32(0)) {
return false;
}
Expand Down Expand Up @@ -677,4 +678,6 @@ contract BtcLightClient is ILightClient, System, IParamSubscriber{
function getRoundCandidates(uint256 roundTimeTag) external override view returns (address[] memory candidates) {
return roundPowerMap[roundTimeTag].candidates;
}


}
11 changes: 10 additions & 1 deletion contracts/BtcLightClient.template
Expand Up @@ -291,8 +291,13 @@ contract BtcLightClient is ILightClient, System, IParamSubscriber{
/// @param nodes Part of the Merkle tree from the tx to the root in LE form (called Merkle proof)
/// @param index of the tx in Merkle tree
/// @return True if the provided tx is confirmed on Bitcoin
function checkTxProof(bytes32 txid, uint32 blockHeight, uint32 confirmBlock, bytes32[] calldata nodes, uint256 index) external view returns (bool) {
function checkTxProof(bytes32 txid, uint32 blockHeight, uint32 confirmBlock, bytes32[] calldata nodes, uint256 index) public view override returns (bool) {
bytes32 blockHash = height2HashMap[blockHeight];
{% if mock %}
if (checkResult == true){
return checkResult;
}
{% endif %}
if (blockHeight + confirmBlock > getChainTipHeight() || txid == bytes32(0) || blockHash == bytes32(0)) {
return false;
}
Expand Down Expand Up @@ -677,4 +682,8 @@ contract BtcLightClient is ILightClient, System, IParamSubscriber{
function getRoundCandidates(uint256 roundTimeTag) external override view returns (address[] memory candidates) {
return roundPowerMap[roundTimeTag].candidates;
}
{% if mock %}
bool public checkResult;
{% endif %}

}
22 changes: 11 additions & 11 deletions contracts/CandidateHub.sol
Expand Up @@ -92,7 +92,7 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
roundInterval = INIT_ROUND_INTERVAL;
validatorCount = INIT_VALIDATOR_COUNT;
maxCommissionChange = MAX_COMMISSION_CHANGE;
roundTag = 7;
roundTag = block.timestamp / INIT_ROUND_INTERVAL;
alreadyInit = true;
}

Expand All @@ -109,6 +109,13 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
return status == (status & ACTIVE_STATUS);
}

/// Whether the input address is operator address of a validator candidate
/// @param operateAddr Operator address of validator candidate
/// @return true/false
function isCandidateByOperate(address operateAddr) external override view returns (bool) {
return operateMap[operateAddr] != 0;
}

/// Jail a validator for some rounds and slash some amount of deposits
/// @param operateAddress The operator address of the validator
/// @param round The number of rounds to jail
Expand Down Expand Up @@ -196,8 +203,8 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {

// calculate the hybrid score for all valid candidates and
// choose top ones to form the validator set of the new round
(uint256[] memory scores, uint256 totalPower, uint256 totalCoin) =
IPledgeAgent(PLEDGE_AGENT_ADDR).getHybridScore(candidates, powers);
(uint256[] memory scores) =
IPledgeAgent(PLEDGE_AGENT_ADDR).getHybridScore(candidates, powers, roundTag);
address[] memory validatorList = getValidators(candidates, scores, validatorCount);

// prepare arguments, and notify ValidatorSet contract
Expand Down Expand Up @@ -225,7 +232,7 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
ISlashIndicator(SLASH_CONTRACT_ADDR).clean();

// notify PledgeAgent contract
IPledgeAgent(PLEDGE_AGENT_ADDR).setNewRound(validatorList, totalPower, totalCoin, roundTag);
IPledgeAgent(PLEDGE_AGENT_ADDR).setNewRound(validatorList, roundTag);

// update validator jail status
for (uint256 i = 0; i < candidateSize; i++) {
Expand Down Expand Up @@ -478,13 +485,6 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
return opAddrs;
}

/// Whether the input address is operator address of a validator candidate
/// @param operateAddr Operator address of validator candidate
/// @return true/false
function isCandidateByOperate(address operateAddr) external view returns (bool) {
return operateMap[operateAddr] != 0;
}

/// Whether the input address is consensus address a validator candidate
/// @param consensusAddr Consensus address of validator candidate
/// @return true/false
Expand Down
28 changes: 17 additions & 11 deletions contracts/CandidateHub.template
Expand Up @@ -60,6 +60,7 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
uint256 public roundTag;
{% if mock %}
bool public controlRoundTimeTag = false;
bool public turnroundFailed = false;
{% endif %}

struct Candidate {
Expand Down Expand Up @@ -94,7 +95,7 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
roundInterval = INIT_ROUND_INTERVAL;
validatorCount = INIT_VALIDATOR_COUNT;
maxCommissionChange = MAX_COMMISSION_CHANGE;
roundTag = 7;
roundTag = block.timestamp / INIT_ROUND_INTERVAL;
alreadyInit = true;
}
{% if mock %}
Expand All @@ -115,6 +116,13 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
return status == (status & ACTIVE_STATUS);
}

/// Whether the input address is operator address of a validator candidate
/// @param operateAddr Operator address of validator candidate
/// @return true/false
function isCandidateByOperate(address operateAddr) external override view returns (bool) {
return operateMap[operateAddr] != 0;
}

/// Jail a validator for some rounds and slash some amount of deposits
/// @param operateAddress The operator address of the validator
/// @param round The number of rounds to jail
Expand Down Expand Up @@ -161,6 +169,11 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
/// The `turn round` workflow
/// @dev this method is called by Golang consensus engine at the end of a round
function turnRound() external onlyCoinbase onlyInit onlyZeroGasPrice {
{% if mock %}
if (turnroundFailed == true){
require(false, "turnRound failed");
}
{% endif %}
// distribute rewards for the about to end round
address[] memory lastCandidates = IValidatorSet(VALIDATOR_CONTRACT_ADDR).distributeReward();

Expand Down Expand Up @@ -208,8 +221,8 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {

// calculate the hybrid score for all valid candidates and
// choose top ones to form the validator set of the new round
(uint256[] memory scores, uint256 totalPower, uint256 totalCoin) =
IPledgeAgent(PLEDGE_AGENT_ADDR).getHybridScore(candidates, powers);
(uint256[] memory scores) =
IPledgeAgent(PLEDGE_AGENT_ADDR).getHybridScore(candidates, powers, roundTag);
address[] memory validatorList = getValidators(candidates, scores, validatorCount);

// prepare arguments, and notify ValidatorSet contract
Expand Down Expand Up @@ -237,7 +250,7 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
ISlashIndicator(SLASH_CONTRACT_ADDR).clean();

// notify PledgeAgent contract
IPledgeAgent(PLEDGE_AGENT_ADDR).setNewRound(validatorList, totalPower, totalCoin, roundTag);
IPledgeAgent(PLEDGE_AGENT_ADDR).setNewRound(validatorList, roundTag);

// update validator jail status
for (uint256 i = 0; i < candidateSize; i++) {
Expand Down Expand Up @@ -490,13 +503,6 @@ contract CandidateHub is ICandidateHub, System, IParamSubscriber {
return opAddrs;
}

/// Whether the input address is operator address of a validator candidate
/// @param operateAddr Operator address of validator candidate
/// @return true/false
function isCandidateByOperate(address operateAddr) external view returns (bool) {
return operateMap[operateAddr] != 0;
}

/// Whether the input address is consensus address a validator candidate
/// @param consensusAddr Consensus address of validator candidate
/// @return true/false
Expand Down

0 comments on commit 5e846bf

Please sign in to comment.