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

[Enhancement] Optimize CairoLib Functions by Overloading with calldata for Gas Efficiency #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
72 changes: 34 additions & 38 deletions src/CairoLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ library CairoLib {
/// @param functionSelector The function selector of the Cairo contract function to be called.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a general comment: you need to keep the memory version of the functions as most of the library is used inside a contract that would do some processing on the input data.
The code is in contradiction with your PR description as the memory version are removed

Backward Compatibility: The original functions with memory parameters are retained to prevent breaking existing integrations.

I think use cases need to be shown: this should to be done in sync with end-to-end tests in the kakarot repo to ensure the usage is relevant and functional. For now, all use case need preprocessing on input hence using calldata is not possible.
You can look at how this library is used in the following PRs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will look more into that.

/// @param data The input data for the Cairo contract function.
/// @return The return data from the Cairo contract function.
function callCairo(uint256 contractAddress, uint256 functionSelector, uint256[] memory data) internal returns (bytes memory) {
function callCairo(uint256 contractAddress, uint256 functionSelector, uint256[] calldata data) internal returns (bytes memory) {
bytes memory callData = abi.encode(contractAddress, functionSelector, data);

(bool success, bytes memory result) = CAIRO_CALL_PRECOMPILE.call(callData);
Expand All @@ -28,25 +28,20 @@ library CairoLib {
return result;
}

function callCairo(CairoCall memory call)
internal
returns (bytes memory)
{
function callCairo(CairoCall calldata call) internal returns (bytes memory) {
return callCairo(call.contractAddress, call.functionSelector, call.data);
}

function callCairo(uint256 contractAddress, uint256 functionSelector) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
return callCairo(contractAddress, functionSelector, data);
return callCairo(contractAddress, functionSelector);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this can work: it would call itself recursively.
You would need to pass the data as the actual function overloaded has a third parameter.

}

function callCairo(uint256 contractAddress, string memory functionName) internal returns (bytes memory) {
uint256[] memory data = new uint256[](0);
function callCairo(uint256 contractAddress, string calldata functionName) internal returns (bytes memory) {
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return callCairo(contractAddress, functionSelector, data);
return callCairo(contractAddress, functionSelector);
}

function callCairo(uint256 contractAddress, string memory functionName, uint256[] memory data) internal returns (bytes memory) {
function callCairo(uint256 contractAddress, string calldata functionName, uint256[] calldata data) internal returns (bytes memory) {
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return callCairo(contractAddress, functionSelector, data);
}
Expand All @@ -57,7 +52,7 @@ library CairoLib {
/// @param functionSelector The function selector of the Cairo contract function to be called.
/// @param data The input data for the Cairo contract function.
/// @return The return data from the Cairo contract function.
function staticcallCairo(uint256 contractAddress, uint256 functionSelector, uint256[] memory data) internal view returns (bytes memory) {
function staticcallCairo(uint256 contractAddress, uint256 functionSelector, uint256[] calldata data) internal view returns (bytes memory) {
bytes memory callData = abi.encode(contractAddress, functionSelector, data);

(bool success, bytes memory result) = CAIRO_CALL_PRECOMPILE.staticcall(callData);
Expand All @@ -66,42 +61,39 @@ library CairoLib {
return result;
}

function staticcallCairo(uint256 contractAddress, string memory functionName)
internal
view
returns (bytes memory)
{
uint256[] memory data = new uint256[](0);
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return staticcallCairo(contractAddress, functionSelector, data);
function staticcallCairo(CairoCall calldata call) internal view returns (bytes memory) {
return staticcallCairo(call.contractAddress, call.functionSelector, call.data);
}

function staticcallCairo(uint256 contractAddress, string memory functionName, uint256[] memory data)
internal
view
returns (bytes memory)
{
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return staticcallCairo(contractAddress, functionSelector, data);
function staticcallCairo(uint256 contractAddress, uint256 functionSelector) internal view returns (bytes memory) {

return staticcallCairo(contractAddress, functionSelector);
}

function staticcallCairo(CairoCall memory call)
internal
view
returns (bytes memory)
{
return staticcallCairo(call.contractAddress, call.functionSelector, call.data);
function staticcallCairo(uint256 contractAddress, string calldata functionName) internal view returns (bytes memory) {

uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return staticcallCairo(contractAddress, functionSelector);
}

function staticcallCairo(uint256 contractAddress, string calldata functionName, uint256[] calldata data) internal view returns (bytes memory) {
uint256 functionSelector = uint256(keccak256(bytes(functionName))) % 2 ** 250;
return staticcallCairo(contractAddress, functionSelector, data);
}

/// @notice Performs a multicall to Cairo contracts deployed on Starknet.
/// @dev Used with intent to modify the state of the Cairo contract.
/// @param calls The array of CairoCall structs to be executed.
function multicallCairo(CairoCall[] memory calls) internal {
function multicallCairo(CairoCall[] calldata calls) internal {
uint256 n_calls = calls.length;
bytes memory callData = abi.encode(n_calls);
for (uint32 i = 0; i < n_calls; i++) {
bytes memory encodedCall = abi.encode(calls[i].contractAddress, calls[i].functionSelector, calls[i].data);
CairoCall calldata call = calls[i];
bytes memory encodedCall = abi.encode(
call.contractAddress,
call.functionSelector,
call.data
);
callData = bytes.concat(callData, encodedCall);
}
(bool success,) = CAIRO_MULTICALL_PRECOMPILE.call(callData);
Expand All @@ -110,13 +102,17 @@ library CairoLib {

/// @notice Performs a multicall to Cairo contracts deployed on Starknet.
/// @dev Used with intent to read the state of the Cairo contract.
/// @dev **This can still mutate the underlying Cairo contract state.**
/// @param calls The array of CairoCall structs to be executed.
function multicallCairoStatic(CairoCall[] memory calls) internal view {
function multicallCairoStatic(CairoCall[] calldata calls) internal view {
uint256 n_calls = calls.length;
bytes memory callData = abi.encode(n_calls);
for (uint32 i = 0; i < n_calls; i++) {
bytes memory encodedCall = abi.encode(calls[i].contractAddress, calls[i].functionSelector, calls[i].data);
CairoCall calldata call = calls[i];
bytes memory encodedCall = abi.encode(
call.contractAddress,
call.functionSelector,
call.data
);
callData = bytes.concat(callData, encodedCall);
}
(bool success,) = CAIRO_MULTICALL_PRECOMPILE.staticcall(callData);
Expand Down