Skip to content

Commit

Permalink
Merge pull request #10 from 0xPolygon/fix/polUpgradeReview
Browse files Browse the repository at this point in the history
Fix/pol upgrade review
  • Loading branch information
simonDos authored Aug 22, 2024
2 parents c795e76 + 064df8a commit 753a269
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 70 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

Ethereum smart contracts that power the [Matic Network](https://polygon.technology/polygon-pos).

## Development
### Install dependencies with

```
npm install
```

### Setup
### Setup git hooks

```
pre-commit install
```

### Compile
### Prepare templates

```
npm run template:process -- --bor-chain-id 15001
Expand All @@ -27,7 +28,30 @@ bor-chain-id should be:
Mainnet = 137
TestnetV4 (Mumbai) = 80001

### Main chain and side chain
### Generate interfaces

```
npm run generate:interfaces
```

### Build

```
forge build
```

## Testing

### Run forge upgrade forktest

```
forge test
```

### Run unit tests


#### Main chain and side chain

- Main chain

Expand All @@ -51,20 +75,14 @@ npm run bor:stop
npm run bor:clean
```

### Run tests
#### Run tests

Run Hardhat test

```
npm test:hardhat
```

Run Foundry test

```
npm test:foundry
```

### Coverage

Run coverage with
Expand Down
55 changes: 28 additions & 27 deletions contracts/staking/stakeManager/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ contract StakeManager is
require(validators[validatorId].contractAddress == msg.sender, "Invalid contract address");
}

constructor() public GovernanceLockable(address(0x0)) initializer {}
constructor() public GovernanceLockable(address(0x0)) {
_disableInitializer();
}

function initialize(
address _registry,
Expand Down Expand Up @@ -108,6 +110,30 @@ contract StakeManager is
delegationEnabled = true;
}

function reinitialize(
address _NFTContract,
address _stakingLogger,
address _validatorShareFactory,
address _extensionCode
) external onlyGovernance {
require(isContract(_extensionCode));
eventsHub = address(0x0);
extensionCode = _extensionCode;
NFTContract = StakingNFT(_NFTContract);
logger = StakingInfo(_stakingLogger);
validatorShareFactory = ValidatorShareFactory(_validatorShareFactory);
}

function initializePOL(
address _tokenNew,
address _migration
) external onlyGovernance {
tokenMatic = IERC20(token);
token = IERC20(_tokenNew);
migration = IPolygonMigration(_migration);
_convertMaticToPOL(tokenMatic.balanceOf(address(this)));
}

function isOwner() public view returns (bool) {
address _owner;
bytes32 position = keccak256("matic.network.proxy.owner");
Expand Down Expand Up @@ -321,30 +347,6 @@ contract StakeManager is
_transferToken(destination, amount, true);
}

function reinitialize(
address _NFTContract,
address _stakingLogger,
address _validatorShareFactory,
address _extensionCode
) external onlyGovernance {
require(isContract(_extensionCode));
eventsHub = address(0x0);
extensionCode = _extensionCode;
NFTContract = StakingNFT(_NFTContract);
logger = StakingInfo(_stakingLogger);
validatorShareFactory = ValidatorShareFactory(_validatorShareFactory);
}

function initializePOL(
address _tokenNew,
address _migration
) external onlyGovernance {
tokenMatic = IERC20(token);
token = IERC20(_tokenNew);
migration = IPolygonMigration(_migration);
_convertMaticToPOL(tokenMatic.balanceOf(address(this)));
}

/**
Public Methods
*/
Expand Down Expand Up @@ -1113,8 +1115,7 @@ contract StakeManager is

function _unstake(uint256 validatorId, uint256 exitEpoch, bool pol) internal {
require(validators[validatorId].deactivationEpoch == 0);
// TODO: if validators unstake and slashed to 0, he will be forced to unstake again
// must think how to handle it correctly

_updateRewards(validatorId);

uint256 amount = validators[validatorId].amount;
Expand Down
17 changes: 1 addition & 16 deletions contracts/staking/validatorShare/ValidatorShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,7 @@ contract ValidatorShare is IValidatorShare, ERC20NonTradable, OwnableLockable, I
uint256 delegatedAmount,
uint256 totalAmountToSlash
) external onlyOwner returns (uint256) {
uint256 _withdrawPool = withdrawPool;
uint256 delegationAmount = delegatedAmount.add(_withdrawPool);
if (delegationAmount == 0) {
return 0;
}
// total amount to be slashed from delegation pool (active + inactive)
uint256 _amountToSlash = delegationAmount.mul(totalAmountToSlash).div(validatorStake.add(delegationAmount));
uint256 _amountToSlashWithdrawalPool = _withdrawPool.mul(_amountToSlash).div(delegationAmount);

// slash inactive pool
uint256 stakeSlashed = _amountToSlash.sub(_amountToSlashWithdrawalPool);
stakeManager.decreaseValidatorDelegatedAmount(validatorId, stakeSlashed);
activeAmount = activeAmount.sub(stakeSlashed);

withdrawPool = withdrawPool.sub(_amountToSlashWithdrawalPool);
return _amountToSlash;
revert("Slashing disabled");
}

