Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coral getter, readme update #97

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@ EnclaveVerifierTest:testRaveEvidence2() (gas: 1123695)
EnclaveVerifierTest:testRaveEvidence3() (gas: 1123760)
EnclaveVerifierTest:testRemoveLeafX509() (gas: 336920)
EnclaveVerifierTest:testSetup() (gas: 115053)
GuardianModuleTest:testRave() (gas: 35926457)
GuardianModuleTest:testRoateGuardianKeyWithInvalidRaveReverts() (gas: 855653)
GuardianModuleTest:testRoateGuardianToInvalidPubKeyReverts() (gas: 22830)
GuardianModuleTest:testRotateGuardianKeyFromNonGuardianReverts() (gas: 20245)
GuardianModuleTest:testRave() (gas: 37350798)
GuardianModuleTest:testRoateGuardianKeyWithInvalidRaveReverts() (gas: 855915)
GuardianModuleTest:testRoateGuardianToInvalidPubKeyReverts() (gas: 22865)
GuardianModuleTest:testRotateGuardianKeyFromNonGuardianReverts() (gas: 20280)
PufferPoolIntegrationTest:testMulticallStrategyDepositOnGoerli() (gas: 302650)
PufferPoolTest:testBurn(address) (runs: 256, μ: 66339, ~: 66324)
PufferPoolTest:testDeposit(address,uint256) (runs: 256, μ: 97060, ~: 97178)
PufferPoolTest:testBurn(address) (runs: 256, μ: 66320, ~: 66306)
PufferPoolTest:testDeposit(address,uint256) (runs: 256, μ: 97002, ~: 97112)
PufferPoolTest:testDepositRevertsForTooSmallAmount() (gas: 15040)
PufferPoolTest:testMultipleDeposits() (gas: 123090)
PufferPoolTest:testRatioChange() (gas: 188957)
PufferPoolTest:testRatioChangeSandwichAttack(uint256,uint256) (runs: 256, μ: 181213, ~: 181399)
PufferPoolTest:testSetup() (gas: 32425)
PufferPoolTest:testStorageS() (gas: 19447)
PufferProtocolTest:testCreateExistingStrategyShouldFail() (gas: 8797746687696156720)
PufferProtocolTest:testCreatePufferStrategy() (gas: 259915)
PufferProtocolTest:testEmptyQueue() (gas: 32870)
PufferProtocolTest:testExtendCommitment() (gas: 459017)
PufferProtocolTest:testProofOfReserve() (gas: 150836)
PufferProtocolTest:testProvisionNode() (gas: 3639823)
PufferProtocolTest:testRegisterInvalidPrivKeyShares() (gas: 78626)
PufferProtocolTest:testRegisterInvalidPubKeyShares() (gas: 78822)
PufferProtocolTest:testRegisterMoreValidatorsThanTheLimit() (gas: 727128)
PufferProtocolTest:testRegisterMultipleValidatorKeysAndDequeue(bytes32,bytes32) (runs: 256, μ: 1922838, ~: 1922838)
PufferProtocolTest:testRegisterToInvalidStrategy() (gas: 56273)
PufferProtocolTest:testRegisterWithInvalidAmountPaid() (gas: 81529)
PufferProtocolTest:testRegisterWithoutRAVE() (gas: 33542)
PufferProtocolTest:testSetProtocolFeeRate() (gas: 33278)
PufferProtocolTest:testSetSmoothingCommitment() (gas: 35089)
PufferProtocolTest:testSetSmoothingCommitment(bytes32) (runs: 256, μ: 52260, ~: 52260)
PufferProtocolTest:testSetup() (gas: 12818)
PufferProtocolTest:testSkipProvisioning() (gas: 873245)
PufferProtocolTest:testStopRegistration() (gas: 998787)
PufferProtocolTest:testUpgrade() (gas: 3881019)
PufferStrategyTest:testBeaconUpgrade() (gas: 103226)
PufferStrategyTest:testGetEigenPod() (gas: 25959)
WithdrawalPoolTest:testWithdrawETH() (gas: 160220)
WithdrawalPoolTest:testWithdrawETHWithSignature() (gas: 172454)
PufferPoolTest:testMultipleDeposits() (gas: 123046)
PufferPoolTest:testRatioChange() (gas: 189082)
PufferPoolTest:testRatioChangeSandwichAttack(uint256,uint256) (runs: 256, μ: 181158, ~: 181333)
PufferPoolTest:testSetup() (gas: 32449)
PufferPoolTest:testStorageS() (gas: 19425)
PufferProtocolTest:testCreateExistingStrategyShouldFail() (gas: 8797746687696156718)
PufferProtocolTest:testCreatePufferStrategy() (gas: 259996)
PufferProtocolTest:testEmptyQueue() (gas: 32760)
PufferProtocolTest:testExtendCommitment() (gas: 459142)
PufferProtocolTest:testGetPayload() (gas: 175380)
PufferProtocolTest:testProofOfReserve() (gas: 151365)
PufferProtocolTest:testProvisionNode() (gas: 3640934)
PufferProtocolTest:testRegisterInvalidPrivKeyShares() (gas: 78739)
PufferProtocolTest:testRegisterInvalidPubKeyShares() (gas: 78935)
PufferProtocolTest:testRegisterMoreValidatorsThanTheLimit() (gas: 727428)
PufferProtocolTest:testRegisterMultipleValidatorKeysAndDequeue(bytes32,bytes32) (runs: 256, μ: 1923812, ~: 1923812)
PufferProtocolTest:testRegisterToInvalidStrategy() (gas: 56354)
PufferProtocolTest:testRegisterWithInvalidAmountPaid() (gas: 81553)
PufferProtocolTest:testRegisterWithInvalidBLSPubKey() (gas: 30727)
PufferProtocolTest:testRegisterWithoutRAVE() (gas: 33532)
PufferProtocolTest:testSetProtocolFeeRate() (gas: 33270)
PufferProtocolTest:testSetSmoothingCommitment() (gas: 35104)
PufferProtocolTest:testSetSmoothingCommitment(bytes32) (runs: 256, μ: 52297, ~: 52297)
PufferProtocolTest:testSetup() (gas: 12817)
PufferProtocolTest:testSkipProvisioning() (gas: 873488)
PufferProtocolTest:testStopRegistration() (gas: 999332)
PufferProtocolTest:testUpgrade() (gas: 4023774)
PufferStrategyTest:testBeaconUpgrade() (gas: 106006)
PufferStrategyTest:testGetEigenPod() (gas: 25937)
WithdrawalPoolTest:testWithdrawETH() (gas: 160176)
WithdrawalPoolTest:testWithdrawETHWithSignature() (gas: 172410)
62 changes: 24 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
# <h1 align="center"> Forge Template </h1>

