Skip to content

Commit

Permalink
chore: blocks per window (#146)
Browse files Browse the repository at this point in the history
* chore: pass blocksPerWindow in init

* chore: moved blocksPerWindow to init
  • Loading branch information
Mikelle authored and mrekucci committed Jun 21, 2024
1 parent 2838db7 commit 64bc907
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 159 deletions.
18 changes: 18 additions & 0 deletions contracts-abi/abi/BidderRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "blocksPerWindow",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "depositForSpecificWindow",
Expand Down Expand Up @@ -237,6 +250,11 @@
"name": "_blockTracker",
"type": "address",
"internalType": "address"
},
{
"name": "_blocksPerWindow",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [],
Expand Down
24 changes: 5 additions & 19 deletions contracts-abi/abi/BlockTracker.abi
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,6 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getWindowFromBlockNumber",
"inputs": [
{
"name": "blockNumber",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "initialize",
Expand All @@ -185,6 +166,11 @@
"name": "_owner",
"type": "address",
"internalType": "address"
},
{
"name": "_blocksPerWindow",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [],
Expand Down
18 changes: 18 additions & 0 deletions contracts-abi/abi/PreConfCommitmentStore.abi
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "blocksPerWindow",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "commitmentDispatchWindow",
Expand Down Expand Up @@ -683,6 +696,11 @@
"name": "_commitmentDispatchWindow",
"type": "uint64",
"internalType": "uint64"
},
{
"name": "_blocksPerWindow",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [],
Expand Down
57 changes: 44 additions & 13 deletions contracts-abi/clients/BidderRegistry/BidderRegistry.go

Large diffs are not rendered by default.

57 changes: 13 additions & 44 deletions contracts-abi/clients/BlockTracker/BlockTracker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions contracts/contracts/BidderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ReentrancyGuardUpgradeable} from "@openzeppelin/contracts-upgradeable/ut

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

/// @title Bidder Registry
/// @author Kartik Chopra
Expand Down Expand Up @@ -58,6 +59,9 @@ contract BidderRegistry is
/// @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,
Expand Down Expand Up @@ -116,12 +120,14 @@ contract BidderRegistry is
address _feeRecipient,
uint16 _feePercent,
address _owner,
address _blockTracker
address _blockTracker,
uint256 _blocksPerWindow
) external initializer {
minDeposit = _minDeposit;
feeRecipient = _feeRecipient;
feePercent = _feePercent;
blockTrackerContract = IBlockTracker(_blockTracker);
blocksPerWindow = _blocksPerWindow;
__Ownable_init(_owner);
}

Expand Down Expand Up @@ -187,8 +193,7 @@ contract BidderRegistry is
lockedFunds[msg.sender][window] = newLockedFunds;

// Calculate the maximum bid per block for the given window
uint256 numberOfRounds = blockTrackerContract.getBlocksPerWindow();
maxBidPerBlock[msg.sender][window] = newLockedFunds / numberOfRounds;
maxBidPerBlock[msg.sender][window] = newLockedFunds / blocksPerWindow;

emit BidderRegistered(msg.sender, newLockedFunds, window);
}
Expand Down Expand Up @@ -295,8 +300,9 @@ contract BidderRegistry is
if (bidState.state != State.Undefined) {
return;
}
uint256 currentWindow = blockTrackerContract.getWindowFromBlockNumber(
blockNumber
uint256 currentWindow = WindowFromBlockNumber.getWindowFromBlockNumber(
blockNumber,
blocksPerWindow
);

uint256 windowAmount = maxBidPerBlock[bidder][currentWindow];
Expand Down
13 changes: 2 additions & 11 deletions contracts/contracts/BlockTracker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ contract BlockTracker is OwnableUpgradeable {
* @dev Initializes the BlockTracker contract with the specified owner.
* @param _owner The address of the contract owner.
*/
function initialize(address _owner) external initializer {
function initialize(address _owner, uint256 _blocksPerWindow) external initializer {
currentWindow = 1;
blocksPerWindow = 10;
blocksPerWindow = _blocksPerWindow;
__Ownable_init(_owner);
}

Expand Down Expand Up @@ -76,15 +76,6 @@ contract BlockTracker is OwnableUpgradeable {
return blockBuilderNameToAddress[builderNameGrafiti];
}

/**
* @dev Returns the window number corresponding to a given block number.
* @param blockNumber The block number.
* @return The window number.
*/
function getWindowFromBlockNumber(uint256 blockNumber) external view returns (uint256) {
return (blockNumber - 1) / blocksPerWindow + 1;
}

/**
* @dev Returns the number of blocks per window.
* @return The number of blocks per window.
Expand Down
Loading

0 comments on commit 64bc907

Please sign in to comment.