Skip to content

Commit

Permalink
unit tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-schultz committed Jan 21, 2025
1 parent 3f289c8 commit 0954c14
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ 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";
import {ACP99Manager, PChainOwner} from "../ACP99Manager.sol";
import {ACP99Manager, PChainOwner, ConversionData} from "../ACP99Manager.sol";
import {ValidatorManager} from "../ValidatorManager.sol";
import {ValidatorMessages} from "../ValidatorMessages.sol";

contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
using SafeERC20 for IERC20Mintable;
Expand All @@ -29,14 +31,25 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {

_setUp();
_mockGetBlockchainID();
_mockInitializeValidatorSet();
app.initializeValidatorSet(_defaultConversionData(), 0);

ConversionData memory conversion = _defaultConversionData();
bytes32 conversionID = sha256(ValidatorMessages.packConversionData(conversion));
_mockInitializeValidatorSet(conversionID);
validatorManager.initializeValidatorSet(conversion, 0);
}

//
// Initialization unit tests
// The pattern in these tests requires that only non-admin validator manager functions are called,
// as each test re-deploys the ERC20TokenStakingManager contract.
//
function testDisableInitialization() public {
app = new ERC20TokenStakingManager(ICMInitializable.Disallowed);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));
app.initialize(_defaultPoSSettings(), token);

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

function testZeroTokenAddress() public {
Expand All @@ -46,7 +59,10 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
ERC20TokenStakingManager.InvalidTokenAddress.selector, address(0)
)
);
app.initialize(_defaultPoSSettings(), IERC20Mintable(address(0)));

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
app.initialize(defaultPoSSettings, IERC20Mintable(address(0)));
}

function testZeroMinimumDelegationFee() public {
Expand All @@ -56,6 +72,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumDelegationFeeBips = 0;
app.initialize(defaultPoSSettings, token);
}
Expand All @@ -70,6 +87,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumDelegationFeeBips = minimumDelegationFeeBips;
app.initialize(defaultPoSSettings, token);
}
Expand All @@ -83,6 +101,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumStakeAmount = DEFAULT_MAXIMUM_STAKE_AMOUNT;
defaultPoSSettings.maximumStakeAmount = DEFAULT_MINIMUM_STAKE_AMOUNT;
app.initialize(defaultPoSSettings, token);
Expand All @@ -95,6 +114,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.maximumStakeMultiplier = 0;
app.initialize(defaultPoSSettings, token);
}
Expand All @@ -109,6 +129,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumStakeDuration = minimumStakeDuration;
app.initialize(defaultPoSSettings, token);
}
Expand All @@ -123,6 +144,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.maximumStakeMultiplier = maximumStakeMultiplier;
app.initialize(defaultPoSSettings, token);
}
Expand All @@ -134,6 +156,7 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.weightToValueFactor = 0;
app.initialize(defaultPoSSettings, token);
}
Expand Down Expand Up @@ -242,15 +265,18 @@ contract ERC20TokenStakingManagerTest is PoSValidatorManagerTest {
app = new ERC20TokenStakingManager(ICMInitializable.Allowed);
token = new ExampleERC20();
rewardCalculator = new ExampleRewardCalculator(DEFAULT_REWARD_RATE);
validatorManager = new ValidatorManager(ICMInitializable.Allowed);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.rewardCalculator = rewardCalculator;
defaultPoSSettings.manager = validatorManager;

validatorManager.initialize(_defaultSettings(address(app)));
app.initialize(defaultPoSSettings, token);

validatorManager = app;
posValidatorManager = app;

return app;
return validatorManager;
}

function _getStakeAssetBalance(address account) internal view override returns (uint256) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {INativeMinter} from
"@avalabs/[email protected]/contracts/interfaces/INativeMinter.sol";
import {ValidatorManagerTest} from "./ValidatorManagerTests.t.sol";
import {Initializable} from "@openzeppelin/[email protected]/proxy/utils/Initializable.sol";
import {ACP99Manager, PChainOwner} from "../ACP99Manager.sol";
import {ACP99Manager, PChainOwner, ConversionData} from "../ACP99Manager.sol";
import {ValidatorManager} from "../ValidatorManager.sol";
import {ValidatorMessages} from "../ValidatorMessages.sol";

contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
NativeTokenStakingManager public app;
Expand All @@ -25,15 +27,25 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {

_setUp();
_mockGetBlockchainID();
_mockInitializeValidatorSet();
app.initializeValidatorSet(_defaultConversionData(), 0);

ConversionData memory conversion = _defaultConversionData();
bytes32 conversionID = sha256(ValidatorMessages.packConversionData(conversion));
_mockInitializeValidatorSet(conversionID);
validatorManager.initializeValidatorSet(conversion, 0);
}

//
// Initialization unit tests
// The pattern in these tests requires that only non-admin validator manager functions are called,
// as each test re-deploys the NativeTokenStakingManager contract.
//
function testDisableInitialization() public {
app = new NativeTokenStakingManager(ICMInitializable.Disallowed);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));

