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

fix: separate oracle and owner accounts in contracts #251

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 54 additions & 4 deletions contracts-abi/abi/BlockTracker.abi
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,37 @@
"name": "initialize",
"inputs": [
{
"name": "_owner",
"name": "blocksPerWindow_",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "oracleAccount_",
"type": "address",
"internalType": "address"
},
{
"name": "_blocksPerWindow",
"type": "uint256",
"internalType": "uint256"
"name": "owner_",
"type": "address",
"internalType": "address"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "oracleAccount",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "owner",
Expand Down Expand Up @@ -240,6 +258,19 @@
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "setOracleAccount",
"inputs": [
{
"name": "newOracleAccount",
"type": "address",
"internalType": "address"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "transferOwnership",
Expand Down Expand Up @@ -322,6 +353,25 @@
],
"anonymous": false
},
{
"type": "event",
"name": "OracleAccountSet",
"inputs": [
{
"name": "oldOracleAccount",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "newOracleAccount",
"type": "address",
"indexed": true,
"internalType": "address"
}
],
"anonymous": false
},
{
"type": "event",
"name": "OwnershipTransferred",
Expand Down
56 changes: 53 additions & 3 deletions contracts-abi/abi/Oracle.abi
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,42 @@
"name": "initialize",
"inputs": [
{
"name": "_preConfContract",
"name": "preConfContract_",
"type": "address",
"internalType": "address"
},
{
"name": "_blockTrackerContract",
"name": "blockTrackerContract_",
"type": "address",
"internalType": "address"
},
{
"name": "_owner",
"name": "oracleAccount_",
"type": "address",
"internalType": "address"
},
{
"name": "owner_",
"type": "address",
"internalType": "address"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "oracleAccount",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "owner",
Expand Down Expand Up @@ -133,6 +151,19 @@
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "setOracleAccount",
"inputs": [
{
"name": "newOracleAccount",
"type": "address",
"internalType": "address"
}
],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "transferOwnership",
Expand Down Expand Up @@ -196,6 +227,25 @@
],
"anonymous": false
},
{
"type": "event",
"name": "OracleAccountSet",
"inputs": [
{
"name": "oldOracleAccount",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "newOracleAccount",
"type": "address",
"indexed": true,
"internalType": "address"
}
],
"anonymous": false
},
{
"type": "event",
"name": "OwnershipTransferred",
Expand Down
231 changes: 218 additions & 13 deletions contracts-abi/clients/BlockTracker/BlockTracker.go

Large diffs are not rendered by default.

231 changes: 218 additions & 13 deletions contracts-abi/clients/Oracle/Oracle.go

Large diffs are not rendered by default.

47 changes: 39 additions & 8 deletions contracts/contracts/BlockTracker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/U
* @dev A contract that tracks Ethereum blocks and their winners.
*/
contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {

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

/// @dev Modifier to ensure that the sender is the oracle account.
modifier onlyOracle() {
require(msg.sender == oracleAccount, "sender isn't oracle account");
_;
}

/// @dev Event emitted when a new L1 block is tracked.
event NewL1Block(
uint256 indexed blockNumber,
Expand All @@ -19,6 +29,9 @@ contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {
/// @dev Event emitted when a new window is created.
event NewWindow(uint256 indexed window);

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

uint256 public currentWindow;
uint256 public blocksPerWindow;

Expand All @@ -32,12 +45,15 @@ contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {

/**
* @dev Initializes the BlockTracker contract with the specified owner.
* @param _owner The address of the contract owner.
* @param blocksPerWindow_ Number of blocks per window.
* @param oracleAccount_ Address of the permissoined oracle account.
* @param owner_ Address of the contract owner.
*/
function initialize(address _owner, uint256 _blocksPerWindow) external initializer {
function initialize(uint256 blocksPerWindow_, address oracleAccount_, address owner_) external initializer {
currentWindow = 1;
blocksPerWindow = _blocksPerWindow;
__Ownable_init(_owner);
blocksPerWindow = blocksPerWindow_;
_setOracleAccount(oracleAccount_);
__Ownable_init(owner_);
}

/// @dev See https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#initializing_the_implementation_contract
Expand All @@ -48,21 +64,21 @@ contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {

/**
* @dev Retrieves the current window number.
* @return The current window number.
* @return currentWindow The current window number.
*/
function getCurrentWindow() external view returns (uint256) {
return currentWindow;
}

/**
* @dev Allows the owner to add a new builder address.
* @dev Allows the oracle account to add a new builder address mapping.
* @param builderName The name of the block builder as it appears on extra data.
* @param builderAddress The Ethereum address of the builder.
*/
function addBuilderAddress(
string memory builderName,
address builderAddress
) external onlyOwner {
) external onlyOracle {
blockBuilderNameToAddress[builderName] = builderAddress;
}

Expand Down Expand Up @@ -93,7 +109,7 @@ contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {
function recordL1Block(
uint256 _blockNumber,
string calldata _winnerGraffiti
) external onlyOwner {
) external onlyOracle {
address _winner = blockBuilderNameToAddress[_winnerGraffiti];
recordBlockWinner(_blockNumber, _winner);
uint256 newWindow = (_blockNumber - 1) / blocksPerWindow + 1;
Expand All @@ -105,6 +121,21 @@ contract BlockTracker is OwnableUpgradeable, UUPSUpgradeable {
emit NewL1Block(_blockNumber, _winner, currentWindow);
}

/// @dev Allows the owner to set the oracle account.
function setOracleAccount(address newOracleAccount) external onlyOwner {
_setOracleAccount(newOracleAccount);
}

/**
* @dev Internal function to set the oracle account.
* @param newOracleAccount The new address of the oracle account.
*/
function _setOracleAccount(address newOracleAccount) internal {
address oldOracleAccount = oracleAccount;
oracleAccount = newOracleAccount;
emit OracleAccountSet(oldOracleAccount, newOracleAccount);
}

/**
* @dev Internal function to record a new block winner
* @param blockNumber The number of the block
Expand Down
50 changes: 40 additions & 10 deletions contracts/contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ contract Oracle is OwnableUpgradeable, 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 Modifier to ensure that the sender is the oracle account.
modifier onlyOracle() {
require(msg.sender == oracleAccount, "sender isn't oracle account");
_;
}

// To shutup the compiler
/// @dev Empty receive function to silence compiler warnings about missing payable functions.
receive() external payable {
Expand All @@ -45,18 +54,21 @@ contract Oracle is OwnableUpgradeable, UUPSUpgradeable {

/**
* @dev Initializes the contract with a PreConfirmations contract.
* @param _preConfContract The address of the pre-confirmations contract.
* @param _blockTrackerContract The address of the block tracker contract.
* @param _owner Owner of the contract, explicitly needed since contract is deployed with create2 factory.
* @param preConfContract_ The address of the pre-confirmations contract.
* @param blockTrackerContract_ The address of the block tracker contract.
* @param oracleAccount_ The address of the oracle account.
* @param owner_ Owner of the contract, explicitly needed since contract is deployed with create2 factory.
*/
function initialize(
address _preConfContract,
address _blockTrackerContract,
address _owner
address preConfContract_,
address blockTrackerContract_,
address oracleAccount_,
address owner_
) external initializer {
preConfContract = IPreConfCommitmentStore(_preConfContract);
blockTrackerContract = IBlockTracker(_blockTrackerContract);
__Ownable_init(_owner);
preConfContract = IPreConfCommitmentStore(preConfContract_);
blockTrackerContract = IBlockTracker(blockTrackerContract_);
_setOracleAccount(oracleAccount_);
__Ownable_init(owner_);
}

/// @dev See https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#initializing_the_implementation_contract
Expand All @@ -68,6 +80,9 @@ contract Oracle is OwnableUpgradeable, UUPSUpgradeable {
/// @dev Event emitted when a commitment is processed.
event CommitmentProcessed(bytes32 indexed commitmentIndex, bool isSlash);

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

// Function to receive and process the block data (this would be automated in a real-world scenario)
/**
* @dev Processes a builder's commitment for a specific block number.
Expand All @@ -83,7 +98,7 @@ contract Oracle is OwnableUpgradeable, UUPSUpgradeable {
address builder,
bool isSlash,
uint256 residualBidPercentAfterDecay
) external onlyOwner {
) external onlyOracle {
require(
blockTrackerContract.getBlockWinner(blockNumber) == builder,
"Builder is not the winner of the block"
Expand All @@ -106,6 +121,21 @@ contract Oracle is OwnableUpgradeable, UUPSUpgradeable {
}
}

/// @dev Allows the owner to set the oracle account.
function setOracleAccount(address newOracleAccount) external onlyOwner {
_setOracleAccount(newOracleAccount);
}

/**
* @dev Internal function to set the oracle account.
* @param newOracleAccount The new address of the oracle account.
*/
function _setOracleAccount(address newOracleAccount) internal {
address oldOracleAccount = oracleAccount;
oracleAccount = newOracleAccount;
emit OracleAccountSet(oldOracleAccount, newOracleAccount);
}

/**
* @dev Internal function to process a commitment, either slashing or rewarding based on the commitment's state.
* @param commitmentIndex The id of the commitment to be processed.
Expand Down
Loading
Loading