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

Adds testing and nit bug fix #81

Merged
merged 2 commits into from
Jan 31, 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
5 changes: 1 addition & 4 deletions contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ contract Oracle is Ownable {
/// @dev Reference to the PreConfCommitmentStore contract interface.
IPreConfCommitmentStore private preConfContract;

IBidderRegistry private bidderRegistry;

/**
* @dev Constructor to initialize the contract with a PreConfirmations contract.
Expand All @@ -56,12 +55,10 @@ contract Oracle is Ownable {
*/
constructor(
address _preConfContract,
address _bidderRegistry,
uint256 _nextRequestedBlockNumber,
address _owner
) Ownable() {
preConfContract = IPreConfCommitmentStore(_preConfContract);
bidderRegistry = IBidderRegistry(_bidderRegistry);
nextRequestedBlockNumber = _nextRequestedBlockNumber;
_transferOwnership(_owner);
}
Expand Down Expand Up @@ -131,7 +128,7 @@ contract Oracle is Ownable {
*/
function unlockFunds(bytes32[] memory bidIDs) external onlyOwner {
for (uint256 i = 0; i < bidIDs.length; i++) {
bidderRegistry.unlockFunds(bidIDs[i]);
preConfContract.unlockBidFunds(bidIDs[i]);
}
}

Expand Down
8 changes: 8 additions & 0 deletions contracts/PreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ contract PreConfCommitmentStore is Ownable {
bidderRegistry.unlockFunds(commitment.commitmentHash);
}

/**
* @dev Initiate a return of funds for a bid that was not slashed.
* @param commitmentDigest The hash of the bid to be unlocked.
*/
function unlockBidFunds(bytes32 commitmentDigest) public onlyOracle {
bidderRegistry.unlockFunds(commitmentDigest);
}

/**
* @dev Initiate a reward for a commitment.
* @param commitmentIndex The hash of the commitment to be rewarded.
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IPreConfirmations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ interface IPreConfCommitmentStore {
function initiateSlash(bytes32 commitmentIndex) external;

function initateReward(bytes32 commitmentIndex) external;

function unlockBidFunds(bytes32 commitmentDigest) external;

function updateOracle(address newOracle) external;

Expand Down
2 changes: 1 addition & 1 deletion scripts/DeployScripts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract DeployScript is Script, Create2Deployer {
bidderRegistry.setPreconfirmationsContract(address(preConfCommitmentStore));
console.log("BidderRegistry updated with PreConfCommitmentStore address:", address(preConfCommitmentStore));

Oracle oracle = new Oracle{salt: salt}(address(preConfCommitmentStore), address(bidderRegistry), nextRequestedBlockNumber, msg.sender);
Oracle oracle = new Oracle{salt: salt}(address(preConfCommitmentStore), nextRequestedBlockNumber, msg.sender);
console.log("Oracle deployed to:", address(oracle));

preConfCommitmentStore.updateOracle(address(oracle));
Expand Down
43 changes: 41 additions & 2 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ contract OracleTest is Test {
event BlockDataRequested(uint256 blockNumber);
event BlockDataReceived(string[] txnList, uint256 blockNumber, string blockBuilderName);
event CommitmentProcessed(bytes32 commitmentHash, bool isSlash);
event FundsRetrieved(bytes32 indexed commitmentDigest, uint256 amount);

function setUp() public {
testNumber = 2;
Expand Down Expand Up @@ -54,7 +55,7 @@ contract OracleTest is Test {
vm.startPrank(ownerInstance);
bidderRegistry.prepay{value: 2 ether}();

oracle = new Oracle(address(preConfCommitmentStore), address(bidderRegistry), 2, ownerInstance);
oracle = new Oracle(address(preConfCommitmentStore), 2, ownerInstance);
oracle.addBuilderAddress("mev builder", ownerInstance);
vm.stopPrank();

Expand Down Expand Up @@ -292,10 +293,13 @@ contract OracleTest is Test {
vm.stopPrank();

bytes32 index1 = constructAndStoreCommitment(bid, blockNumber, txn1, bidderPk, providerPk);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - bid);
bytes32 index2 = constructAndStoreCommitment(bid, blockNumber, txn2, bidderPk, providerPk);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 2*bid);
bytes32 index3 = constructAndStoreCommitment(bid, blockNumber, txn3, bidderPk, providerPk);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 3*bid);
bytes32 index4 = constructAndStoreCommitment(bid, blockNumber, txn4, bidderPk, providerPk);

assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether - 4*bid);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress(blockBuilderName, provider);
Expand All @@ -317,6 +321,41 @@ contract OracleTest is Test {
assertEq(bidderRegistry.getProviderAmount(provider), 4*bid);
}


function test_process_commitment_and_return() public {
string memory txn = "0x6d9c53ad81249775f8c082b11ac293b2e19194ff791bd1c4fd37683310e90d08";
uint64 blockNumber = 200;
uint64 bid = 2;
(address bidder, uint256 bidderPk) = makeAddrAndKey("alice");
(address provider, uint256 providerPk) = makeAddrAndKey("kartik");

vm.deal(bidder, 200000 ether);
vm.startPrank(bidder);
bidderRegistry.prepay{value: 250 ether }();
vm.stopPrank();

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

bytes32 index = constructAndStoreCommitment(bid, blockNumber, txn, bidderPk, providerPk);
PreConfCommitmentStore.PreConfCommitment memory commitment = preConfCommitmentStore.getCommitment(index);

vm.startPrank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
bytes32[] memory commitments = new bytes32[](1);
commitments[0] = commitment.commitmentHash;

vm.expectEmit(true, false, false, true);
emit FundsRetrieved(commitment.commitmentHash, bid);
oracle.unlockFunds(commitments);


assertEq(providerRegistry.checkStake(provider) , 250 ether);
assertEq(bidderRegistry.bidderPrepaidBalances(bidder), 250 ether);
}


/**
constructAndStoreCommitment is a helper function to construct and store a commitment
*/
Expand Down
Loading