Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test coverage #624

Merged
merged 19 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions contracts/validator-manager/PoSValidatorManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ abstract contract PoSValidatorManager is
} else if (validator.status == ValidatorStatus.Active) {
delegationEndTime = uint64(block.timestamp);
} else {
// Should be unreachable.
revert InvalidValidatorStatus(validator.status);
}

Expand Down Expand Up @@ -824,6 +825,7 @@ abstract contract PoSValidatorManager is

Validator memory validator = getValidator(delegator.validationID);
if (validator.messageNonce == 0) {
// Should be unreachable.
revert InvalidDelegationID(delegationID);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/validator-manager/ValidatorMessages.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ library ValidatorMessages {
* @param conversionData The struct representing data to pack into the message.
* @return The packed message.
*/
function packConversionData(ConversionData calldata conversionData)
function packConversionData(ConversionData memory conversionData)
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
external
pure
returns (bytes memory)
Expand Down
209 changes: 55 additions & 154 deletions contracts/validator-manager/tests/ERC20TokenStakingManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ pragma solidity 0.8.25;

import {PoSValidatorManagerTest} from "./PoSValidatorManagerTests.t.sol";
import {ERC20TokenStakingManager} from "../ERC20TokenStakingManager.sol";
import {PoSValidatorManager} from "../PoSValidatorManager.sol";
import {PoSValidatorManager, PoSValidatorManagerSettings} from "../PoSValidatorManager.sol";
import {ExampleRewardCalculator} from "../ExampleRewardCalculator.sol";
import {
ValidatorManagerSettings,
ValidatorRegistrationInput,
IValidatorManager
} from "../interfaces/IValidatorManager.sol";
import {PoSValidatorManagerSettings} from "../interfaces/IPoSValidatorManager.sol";
import {IRewardCalculator} from "../interfaces/IRewardCalculator.sol";
import {ValidatorRegistrationInput, IValidatorManager} from "../interfaces/IValidatorManager.sol";
import {ICMInitializable} from "../../utilities/ICMInitializable.sol";
import {ExampleERC20} from "@mocks/ExampleERC20.sol";
import {IERC20} from "@openzeppelin/[email protected]/token/ERC20/IERC20.sol";
import {IERC20Mintable} from "../interfaces/IERC20Mintable.sol";
import {SafeERC20} from "@openzeppelin/[email protected]/token/ERC20/utils/SafeERC20.sol";
import {Initializable} from "@openzeppelin/[email protected]/proxy/utils/Initializable.sol";
import {ValidatorManagerTest} from "./ValidatorManagerTests.t.sol";

contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
Expand All @@ -38,29 +33,31 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app.initializeValidatorSet(_defaultConversionData(), 0);
}

function testDisableInitialization() public {
app = new ERC20TokenStakingManager(ICMInitializable.Disallowed);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));
app.initialize(_defaultPoSSettings(), token);
}
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved

function testZeroTokenAddress() public {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
vm.expectRevert(
abi.encodeWithSelector(
ERC20TokenStakingManager.InvalidTokenAddress.selector, address(0)
)
);
app.initialize(_defaultPoSSettings(), IERC20Mintable(address(0)));
}

function testZeroMinimumDelegationFee() public {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
vm.expectRevert(
abi.encodeWithSelector(PoSValidatorManager.InvalidDelegationFee.selector, 0)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: 0,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
geoff-vball marked this conversation as resolved.
Show resolved Hide resolved
defaultPoSSettings.minimumDelegationFeeBips = 0;
app.initialize(defaultPoSSettings, token);
}

function testMaxMinimumDelegationFee() public {
Expand All @@ -71,24 +68,10 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
PoSValidatorManager.InvalidDelegationFee.selector, minimumDelegationFeeBips
)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: minimumDelegationFeeBips,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.minimumDelegationFeeBips = minimumDelegationFeeBips;
app.initialize(defaultPoSSettings, token);
}

function testInvalidStakeAmountRange() public {
Expand All @@ -98,77 +81,36 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
PoSValidatorManager.InvalidStakeAmount.selector, DEFAULT_MAXIMUM_STAKE_AMOUNT
)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.minimumStakeAmount = DEFAULT_MAXIMUM_STAKE_AMOUNT;
defaultPoSSettings.maximumStakeAmount = DEFAULT_MINIMUM_STAKE_AMOUNT;
app.initialize(defaultPoSSettings, token);
}

function testZeroMaxStakeMultiplier() public {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
vm.expectRevert(
abi.encodeWithSelector(PoSValidatorManager.InvalidStakeMultiplier.selector, 0)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: 0,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.maximumStakeMultiplier = 0;
app.initialize(defaultPoSSettings, token);
}

function testMinStakeDurationTooLow() public {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
uint64 minStakeDuration = DEFAULT_CHURN_PERIOD - 1;
uint64 minimumStakeDuration = DEFAULT_CHURN_PERIOD - 1;
vm.expectRevert(
abi.encodeWithSelector(
PoSValidatorManager.InvalidMinStakeDuration.selector, minStakeDuration
PoSValidatorManager.InvalidMinStakeDuration.selector, minimumStakeDuration
)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: minStakeDuration,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.minimumStakeDuration = minimumStakeDuration;
app.initialize(defaultPoSSettings, token);
}

function testMaxStakeMultiplierOverLimit() public {
Expand All @@ -179,49 +121,21 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
PoSValidatorManager.InvalidStakeMultiplier.selector, maximumStakeMultiplier
)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: maximumStakeMultiplier,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.maximumStakeMultiplier = maximumStakeMultiplier;
app.initialize(defaultPoSSettings, token);
}

function testZeroWeightToValueFactor() public {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
vm.expectRevert(
abi.encodeWithSelector(PoSValidatorManager.ZeroWeightToValueFactor.selector)
);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: 0,
rewardCalculator: IRewardCalculator(address(0)),
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.weightToValueFactor = 0;
app.initialize(defaultPoSSettings, token);
}

function testInvalidValidatorMinStakeDuration() public {
Expand Down Expand Up @@ -309,24 +223,11 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
token = new ExampleERC20();
rewardCalculator = new ExampleRewardCalculator(DEFAULT_REWARD_RATE);
app.initialize(
PoSValidatorManagerSettings({
baseSettings: ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
minimumStakeAmount: DEFAULT_MINIMUM_STAKE_AMOUNT,
maximumStakeAmount: DEFAULT_MAXIMUM_STAKE_AMOUNT,
minimumStakeDuration: DEFAULT_MINIMUM_STAKE_DURATION,
minimumDelegationFeeBips: DEFAULT_MINIMUM_DELEGATION_FEE_BIPS,
maximumStakeMultiplier: DEFAULT_MAXIMUM_STAKE_MULTIPLIER,
weightToValueFactor: DEFAULT_WEIGHT_TO_VALUE_FACTOR,
rewardCalculator: rewardCalculator,
uptimeBlockchainID: DEFAULT_SOURCE_BLOCKCHAIN_ID
}),
token
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.rewardCalculator = rewardCalculator;
app.initialize(defaultPoSSettings, token);

validatorManager = app;
posValidatorManager = app;

Expand Down
Loading