Skip to content

Commit

Permalink
add code doc and fix return code in precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 23, 2024
1 parent f7829db commit 9353a8d
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/kakarot/precompiles/precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,30 @@ namespace Precompiles {
}

// @notice A placeholder for attempts to call a precompile without permissions
// @dev Halts execution.
// @param evm_address The evm_address.
// @param input_len The length of the input array.
// @param input The input array.
// @dev Halts execution with an unauthorized precompile error.
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func unauthorized_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}() -> (output_len: felt, output: felt*, gas_used: felt, reverted: felt) {
let (revert_reason_len, revert_reason) = Errors.unauthorizedPrecompile();
return (revert_reason_len, revert_reason, 0, Errors.REVERT);
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}

// @notice A placeholder for precompile that don't exist.
// @dev Halts execution.
// @param evm_address The evm_address.
// @param input_len The length of the input array.
// @param input The input array.
// @notice A placeholder for precompiles that don't exist.
// @dev Halts execution with an unknown precompile error.
// @param evm_address The address of the unknown precompile.
// @param input_len The length of the input array (unused).
// @param input The input array (unused).
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func unknown_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -192,11 +197,15 @@ namespace Precompiles {
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}

// @notice A placeholder for precompile that are not implemented yet.
// @dev Halts execution.
// @param evm_address The evm_address.
// @param input_len The length of the input array.
// @param input The input array.
// @notice A placeholder for precompiles that are not implemented yet.
// @dev Halts execution with a not implemented precompile error.
// @param evm_address The address of the not implemented precompile.
// @param input_len The length of the input array (unused).
// @param input The input array (unused).
// @return output_len The length of the error message.
// @return output The error message.
// @return gas_used The gas used (always 0 for this function).
// @return reverted The reverted code (EXCEPTIONAL_HALT).
func not_implemented_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -209,6 +218,15 @@ namespace Precompiles {
return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT);
}

// @notice Executes an external precompile using a Cairo 1 helper contract.
// @dev Calls the library_call_exec_precompile function of the ICairo1Helpers interface.
// @param evm_address The address of the external precompile.
// @param input_len The length of the input array.
// @param input The input array.
// @return output_len The length of the output data.
// @return output The output data.
// @return gas_used The gas used by the precompile execution.
// @return reverted 0 if successful, EXCEPTIONAL_HALT if execution failed.
func external_precompile{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
Expand All @@ -229,6 +247,10 @@ namespace Precompiles {
class_hash=implementation, address=evm_address, data_len=input_len, data=input
);
let gas = gas_if_success * success;
return (return_data_len, return_data, gas, 1 - success);
if (success != FALSE) {
return (return_data_len, return_data, gas, 0);
}
// Precompiles can only revert with exceptions. Thus if the execution failed, it's an error EXCEPTIONAL_HALT.
return (return_data_len, return_data, gas, Errors.EXCEPTIONAL_HALT);
}
}

0 comments on commit 9353a8d

Please sign in to comment.