Skip to content

Commit

Permalink
Merge pull request DMDcoin#234 from SurfingNerd/i231-phase-swith-for-…
Browse files Browse the repository at this point in the history
…dao-2

I231 phase swith for dao
  • Loading branch information
SurfingNerd authored Jul 23, 2024
2 parents e1e2cf3 + 56f0d14 commit e8c4032
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
16 changes: 16 additions & 0 deletions contracts/BlockRewardHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/
import { IBlockRewardHbbft } from "./interfaces/IBlockRewardHbbft.sol";
import { IStakingHbbft } from "./interfaces/IStakingHbbft.sol";
import { IValidatorSetHbbft } from "./interfaces/IValidatorSetHbbft.sol";
import { IGovernancePot } from "./interfaces/IGovernancePot.sol";
import { SYSTEM_ADDRESS } from "./lib/Constants.sol";
import { Unauthorized, ValidatorsListEmpty, ZeroAddress } from "./lib/Errors.sol";
import { TransferUtils } from "./utils/TransferUtils.sol";
Expand Down Expand Up @@ -442,6 +443,21 @@ contract BlockRewardHbbft is
} else if (block.timestamp >= stakingContract.stakingFixedEpochEndTime()) {
validatorSetContract.handleFailedKeyGeneration();
}

// on closing a block, the governance pot is able to execute functions.
// those calls are able to fail.
// more details: https://github.com/DMDcoin/diamond-contracts-core/issues/231
IGovernancePot governancePot = IGovernancePot(governancePotAddress);

// solhint-disable no-empty-blocks
try governancePot.switchPhase() {
// all good, we just wanted to catch.
} catch {
// we got an error on switch phase.
// phase switching currently not working in an automated way.
// This is a problem in the DAO, but closing blocks should still work as expected.
}
// solhint-enable no-empty-blocks
}

function _markRewardedValidators(
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/IGovernancePot.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: Apache 2.0
pragma solidity =0.8.25;

interface IGovernancePot {
function switchPhase() external;
}
27 changes: 27 additions & 0 deletions contracts/mockContracts/DaoMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache 2.0
pragma solidity =0.8.25;


import { IGovernancePot } from "../interfaces/IGovernancePot.sol";

contract DaoMock is IGovernancePot {

uint256 public phaseCounter;

error SwitchPhaseReverted();

function switchPhase() external {
phaseCounter += 1;

// in order to also test the scenario that the DAO switchPhase() reverts,
// and the blocks are still able to get closed,
// we revert here.
if (phaseCounter > 4) {
revert SwitchPhaseReverted();
}
}

receive() external payable {

}
}
1 change: 1 addition & 0 deletions contracts/utils/TransferUtils.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: Apache 2.0
pragma solidity =0.8.25;

library TransferUtils {
Expand Down
11 changes: 10 additions & 1 deletion test/BlockRewardHbbft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../src/types";

import { getNValidatorsPartNAcks } from "./testhelpers/data";
import { GovernanceAddress, deployDao } from "./testhelpers/daoDeployment";

// one epoch in 1 day.
const STAKING_FIXED_EPOCH_DURATION = 86400n;
Expand All @@ -25,11 +26,13 @@ const DELEGATOR_MIN_STAKE = ethers.parseEther('100');
const MAX_STAKE = ethers.parseEther('100000');

const SystemAccountAddress = '0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE';
const GovernanceAddress = '0xDA0da0da0Da0Da0Da0DA00DA0da0da0DA0DA0dA0';


const addToDeltaPotValue = ethers.parseEther('60');
const validatorInactivityThreshold = 365n * 86400n // 1 year

let contractDeployCounter = 0;

describe('BlockRewardHbbft', () => {
let owner: HardhatEthersSigner;
let accounts: HardhatEthersSigner[];
Expand Down Expand Up @@ -80,6 +83,12 @@ describe('BlockRewardHbbft', () => {
async function deployContractsFixture() {
const { parts, acks } = getNValidatorsPartNAcks(initialValidators.length);

contractDeployCounter = contractDeployCounter + 1;

// every second deployment we add the DAOMock contract,
// so we also cover the possibility that no contract was deployed.
await deployDao();

const ConnectivityTrackerFactory = await ethers.getContractFactory("ConnectivityTrackerHbbftMock");
const connectivityTrackerContract = await ConnectivityTrackerFactory.deploy();
await connectivityTrackerContract.waitForDeployment();
Expand Down
18 changes: 18 additions & 0 deletions test/testhelpers/daoDeployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ethers, network, upgrades } from "hardhat";


export const GovernanceAddress = '0xDA0da0da0Da0Da0Da0DA00DA0da0da0DA0DA0dA0';

/// deploys the DAO Mock on the predefined address `GovernanceAddress`.
export async function deployDao() {

// we fake the deployment of a governance contract here.
const DaoMockFactory = await ethers.getContractFactory("DaoMock");
let deployedDaoMock = await (await DaoMockFactory.deploy()).waitForDeployment();
let daoMockBytecode = await deployedDaoMock.getDeployedCode();

await network.provider.send("hardhat_setCode", [
GovernanceAddress,
daoMockBytecode!,
]);
}

0 comments on commit e8c4032

Please sign in to comment.