Skip to content

Commit

Permalink
any validator can vote (active or inactive)
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalman6 committed Sep 5, 2024
1 parent 037fb92 commit 8df78b0
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 23 deletions.
4 changes: 1 addition & 3 deletions contracts/DiamondDao.sol
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,7 @@ contract DiamondDao is IDiamondDao, Initializable, ReentrancyGuardUpgradeable, V
}

function _isValidator(address stakingAddress) private view returns (bool) {
address miningAddress = validatorSet.miningByStakingAddress(stakingAddress);

return validatorSet.isValidatorOrPending(miningAddress) && !validatorSet.isValidatorBanned(miningAddress);
return stakingHbbft.isPoolValid(stakingAddress);
}

/**
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IStakingHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pragma solidity =0.8.25;
interface IStakingHbbft {
function stakeAmountTotal(address) external view returns (uint256);
function totalStakedAmount() external view returns (uint256);
function isPoolValid(address) external view returns (bool);
}
2 changes: 0 additions & 2 deletions contracts/interfaces/IValidatorSetHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ pragma solidity =0.8.25;
interface IValidatorSetHbbft {
function isValidator(address) external view returns (bool);

function isValidatorBanned(address) external view returns (bool);

function isValidatorOrPending(address) external view returns (bool);

function miningByStakingAddress(address) external view returns (address);
Expand Down
11 changes: 10 additions & 1 deletion contracts/mocks/MockStakingHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
pragma solidity =0.8.25;

import { IStakingHbbft } from "../interfaces/IStakingHbbft.sol";
import { IValidatorSetHbbft } from "../interfaces/IValidatorSetHbbft.sol";

contract MockStakingHbbft is IStakingHbbft {
IValidatorSetHbbft public validatorSet;
uint256 public totalStakedAmount;
uint256 public delegatorMinStake = 100 ether;
mapping(address => uint256) private _stakeAmountTotal;
Expand Down Expand Up @@ -44,7 +46,9 @@ contract MockStakingHbbft is IStakingHbbft {
_;
}

constructor() {}
constructor(address _vs) {
validatorSet = IValidatorSetHbbft(_vs);
}

function setStake(address staking, uint256 stakeAmount) external {
_stakeAmountTotal[staking] = stakeAmount;
Expand Down Expand Up @@ -129,4 +133,9 @@ contract MockStakingHbbft is IStakingHbbft {
function getAllowedParamsRangeWithSelector(bytes4 _selector) external view returns (ParameterRange memory) {
return allowedParameterRange[_selector];
}

function isPoolValid(address _stakingAddress) public view returns (bool) {
address miningAddress = validatorSet.miningByStakingAddress(_stakingAddress);
return validatorSet.isValidator(miningAddress);
}
}
9 changes: 0 additions & 9 deletions contracts/mocks/MockValidatorSetHbbft.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ contract MockValidatorSetHbbft is IValidatorSetHbbft {
mapping(address => address) private _stakingToMining;
mapping(address => bool) private _isValidator;
mapping(address => bool) private _availability;
mapping(address => uint256) public bannedUntil;

receive() external payable {
revert();
Expand All @@ -21,10 +20,6 @@ contract MockValidatorSetHbbft is IValidatorSetHbbft {
_availability[mining] = available;
}

function addBanned(address _miningAddress, uint256 _bannedUntil) external {
bannedUntil[_miningAddress] = _bannedUntil;
}

function remove(address staking) external {
address mining = _stakingToMining[staking];

Expand All @@ -51,10 +46,6 @@ contract MockValidatorSetHbbft is IValidatorSetHbbft {
return _availability[mining] ? block.number : 0;
}

function isValidatorBanned(address _miningAddress) public view returns (bool) {
return block.timestamp <= bannedUntil[_miningAddress];
}

function isValidatorOrPending(address _miningAddress) public view returns (bool) {
return _isValidator[_miningAddress];
}
Expand Down
8 changes: 3 additions & 5 deletions test/DiamondDao.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("DiamondDao contract", function () {
const mockValidatorSet = await mockFactory.deploy();
await mockValidatorSet.waitForDeployment();

const mockStaking = await stakingFactory.deploy();
const mockStaking = await stakingFactory.deploy(await mockValidatorSet.getAddress());
await mockStaking.waitForDeployment();

const startTime = await time.latest();
Expand Down Expand Up @@ -762,16 +762,14 @@ describe("DiamondDao contract", function () {
.withArgs(voter.address);
});

it("should revert vote by banned validator", async function () {
const { dao, mockValidatorSet } = await loadFixture(deployFixture);
it("should revert vote by inactive validator", async function () {
const { dao } = await loadFixture(deployFixture);

const proposer = users[10];
const voter = users[9];

const { proposalId } = await createProposal(dao, proposer, "a");

await mockValidatorSet.add(voter.address, voter.address, false);
await mockValidatorSet.addBanned(voter.address, Math.floor(new Date().getTime() / 1000) + 1000000);
await swithPhase(dao);

await expect(
Expand Down
2 changes: 1 addition & 1 deletion test/ProposalExecution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("DAO proposal execution", function () {
const mockValidatorSet = await mockFactory.deploy();
await mockValidatorSet.waitForDeployment();

const mockStaking = await stakingFactory.deploy();
const mockStaking = await stakingFactory.deploy(await mockValidatorSet.getAddress());
await mockStaking.waitForDeployment();

const startTime = await time.latest();
Expand Down
2 changes: 1 addition & 1 deletion test/ProposalFinalization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("Proposal Acceptance Threshold", function () {
const mockValidatorSet = await mockFactory.deploy();
await mockValidatorSet.waitForDeployment();

const mockStaking = await stakingFactory.deploy();
const mockStaking = await stakingFactory.deploy(await mockValidatorSet.getAddress());
await mockStaking.waitForDeployment();

const startTime = await time.latest();
Expand Down
2 changes: 1 addition & 1 deletion test/ProposalValueGuards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("DAO Ecosystem Paramater Change Value Guards Test", function () {
const mockValidatorSet = await mockFactory.deploy();
await mockValidatorSet.waitForDeployment();

const mockStaking = await stakingFactory.deploy();
const mockStaking = await stakingFactory.deploy(await mockValidatorSet.getAddress());
await mockStaking.waitForDeployment();

const startTime = await time.latest();
Expand Down

0 comments on commit 8df78b0

Please sign in to comment.