Skip to content

Commit

Permalink
Use call instead of delegate call in precompile calls (#178)
Browse files Browse the repository at this point in the history
* Use call instead of delegate call in precompile calls

* Apply for real

* Another fix

* Fix tests
  • Loading branch information
ferranbt authored Feb 5, 2024
1 parent fef4a56 commit 2c090a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
8 changes: 4 additions & 4 deletions suave/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ address public constant {{encodeAddrName .Name}} =
{{end}}
// Returns whether execution is off- or on-chain
function isConfidential() internal view returns (bool b) {
(bool success, bytes memory isConfidentialBytes) = IS_CONFIDENTIAL_ADDR.staticcall("");
function isConfidential() internal returns (bool b) {
(bool success, bytes memory isConfidentialBytes) = IS_CONFIDENTIAL_ADDR.call("");
if (!success) {
revert PeekerReverted(IS_CONFIDENTIAL_ADDR, isConfidentialBytes);
}
Expand All @@ -411,9 +411,9 @@ function isConfidential() internal view returns (bool b) {
}
{{range .Functions}}
function {{.Name}}({{range .Input}}{{styp .Typ}} {{.Name}}, {{end}}) internal view returns ({{range .Output.Fields}}{{styp .Typ}}, {{end}}) {
function {{.Name}}({{range .Input}}{{styp .Typ}} {{.Name}}, {{end}}) internal returns ({{range .Output.Fields}}{{styp .Typ}}, {{end}}) {
{{if .IsConfidential}}require(isConfidential());{{end}}
(bool success, bytes memory data) = {{encodeAddrName .Name}}.staticcall(abi.encode({{range .Input}}{{.Name}}, {{end}}));
(bool success, bytes memory data) = {{encodeAddrName .Name}}.call(abi.encode({{range .Input}}{{.Name}}, {{end}}));
if (!success) {
revert PeekerReverted({{encodeAddrName .Name}}, data);
}
Expand Down
72 changes: 32 additions & 40 deletions suave/sol/libraries/Suave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ library Suave {
address public constant SUBMIT_ETH_BLOCK_TO_RELAY = 0x0000000000000000000000000000000042100002;

// Returns whether execution is off- or on-chain
function isConfidential() internal view returns (bool b) {
(bool success, bytes memory isConfidentialBytes) = IS_CONFIDENTIAL_ADDR.staticcall("");
function isConfidential() internal returns (bool b) {
(bool success, bytes memory isConfidentialBytes) = IS_CONFIDENTIAL_ADDR.call("");
if (!success) {
revert PeekerReverted(IS_CONFIDENTIAL_ADDR, isConfidentialBytes);
}
Expand All @@ -112,91 +112,90 @@ library Suave {

function buildEthBlock(BuildBlockArgs memory blockArgs, DataId dataId, string memory namespace)
internal
view
returns (bytes memory, bytes memory)
{
(bool success, bytes memory data) = BUILD_ETH_BLOCK.staticcall(abi.encode(blockArgs, dataId, namespace));
(bool success, bytes memory data) = BUILD_ETH_BLOCK.call(abi.encode(blockArgs, dataId, namespace));
if (!success) {
revert PeekerReverted(BUILD_ETH_BLOCK, data);
}

return abi.decode(data, (bytes, bytes));
}

function confidentialInputs() internal view returns (bytes memory) {
(bool success, bytes memory data) = CONFIDENTIAL_INPUTS.staticcall(abi.encode());
function confidentialInputs() internal returns (bytes memory) {
(bool success, bytes memory data) = CONFIDENTIAL_INPUTS.call(abi.encode());
if (!success) {
revert PeekerReverted(CONFIDENTIAL_INPUTS, data);
}

return data;
}

function confidentialRetrieve(DataId dataId, string memory key) internal view returns (bytes memory) {
(bool success, bytes memory data) = CONFIDENTIAL_RETRIEVE.staticcall(abi.encode(dataId, key));
function confidentialRetrieve(DataId dataId, string memory key) internal returns (bytes memory) {
(bool success, bytes memory data) = CONFIDENTIAL_RETRIEVE.call(abi.encode(dataId, key));
if (!success) {
revert PeekerReverted(CONFIDENTIAL_RETRIEVE, data);
}

return data;
}

function confidentialStore(DataId dataId, string memory key, bytes memory value) internal view {
(bool success, bytes memory data) = CONFIDENTIAL_STORE.staticcall(abi.encode(dataId, key, value));
function confidentialStore(DataId dataId, string memory key, bytes memory value) internal {
(bool success, bytes memory data) = CONFIDENTIAL_STORE.call(abi.encode(dataId, key, value));
if (!success) {
revert PeekerReverted(CONFIDENTIAL_STORE, data);
}
}

function doHTTPRequest(HttpRequest memory request) internal view returns (bytes memory) {
(bool success, bytes memory data) = DO_HTTPREQUEST.staticcall(abi.encode(request));
function doHTTPRequest(HttpRequest memory request) internal returns (bytes memory) {
(bool success, bytes memory data) = DO_HTTPREQUEST.call(abi.encode(request));
if (!success) {
revert PeekerReverted(DO_HTTPREQUEST, data);
}

return abi.decode(data, (bytes));
}

function ethcall(address contractAddr, bytes memory input1) internal view returns (bytes memory) {
(bool success, bytes memory data) = ETHCALL.staticcall(abi.encode(contractAddr, input1));
function ethcall(address contractAddr, bytes memory input1) internal returns (bytes memory) {
(bool success, bytes memory data) = ETHCALL.call(abi.encode(contractAddr, input1));
if (!success) {
revert PeekerReverted(ETHCALL, data);
}

return abi.decode(data, (bytes));
}

function extractHint(bytes memory bundleData) internal view returns (bytes memory) {
function extractHint(bytes memory bundleData) internal returns (bytes memory) {
require(isConfidential());
(bool success, bytes memory data) = EXTRACT_HINT.staticcall(abi.encode(bundleData));
(bool success, bytes memory data) = EXTRACT_HINT.call(abi.encode(bundleData));
if (!success) {
revert PeekerReverted(EXTRACT_HINT, data);
}

return data;
}

function fetchDataRecords(uint64 cond, string memory namespace) internal view returns (DataRecord[] memory) {
(bool success, bytes memory data) = FETCH_DATA_RECORDS.staticcall(abi.encode(cond, namespace));
function fetchDataRecords(uint64 cond, string memory namespace) internal returns (DataRecord[] memory) {
(bool success, bytes memory data) = FETCH_DATA_RECORDS.call(abi.encode(cond, namespace));
if (!success) {
revert PeekerReverted(FETCH_DATA_RECORDS, data);
}

return abi.decode(data, (DataRecord[]));
}

function fillMevShareBundle(DataId dataId) internal view returns (bytes memory) {
function fillMevShareBundle(DataId dataId) internal returns (bytes memory) {
require(isConfidential());
(bool success, bytes memory data) = FILL_MEV_SHARE_BUNDLE.staticcall(abi.encode(dataId));
(bool success, bytes memory data) = FILL_MEV_SHARE_BUNDLE.call(abi.encode(dataId));
if (!success) {
revert PeekerReverted(FILL_MEV_SHARE_BUNDLE, data);
}

return data;
}

function newBuilder() internal view returns (string memory) {
(bool success, bytes memory data) = NEW_BUILDER.staticcall(abi.encode());
function newBuilder() internal returns (string memory) {
(bool success, bytes memory data) = NEW_BUILDER.call(abi.encode());
if (!success) {
revert PeekerReverted(NEW_BUILDER, data);
}
Expand All @@ -209,9 +208,9 @@ library Suave {
address[] memory allowedPeekers,
address[] memory allowedStores,
string memory dataType
) internal view returns (DataRecord memory) {
) internal returns (DataRecord memory) {
(bool success, bytes memory data) =
NEW_DATA_RECORD.staticcall(abi.encode(decryptionCondition, allowedPeekers, allowedStores, dataType));
NEW_DATA_RECORD.call(abi.encode(decryptionCondition, allowedPeekers, allowedStores, dataType));
if (!success) {
revert PeekerReverted(NEW_DATA_RECORD, data);
}
Expand All @@ -230,29 +229,28 @@ library Suave {

function signEthTransaction(bytes memory txn, string memory chainId, string memory signingKey)
internal
view
returns (bytes memory)
{
(bool success, bytes memory data) = SIGN_ETH_TRANSACTION.staticcall(abi.encode(txn, chainId, signingKey));
(bool success, bytes memory data) = SIGN_ETH_TRANSACTION.call(abi.encode(txn, chainId, signingKey));
if (!success) {
revert PeekerReverted(SIGN_ETH_TRANSACTION, data);
}

return abi.decode(data, (bytes));
}

function signMessage(bytes memory digest, string memory signingKey) internal view returns (bytes memory) {
function signMessage(bytes memory digest, string memory signingKey) internal returns (bytes memory) {
require(isConfidential());
(bool success, bytes memory data) = SIGN_MESSAGE.staticcall(abi.encode(digest, signingKey));
(bool success, bytes memory data) = SIGN_MESSAGE.call(abi.encode(digest, signingKey));
if (!success) {
revert PeekerReverted(SIGN_MESSAGE, data);
}

return abi.decode(data, (bytes));
}

function simulateBundle(bytes memory bundleData) internal view returns (uint64) {
(bool success, bytes memory data) = SIMULATE_BUNDLE.staticcall(abi.encode(bundleData));
function simulateBundle(bytes memory bundleData) internal returns (uint64) {
(bool success, bytes memory data) = SIMULATE_BUNDLE.call(abi.encode(bundleData));
if (!success) {
revert PeekerReverted(SIMULATE_BUNDLE, data);
}
Expand All @@ -262,10 +260,9 @@ library Suave {

function simulateTransaction(string memory sessionid, bytes memory txn)
internal
view
returns (SimulateTransactionResult memory)
{
(bool success, bytes memory data) = SIMULATE_TRANSACTION.staticcall(abi.encode(sessionid, txn));
(bool success, bytes memory data) = SIMULATE_TRANSACTION.call(abi.encode(sessionid, txn));
if (!success) {
revert PeekerReverted(SIMULATE_TRANSACTION, data);
}
Expand All @@ -275,25 +272,20 @@ library Suave {

function submitBundleJsonRPC(string memory url, string memory method, bytes memory params)
internal
view
returns (bytes memory)
{
require(isConfidential());
(bool success, bytes memory data) = SUBMIT_BUNDLE_JSON_RPC.staticcall(abi.encode(url, method, params));
(bool success, bytes memory data) = SUBMIT_BUNDLE_JSON_RPC.call(abi.encode(url, method, params));
if (!success) {
revert PeekerReverted(SUBMIT_BUNDLE_JSON_RPC, data);
}

return data;
}

function submitEthBlockToRelay(string memory relayUrl, bytes memory builderBid)
internal
view
returns (bytes memory)
{
function submitEthBlockToRelay(string memory relayUrl, bytes memory builderBid) internal returns (bytes memory) {
require(isConfidential());
(bool success, bytes memory data) = SUBMIT_ETH_BLOCK_TO_RELAY.staticcall(abi.encode(relayUrl, builderBid));
(bool success, bytes memory data) = SUBMIT_ETH_BLOCK_TO_RELAY.call(abi.encode(relayUrl, builderBid));
if (!success) {
revert PeekerReverted(SUBMIT_ETH_BLOCK_TO_RELAY, data);
}
Expand Down
4 changes: 2 additions & 2 deletions suave/sol/standard_peekers/bundles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ contract EthBlockContract is AnyBundleContract {
uint64 blockHeight,
Suave.DataId[] memory records,
string memory namespace
) public view returns (Suave.DataRecord memory, bytes memory) {
) public returns (Suave.DataRecord memory, bytes memory) {
address[] memory allowedPeekers = new address[](2);
allowedPeekers[0] = address(this);
allowedPeekers[1] = Suave.BUILD_ETH_BLOCK;
Expand All @@ -331,7 +331,7 @@ contract EthBlockContract is AnyBundleContract {
return (dataRecord, builderBid);
}

function unlock(Suave.DataId dataId, bytes memory signedBlindedHeader) public view returns (bytes memory) {
function unlock(Suave.DataId dataId, bytes memory signedBlindedHeader) public returns (bytes memory) {
require(Suave.isConfidential());

// TODO: verify the header is correct
Expand Down

0 comments on commit 2c090a1

Please sign in to comment.