app.initialize(_defaultPoSSettings());
PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
app.initialize(defaultPoSSettings);
}

function testZeroMinimumDelegationFee() public {
Expand All @@ -43,6 +55,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumDelegationFeeBips = 0;
app.initialize(defaultPoSSettings);
}
Expand All @@ -57,6 +70,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumDelegationFeeBips = minimumDelegationFeeBips;
app.initialize(defaultPoSSettings);
}
Expand All @@ -70,6 +84,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumStakeAmount = DEFAULT_MAXIMUM_STAKE_AMOUNT;
defaultPoSSettings.maximumStakeAmount = DEFAULT_MINIMUM_STAKE_AMOUNT;
app.initialize(defaultPoSSettings);
Expand All @@ -82,6 +97,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.maximumStakeMultiplier = 0;
app.initialize(defaultPoSSettings);
}
Expand All @@ -96,6 +112,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.maximumStakeMultiplier = maximumStakeMultiplier;
app.initialize(defaultPoSSettings);
}
Expand All @@ -107,6 +124,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.weightToValueFactor = 0;
app.initialize(defaultPoSSettings);
}
Expand All @@ -121,6 +139,7 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.manager = validatorManager;
defaultPoSSettings.minimumStakeDuration = minStakeDuration;
app.initialize(defaultPoSSettings);
}
Expand Down Expand Up @@ -199,14 +218,18 @@ contract NativeTokenStakingManagerTest is PoSValidatorManagerTest {
// Construct the object under test
app = new TestableNativeTokenStakingManager(ICMInitializable.Allowed);
rewardCalculator = new ExampleRewardCalculator(DEFAULT_REWARD_RATE);
validatorManager = new ValidatorManager(ICMInitializable.Allowed);

PoSValidatorManagerSettings memory defaultPoSSettings = _defaultPoSSettings();
defaultPoSSettings.rewardCalculator = rewardCalculator;
defaultPoSSettings.manager = validatorManager;

validatorManager.initialize(_defaultSettings(address(app)));
app.initialize(defaultPoSSettings);

validatorManager = app;
posValidatorManager = app;
return app;

return validatorManager;
}

function _getStakeAssetBalance(address account) internal view override returns (uint256) {
Expand Down
37 changes: 15 additions & 22 deletions contracts/validator-manager/tests/PoAValidatorManagerTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
pragma solidity 0.8.25;

import {PoAValidatorManager} from "../PoAValidatorManager.sol";
import {ValidatorManager, ValidatorManagerSettings} from "../ValidatorManager.sol";
import {ValidatorManager} from "../ValidatorManager.sol";
import {ValidatorManagerTest} from "./ValidatorManagerTests.t.sol";
import {ICMInitializable} from "@utilities/ICMInitializable.sol";
import {OwnableUpgradeable} from
"@openzeppelin/[email protected]/access/OwnableUpgradeable.sol";
import {Initializable} from "@openzeppelin/[email protected]/proxy/utils/Initializable.sol";
import {ACP99Manager, PChainOwner} from "../ACP99Manager.sol";
import {ACP99Manager, PChainOwner, ConversionData} from "../ACP99Manager.sol";
import {ValidatorManager} from "../ValidatorManager.sol";
import {ValidatorMessages} from "../ValidatorMessages.sol";

contract PoAValidatorManagerTest is ValidatorManagerTest {
PoAValidatorManager public app;
Expand All @@ -24,21 +26,17 @@ contract PoAValidatorManagerTest is ValidatorManagerTest {

_setUp();
_mockGetBlockchainID();
_mockInitializeValidatorSet();
app.initializeValidatorSet(_defaultConversionData(), 0);

ConversionData memory conversion = _defaultConversionData();
bytes32 conversionID = sha256(ValidatorMessages.packConversionData(conversion));
_mockInitializeValidatorSet(conversionID);
validatorManager.initializeValidatorSet(conversion, 0);
}

function testDisableInitialization() public {
app = new PoAValidatorManager(ICMInitializable.Disallowed);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));
app.initialize(
ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
address(this)
);
app.initialize(validatorManager, address(this));
}

function testInvalidOwnerRegistration() public {
Expand Down Expand Up @@ -114,17 +112,12 @@ contract PoAValidatorManagerTest is ValidatorManagerTest {

function _setUp() internal override returns (ACP99Manager) {
app = new PoAValidatorManager(ICMInitializable.Allowed);
app.initialize(
ValidatorManagerSettings({
subnetID: DEFAULT_SUBNET_ID,
churnPeriodSeconds: DEFAULT_CHURN_PERIOD,
maximumChurnPercentage: DEFAULT_MAXIMUM_CHURN_PERCENTAGE
}),
address(this)
);
validatorManager = app;
validatorManager = new ValidatorManager(ICMInitializable.Allowed);

validatorManager.initialize(_defaultSettings(address(app)));
app.initialize(validatorManager, address(this));

return app;
return validatorManager;
}

// solhint-disable-next-line no-empty-blocks
Expand Down
Loading

0 comments on commit 0954c14

Please sign in to comment.