-
Notifications
You must be signed in to change notification settings - Fork 3
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
MohamadSafi
wants to merge
1
commit into
kkrt-labs:main
Choose a base branch
from
MohamadSafi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,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 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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this can work: it would call itself recursively. |
||
} | ||
|
||
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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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.