Skip to content

Commit

Permalink
Add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SamBorisov committed Nov 15, 2024
1 parent 80cb917 commit 2621e6a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions contracts/HydraChain/modules/Inspector/Inspector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ abstract contract Inspector is IInspector, ValidatorManager {
* @inheritdoc IInspector
*/
function isSubjectToFinishBan(address account) public view returns (bool) {
if (validators[account].status == ValidatorStatus.Banned) {
return false;
}
// check if the owner (governance) is calling
if (msg.sender == owner()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ abstract contract ValidatorManager is
* @inheritdoc IValidatorManager
*/
function deactivateValidator(address account) external onlyHydraStaking {
if (validators[account].status != ValidatorStatus.Active) revert Unauthorized("MUST_BE_ACTIVE");

validators[account].status = ValidatorStatus.Registered;
activeValidatorsCount--;
}
Expand Down
3 changes: 2 additions & 1 deletion contracts/PriceOracle/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ contract PriceOracle is IPriceOracle, System, Initializable, HydraChainConnector
mapping(uint256 => uint256) public pricePerDay;
mapping(uint256 => List) public priceVotesForDay;

uint256 constant MAX_UINT224 = type(uint224).max;

Check warning on line 23 in contracts/PriceOracle/PriceOracle.sol

View workflow job for this annotation

GitHub Actions / lint

Explicitly mark visibility of state
uint256 public constant VOTING_POWER_PERCENTAGE_NEEDED = 61;
uint256 public constant DAILY_VOTING_START_TIME = 36 minutes;
uint256 public constant DAILY_VOTING_END_TIME = DAILY_VOTING_START_TIME + 3 hours;
Expand All @@ -37,7 +38,7 @@ contract PriceOracle is IPriceOracle, System, Initializable, HydraChainConnector
* @inheritdoc IPriceOracle
*/
function vote(uint256 price) external {
if (price == 0) {
if (price == 0 || price > MAX_UINT224) {
revert InvalidPrice();
}

Expand Down
20 changes: 20 additions & 0 deletions test/HydraChain/Inspector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,26 @@ export function RunInspectorTests(): void {
.to.not.be.reverted;
});

it("should not ban validator if he is already banned", async function () {
const { systemHydraChain, hydraStaking } = await loadFixture(this.fixtures.stakedValidatorsStateFixture);

// commit a couple of epochs in order to have a timestamp
await commitEpochs(
systemHydraChain,
hydraStaking,
[this.signers.validators[0], this.signers.validators[1]],
5, // number of epochs to commit
this.epochSize
);

await expect(systemHydraChain.connect(this.signers.governance).banValidator(this.signers.validators[0].address))
.to.not.be.reverted;

await expect(
systemHydraChain.connect(this.signers.governance).banValidator(this.signers.validators[0].address)
).to.be.revertedWithCustomError(systemHydraChain, "NoBanSubject");
});

it("should set bansInitiate to 0 on ban", async function () {
const { hydraChain } = await loadFixture(this.fixtures.banInitiatedFixtureFunction);

Expand Down
11 changes: 11 additions & 0 deletions test/PriceOracle/PriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ethers, network } from "hardhat";
/* eslint-disable node/no-extraneous-import */
import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { BigNumber } from "ethers";

import * as mcl from "../../ts/mcl";
import { CHAIN_ID, DAY, DOMAIN, ERRORS, INITIAL_PRICE, MAX_ACTIVE_VALIDATORS } from "../constants";
Expand Down Expand Up @@ -57,6 +58,16 @@ export function RunPriceOracleTests(): void {
);
});

it("should revert vote with price bigger than max uint224", async function () {
const { priceOracle } = await loadFixture(this.fixtures.stakedValidatorsStateFixture);

const maxUint224 = BigNumber.from(2).pow(224).sub(1);

await expect(
priceOracle.connect(this.signers.validators[0]).vote(maxUint224.add(1))
).to.be.revertedWithCustomError(priceOracle, "InvalidPrice");
});

it("should revert when not in voting time", async function () {
const { priceOracle } = await loadFixture(this.fixtures.initializedHydraChainStateFixture);

Expand Down
5 changes: 4 additions & 1 deletion test/VestingManager/VestingManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export function RunVestingManagerTests(): void {
describe("", function () {
describe("VestingManager initializations", function () {
it("should validate values when VestingManager is cloned", async function () {
const { vestManager, hydraDelegation, vestManagerOwner } = await loadFixture(this.fixtures.vestManagerFixture);
const { vestManager, hydraDelegation, vestManagerOwner, liquidToken } = await loadFixture(
this.fixtures.vestManagerFixture
);

expect(await vestManager.HYDRA_DELEGATION()).to.equal(hydraDelegation.address);
expect(await vestManager.LIQUIDITY_TOKEN()).to.equal(liquidToken.address);
expect(await vestManager.owner()).to.equal(vestManagerOwner.address);
});

Expand Down
2 changes: 1 addition & 1 deletion test/forge/InitializedContracts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract contract InitializedContracts is Test {
aprCalculator.initialize(governance, address(hydraChain), address(priceOracle), prices);

// ⭐️ Initialize VestingManagerFactory
vestingManagerFactory.initialize(address(hydraDelegation));
vestingManagerFactory.initialize(address(hydraDelegation), address(liquidityToken));

// ⭐️ Initialize PriceOracle
priceOracle.initialize(address(hydraChain), address(aprCalculator));
Expand Down
5 changes: 4 additions & 1 deletion test/forge/PriceModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract TestUpdatePrice is InitlizeAPR {

function test_updateBonusesWithPricesGoingUp() public {
vm.startPrank(governance);
for (uint256 i = 0; i < 115; i++) {
for (uint256 i = 1; i < 116; i++) {
aprCalculator.updatePrice(600 * i, i);
assertEq(aprCalculator.latestDailyPrice(), 600 * i);
assertEq(aprCalculator.pricePerDay(i), 600 * i);
Expand All @@ -76,6 +76,9 @@ contract TestUpdatePrice is InitlizeAPR {
vm.startPrank(governance);
for (uint256 i = 0; i < 500; i++) {
uint256 price = generateRandomAmount(i);
if (price == 0) {
price = 1;
}
aprCalculator.updatePrice(price, i);
assertEq(aprCalculator.latestDailyPrice(), price);
assertEq(aprCalculator.pricePerDay(i), price);
Expand Down

0 comments on commit 2621e6a

Please sign in to comment.