Skip to content

Commit

Permalink
refactor: storage contracts for core
Browse files Browse the repository at this point in the history
  • Loading branch information
shaspitz committed Aug 14, 2024
1 parent 493b8c9 commit bf9bfd7
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 206 deletions.
78 changes: 2 additions & 76 deletions contracts/contracts/core/BidderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/acces
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import {IBidderRegistry} from "../interfaces/IBidderRegistry.sol";
import {BidderRegistryStorage} from "./BidderRegistryStorage.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";
import {WindowFromBlockNumber} from "../utils/WindowFromBlockNumber.sol";
import {FeePayout} from "../utils/FeePayout.sol";
Expand All @@ -14,86 +15,11 @@ import {FeePayout} from "../utils/FeePayout.sol";
/// @notice This contract is for bidder registry and staking.
contract BidderRegistry is
IBidderRegistry,
BidderRegistryStorage,
Ownable2StepUpgradeable,
ReentrancyGuardUpgradeable,
UUPSUpgradeable
{
using FeePayout for FeePayout.Tracker;

/// @dev For improved precision
uint256 constant public PRECISION = 10 ** 25;
uint256 constant public PERCENT = 100 * PRECISION;

/// @dev Address of the pre-confirmations contract
address public preConfirmationsContract;

/// @dev Fee percent that would be taken by protocol when provider is slashed
uint16 public feePercent;

/// @dev Block tracker contract
IBlockTracker public blockTrackerContract;

/// Struct enabling automatic protocol fee payouts
FeePayout.Tracker public protocolFeeTracker;

/// @dev Mapping for if bidder is registered
mapping(address => bool) public bidderRegistered;

// Mapping from bidder addresses and window numbers to their locked funds
mapping(address => mapping(uint256 => uint256)) public lockedFunds;

// Mapping from bidder addresses and blocks to their used funds
mapping(address => mapping(uint64 => uint256)) public usedFunds;

/// Mapping from bidder addresses and window numbers to their funds per window
mapping(address => mapping(uint256 => uint256)) public maxBidPerBlock;

/// @dev Mapping from bidder addresses to their locked amount based on bidID (commitmentDigest)
mapping(bytes32 => BidState) public bidPayment;

/// @dev Amount assigned to bidders
mapping(address => uint256) public providerAmount;

/// @dev Amount assigned to bidders
uint256 public blocksPerWindow;

/// @dev Event emitted when a bidder is registered with their deposited amount
event BidderRegistered(
address indexed bidder,
uint256 indexed depositedAmount,
uint256 indexed windowNumber
);

/// @dev Event emitted when funds are retrieved from a bidder's deposit
event FundsRetrieved(
bytes32 indexed commitmentDigest,
address indexed bidder,
uint256 indexed window,
uint256 amount
);

/// @dev Event emitted when funds are retrieved from a bidder's deposit
event FundsRewarded(
bytes32 indexed commitmentDigest,
address indexed bidder,
address indexed provider,
uint256 window,
uint256 amount
);

/// @dev Event emitted when a bidder withdraws their deposit
event BidderWithdrawal(
address indexed bidder,
uint256 indexed window,
uint256 indexed amount
);

/// @dev Event emitted when the protocol fee recipient is updated
event ProtocolFeeRecipientUpdated(address indexed newProtocolFeeRecipient);

/// @dev Event emitted when the fee payout period in blocks is updated
event FeePayoutPeriodBlocksUpdated(uint256 indexed newFeePayoutPeriodBlocks);

/**
* @dev Modifier to restrict a function to only be callable by the pre-confirmations contract.
*/
Expand Down
47 changes: 47 additions & 0 deletions contracts/contracts/core/BidderRegistryStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity 0.8.20;

import {FeePayout} from "../utils/FeePayout.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";
import {IBidderRegistry} from "../interfaces/IBidderRegistry.sol";

abstract contract BidderRegistryStorage {
using FeePayout for FeePayout.Tracker;

/// @dev For improved precision
uint256 constant public PRECISION = 10 ** 25;
uint256 constant public PERCENT = 100 * PRECISION;

/// @dev Address of the pre-confirmations contract
address public preConfirmationsContract;

/// @dev Fee percent that would be taken by protocol when provider is slashed
uint16 public feePercent;

/// @dev Block tracker contract
IBlockTracker public blockTrackerContract;

/// Struct enabling automatic protocol fee payouts
FeePayout.Tracker public protocolFeeTracker;

/// @dev Mapping for if bidder is registered
mapping(address => bool) public bidderRegistered;

// Mapping from bidder addresses and window numbers to their locked funds
mapping(address => mapping(uint256 => uint256)) public lockedFunds;

// Mapping from bidder addresses and blocks to their used funds
mapping(address => mapping(uint64 => uint256)) public usedFunds;

/// Mapping from bidder addresses and window numbers to their funds per window
mapping(address => mapping(uint256 => uint256)) public maxBidPerBlock;

/// @dev Mapping from bidder addresses to their locked amount based on bidID (commitmentDigest)
mapping(bytes32 => IBidderRegistry.BidState) public bidPayment;

/// @dev Amount assigned to bidders
mapping(address => uint256) public providerAmount;

/// @dev Amount assigned to bidders
uint256 public blocksPerWindow;
}
17 changes: 3 additions & 14 deletions contracts/contracts/core/BlockTracker.sol
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity 0.8.20;

import {IBlockTracker} from "../interfaces/IBlockTracker.sol";
import {BlockTrackerStorage} from "./BlockTrackerStorage.sol";
import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";

/**
* @title BlockTracker
* @dev A contract that tracks Ethereum blocks and their winners.
*/
contract BlockTracker is IBlockTracker, Ownable2StepUpgradeable, UUPSUpgradeable {

/// @dev Permissioned address of the oracle account.
address public oracleAccount;

uint256 public currentWindow;
uint256 public blocksPerWindow;

// Mapping from block number to the winner's address
mapping(uint256 => address) public blockWinners;

/// @dev Maps builder names to their respective Ethereum addresses.
mapping(string => address) public blockBuilderNameToAddress;
contract BlockTracker is IBlockTracker, BlockTrackerStorage, Ownable2StepUpgradeable, UUPSUpgradeable {

/// @dev Modifier to ensure that the sender is the oracle account.
modifier onlyOracle() {
Expand Down
17 changes: 17 additions & 0 deletions contracts/contracts/core/BlockTrackerStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity 0.8.20;

abstract contract BlockTrackerStorage {
/// @dev Permissioned address of the oracle account.
address public oracleAccount;

uint256 public currentWindow;

uint256 public blocksPerWindow;

// Mapping from block number to the winner's address
mapping(uint256 => address) public blockWinners;

/// @dev Maps builder names to their respective Ethereum addresses.
mapping(string => address) public blockBuilderNameToAddress;
}
32 changes: 6 additions & 26 deletions contracts/contracts/core/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,16 @@ import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/acces
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {IPreconfManager} from "../interfaces/IPreconfManager.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";

/// @title Oracle Contract
/// @author Kartik Chopra
/// @notice This contract is for settling commitments made by providers.
import {OracleStorage} from "./OracleStorage.sol";
import {IOracle} from "../interfaces/IOracle.sol";

/**
* @title Oracle - A contract for Fetching L1 Block Builder Info and Block Data.
* @title Oracle
* @notice A contract for Fetching L1 Block Builder Info and Block Data.
* @author Kartik Chopra
* @dev This contract serves as an oracle to fetch and process Ethereum Layer 1 block data.
*/
contract Oracle is Ownable2StepUpgradeable, UUPSUpgradeable {
/// @dev Maps builder names to their respective Ethereum addresses.
mapping(string => address) public blockBuilderNameToAddress;

/// @dev Permissioned address of the oracle account.
address public oracleAccount;

/// @dev Reference to the PreconfManager contract interface.
IPreconfManager private _preConfContract;

/// @dev Reference to the BlockTracker contract interface.
IBlockTracker private _blockTrackerContract;

/// @dev Event emitted when the oracle account is set.
event OracleAccountSet(
address indexed oldOracleAccount,
address indexed newOracleAccount
);

/// @dev Event emitted when a commitment is processed.
event CommitmentProcessed(bytes32 indexed commitmentIndex, bool isSlash);
contract Oracle is OracleStorage, IOracle, Ownable2StepUpgradeable, UUPSUpgradeable {

/// @dev Modifier to ensure that the sender is the oracle account.
modifier onlyOracle() {
Expand Down
19 changes: 19 additions & 0 deletions contracts/contracts/core/OracleStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity 0.8.20;

import {IPreconfManager} from "../interfaces/IPreconfManager.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";

abstract contract OracleStorage {
/// @dev Maps builder names to their respective Ethereum addresses.
mapping(string => address) public blockBuilderNameToAddress;

/// @dev Permissioned address of the oracle account.
address public oracleAccount;

/// @dev Reference to the PreconfManager contract interface.
IPreconfManager internal _preConfContract;

/// @dev Reference to the BlockTracker contract interface.
IBlockTracker internal _blockTrackerContract;
}
31 changes: 2 additions & 29 deletions contracts/contracts/core/PreconfManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {IProviderRegistry} from "../interfaces/IProviderRegistry.sol";
import {IBidderRegistry} from "../interfaces/IBidderRegistry.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";
import {IPreconfManager} from "../interfaces/IPreconfManager.sol";
import {PreconfManagerStorage} from "./PreconfManagerStorage.sol";
import {WindowFromBlockNumber} from "../utils/WindowFromBlockNumber.sol";

/**
Expand All @@ -16,6 +17,7 @@ import {WindowFromBlockNumber} from "../utils/WindowFromBlockNumber.sol";
*/
contract PreconfManager is
IPreconfManager,
PreconfManagerStorage,
Ownable2StepUpgradeable,
UUPSUpgradeable
{
Expand Down Expand Up @@ -56,35 +58,6 @@ contract PreconfManager is
// Hex characters
bytes public constant HEXCHARS = "0123456789abcdef";

// Represents the dispatch window in milliseconds
uint64 public commitmentDispatchWindow;

/// @dev Address of the oracle contract
address public oracleContract;

/// @dev The number of blocks per window
uint256 public blocksPerWindow;

/// @dev Address of provider registry
IProviderRegistry public providerRegistry;

/// @dev Address of bidderRegistry
IBidderRegistry public bidderRegistry;

/// @dev Address of blockTracker
IBlockTracker public blockTracker;

/// @dev Mapping from provider to commitments count
mapping(address => uint256) public commitmentsCount;

/// @dev Commitment Hash -> Opened Commitemnt
/// @dev Only stores valid commitments
mapping(bytes32 => OpenedCommitment) public openedCommitments;

/// @dev Unopened Commitment Hash -> Unopened Commitment
/// @dev Only stores valid unopened commitments
mapping(bytes32 => UnopenedCommitment) public unopenedCommitments;

/**
* @dev Makes sure transaction sender is oracle contract
*/
Expand Down
38 changes: 38 additions & 0 deletions contracts/contracts/core/PreconfManagerStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: BSL 1.1
pragma solidity 0.8.20;

import {IProviderRegistry} from "../interfaces/IProviderRegistry.sol";
import {IBidderRegistry} from "../interfaces/IBidderRegistry.sol";
import {IBlockTracker} from "../interfaces/IBlockTracker.sol";
import {IPreconfManager} from "../interfaces/IPreconfManager.sol";

abstract contract PreconfManagerStorage {
// Represents the dispatch window in milliseconds
uint64 public commitmentDispatchWindow;

/// @dev Address of the oracle contract
address public oracleContract;

/// @dev The number of blocks per window
uint256 public blocksPerWindow;

/// @dev Address of provider registry
IProviderRegistry public providerRegistry;

/// @dev Address of bidderRegistry
IBidderRegistry public bidderRegistry;

/// @dev Address of blockTracker
IBlockTracker public blockTracker;

/// @dev Mapping from provider to commitments count
mapping(address => uint256) public commitmentsCount;

/// @dev Commitment Hash -> Opened Commitemnt
/// @dev Only stores valid commitments
mapping(bytes32 => IPreconfManager.OpenedCommitment) public openedCommitments;

/// @dev Unopened Commitment Hash -> Unopened Commitment
/// @dev Only stores valid unopened commitments
mapping(bytes32 => IPreconfManager.UnopenedCommitment) public unopenedCommitments;
}
Loading

0 comments on commit bf9bfd7

Please sign in to comment.