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

Feature: Adds bundles to Oracle and Pre-Confirmations flow #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions contracts/Oracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract Oracle is Ownable {
// mapping of txns to bool to check if txns exists
// Stores all proccessed txns for onw
// TODO(@ckartik): This may be too restricvie in the log run as an appraoch
mapping(string => bool) txnHashes;
mapping(string => uint) txnHashes;


// Event to request block data
Expand Down Expand Up @@ -82,7 +82,7 @@ contract Oracle is Ownable {
address builder = blockBuilderNameToAddress[blockBuilderName];

for (uint256 i = 0; i < txnList.length; i++) {
txnHashes[txnList[i]] = true;
txnHashes[txnList[i]] = i+1;
}

// Placeholder: Process the block data and determine the commitment's validity
Expand All @@ -91,7 +91,24 @@ contract Oracle is Ownable {
for (uint256 i = 0; i < commitmentHashes.length; i++) {
IPreConfCommitmentStore.PreConfCommitment memory commitment = preConfContract.getCommitment(commitmentHashes[i]);
if (commitment.commiter == builder) {
if (txnHashes[commitment.txnHash]){
bool commitmentSucceeded = true;

// TODO(@ckartik): Ensure we check for correct encoding during commtiment storage.
(string[] memory commitedTransactions) = abi.decode(bytes(commitment.txnHash), (string[]));
// Determine if bundle order was satisfied
for (uint256 j = 0; j < commitedTransactions.length; j++) {
if (txnHashes[commitedTransactions[j]] == 0) {

commitmentSucceeded = false;
break;
}
if (j+1 < commitedTransactions.length && txnHashes[commitedTransactions[j]] + 1 != txnHashes[commitedTransactions[j+1]]) {
commitmentSucceeded = false;
break;
}
}

if (commitmentSucceeded){
this.processCommitment(commitmentHashes[i], false);
}
else {
Expand Down
98 changes: 95 additions & 3 deletions test/OracleTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ contract OracleTest is Test {
providerRegistry.registerAndStake{value: 250 ether}();
vm.stopPrank();

constructAndStoreCommitment(bid, blockNumber, txnList[0], userPk, providerPk);
bytes memory txnhashList = abi.encode(txnList);

constructAndStoreCommitment(bid, blockNumber, string(txnhashList), userPk, providerPk);
vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress("kartik builder", provider);
vm.expectEmit(true, true, false, true);
Expand All @@ -213,6 +215,95 @@ contract OracleTest is Test {
}


function test_ReceiveBlockDataWithCommitmentsBundle() public {
string[] memory txnList = new string[](3);
txnList[0] = string(abi.encodePacked(keccak256("0xfrontrun")));
txnList[1] = string(abi.encodePacked(keccak256("0xmev")));
txnList[2] = string(abi.encodePacked(keccak256("0xbackrun")));

uint64 blockNumber = 200;
uint64 bid = 2;
string memory blockBuilderName = "kartik builder";
(address user, uint256 userPk) = makeAddrAndKey("alice");
(address provider, uint256 providerPk) = makeAddrAndKey("kartik");

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

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

bytes memory txnhashList = abi.encode(txnList);

constructAndStoreCommitment(bid, blockNumber, string(txnhashList), userPk, providerPk);
vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress("kartik builder", provider);
vm.expectEmit(true, true, false, true);
emit BlockDataReceived(txnList, blockNumber, blockBuilderName);
oracle.receiveBlockData(txnList, blockNumber, blockBuilderName);

bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber);
assertEq(commitmentHashes.length, 1);
assertEq(userRegistry.getProviderAmount(provider), bid);

}


function test_ReceiveBlockDataWithCommitmentsBundleNotAtomicMustSlash() public {
string[] memory commitedTxnList = new string[](3);
commitedTxnList[0] = string(abi.encodePacked(keccak256("0xfrontrun")));
commitedTxnList[1] = string(abi.encodePacked(keccak256("0xmev")));
commitedTxnList[2] = string(abi.encodePacked(keccak256("0xbackrun")));

string[] memory builderPayload = new string[](4);
builderPayload[0] = string(abi.encodePacked(keccak256("0xfrontrun")));
builderPayload[1] = string(abi.encodePacked(keccak256("0xmev")));
builderPayload[2] = string(abi.encodePacked(keccak256("0xbuilderbackrun")));
builderPayload[3] = string(abi.encodePacked(keccak256("0xbackrun")));

uint64 blockNumber = 200;
uint64 bid = 2;
string memory blockBuilderName = "kartik builder";
(address user, uint256 userPk) = makeAddrAndKey("alice");
(address provider, uint256 providerPk) = makeAddrAndKey("bob");

vm.deal(user, 200000 ether);
vm.deal(provider, 200000 ether);

vm.startPrank(user);
userRegistry.registerAndStake{value: 250 ether }();
vm.stopPrank();

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

uint256 ogStake = providerRegistry.checkStake(provider);

constructAndStoreCommitment(bid, blockNumber, string(abi.encode(commitedTxnList)), userPk, providerPk);
vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress("kartik builder", provider);
vm.expectEmit(true, true, false, true);
emit BlockDataReceived(builderPayload, blockNumber, blockBuilderName);
oracle.receiveBlockData(builderPayload, blockNumber, blockBuilderName);

bytes32[] memory commitmentHashes = preConfCommitmentStore.getCommitmentsByBlockNumber(blockNumber);
assertEq(commitmentHashes.length, 1);

// Ensuring no rewards
assertEq(userRegistry.getProviderAmount(provider), 0);

// Detect slashing
uint256 postSlashStake = providerRegistry.checkStake(provider);
assertEq(postSlashStake + bid, ogStake);
assertEq(userRegistry.checkStake(user), 250 ether);

}

function test_ReceiveBlockDataWithCommitmentsSlashed() public {
string[] memory txnList = new string[](1);
txnList[0] = string(abi.encodePacked(keccak256("0xkartik")));
Expand All @@ -235,8 +326,9 @@ contract OracleTest is Test {

uint256 ogStake = providerRegistry.checkStake(provider);

string memory commitedTxn = string(abi.encodePacked(keccak256("0xSlash")));
constructAndStoreCommitment(bid, blockNumber, commitedTxn, userPk, providerPk);
string[] memory commitedTxnList = new string[](1);
commitedTxnList[0] = string(abi.encodePacked(keccak256("0xSlash")));
constructAndStoreCommitment(bid, blockNumber, string(abi.encode(commitedTxnList)), userPk, providerPk);
vm.prank(address(0x6d503Fd50142C7C469C7c6B64794B55bfa6883f3));
oracle.addBuilderAddress("kartik builder", provider);
vm.expectEmit(true, true, false, true);
Expand Down