Skip to content

Commit

Permalink
Remove duplicate code from the test contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas committed Dec 4, 2024
1 parent 8a07d80 commit f0b4d20
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 394 deletions.
12 changes: 12 additions & 0 deletions src/BaseDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ abstract contract BaseDelegation is Delegation, PausableUpgradeable, Ownable2Ste
require(success, "deposit failed");
}

function deposit(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public virtual payable;

function deposit2(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public virtual;

function _increaseDeposit(uint256 amount) internal virtual {
// topup the deposit only if already activated as a validator
if (_isActivated()) {
Expand Down
16 changes: 16 additions & 0 deletions src/LiquidDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ contract LiquidDelegation is BaseDelegation, ILiquidDelegation {
return $.lst;
}

function deposit(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public override payable {
revert("not implemented");
}

function deposit2(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public override {
revert("not implemented");
}

function stake() external override payable {
revert("not implemented");
}
Expand Down
4 changes: 2 additions & 2 deletions src/LiquidDelegationV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ contract LiquidDelegationV2 is BaseDelegation, ILiquidDelegation {
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public onlyOwner {
) public override onlyOwner {
_deposit(
blsPubKey,
peerId,
Expand All @@ -67,7 +67,7 @@ contract LiquidDelegationV2 is BaseDelegation, ILiquidDelegation {
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public payable onlyOwner {
) public override payable onlyOwner {
_deposit(
blsPubKey,
peerId,
Expand Down
16 changes: 16 additions & 0 deletions src/NonLiquidDelegation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ contract NonLiquidDelegation is BaseDelegation, INonLiquidDelegation {
__BaseDelegation_init(initialOwner);
}

function deposit(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public override payable {
revert("not implemented");
}

function deposit2(
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public override {
revert("not implemented");
}

function stake() external payable override {
revert("not implemented");
}
Expand Down
4 changes: 2 additions & 2 deletions src/NonLiquidDelegationV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract NonLiquidDelegationV2 is BaseDelegation, INonLiquidDelegation {
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public onlyOwner {
) public override onlyOwner {
_deposit(
blsPubKey,
peerId,
Expand All @@ -136,7 +136,7 @@ contract NonLiquidDelegationV2 is BaseDelegation, INonLiquidDelegation {
bytes calldata blsPubKey,
bytes calldata peerId,
bytes calldata signature
) public payable onlyOwner {
) public payable override onlyOwner {
_deposit(
blsPubKey,
peerId,
Expand Down
174 changes: 174 additions & 0 deletions test/BaseDelegation.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.26;

import {BaseDelegation} from "src/BaseDelegation.sol";
import {Delegation} from "src/Delegation.sol";
import {Deposit, InitialStaker} from "@zilliqa/zq2/deposit.sol";
import {Console} from "src/Console.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {Test, Vm} from "forge-std/Test.sol";
import "forge-std/console.sol";

contract PopVerifyPrecompile {
function popVerify(bytes memory, bytes memory) public pure returns(bool) {
return true;
}
}

abstract contract BaseDelegationTest is Test {
address payable proxy;
address oldImplementation;
bytes initializerCall;
address payable newImplementation;
bytes reinitializerCall;
address owner;
address[4] stakers = [
0xd819fFcE7A58b1E835c25617Db7b46a00888B013,
0x092E5E57955437876dA9Df998C96e2BE19341670,
0xeA78aAE5Be606D2D152F00760662ac321aB8F017,
0x6603A37980DF7ef6D44E994B3183A15D0322B7bF
];

constructor() {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
owner = vm.addr(deployerPrivateKey);
//console.log("Signer is %s", owner);
}

function storeDelegation() internal virtual;

function setUp() public {
vm.chainId(33469);
vm.deal(owner, 100_000 ether);
vm.startPrank(owner);

proxy = payable(
new ERC1967Proxy(oldImplementation, initializerCall)
);
/*
console.log(
"Proxy deployed: %s \r\n Implementation deployed: %s",
proxy,
oldImplementation
);
//*/

BaseDelegation oldDelegation = BaseDelegation(
proxy
);
/*
console.log("Deployed version: %s",
oldDelegation.version()
);
console.log("Owner is %s",
oldDelegation.owner()
);
//*/

/*
console.log("New implementation deployed: %s",
newImplementation
);
//*/

oldDelegation.upgradeToAndCall(
newImplementation,
reinitializerCall
);

storeDelegation();
BaseDelegation delegation = BaseDelegation(
proxy
);

/*
console.log("Upgraded to version: %s",
delegation.version()
);
//*/
/*
Console.log("Old commission rate: %s.%s%s%%",
delegation.getCommissionNumerator(),
2
);
//*/
uint256 commissionNumerator = 1_000;
delegation.setCommissionNumerator(commissionNumerator);
/*
Console.log("New commission rate: %s.%s%s%%",
delegation.getCommissionNumerator(),
2
);
//*/

InitialStaker[] memory initialStakers = new InitialStaker[](0);
//vm.deployCodeTo("Deposit.sol", delegation.DEPOSIT_CONTRACT());
vm.etch(
delegation.DEPOSIT_CONTRACT(), //0x000000000000000000005a494C4445504F534954,
address(new Deposit(10_000_000 ether, 256, 10, initialStakers)).code
);
vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(11)), bytes32(uint256(block.number / 10)));
vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(12)), bytes32(uint256(10_000_000 ether)));
vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(13)), bytes32(uint256(256)));
vm.store(delegation.DEPOSIT_CONTRACT(), bytes32(uint256(14)), bytes32(uint256(10)));
/*
console.log("Deposit.minimimStake() =", Deposit(delegation.DEPOSIT_CONTRACT()).minimumStake());
console.log("Deposit.maximumStakers() =", Deposit(delegation.DEPOSIT_CONTRACT()).maximumStakers());
console.log("Deposit.blocksPerEpoch() =", Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch());
//*/

vm.etch(address(0x5a494c80), address(new PopVerifyPrecompile()).code);

vm.stopPrank();
}

function deposit(
BaseDelegation delegation,
uint256 depositAmount,
bool initialDeposit
) internal {
if (initialDeposit) {
vm.deal(owner, owner.balance + depositAmount);
vm.startPrank(owner);

delegation.deposit{
value: depositAmount
}(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"),
bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"),
bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a")
);
} else {
vm.deal(stakers[0], stakers[0].balance + depositAmount);
vm.startPrank(stakers[0]);

vm.expectEmit(
true,
false,
false,
false,
address(delegation)
);
emit Delegation.Staked(
stakers[0],
depositAmount,
""
);

delegation.stake{
value: depositAmount
}();

vm.startPrank(owner);

delegation.deposit2(
bytes(hex"92fbe50544dce63cfdcc88301d7412f0edea024c91ae5d6a04c7cd3819edfc1b9d75d9121080af12e00f054d221f876c"),
bytes(hex"002408011220d5ed74b09dcbe84d3b32a56c01ab721cf82809848b6604535212a219d35c412f"),
bytes(hex"b14832a866a49ddf8a3104f8ee379d29c136f29aeb8fccec9d7fb17180b99e8ed29bee2ada5ce390cb704bc6fd7f5ce814f914498376c4b8bc14841a57ae22279769ec8614e2673ba7f36edc5a4bf5733aa9d70af626279ee2b2cde939b4bd8a")
);
}
// wait 2 epochs for the change to the deposit to take affect
vm.roll(block.number + Deposit(delegation.DEPOSIT_CONTRACT()).blocksPerEpoch() * 2);
}
}
Loading

0 comments on commit f0b4d20

Please sign in to comment.