suapp-examples icon indicating copy to clipboard operation
suapp-examples copied to clipboard

Fix Private OFA

Open dmarzzz opened this issue 1 year ago • 1 comments

I believe the private OFA example is messed up because we're not submitting the backrun on newMatch but instead when we call a separate function. Also passing the builderURL in from outside world means you can point it to a non-TEE builder and leak the data.

dmarzzz avatar Aug 05 '24 16:08 dmarzzz

"I don't quite understand this case either. To print some function information from the contract call on the console, I modified the corresponding smart contract." “// SPDX-License-Identifier: UNLICENSED // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.8;

import "../lib/suave-std/src/suavelib/Suave.sol"; import "../lib/suave-std/src/Context.sol"; import "../lib/suave-std/src/Suapp.sol";

contract OFAPrivate is Suapp { // Struct to hold hint-related information for an order. struct HintOrder { Suave.DataId id; bytes hint; }

event HintEvent(Suave.DataId id, bytes hint, uint64 decryptionCondition);

event BundleEmitted(string bundleRawResponse);

event NewOrder(uint64 decryptionCondition);

event NewMatch(Suave.DataId shareDataRecordId, uint64 decryptionCondition);

event EmitMatchDataRecordAndHint(string builderUrl, Suave.DataId dataRecordId, bytes bundleData, bytes response);

// Internal function to save order details and generate a hint.
function saveOrder(uint64 decryptionCondition) internal returns (HintOrder memory) {
    // Retrieve the bundle data from the confidential inputs
    bytes memory bundleData = Context.confidentialInputs();

    // Simulate the bundle and extract its score.
    uint64 egp = Suave.simulateBundle(bundleData);

    // Extract a hint about this bundle that is going to be leaked
    // to external applications.
    bytes memory hint = Suave.extractHint(bundleData);

    address[] memory allowedList = new address[](2);
    allowedList[0] = address(this);
    // Precompiled function fetchDataRecords function address, retrieves all data records related to the specified decryption condition and namespace
    allowedList[1] = 0x0000000000000000000000000000000043200001;

    // Store the bundle and the simulation results in the confidential datastore.
    Suave.DataRecord memory dataRecord = Suave.newDataRecord(decryptionCondition, allowedList, allowedList, "");
    Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundles", bundleData);
    Suave.confidentialStore(dataRecord.id, "mevshare:v0:ethBundleSimResults", abi.encode(egp));

    HintOrder memory hintOrder;
    hintOrder.id = dataRecord.id;
    hintOrder.hint = hint;

    return hintOrder;
}

function emitHint(HintOrder memory order, uint64 decryptionCondition) public {
    emit HintEvent(order.id, order.hint, decryptionCondition);
}

// Function to create a new user order
function newOrder(uint64 decryptionCondition) external returns (bytes memory) {
    HintOrder memory hintOrder = saveOrder(decryptionCondition);
    // Logic written in kettle
    emit NewOrder(decryptionCondition);
    return abi.encodeWithSelector(this.emitHint.selector, hintOrder, decryptionCondition);
}

// Function to match and backrun another dataRecord.
function newMatch(Suave.DataId shareDataRecordId, uint64 decryptionCondition) external returns (bytes memory) {
    HintOrder memory hintOrder = saveOrder(decryptionCondition);

    // Merge the dataRecords and store them in the confidential datastore.
    // The 'fillMevShareBundle' precompile will use this information to send the bundles.
    Suave.DataId[] memory dataRecords = new Suave.DataId[](2);
    dataRecords[0] = shareDataRecordId;
    dataRecords[1] = hintOrder.id;
    Suave.confidentialStore(hintOrder.id, "mevshare:v0:mergedDataRecords", abi.encode(dataRecords));
   // Added log
    emit NewMatch(shareDataRecordId, decryptionCondition);

    return abi.encodeWithSelector(this.emitHint.selector, hintOrder, decryptionCondition);
}

function emitMatchDataRecordAndHintCallback(string memory bundleRawResponse) external {
    emit BundleEmitted(bundleRawResponse);
}

function emitMatchDataRecordAndHint(string memory builderUrl, Suave.DataId dataRecordId)
    external
    returns (bytes memory)
{
    bytes memory bundleData = Suave.fillMevShareBundle(dataRecordId);
    bytes memory response = submitBundle(builderUrl, bundleData);
  Added log

emit EmitMatchDataRecordAndHint(builderUrl, dataRecordId, bundleData, response); return abi.encodeWithSelector(this.emitMatchDataRecordAndHintCallback.selector, response); } }

", however when I execute the main.go file, no log is displayed at all.

wzl521 avatar Oct 11 '24 05:10 wzl521