Skip to content

Commit

Permalink
test(Profile): add concrete test for zk-evm integration
Browse files Browse the repository at this point in the history
  • Loading branch information
TuDo1403 committed Oct 25, 2024
1 parent 882e98d commit 452796c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/mocks/MockProfile.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import "./MockPrecompile.sol";
contract MockProfile is Profile {
bool internal _verificationFailed;

function addNewProfile(
function exposed_addNewProfile(
CandidateProfile memory profile
) external onlyAdmin {
) external {
CandidateProfile storage _profile = _id2Profile[profile.id];
if (_profile.id != address(0)) revert ErrExistentProfile();
_addNewProfile(_profile, profile);
Expand Down
46 changes: 45 additions & 1 deletion test/foundry/mocks/MockValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@ pragma solidity >=0.8.17 <0.9.0;
import { TConsensus } from "src/udvts/Types.sol";

contract MockValidatorSet {
uint256 _period;
struct ValidatorCandidate {
/**
* @dev The address of the candidate admin.
* @custom shadowed-storage This storage slot is always kept in sync with {Profile-CandidateProfile}.admin.
*/
address __shadowedAdmin;
/**
* @dev Address of the validator that produces block, e.g. block.coinbase. This is so-called validator address.
* @custom shadowed-storage This storage slot is always kept in sync with {Profile-CandidateProfile}.consensus.
*/
TConsensus __shadowedConsensus;
/**
* @dev Address that receives mining reward of the validator
* @custom shadowed-storage This storage slot is always kept in sync with {Profile-CandidateProfile}.treasury.
*/
address payable __shadowedTreasury;
/// @dev Address of the bridge operator corresponding to the candidate
address ____deprecatedBridgeOperatorAddr;
/**
* @dev The percentage of reward that validators can be received, the rest goes to the delegators.
* Values in range [0; 100_00] stands for 0-100%
*/
uint256 commissionRate;
/// @dev The timestamp that scheduled to revoke the candidate (no schedule=0)
uint256 revokingTimestamp;
/// @dev The deadline that the candidate must top up staking amount to keep it larger than or equal to the threshold (no deadline=0)
uint256 topupDeadline;
}

uint256 private _period;
mapping(address id => ValidatorCandidate) private _candidate;

function setPeriod(
uint256 period
Expand All @@ -27,4 +57,18 @@ contract MockValidatorSet {
function isCandidateAdminById(address, /*cid*/ address /*admin*/ ) external pure returns (bool) {
return true;
}

function getCandidateInfoById(
address id
) external view returns (ValidatorCandidate memory validatorCandidate) {
return _candidate[id];
}

function exposed_setValidatorCandidate(address id, ValidatorCandidate memory candidate) external {
_candidate[id] = candidate;
}

function exposed_setRenounceStatus(address id, uint256 revokingTimestamp) external {
_candidate[id].revokingTimestamp = revokingTimestamp;
}
}
53 changes: 53 additions & 0 deletions test/foundry/unit/ronin/profile/Profile.base.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.17 <0.9.0;

import { Test } from "forge-std/Test.sol";

import { TransparentUpgradeableProxyV2 } from "src/extensions/TransparentUpgradeableProxyV2.sol";
import { IProfile } from "src/interfaces/IProfile.sol";
import { MockProfile } from "src/mocks/MockProfile.sol";
import { TConsensus } from "src/udvts/Types.sol";
import { MockValidatorSet } from "test/foundry/mocks/MockValidatorSet.sol";

abstract contract Profile_Base_Unit_Test is Test {
MockProfile internal _profile;
MockValidatorSet internal _validatorSet;
address internal _id = makeAddr("default-id");
address internal _proxyAdmin = makeAddr("proxy-admin");
address internal _staking = makeAddr("staking");
address internal _validatorAdmin = makeAddr("default-admin");
address internal _zkRollupManager = makeAddr("zk-rollup-manager");
bytes32 constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

function setUp() public virtual {
_validatorSet = new MockValidatorSet();

MockProfile _profileLogic = new MockProfile();
_profile = MockProfile(address(new TransparentUpgradeableProxyV2(address(_profileLogic), _proxyAdmin, "")));
_profile.initialize(address(_validatorSet));
_profile.initializeV2(_staking, address(0));
_profile.initializeV3(10);
_profile.initializeV4(_zkRollupManager);

_profile.exposed_addNewProfile(
IProfile.CandidateProfile({
id: _id,
consensus: TConsensus.wrap(_id),
admin: _validatorAdmin,
treasury: payable(_id),
__reservedGovernor: address(0),
vrfKeyHash: 0x0,
pubkey: "",
profileLastChange: 0,
oldPubkey: "",
oldConsensus: TConsensus.wrap(address(0)),
registeredAt: 0,
vrfKeyHashLastChange: 0,
rollupId: 0,
aggregator: address(0),
sequencer: address(0)
})
);

vm.stopPrank();
}
}
64 changes: 7 additions & 57 deletions test/foundry/unit/ronin/profile/Profile.concrete.unit.t.sol
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.17 <0.9.0;

import { Test } from "forge-std/Test.sol";

import { TransparentUpgradeableProxyV2 } from "src/extensions/TransparentUpgradeableProxyV2.sol";
import { IProfile } from "src/interfaces/IProfile.sol";
import { MockProfile } from "src/mocks/MockProfile.sol";
import { TConsensus } from "src/udvts/Types.sol";
import { MockValidatorSet } from "test/foundry/mocks/MockValidatorSet.sol";

contract Profile_Concrete_Unit_Test is Test {
MockProfile internal _profile;
MockValidatorSet internal _validatorSetContract;
address internal immutable _stakingContract = address(0x10000);
address internal immutable _validatorAdmin = address(0x20001);
bytes32 constant ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

function setUp() public virtual {
_validatorSetContract = new MockValidatorSet();

MockProfile _profileLogic = new MockProfile();
_profile = MockProfile(address(new TransparentUpgradeableProxyV2(address(_profileLogic), address(1), "")));
_profile.initialize(address(_validatorSetContract));
_profile.initializeV2(_stakingContract, address(0));
_profile.initializeV3(10);

vm.startPrank(address(1));

TransparentUpgradeableProxyV2 _proxy = TransparentUpgradeableProxyV2(payable(address(_profile)));
_proxy.functionDelegateCall(
abi.encodeWithSelector(
MockProfile.addNewProfile.selector,
IProfile.CandidateProfile({
id: address(0x20000),
consensus: TConsensus.wrap(address(0x20000)),
admin: _validatorAdmin,
treasury: payable(address(0x20000)),
__reservedGovernor: address(0),
vrfKeyHash: 0x0,
pubkey: "",
profileLastChange: 0,
oldPubkey: "",
oldConsensus: TConsensus.wrap(address(0)),
registeredAt: 0,
vrfKeyHashLastChange: 0,
rollupId: 0,
aggregator: address(0),
sequencer: address(0)
})
)
);

vm.stopPrank();
}
import "./Profile.base.unit.t.sol";

contract Profile_Concrete_Unit_Test is Profile_Base_Unit_Test {
function testConcrete_RevertWhen_ChangePubkey() external {
IProfile.CandidateProfile memory _validatorProfile;

Expand All @@ -74,10 +24,10 @@ contract Profile_Concrete_Unit_Test is Test {
vm.stopPrank();
}

function test_RevertWhen_ApplyValidatorCandidate() external {
function testConcrete_RevertWhen_ApplyValidatorCandidate() external {
_profile.setVerificationFailed(true);

vm.startPrank(_stakingContract);
vm.startPrank(_staking);
vm.expectRevert(abi.encodeWithSelector(IProfile.ErrInvalidProofOfPossession.selector, "0xcc", ""));
_profile.execApplyValidatorCandidate({
admin: address(0x30000),
Expand All @@ -99,7 +49,7 @@ contract Profile_Concrete_Unit_Test is Test {
vm.stopPrank();
}

function test_RevertWhen_ChangePubkeyCooldownNotEnded() external {
function testConcrete_RevertWhen_ChangePubkeyCooldownNotEnded() external {
vm.startPrank(_validatorAdmin);
vm.warp(block.timestamp + 11);

Expand All @@ -118,8 +68,8 @@ contract Profile_Concrete_Unit_Test is Test {
vm.stopPrank();
}

function test_ArePublicKeysRegistered() external {
vm.startPrank(_stakingContract);
function testConcrete_ArePublicKeysRegistered() external {
vm.startPrank(_staking);

_profile.setVerificationFailed(false);
_profile.execApplyValidatorCandidate({
Expand Down

0 comments on commit 452796c

Please sign in to comment.