Skip to content

Commit

Permalink
the last references of bid
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarzzz committed Dec 16, 2023
1 parent b4f3498 commit 958895e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
10 changes: 5 additions & 5 deletions core/vm/contracts_suave_runtime_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ var _ SuaveRuntime = &mockRuntime{}
type mockRuntime struct {
}

func (m *mockRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, bidId types.DataId, namespace string) ([]byte, []byte, error) {
func (m *mockRuntime) buildEthBlock(blockArgs types.BuildBlockArgs, dataId types.DataId, namespace string) ([]byte, []byte, error) {
return []byte{0x1}, []byte{0x1}, nil
}

func (m *mockRuntime) confidentialInputs() ([]byte, error) {
return []byte{0x1}, nil
}

func (m *mockRuntime) confidentialRetrieve(bidId types.DataId, key string) ([]byte, error) {
func (m *mockRuntime) confidentialRetrieve(dataId types.DataId, key string) ([]byte, error) {
return []byte{0x1}, nil
}

func (m *mockRuntime) confidentialStore(bidId types.DataId, key string, data1 []byte) error {
func (m *mockRuntime) confidentialStore(dataId types.DataId, key string, data1 []byte) error {
return nil
}

Expand All @@ -43,11 +43,11 @@ func (m *mockRuntime) fetchDataRecords(cond uint64, namespace string) ([]types.D
return []types.DataRecord{{}}, nil
}

func (m *mockRuntime) fillMevShareBundle(bidId types.DataId) ([]byte, error) {
func (m *mockRuntime) fillMevShareBundle(dataId types.DataId) ([]byte, error) {
return []byte{0x1}, nil
}

func (m *mockRuntime) newDataRecord(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, bidType string) (types.DataRecord, error) {
func (m *mockRuntime) newDataRecord(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, dataType string) (types.DataRecord, error) {
return types.DataRecord{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/vm/suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func checkIsPrecompileCallAllowed(suaveContext *SuaveContext, precompile common.
}

// In question!
// For now both the precompile *and* at least one caller must be allowed to allow access to bid data
// For now both the precompile *and* at least one caller must be allowed to allow access to confidential data
// Alternative is to simply allow if any of the callers is allowed
isPrecompileAllowed := slices.Contains(record.AllowedPeekers, precompile)

Expand Down
4 changes: 2 additions & 2 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,8 @@ func (w *worker) buildBlockFromBundles(ctx context.Context, args *types.BuildBlo
currNonce := work.state.GetNonce(ephemeralAddr)
// HACK to include payment txn
// multi refund block untested
bidTx := bundle.Txs[0] // NOTE : assumes first txn is refund recipient
refundAddr, err := types.Sender(types.LatestSignerForChainID(bidTx.ChainId()), bidTx)
userTx := bundle.Txs[0] // NOTE : assumes first txn is refund recipient
refundAddr, err := types.Sender(types.LatestSignerForChainID(userTx.ChainId()), userTx)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion suave/cstore/local_store_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (l *LocalConfidentialStore) FetchRecordByID(dataId suave.DataId) (suave.Dat

bid, found := l.records[dataId]
if !found {
return suave.DataRecord{}, errors.New("bid not found")
return suave.DataRecord{}, errors.New("record not found")
}

return bid, nil
Expand Down
59 changes: 29 additions & 30 deletions suave/sol/standard_peekers/bids.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ contract AnyBundleContract {
return abi.decode(confidentialInputs, (bytes));
}

// Bids to this contract should not be trusted!
function emitDataRecord(Suave.DataRecord calldata dataRecord) public {
emit DataRecordEvent(dataRecord.id, dataRecord.decryptionCondition, dataRecord.allowedPeekers);
}
Expand Down Expand Up @@ -62,7 +61,7 @@ contract EthBundleSenderContract is BundleContract {
}
}

contract MevShareBidContract is AnyBundleContract {
contract MevShareContract is AnyBundleContract {

event HintEvent(
Suave.DataId dataId,
Expand Down Expand Up @@ -121,7 +120,7 @@ contract MevShareBidContract is AnyBundleContract {
Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundles", matchBundleData);
Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundleSimResults", abi.encode(0));

//4. merge bids
//4. merge data records
Suave.DataId[] memory dataRecords = new Suave.DataId[](2);
dataRecords[0] = sharedataId;
dataRecords[1] = dataRecord.id;
Expand All @@ -138,7 +137,7 @@ contract MevShareBidContract is AnyBundleContract {
}
}

contract MevShareBundleSenderContract is MevShareBidContract {
contract MevShareBundleSenderContract is MevShareContract {
string[] public builderUrls;

constructor(string[] memory builderUrls_) {
Expand All @@ -151,17 +150,17 @@ contract MevShareBundleSenderContract is MevShareBidContract {
Suave.submitBundleJsonRPC(builderUrls[i], "mev_sendBundle", bundleData);
}

return MevShareBidContract.emitMatchDataRecordAndHint(dataRecord, matchHint);
return MevShareContract.emitMatchDataRecordAndHint(dataRecord, matchHint);
}
}

/* Not tested or implemented on the precompile side */
struct EgpBidPair {
struct EgpRecordPair {
uint64 egp; // in wei, beware overflow
Suave.DataId dataId;
}

contract EthBlockBidContract is AnyBundleContract {
contract EthBlockContract is AnyBundleContract {

event BuilderBoostBidEvent(
Suave.DataId dataId,
Expand All @@ -187,10 +186,10 @@ contract EthBlockBidContract is AnyBundleContract {
Suave.DataRecord[] memory allShareUserDataRecords = Suave.fetchDataRecords(blockHeight, "mevshare:v0:unmatchedBundles");

if (allShareUserDataRecords.length == 0) {
revert Suave.PeekerReverted(address(this), "no bids");
revert Suave.PeekerReverted(address(this), "no data records");
}

Suave.DataRecord[] memory allBids = new Suave.DataRecord[](allShareUserDataRecords.length);
Suave.DataRecord[] memory allRecords = new Suave.DataRecord[](allShareUserDataRecords.length);
for (uint i = 0; i < allShareUserDataRecords.length; i++) {
// TODO: sort matches by egp first!
Suave.DataRecord memory dataRecordToInsert = allShareUserDataRecords[i]; // will be updated with the best match if any
Expand All @@ -202,29 +201,29 @@ contract EthBlockBidContract is AnyBundleContract {
break;
}
}
allBids[i] = dataRecordToInsert;
allRecords[i] = dataRecordToInsert;
}

EgpBidPair[] memory bidsByEGP = new EgpBidPair[](allBids.length);
for (uint i = 0; i < allBids.length; i++) {
bytes memory simResults = Suave.confidentialRetrieve(allBids[i].id, "mevshare:v0:ethBundleSimResults");
EgpRecordPair[] memory bidsByEGP = new EgpRecordPair[](allRecords.length);
for (uint i = 0; i < allRecords.length; i++) {
bytes memory simResults = Suave.confidentialRetrieve(allRecords[i].id, "mevshare:v0:ethBundleSimResults");
uint64 egp = abi.decode(simResults, (uint64));
bidsByEGP[i] = EgpBidPair(egp, allBids[i].id);
bidsByEGP[i] = EgpRecordPair(egp, allRecords[i].id);
}

// Bubble sort, cause why not
uint n = bidsByEGP.length;
for (uint i = 0; i < n - 1; i++) {
for (uint j = i + 1; j < n; j++) {
if (bidsByEGP[i].egp < bidsByEGP[j].egp) {
EgpBidPair memory temp = bidsByEGP[i];
EgpRecordPair memory temp = bidsByEGP[i];
bidsByEGP[i] = bidsByEGP[j];
bidsByEGP[j] = temp;
}
}
}

Suave.DataId[] memory alldataIds = new Suave.DataId[](allBids.length);
Suave.DataId[] memory alldataIds = new Suave.DataId[](allRecords.length);
for (uint i = 0; i < bidsByEGP.length; i++) {
alldataIds[i] = bidsByEGP[i].dataId;
}
Expand All @@ -235,55 +234,55 @@ contract EthBlockBidContract is AnyBundleContract {
function buildFromPool(Suave.BuildBlockArgs memory blockArgs, uint64 blockHeight) public returns (bytes memory) {
require(Suave.isConfidential());

Suave.DataRecord[] memory allBids = Suave.fetchDataRecords(blockHeight, "default:v0:ethBundles");
if (allBids.length == 0) {
revert Suave.PeekerReverted(address(this), "no bids");
Suave.DataRecord[] memory allRecords = Suave.fetchDataRecords(blockHeight, "default:v0:ethBundles");
if (allRecords.length == 0) {
revert Suave.PeekerReverted(address(this), "no data records");
}

EgpBidPair[] memory bidsByEGP = new EgpBidPair[](allBids.length);
for (uint i = 0; i < allBids.length; i++) {
bytes memory simResults = Suave.confidentialRetrieve(allBids[i].id, "default:v0:ethBundleSimResults");
EgpRecordPair[] memory bidsByEGP = new EgpRecordPair[](allRecords.length);
for (uint i = 0; i < allRecords.length; i++) {
bytes memory simResults = Suave.confidentialRetrieve(allRecords[i].id, "default:v0:ethBundleSimResults");
uint64 egp = abi.decode(simResults, (uint64));
bidsByEGP[i] = EgpBidPair(egp, allBids[i].id);
bidsByEGP[i] = EgpRecordPair(egp, allRecords[i].id);
}

// Bubble sort, cause why not
uint n = bidsByEGP.length;
for (uint i = 0; i < n - 1; i++) {
for (uint j = i + 1; j < n; j++) {
if (bidsByEGP[i].egp < bidsByEGP[j].egp) {
EgpBidPair memory temp = bidsByEGP[i];
EgpRecordPair memory temp = bidsByEGP[i];
bidsByEGP[i] = bidsByEGP[j];
bidsByEGP[j] = temp;
}
}
}

Suave.DataId[] memory alldataIds = new Suave.DataId[](allBids.length);
Suave.DataId[] memory alldataIds = new Suave.DataId[](allRecords.length);
for (uint i = 0; i < bidsByEGP.length; i++) {
alldataIds[i] = bidsByEGP[i].dataId;
}

return buildAndEmit(blockArgs, blockHeight, alldataIds, "");
}

function buildAndEmit(Suave.BuildBlockArgs memory blockArgs, uint64 blockHeight, Suave.DataId[] memory bids, string memory namespace) public virtual returns (bytes memory) {
function buildAndEmit(Suave.BuildBlockArgs memory blockArgs, uint64 blockHeight, Suave.DataId[] memory records, string memory namespace) public virtual returns (bytes memory) {
require(Suave.isConfidential());

(Suave.DataRecord memory blockBid, bytes memory builderBid) = this.doBuild(blockArgs, blockHeight, bids, namespace);
(Suave.DataRecord memory blockBid, bytes memory builderBid) = this.doBuild(blockArgs, blockHeight, records, namespace);

emit BuilderBoostBidEvent(blockBid.id, builderBid);
emit DataRecordEvent(blockBid.id, blockBid.decryptionCondition, blockBid.allowedPeekers);
return bytes.concat(this.emitBuilderBidAndBid.selector, abi.encode(blockBid, builderBid));
}

function doBuild(Suave.BuildBlockArgs memory blockArgs, uint64 blockHeight, Suave.DataId[] memory bids, string memory namespace) public view returns (Suave.DataRecord memory, bytes memory) {
function doBuild(Suave.BuildBlockArgs memory blockArgs, uint64 blockHeight, Suave.DataId[] memory records, string memory namespace) public view returns (Suave.DataRecord memory, bytes memory) {
address[] memory allowedPeekers = new address[](2);
allowedPeekers[0] = address(this);
allowedPeekers[1] = Suave.BUILD_ETH_BLOCK;

Suave.DataRecord memory blockBid = Suave.newDataRecord(blockHeight, allowedPeekers, allowedPeekers, "default:v0:mergedDataRecords");
Suave.confidentialStore(blockBid.id, "default:v0:mergedDataRecords", abi.encode(bids));
Suave.confidentialStore(blockBid.id, "default:v0:mergedDataRecords", abi.encode(records));

(bytes memory builderBid, bytes memory payload) = Suave.buildEthBlock(blockArgs, blockBid.id, namespace);
Suave.confidentialStore(blockBid.id, "default:v0:builderPayload", payload); // only through this.unlock
Expand All @@ -307,7 +306,7 @@ contract EthBlockBidContract is AnyBundleContract {
}
}

contract EthBlockBidSenderContract is EthBlockBidContract {
contract EthBlockBidSenderContract is EthBlockContract {
string boostRelayUrl;

constructor(string memory boostRelayUrl_) {
Expand Down

0 comments on commit 958895e

Please sign in to comment.