Skip to content

Commit

Permalink
Merge pull request #227 from chainbound/lore/test/registry-el-integra…
Browse files Browse the repository at this point in the history
…tion

test(contracts): more on EL integration
  • Loading branch information
merklefruit authored Sep 13, 2024
2 parents 15df47a + ac3a615 commit 9a8bfeb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bolt-contracts/src/contracts/BoltManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ contract BoltManager is IBoltManager, Ownable {

uint48 epochStartTs = getEpochStartTs(getEpochAtTs(Time.timestamp()));
IBoltValidators.Validator memory validator = validators.getValidatorByPubkeyHash(pubkeyHash);

address operator = validator.authorizedOperator;

status.pubkeyHash = pubkeyHash;
Expand Down Expand Up @@ -624,6 +625,7 @@ contract BoltManager is IBoltManager, Ownable {

uint48 epochStartTs = getEpochStartTs(getEpochAtTs(Time.timestamp()));
IBoltValidators.Validator memory validator = validators.getValidatorByPubkeyHash(pubkeyHash);

address operator = validator.authorizedOperator;

status.pubkeyHash = pubkeyHash;
Expand Down
96 changes: 90 additions & 6 deletions bolt-contracts/test/BoltManager.EigenLayer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {Test, console} from "forge-std/Test.sol";

import {BoltValidators} from "../src/contracts/BoltValidators.sol";
import {BoltManager} from "../src/contracts/BoltManager.sol";
import {IBoltValidators} from "../src/interfaces/IBoltValidators.sol";
import {IBoltManager} from "../src/interfaces/IBoltManager.sol";

import {AVSDirectoryStorage} from "@eigenlayer/src/contracts/core/AVSDirectoryStorage.sol";
import {DelegationManagerStorage} from "@eigenlayer/src/contracts/core/DelegationManagerStorage.sol";
Expand All @@ -17,12 +19,17 @@ import {EigenLayerDeployer} from "../test/fixtures/EigenLayerDeployer.f.sol";
import {BLS12381} from "../src/lib/bls/BLS12381.sol";

contract BoltManagerEigenLayerTest is Test {
using BLS12381 for BLS12381.G1Point;

uint48 public constant EPOCH_DURATION = 1 days;

BoltValidators public validators;
BoltManager public manager;
EigenLayerDeployer public eigenLayerDeployer;

address staker = makeAddr("staker");
address validator = makeAddr("validator");
BLS12381.G1Point validatorPubkey = BLS12381.generatorG1();
address operator;
uint256 operatorSk;

Expand Down Expand Up @@ -52,11 +59,17 @@ contract BoltManagerEigenLayerTest is Test {
);
}

function _eigenLayerOptInRoutine() internal {
function _adminRoutine() internal {
// PART 0: Admin setup -- Collateral whitelist
vm.startPrank(admin);
manager.addWhitelistedEigenLayerCollateral(address(eigenLayerDeployer.weth()));
vm.stopPrank();
assertEq(manager.getWhitelistedEigenLayerCollaterals().length, 1);
assertEq(manager.getWhitelistedEigenLayerCollaterals()[0], address(eigenLayerDeployer.weth()));
}

function _eigenLayerOptInRoutine() internal {
_adminRoutine();

// PART 1: External EigenLayer opt-in to BOLT AVS

Expand Down Expand Up @@ -137,13 +150,13 @@ contract BoltManagerEigenLayerTest is Test {
// 1. --- Register Validator in BoltValidators ---

// pubkeys aren't checked, any point will be fine
BLS12381.G1Point memory pubkey = BLS12381.generatorG1();
validatorPubkey = BLS12381.generatorG1();

vm.prank(validator);
validators.registerValidatorUnsafe(pubkey, staker, operator);
assertEq(validators.getValidatorByPubkey(pubkey).exists, true);
assertEq(validators.getValidatorByPubkey(pubkey).authorizedOperator, operator);
assertEq(validators.getValidatorByPubkey(pubkey).authorizedCollateralProvider, staker);
validators.registerValidatorUnsafe(validatorPubkey, staker, operator);
assertEq(validators.getValidatorByPubkey(validatorPubkey).exists, true);
assertEq(validators.getValidatorByPubkey(validatorPubkey).authorizedOperator, operator);
assertEq(validators.getValidatorByPubkey(validatorPubkey).authorizedCollateralProvider, staker);

// 2. --- Operator and strategy registration into BoltManager (middleware) ---

Expand All @@ -169,4 +182,75 @@ contract BoltManagerEigenLayerTest is Test {
assertEq(amount, 1 ether);
assertEq(totalStake, 1 ether);
}

function test_getEigenLayerProposerStatus() public {
_eigenLayerOptInRoutine();

bytes32 pubkeyHash = _pubkeyHash(validatorPubkey);

BoltManager.ProposerStatus memory status = manager.getEigenLayerProposerStatus(pubkeyHash);
assertEq(status.pubkeyHash, pubkeyHash);
assertEq(status.operator, operator);
assertEq(status.active, true);
assertEq(status.collaterals.length, 1);
assertEq(status.amounts.length, 1);
assertEq(status.collaterals[0], address(eigenLayerDeployer.weth()));
assertEq(status.amounts[0], 1 ether);
}

function testProposersLookaheadStatus() public {
// This also opts in the operator which is needed
_eigenLayerOptInRoutine();
bytes32[] memory pubkeyHashes = new bytes32[](10);

// register 10 proposers with random pubkeys
for (uint256 i = 0; i < 10; i++) {
BLS12381.G1Point memory pubkey = BLS12381.generatorG1();
pubkey.x[0] = pubkey.x[0] + i + 2;
pubkey.y[0] = pubkey.y[0] + i + 2;

pubkeyHashes[i] = _pubkeyHash(pubkey);
validators.registerValidatorUnsafe(pubkey, staker, operator);
}

BoltManager.ProposerStatus[] memory statuses = manager.getEigenLayerProposersStatus(pubkeyHashes);
assertEq(statuses.length, 10);
}

function testGetNonExistentProposerStatus() public {
_eigenLayerOptInRoutine();

bytes32 pubkeyHash = bytes32(uint256(1));

vm.expectRevert(IBoltValidators.ValidatorDoesNotExist.selector);
manager.getEigenLayerProposerStatus(pubkeyHash);
}

function testGetWhitelistedCollaterals() public {
_adminRoutine();
address[] memory collaterals = manager.getWhitelistedEigenLayerCollaterals();
assertEq(collaterals.length, 1);
assertEq(collaterals[0], address(eigenLayerDeployer.weth()));
}

function testNonWhitelistedCollateral() public {
_adminRoutine();
vm.startPrank(admin);
manager.removeWhitelistedEigenLayerCollateral(address(eigenLayerDeployer.weth()));
vm.stopPrank();

address strat = address(eigenLayerDeployer.wethStrat());
vm.startPrank(admin);
vm.expectRevert(IBoltManager.CollateralNotWhitelisted.selector);
manager.registerEigenLayerStrategy(strat);
vm.stopPrank();
}

/// @notice Compute the hash of a BLS public key
function _pubkeyHash(
BLS12381.G1Point memory _pubkey
) internal pure returns (bytes32) {
uint256[2] memory compressedPubKey = _pubkey.compress();
return keccak256(abi.encodePacked(compressedPubKey));
}
}
File renamed without changes.

0 comments on commit 9a8bfeb

Please sign in to comment.