Skip to content

Commit

Permalink
fix: providers specify BLS pubkey during registration (#214)
Browse files Browse the repository at this point in the history
* feat: builder specifies bls pubkey e2e

* fix: use proper event for test_ProviderStakeAndRegister

* fix: generate blspubkeys for providers

* fix: use rand bytes len 48

* fix: proto resp + test

* fix: keep amount as path param

* feat: logging

* fix: decode string

* fix: handle 0x prefix

* chore: rm log
  • Loading branch information
shaspitz authored Jul 9, 2024
1 parent 27ff161 commit 9c72fd2
Show file tree
Hide file tree
Showing 14 changed files with 693 additions and 343 deletions.
78 changes: 77 additions & 1 deletion contracts-abi/abi/ProviderRegistry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,51 @@
"type": "receive",
"stateMutability": "payable"
},
{
"type": "function",
"name": "EOAToBLSPubkey",
"inputs": [
{
"name": "",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "",
"type": "bytes",
"internalType": "bytes"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "PERCENT",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "PRECISION",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "UPGRADE_INTERFACE_VERSION",
Expand Down Expand Up @@ -109,6 +154,25 @@
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getBLSKey",
"inputs": [
{
"name": "provider",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "",
"type": "bytes",
"internalType": "bytes"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "initialize",
Expand Down Expand Up @@ -230,7 +294,13 @@
{
"type": "function",
"name": "registerAndStake",
"inputs": [],
"inputs": [
{
"name": "blsPublicKey",
"type": "bytes",
"internalType": "bytes"
}
],
"outputs": [],
"stateMutability": "payable"
},
Expand Down Expand Up @@ -457,6 +527,12 @@
"type": "uint256",
"indexed": false,
"internalType": "uint256"
},
{
"name": "blsPublicKey",
"type": "bytes",
"indexed": false,
"internalType": "bytes"
}
],
"anonymous": false
Expand Down
163 changes: 144 additions & 19 deletions contracts-abi/clients/ProviderRegistry/ProviderRegistry.go

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions contracts/contracts/ProviderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ contract ProviderRegistry is
ReentrancyGuardUpgradeable
{
/// @dev For improved precision
uint256 constant PRECISION = 10 ** 25;
uint256 constant PERCENT = 100 * PRECISION;
uint256 public constant PRECISION = 10 ** 25;
uint256 public constant PERCENT = 100 * PRECISION;

/// @dev Minimum stake required for registration
uint256 public minStake;
Expand All @@ -39,14 +39,17 @@ contract ProviderRegistry is
/// @dev Mapping from provider address to whether they are registered or not
mapping(address => bool) public providerRegistered;

/// @dev Mapping from a provider's EOA address to their BLS public key
mapping(address => bytes) public EOAToBLSPubkey;

/// @dev Mapping from provider addresses to their staked amount
mapping(address => uint256) public providerStakes;

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

/// @dev Event for provider registration
event ProviderRegistered(address indexed provider, uint256 stakedAmount);
event ProviderRegistered(address indexed provider, uint256 stakedAmount, bytes blsPublicKey);

/// @dev Event for depositing funds
event FundsDeposited(address indexed provider, uint256 amount);
Expand Down Expand Up @@ -123,14 +126,17 @@ contract ProviderRegistry is

/**
* @dev Register and stake function for providers.
* @param blsPublicKey The BLS public key of the provider.
* The validity of this key must be verified manually off-chain.
*/
function registerAndStake() public payable {
function registerAndStake(bytes calldata blsPublicKey) public payable {
require(!providerRegistered[msg.sender], "Provider already registered");
require(msg.value >= minStake, "Insufficient stake");
require(blsPublicKey.length == 48, "Invalid BLS public key length");
EOAToBLSPubkey[msg.sender] = blsPublicKey;
providerStakes[msg.sender] = msg.value;
providerRegistered[msg.sender] = true;

emit ProviderRegistered(msg.sender, msg.value);
emit ProviderRegistered(msg.sender, msg.value, blsPublicKey);
}

/**
Expand Down Expand Up @@ -255,4 +261,9 @@ contract ProviderRegistry is
(bool success, ) = provider.call{value: stake}("");
require(success, "Couldn't transfer stake to provider");
}

/// @dev Returns the BLS public key corresponding to a provider's staked EOA address.
function getBLSKey(address provider) external view returns (bytes memory) {
return EOAToBLSPubkey[provider];
}
}
2 changes: 1 addition & 1 deletion contracts/contracts/interfaces/IProviderRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.20;

interface IProviderRegistry {
function registerAndStake() external payable;
function registerAndStake(bytes calldata blsPublicKey) external payable;

function checkStake(address provider) external view returns (uint256);

Expand Down
61 changes: 31 additions & 30 deletions contracts/test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ import {Upgrades} from "openzeppelin-foundry-upgrades/Upgrades.sol";
import {WindowFromBlockNumber} from "../contracts/utils/WindowFromBlockNumber.sol";

contract OracleTest is Test {
address internal owner;
address public owner;
using ECDSA for bytes32;
Oracle internal oracle;
PreConfCommitmentStore internal preConfCommitmentStore;
uint16 internal feePercent;
uint256 internal minStake;
address internal feeRecipient;
ProviderRegistry internal providerRegistry;
uint256 testNumber;
uint64 testNumber2;
BidderRegistry internal bidderRegistry;
BlockTracker internal blockTracker;
Oracle public oracle;
PreConfCommitmentStore public preConfCommitmentStore;
uint16 public feePercent;
uint256 public minStake;
address public feeRecipient;
ProviderRegistry public providerRegistry;
uint256 public testNumber;
uint64 public testNumber2;
BidderRegistry public bidderRegistry;
BlockTracker public blockTracker;
TestCommitment internal _testCommitmentAliceBob;
uint64 internal dispatchTimestampTesting;
bytes internal sharedSecretKey;
uint256 internal blocksPerWindow;
uint64 public dispatchTimestampTesting;
bytes public sharedSecretKey;
uint256 public blocksPerWindow;
bytes public constant validBLSPubkey = hex"80000cddeec66a800e00b0ccbb62f12298073603f5209e812abbac7e870482e488dd1bbe533a9d44497ba8b756e1e82b";

struct TestCommitment {
uint64 bid;
Expand Down Expand Up @@ -178,7 +179,7 @@ contract OracleTest is Test {

vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
providerRegistry.registerAndStake{value: 250 ether}(validBLSPubkey);
vm.stopPrank();

bytes32 index = constructAndStoreCommitment(
Expand Down Expand Up @@ -223,7 +224,7 @@ contract OracleTest is Test {

vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
providerRegistry.registerAndStake{value: 250 ether}(validBLSPubkey);
vm.stopPrank();

bytes32 index = constructAndStoreCommitment(
Expand Down Expand Up @@ -274,7 +275,7 @@ contract OracleTest is Test {

vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
providerRegistry.registerAndStake{value: 250 ether}(validBLSPubkey);
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(
Expand Down Expand Up @@ -347,7 +348,7 @@ contract OracleTest is Test {

vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
providerRegistry.registerAndStake{value: 250 ether}(validBLSPubkey);
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(
Expand Down Expand Up @@ -457,13 +458,13 @@ contract OracleTest is Test {

vm.deal(provider, 200000 ether);
vm.startPrank(provider);
providerRegistry.registerAndStake{value: 250 ether}();
providerRegistry.registerAndStake{value: 250 ether}(validBLSPubkey);
vm.stopPrank();

bytes32[] memory commitments = new bytes32[](4);
bytes[] memory bidSignatures = new bytes[](4);
bytes[] memory commitmentSignatures = new bytes[](4);
for (uint i = 0; i < commitments.length; i++) {
for (uint256 i = 0; i < commitments.length; i++) {
(
commitments[i],
bidSignatures[i],
Expand All @@ -486,7 +487,7 @@ contract OracleTest is Test {
blockTracker.recordL1Block(blockNumber, "test");
vm.stopPrank();

for (uint i = 0; i < commitments.length; i++) {
for (uint256 i = 0; i < commitments.length; i++) {
vm.startPrank(provider);
preConfCommitmentStore.openCommitment(
commitments[i],
Expand All @@ -503,7 +504,7 @@ contract OracleTest is Test {
}

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
for (uint i = 0; i < commitments.length; i++) {
for (uint256 i = 0; i < commitments.length; i++) {
vm.expectEmit(true, false, false, true);
emit CommitmentProcessed(commitments[i], false);
oracle.processBuilderCommitmentForBlockNumber(
Expand Down Expand Up @@ -566,7 +567,7 @@ contract OracleTest is Test {
string memory txnHash,
uint64 bid,
uint64 blockNumber
) internal view returns (bytes32) {
) public view returns (bytes32) {
return
preConfCommitmentStore.getBidHash(
txnHash,
Expand All @@ -580,7 +581,7 @@ contract OracleTest is Test {
function getBidSignature(
uint256 bidderPk,
bytes32 bidHash
) internal pure returns (bytes memory) {
) public pure returns (bytes memory) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(bidderPk, bidHash);
return abi.encodePacked(r, s, v);
}
Expand All @@ -591,7 +592,7 @@ contract OracleTest is Test {
uint64 blockNumber,
bytes32 bidHash,
bytes memory bidSignature
) internal view returns (bytes32) {
) public view returns (bytes32) {
return
preConfCommitmentStore.getPreConfHash(
txnHash,
Expand All @@ -608,7 +609,7 @@ contract OracleTest is Test {
function getCommitmentSignature(
uint256 signerPk,
bytes32 commitmentHash
) internal pure returns (bytes memory) {
) public pure returns (bytes memory) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPk, commitmentHash);
return abi.encodePacked(r, s, v);
}
Expand All @@ -618,7 +619,7 @@ contract OracleTest is Test {
bytes32 commitmentHash,
bytes memory commitmentSignature,
uint64 dispatchTimestamp
) internal returns (bytes32) {
) public returns (bytes32) {
vm.startPrank(provider);
bytes32 encryptedCommitmentIndex = preConfCommitmentStore
.storeEncryptedCommitment(
Expand All @@ -630,7 +631,7 @@ contract OracleTest is Test {
return encryptedCommitmentIndex;
}

function recordBlockData(address provider, uint64 blockNumber) internal {
function recordBlockData(address provider, uint64 blockNumber) public {
vm.startPrank(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3);
blockTracker.addBuilderAddress("test", provider);
blockTracker.recordL1Block(blockNumber, "test");
Expand All @@ -645,7 +646,7 @@ contract OracleTest is Test {
string memory txnHash,
bytes memory bidSignature,
bytes memory commitmentSignature
) internal returns (bytes32) {
) public returns (bytes32) {
vm.startPrank(provider);
bytes32 commitmentIndex = preConfCommitmentStore.openCommitment(
encryptedCommitmentIndex,
Expand Down Expand Up @@ -717,7 +718,7 @@ contract OracleTest is Test {

function _bytesToHexString(
bytes memory _bytes
) public pure returns (string memory) {
) internal pure returns (string memory) {
bytes memory HEXCHARS = "0123456789abcdef";
bytes memory _string = new bytes(_bytes.length * 2);
for (uint256 i = 0; i < _bytes.length; i++) {
Expand Down
Loading

0 comments on commit 9c72fd2

Please sign in to comment.