Skip to content

Commit

Permalink
feature update (#156)
Browse files Browse the repository at this point in the history
optimize
oops
  • Loading branch information
teddy-nodeset authored Apr 19, 2024
1 parent 814a4b7 commit 21eb43e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 80 deletions.
85 changes: 39 additions & 46 deletions contracts/Operator/NodeAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.17;

import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';

import './NodeAccountFactory.sol';
import './OperatorDistributor.sol';
Expand Down Expand Up @@ -73,7 +74,8 @@ contract NodeAccount is UpgradeableBase, Errors {
address _directory,
address _nodeOperator,
address _predictedAddress,
ValidatorConfig calldata _config
ValidatorConfig calldata _config,
bytes memory _sig
) public payable initializer {
super.initialize(_directory);

Expand All @@ -84,7 +86,6 @@ contract NodeAccount is UpgradeableBase, Errors {
vaf = NodeAccountFactory(msg.sender);
configs[_config.expectedMinipoolAddress] = _config;


Directory directory = Directory(_directory);

bool isWhitelisted = Whitelist(directory.getWhitelistAddress()).getIsAddressInWhitelist(_nodeOperator);
Expand All @@ -97,30 +98,14 @@ contract NodeAccount is UpgradeableBase, Errors {
IRocketNodeManager(directory.getRocketNodeManagerAddress()).setRPLWithdrawalAddress(address(this), dp, true);
IRocketStorage(directory.getRocketStorageAddress()).setWithdrawalAddress(address(this), dp, true);

_createMinipool(
_config.bondAmount,
_config.minimumNodeFee,
_config.validatorPubkey,
_config.validatorSignature,
_config.depositDataRoot,
_config.salt,
_config.expectedMinipoolAddress
);
_createMinipool(_config, _sig);
}

function createMinipool(ValidatorConfig calldata _config) public payable {
function createMinipool(ValidatorConfig calldata _config, bytes memory _sig) public payable {
require(msg.sender == nodeOperator, 'only nodeOperator');
require(msg.value == vaf.lockThreshhold(), 'NodeAccount: must lock 1 ether');

_createMinipool(
_config.bondAmount,
_config.minimumNodeFee,
_config.validatorPubkey,
_config.validatorSignature,
_config.depositDataRoot,
_config.salt,
_config.expectedMinipoolAddress
);
_createMinipool(_config, _sig);
}

function _registerNode(string calldata _timezoneLocation, uint256 _bond, address _nodeOperator) internal {
Expand All @@ -132,44 +117,52 @@ contract NodeAccount is UpgradeableBase, Errors {
);
}

function _createMinipool(
uint256 _bondAmount,
uint256 _minimumNodeFee,
bytes calldata _validatorPubkey,
bytes calldata _validatorSignature,
bytes32 _depositDataRoot,
uint256 _salt,
address _expectedMinipoolAddress
) internal {
function _createMinipool(ValidatorConfig calldata _config, bytes memory _sig) internal {
if (vaf.preSignedExitMessageCheck()) {
console.log('_createMinipool: message hash');
console.logBytes32(
keccak256(abi.encodePacked(_config.expectedMinipoolAddress, _config.salt, address(vaf)))
);
address recoveredAddress = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(abi.encodePacked(_config.expectedMinipoolAddress, _config.salt, address(vaf)))
),
_sig
);
require(
_directory.hasRole(Constants.ADMIN_SERVER_ROLE, recoveredAddress),
'signer must have permission from admin server role'
);
}

uint256 targetBond = vaf.targetBond();
if (targetBond != _bondAmount) {
revert BadBondAmount(targetBond, _bondAmount);
if (targetBond != _config.bondAmount) {
revert BadBondAmount(targetBond, _config.bondAmount);
}
if (targetBond > address(this).balance - totalEthLocked) {
revert InsufficientBalance(targetBond, address(this).balance - totalEthLocked);
}
require(lockedEth[_expectedMinipoolAddress] == 0, 'minipool already initialized');
require(lockedEth[_config.expectedMinipoolAddress] == 0, 'minipool already initialized');

lockedEth[_expectedMinipoolAddress] = msg.value;
lockedEth[_config.expectedMinipoolAddress] = msg.value;
totalEthLocked += msg.value;
lockStarted[_expectedMinipoolAddress] = block.timestamp;
lockStarted[_config.expectedMinipoolAddress] = block.timestamp;

address _nodeOperator = nodeOperator;
OperatorDistributor od = OperatorDistributor(_directory.getOperatorDistributorAddress());
od.OnMinipoolCreated(_expectedMinipoolAddress, nodeOperator, _bondAmount);
od.rebalanceRplStake(address(this), od.nodeOperatorEthStaked(_nodeOperator));
od.OnMinipoolCreated(_config.expectedMinipoolAddress, nodeOperator, _config.bondAmount);
od.rebalanceRplStake(address(this), od.nodeOperatorEthStaked(nodeOperator));

console.log('_createMinipool()');
IRocketNodeDeposit(_directory.getRocketNodeDepositAddress()).deposit{value: targetBond}(
_bondAmount,
_minimumNodeFee,
_validatorPubkey,
_validatorSignature,
_depositDataRoot,
_salt,
_expectedMinipoolAddress
_config.bondAmount,
_config.minimumNodeFee,
_config.validatorPubkey,
_config.validatorSignature,
_config.depositDataRoot,
_config.salt,
_config.expectedMinipoolAddress
);
IMinipool minipool = IMinipool(_expectedMinipoolAddress);
IMinipool minipool = IMinipool(_config.expectedMinipoolAddress);
console.log('_createMinipool.status', uint256(minipool.getStatus()));
}

Expand Down
6 changes: 4 additions & 2 deletions contracts/Operator/NodeAccountFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ contract NodeAccountFactory is UpgradeableBase, Errors {

function createNewNodeAccount(
NodeAccount.ValidatorConfig calldata _config,
address _predictedAddress
address _predictedAddress,
bytes memory _sig
) public payable returns (address) {
require(hasSufficentLiquidity(_config.bondAmount), 'NodeAccount: protocol must have enough rpl and eth');
require(msg.value == lockThreshhold, 'NodeAccount: must lock 1 ether');
Expand All @@ -64,7 +65,8 @@ contract NodeAccountFactory is UpgradeableBase, Errors {
address(_directory),
msg.sender,
_predictedAddress,
_config
_config,
_sig
)
);

Expand Down
12 changes: 6 additions & 6 deletions gas-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
·····························|·························|·············|·············|·············|···············|··············
| NodeAccount · stake · 230278 · 247378 · 240538 · 10 · - │
·····························|·························|·············|·············|·············|···············|··············
| NodeAccountFactory · createNewNodeAccount · 3117538 · 3384556 · 3255146 · 12 · - │
| NodeAccountFactory · createNewNodeAccount · 3135136 · 3402154 · 3272736 · 12 · - │
·····························|·························|·············|·············|·············|···············|··············
| OperatorDistributor · processNextMinipool · 187327 · 768286 · 457776 · 5 · - │
| OperatorDistributor · processNextMinipool · 187416 · 768375 · 457865 · 5 · - │
·····························|·························|·············|·············|·············|···············|··············
| PriceFetcher · useFallback · - · - · 41967 · 2 · - │
·····························|·························|·············|·············|·············|···············|··············
Expand Down Expand Up @@ -71,7 +71,7 @@
·····························|·························|·············|·············|·············|···············|··············
| WETHVault · setRplCoverageRatio · - · - · 44251 · 1 · - │
·····························|·························|·············|·············|·············|···············|··············
| WhitelistV2 · addOperator · 344635 · 1030410 · 470659 · 25 · - │
| WhitelistV2 · addOperator · 344635 · 1030499 · 470666 · 25 · - │
·····························|·························|·············|·············|·············|···············|··············
| WhitelistV2 · addOperators · - · - · 629020 · 4 · - │
·····························|·························|·············|·············|·············|···············|··············
Expand All @@ -83,7 +83,7 @@
·····························|·························|·············|·············|·············|···············|··············
| YieldDistributor · finalizeInterval · 271297 · 294251 · 278948 · 3 · - │
·····························|·························|·············|·············|·············|···············|··············
| YieldDistributor · harvest · 279354 · 813760 · 695356 · 14 · - │
| YieldDistributor · harvest · 279354 · 813849 · 695433 · 14 · - │
·····························|·························|·············|·············|·············|···············|··············
| Deployments · · % of limit · │
·······················································|·············|·············|·············|···············|··············
Expand All @@ -107,9 +107,9 @@
·······················································|·············|·············|·············|···············|··············
| MockUniswapV3Pool · - · - · 131311 · 0.4 % · - │
·······················································|·············|·············|·············|···············|··············
| NodeAccount · - · - · 3756366 · 12.5 % · - │
| NodeAccount · - · - · 4093426 · 13.6 % · - │
·······················································|·············|·············|·············|···············|··············
| NodeAccountFactory · - · - · 2151507 · 7.2 % · - │
| NodeAccountFactory · - · - · 2177120 · 7.3 % · - │
·······················································|·············|·············|·············|···············|··············
| OperatorDistributor · - · - · 4023176 · 13.4 % · - │
·······················································|·············|·············|·············|···············|··············
Expand Down
2 changes: 1 addition & 1 deletion test/scenario/test-node-operator-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Node Operator Onboarding", function () {
expect(await protocol.NodeAccountFactory.hasSufficentLiquidity(bondValue)).equals(true);


const NodeAccount = await deployNodeAccount(signers.hyperdriver, protocol, signers, bondValue);
const NodeAccount = await deployNodeAccount(setupData, bondValue);

console.log("VAF:")
console.log(NodeAccount)
Expand Down
18 changes: 12 additions & 6 deletions test/test-validator-account-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { OperatorStruct } from "../typechain-types/contracts/Whitelist/Whitelist";
import { protocolFixture } from "./test";
import { BigNumber } from "ethers";
import { assertAddOperator, countProxyCreatedEvents, getNextContractAddress, getNextFactoryContractAddress, predictDeploymentAddress, prepareOperatorDistributionContract } from "./utils/utils";
import { approveHasSignedExitMessageSig, assertAddOperator, countProxyCreatedEvents, getNextContractAddress, getNextFactoryContractAddress, predictDeploymentAddress, prepareOperatorDistributionContract } from "./utils/utils";
import { generateDepositData } from "./rocketpool/_helpers/minipool";

describe("Validator Account Factory", function () {
Expand Down Expand Up @@ -38,7 +38,9 @@ describe("Validator Account Factory", function () {
expectedMinipoolAddress: depositData.minipoolAddress
}

await protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(config, nextAddress, {
const sig = await approveHasSignedExitMessageSig(setupData, '0x'+config.expectedMinipoolAddress, config.salt)

await protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(config, nextAddress, sig, {
value: ethers.utils.parseEther("1")
})

Expand Down Expand Up @@ -72,8 +74,9 @@ describe("Validator Account Factory", function () {
salt: salt,
expectedMinipoolAddress: depositData.minipoolAddress
}
const sig = await approveHasSignedExitMessageSig(setupData, '0x'+config.expectedMinipoolAddress, config.salt)

await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(config, nextAddress, {
await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(config, nextAddress,sig, {
value: ethers.utils.parseEther("1")
})).to.be.revertedWith("Whitelist: Provided address is not an allowed operator!")
});
Expand All @@ -94,8 +97,9 @@ describe("Validator Account Factory", function () {
salt: 0,
expectedMinipoolAddress: "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97"
}
const sig = await approveHasSignedExitMessageSig(setupData, badConfig.expectedMinipoolAddress, badConfig.salt)

await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress, {
await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress,sig, {
value: ethers.utils.parseEther("1")
})).to.be.revertedWithCustomError(protocol.NodeAccountFactory, "BadPredictedCreation");
})
Expand All @@ -116,8 +120,9 @@ describe("Validator Account Factory", function () {
salt: 0,
expectedMinipoolAddress: "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97"
}
const sig = await approveHasSignedExitMessageSig(setupData, badConfig.expectedMinipoolAddress, badConfig.salt)

await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress, {
await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress,sig,{
value: ethers.utils.parseEther("0")
})).to.be.revertedWith("NodeAccount: must lock 1 ether");
});
Expand All @@ -138,8 +143,9 @@ describe("Validator Account Factory", function () {
salt: 0,
expectedMinipoolAddress: "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97"
}
const sig = await approveHasSignedExitMessageSig(setupData, badConfig.expectedMinipoolAddress, badConfig.salt)

await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress, {
await expect(protocol.NodeAccountFactory.connect(signers.hyperdriver).createNewNodeAccount(badConfig, nextBadAddress, sig,{
value: ethers.utils.parseEther("1")
})).to.be.revertedWith("NodeAccount: protocol must have enough rpl and eth");
});
Expand Down
Loading

0 comments on commit 21eb43e

Please sign in to comment.