**Template repository for getting started quickly with Foundry projects**

![Github Actions](https://github.com/foundry-rs/forge-template/workflows/CI/badge.svg)

## Getting Started

Click "Use this template" on [GitHub](https://github.com/foundry-rs/forge-template) to create a new repository with this repo as the initial state.

Or, if your repo already exists, run:
```sh
forge init
forge build
forge test
# <h1 align="center"> Puffer Protocol </h1>
[![Github Actions][gha-badge]][gha] [![Website][Website-badge]][Website] [![Docs][docs-badge]][docs]
[![Discord][discord-badge]][discord] [![X][X-badge]][X] [![Foundry][foundry-badge]][foundry]

[Website-badge]: https://img.shields.io/badge/WEBSITE-8A2BE2
[Website]: https://www.puffer.fi
[X-badge]: https://img.shields.io/twitter/follow/puffer_finance
[X]: https://twitter.com/puffer_finance
[discord]: https://discord.gg/pufferfi
[docs-badge]: https://img.shields.io/badge/DOCS-8A2BE2
[docs]: https://docs.puffer.fi/
[discord-badge]: https://dcbadge.vercel.app/api/server/pufferfi?style=flat
[gha]: https://github.com/PufferFinance/PufferPool/actions
[gha-badge]: https://github.com/PufferFinance/PufferPool/actions/workflows/ci.yml/badge.svg
[foundry]: https://getfoundry.sh
[foundry-badge]: https://img.shields.io/badge/Built%20with-Foundry-FFDB1C.svg

![PUFFERS](image.png)

# Tests

Installing dependencies and running tests can be executed running:
```

## Writing your first test

All you need is to `import forge-std/Test.sol` and then inherit it from your test contract. Forge-std's Test contract comes with a pre-instatiated [cheatcodes environment](https://book.getfoundry.sh/cheatcodes/), the `vm`. It also has support for [ds-test](https://book.getfoundry.sh/reference/ds-test.html)-style logs and assertions. Finally, it supports Hardhat's [console.log](https://github.com/brockelmore/forge-std/blob/master/src/console.sol). The logging functionalities require `-vvvv`.

```solidity
pragma solidity 0.8.10;

import "forge-std/Test.sol";

contract ContractTest is Test {
function testExample() public {
vm.roll(100);
console.log(1);
emit log("hi");
assertTrue(true);
}
}
```

## Development

This project uses [Foundry](https://getfoundry.sh). See the [book](https://book.getfoundry.sh/getting-started/installation.html) for instructions on how to install and use Foundry.
forge test -vvv --match-path './test/unit/*'
```
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion script/1_DeployGuardians.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Safe } from "safe-contracts/Safe.sol";
import { SafeProxy } from "safe-contracts/proxies/SafeProxy.sol";
import { SafeProxyFactory } from "safe-contracts/proxies/SafeProxyFactory.sol";
import { console } from "forge-std/console.sol";
import { AccessManager } from "openzeppelin/access/manager/AccessManager.sol";
import { Strings } from "openzeppelin/utils/Strings.sol";

// forge script script/1_DeployGuardians.s.sol:DeployGuardians --rpc-url=$EPHEMERY_RPC_URL --sig 'run(address[] calldata, uint256)' "[0x5F9a7EA6A79Ef04F103bfe7BD45dA65476a5155C]" 1
Expand All @@ -24,14 +25,17 @@ contract DeployGuardians is BaseScript {

EnclaveVerifier verifier = new EnclaveVerifier(100);

AccessManager accessManager = new AccessManager(_broadcaster);

Safe guardiansSafe = this.deploySafe(guardians, threshold, address(0), "");

GuardianModule module = new GuardianModule(verifier, guardiansSafe);
GuardianModule module = new GuardianModule(verifier, guardiansSafe, address(accessManager));

// console.log(address(guardiansSafe), "<-- Guardians multisig deployed");

string memory obj = "";
vm.serializeAddress(obj, "guardians", address(guardiansSafe));
vm.serializeAddress(obj, "accessManager", address(accessManager));
vm.serializeAddress(obj, "guardianModule", address(module));
vm.serializeAddress(obj, "safeProxyFactory", address(safeProxy));
vm.serializeAddress(obj, "safeImplementation", address(safeImplementation));
Expand Down
2 changes: 1 addition & 1 deletion script/DeployPuffer.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract DeployPuffer is BaseScript {

PufferProtocol pufferProtocolImpl;

AccessManager accessManager = new AccessManager(_broadcaster);
AccessManager accessManager = AccessManager(stdJson.readAddress(guardiansDeployment, ".accessManager"));

{
// PufferTreasury
Expand Down
89 changes: 71 additions & 18 deletions src/GuardianModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
pragma solidity >=0.8.0 <0.9.0;

import { Safe } from "safe-contracts/Safe.sol";
import { AccessManaged } from "openzeppelin/access/manager/AccessManaged.sol";
import { IGuardianModule } from "puffer/interface/IGuardianModule.sol";
import { PufferProtocol } from "puffer/PufferProtocol.sol";
import { IEnclaveVerifier } from "puffer/EnclaveVerifier.sol";
import { RaveEvidence } from "puffer/struct/RaveEvidence.sol";
import { ECDSA } from "openzeppelin/utils/cryptography/ECDSA.sol";
import { MessageHashUtils } from "openzeppelin/utils/cryptography/MessageHashUtils.sol";
import { Ownable } from "openzeppelin/access/Ownable.sol";

/**
* @title Guardian module
* @author Puffer finance
* @dev This contract is both {Safe} module, and a logic contract to be called via `delegatecall` from {Safe} (GuardianAccount)
* @custom:security-contact [email protected]
*/
contract GuardianModule is Ownable, IGuardianModule {
contract GuardianModule is AccessManaged, IGuardianModule {
using ECDSA for bytes32;
using MessageHashUtils for bytes32;

Expand All @@ -25,24 +25,44 @@ contract GuardianModule is Ownable, IGuardianModule {
*/
uint256 internal constant _ECDSA_KEY_LENGTH = 65;

/**
* @notice Enclave Verifier smart contract
*/
IEnclaveVerifier public immutable enclaveVerifier;

/**
* @notice Guardians {Safe}
*/
Safe public immutable GUARDIANS;

/**
* @dev MRSIGNER value for SGX
*/
bytes32 mrsigner;
bytes32 mrenclave;

/**
* @dev Mapping from guardian address => enclave address
* @dev MRENCLAVE value for SGX
*/
mapping(address => address) internal _guardianEnclaves;
bytes32 mrenclave;

constructor(IEnclaveVerifier verifier, Safe guardians) Ownable(msg.sender) {
struct GuardianData {
bytes enclavePubKey;
address enclaveAddress;
}

mapping(address guardian => GuardianData data) internal _guardianEnclaves;

constructor(IEnclaveVerifier verifier, Safe guardians, address pufferAuthority) AccessManaged(pufferAuthority) {
require(address(verifier) != address(0));
require(address(guardians) != address(0));
require(address(pufferAuthority) != address(0));
enclaveVerifier = verifier;
GUARDIANS = guardians;
}

function setGuardianEnclaveMeasurements(bytes32 newMrenclave, bytes32 newMrsigner) public {
//@audit don't forget owner modifier
/**
* @inheritdoc IGuardianModule
*/
function setGuardianEnclaveMeasurements(bytes32 newMrenclave, bytes32 newMrsigner) external restricted {
bytes32 previousMrEnclave = mrenclave;
bytes32 previousMrsigner = mrsigner;
mrenclave = newMrenclave;
Expand All @@ -51,8 +71,11 @@ contract GuardianModule is Ownable, IGuardianModule {
emit MrSignerChanged(previousMrsigner, newMrsigner);
}

/**
* @inheritdoc IGuardianModule
*/
function validateGuardianSignatures(
bytes memory pubKey,
bytes calldata pubKey,
bytes calldata signature,
bytes32 depositDataRoot,
bytes calldata withdrawalCredentials,
Expand All @@ -63,13 +86,13 @@ contract GuardianModule is Ownable, IGuardianModule {
bytes32 msgToBeSigned = getMessageToBeSigned(pubKey, signature, withdrawalCredentials, depositDataRoot);

address[] memory enclaveAddresses = getGuardiansEnclaveAddresses();
uint256 validSignatures = 0;
uint256 validSignatures;

// Iterate through guardian enclave addresses and make sure that the signers match
for (uint256 i = 0; i < enclaveAddresses.length;) {
for (uint256 i; i < enclaveAddresses.length;) {
address currentSigner = ECDSA.recover(msgToBeSigned, guardianEnclaveSignatures[i]);
if (currentSigner == enclaveAddresses[i]) {
validSignatures++;
++validSignatures;
}
unchecked {
++i;
Expand All @@ -81,6 +104,9 @@ contract GuardianModule is Ownable, IGuardianModule {
}
}

/**
* @inheritdoc IGuardianModule
*/
function getMessageToBeSigned(
bytes memory pubKey,
bytes calldata signature,
Expand All @@ -90,13 +116,16 @@ contract GuardianModule is Ownable, IGuardianModule {
return keccak256(abi.encode(pubKey, withdrawalCredentials, signature, depositDataRoot)).toEthSignedMessageHash();
}

/**
* @inheritdoc IGuardianModule
*/
function rotateGuardianKey(uint256 blockNumber, bytes calldata pubKey, RaveEvidence calldata evidence) external {
address guardian = msg.sender;

// Because this is called from the safe via .delegateCall
// address(this) equals to {Safe}
// This will revert if the caller is not one of the {Safe} owners
if (!GUARDIANS.isOwner(msg.sender)) {
if (!GUARDIANS.isOwner(guardian)) {
revert Unauthorized();
}

Expand All @@ -120,27 +149,51 @@ contract GuardianModule is Ownable, IGuardianModule {
// pubKey[1:] means we need to strip the first byte '0x' if we want to get the correct address
address computedAddress = address(uint160(uint256(keccak256(pubKey[1:]))));

_guardianEnclaves[guardian] = computedAddress;
_guardianEnclaves[guardian].enclaveAddress = computedAddress;
_guardianEnclaves[guardian].enclavePubKey = pubKey;

emit RotatedGuardianKey(guardian, computedAddress, pubKey);
}

/**
* @inheritdoc IGuardianModule
*/
function isGuardiansEnclaveAddress(address guardian, address enclave) external view returns (bool) {
// Assert if the stored enclaveAddress equals enclave
return _guardianEnclaves[guardian] == enclave;
return _guardianEnclaves[guardian].enclaveAddress == enclave;
}

/**
* @inheritdoc IGuardianModule
*/
function getGuardiansEnclaveAddresses() public view returns (address[] memory) {
address[] memory guardians = GUARDIANS.getOwners();
address[] memory enclaveAddresses = new address[](guardians.length);

for (uint256 i = 0; i < guardians.length;) {
enclaveAddresses[i] = _guardianEnclaves[guardians[i]];
for (uint256 i; i < guardians.length;) {
enclaveAddresses[i] = _guardianEnclaves[guardians[i]].enclaveAddress;
unchecked {
++i;
}
}

return enclaveAddresses;
}

/**
* @inheritdoc IGuardianModule
*/
function getGuardiansEnclavePubkeys() public view returns (bytes[] memory) {
address[] memory guardians = GUARDIANS.getOwners();
bytes[] memory enclavePubkeys = new bytes[](guardians.length);

for (uint256 i; i < guardians.length;) {
enclavePubkeys[i] = _guardianEnclaves[guardians[i]].enclavePubKey;
unchecked {
++i;
}
}

return enclavePubkeys;
}
}
12 changes: 11 additions & 1 deletion src/PufferProtocol.sol
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ contract PufferProtocol is IPufferProtocol, AccessManagedUpgradeable, UUPSUpgrad
}

if (block.number - $.lastUpdate < _UPDATE_INTERVAL) {
revert InvalidData();
revert OutsideUpdateWindow();
}
$.ethAmount = ethAmount;
$.lockedETH = lockedETH;
Expand Down Expand Up @@ -453,6 +453,16 @@ contract PufferProtocol is IPufferProtocol, AccessManagedUpgradeable, UUPSUpgrad
return abi.encodePacked(bytes1(uint8(1)), bytes11(0), IPufferStrategy(strategy).getEigenPod());
}

function getPayload(bytes32 strategyName) external view returns (bytes[] memory, bytes memory, uint256) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonVranek How should we name this?

ProtocolStorage storage $ = _getPufferProtocolStorage();

bytes[] memory pubKeys = $.guardianModule.getGuardiansEnclavePubkeys();
bytes memory withdrawalCredentials = getWithdrawalCredentials(address($.strategies[strategyName]));
uint256 threshold = GUARDIANS.getThreshold();

return (pubKeys, withdrawalCredentials, threshold);
}

function _setSmoothingCommitment(bytes32 strategyName, uint256 smoothingCommitment) internal {
ProtocolStorage storage $ = _getPufferProtocolStorage();
uint256 oldSmoothingCommitment = $.smoothingCommitments[strategyName];
Expand Down
Loading