function updateDelegation(bool _delegation) external onlyOwner {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ contract UpgradeDepositManager_Sepolia is Script {

// STEP 4
// call migrateMatic on the new DepositManager, migrating all MATIC
ERC20 maticToken = ERC20(matic);
uint256 amount = maticToken.balanceOf(address(depositManagerProxy));
bytes memory payloadMigrateMatic = abi.encodeWithSelector(
governance.update.selector, address(depositManagerProxy), abi.encodeWithSelector(depositManager.migrateMatic.selector, amount)
governance.update.selector, address(depositManagerProxy), abi.encodeWithSelector(depositManager.migrateMatic.selector)
);

console.log("\n Send payloadMigrateMatic to: ", governanceAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script {
// deploy new DepositManager version
depositManagerImpl = DepositManager(payable(deployCode("out/DepositManager.sol/DepositManager.json")));

console.log("deployed DepositManager implementation at: ", address(validatorShareImpl));
console.log("deployed DepositManager implementation at: ", address(depositManagerImpl));

vm.stopBroadcast();
}
Expand All @@ -118,7 +118,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script {
// STEP 1
// Update ValidatorShare registry entry
bytes memory payloadRegistry1 = abi.encodeCall(
governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("validatorShare"), address(validatorShareImpl))))
Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("validatorShare"), address(validatorShareImpl))))
);

console.log("Created payloadRegistry1 for: ", address(governance));
Expand All @@ -134,7 +134,7 @@ contract UpgradeStake_DepositManager_Mainnet is Script {
// STEP 3
// Call initializePOL
bytes memory payloadInitializePol3 = abi.encodeCall(
governance.update, (address(stakeManagerProxy), abi.encodeCall(stakeManagerProxy.initializePOL, (address(polToken), migrationAddress)))
Governance.update, (address(stakeManagerProxy), abi.encodeCall(StakeManager.initializePOL, (address(polToken), migrationAddress)))
);

console.log("Created payloadInitializePol3 for: ", address(governance));
Expand All @@ -143,49 +143,49 @@ contract UpgradeStake_DepositManager_Mainnet is Script {
// STEP 4
// Call updateContractMap on registry to add "pol"
bytes memory payloadContractMapPol4 =
abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("pol"), address(polToken)))));
abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("pol"), address(polToken)))));

console.log("Send payloadContractMapPol4 to: ", address(governance));
console.log("Created payloadContractMapPol4 for: ", address(governance));
console.logBytes(payloadContractMapPol4);

// STEP 5
// Call updateContractMap on registry to add "matic"
bytes memory payloadContractMapMatic5 =
abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("matic"), address(maticToken)))));
abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("matic"), address(maticToken)))));

console.log("Send payloadContractMapMatic5 to: ", address(governance));
console.log("Created payloadContractMapMatic5 for: ", address(governance));
console.logBytes(payloadContractMapMatic5);

// STEP 6
// Call updateContractMap on registry to add "polygonMigration"
bytes memory payloadContractMapMigration6 = abi.encodeCall(
governance.update, (address(registry), abi.encodeCall(registry.updateContractMap, (keccak256("polygonMigration"), migrationAddress)))
Governance.update, (address(registry), abi.encodeCall(Registry.updateContractMap, (keccak256("polygonMigration"), migrationAddress)))
);

console.log("Send payloadContractMapMigration6 to: ", address(governance));
console.log("Created payloadContractMapMigration6 for: ", address(governance));
console.logBytes(payloadContractMapMigration6);

// STEP 7
// call mapToken on the Registry to map POL to the PoS native gas token address (1010)
bytes memory payloadMapToken7 =
abi.encodeCall(governance.update, (address(registry), abi.encodeCall(registry.mapToken, (address(polToken), nativeGasTokenAddress, false))));
abi.encodeCall(Governance.update, (address(registry), abi.encodeCall(Registry.mapToken, (address(polToken), nativeGasTokenAddress, false))));

console.log("Send payloadMapToken7 to: ", address(governance));
console.log("Created payloadMapToken7 for: ", address(governance));
console.logBytes(payloadMapToken7);

// STEP 8
// update impl of proxy to DepositManager
bytes memory payloadUpgradeDepositManager8 = abi.encodeCall(DepositManagerProxy.updateImplementation, (address(depositManagerImpl)));

console.log("Send payloadUpgradeDepositManager8 to: ", address(depositManagerProxy));
console.log("Created payloadUpgradeDepositManager8 for: ", address(depositManagerProxy));
console.logBytes(payloadUpgradeDepositManager8);

// STEP 9
// call migrateMatic on the new DepositManager, migrating all MATIC
bytes memory payloadMigrateMatic9 =
abi.encodeCall(governance.update, (address(depositManagerProxy), abi.encodeCall(depositManagerProxy.migrateMatic, ())));
abi.encodeCall(Governance.update, (address(depositManagerProxy), abi.encodeCall(DepositManager.migrateMatic, ())));

console.log("Send payloadMigrateMatic9 to: ", address(governance));
console.log("Created payloadMigrateMatic9 for: ", address(governance));
console.logBytes(payloadMigrateMatic9);

console.log("----------------------");
Expand Down

0 comments on commit 753a269

Please sign in to comment.