Skip to content

Commit

Permalink
Clean code (kkrt-labs#224)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

## Pull request type

<!-- Please try to limit your pull request to one type, submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [x] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: N/A

## What is the new behavior?

- Clean unused implicit args
- refacto swap
- remove unused imports
- cairo lint rule
## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
0xLucqs authored Nov 5, 2022
1 parent 4cb7008 commit 2959f2f
Show file tree
Hide file tree
Showing 22 changed files with 328 additions and 435 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ package.json
node_modules
package-lock.json
tsconfig.json

lint.sarif
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
.PHONY: build test coverage

build:
$(MAKE) clean
poetry run starknet-compile ./src/kakarot/kakarot.cairo --output build/kakarot.json --disable_hint_validation --cairo_path ./src --abi build/kakarot_abi.json
poetry run starknet-compile ./src/kakarot/accounts/contract/contract_account.cairo --output build/contract_account.json --disable_hint_validation --cairo_path ./src --abi build/contract_account_abi.json
poetry run starknet-compile ./src/kakarot/accounts/eoa/externally_owned_account.cairo --output build/externally_owned_account.json --disable_hint_validation --cairo_path ./src --abi build/externally_owned_account_abi.json
poetry run starknet-compile ./src/kakarot/accounts/registry/account_registry.cairo --output build/account_registry.json --disable_hint_validation --cairo_path ./src --abi build/account_registry_abi.json

build-mac:
$(MAKE) clean
starknet-compile ./src/kakarot/kakarot.cairo --output build/kakarot.json --disable_hint_validation --cairo_path ./src --abi build/kakarot_abi.json
starknet-compile ./src/kakarot/accounts/contract/contract_account.cairo --output build/contract_account.json --disable_hint_validation --cairo_path ./src --abi build/contract_account_abi.json
starknet-compile ./src/kakarot/accounts/eoa/externally_owned_account.cairo --output build/externally_owned_account.json --disable_hint_validation --cairo_path ./src --abi build/externally_owned_account_abi.json
starknet-compile ./src/kakarot/accounts/registry/account_registry.cairo --output build/account_registry.json --disable_hint_validation --cairo_path ./src --abi build/account_registry_abi.json


setup:
poetry install --no-root

Expand Down Expand Up @@ -36,8 +44,14 @@ clean:
rm -rf build
mkdir build

run-test:
lint:
amarna ./src/kakarot -o lint.sarif -rules unused-imports,dead-store,unknown-decorator,unused-arguments

run-test-log:
poetry run pytest tests/test_zk_evm.py::TestZkEVM -k $(test) -s --log-cli-level=INFO

run-test:
poetry run pytest -s tests/test_zk_evm.py::TestZkEVM -k $(test)

format-mac:
cairo-format src/**/*.cairo -i
Expand Down
51 changes: 41 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.9"
cairo-lang = "^0.10.1"
openzeppelin-cairo-contracts = "0.4.0"
openzeppelin-cairo-contracts = "0.5.0"
amarna = "^0.1.5"

[tool.poetry.group.dev.dependencies]
cairo-coverage = "^0.1.0"
Expand Down
56 changes: 14 additions & 42 deletions src/kakarot/execution_context.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ from starkware.cairo.common.bool import TRUE, FALSE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math import assert_le, assert_nn
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.uint256 import Uint256

// Internal dependencies
from utils.utils import Helpers
from kakarot.model import model
from kakarot.memory import Memory
from kakarot.stack import Stack
from kakarot.constants import Constants
from kakarot.constants import native_token_address, registry_address
from kakarot.interfaces.interfaces import IEth, IRegistry, IEvm_Contract
from kakarot.constants import registry_address
from kakarot.interfaces.interfaces import IRegistry, IEvm_Contract

// @title ExecutionContext related functions.
// @notice This file contains functions related to the execution context.
Expand All @@ -29,12 +28,7 @@ namespace ExecutionContext {
// @param code The code to execute.
// @param calldata The calldata.
// @return The initialized execution context.
func init{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(code: felt*, code_len: felt, calldata: felt*) -> model.ExecutionContext* {
func init(code: felt*, code_len: felt, calldata: felt*) -> model.ExecutionContext* {
alloc_locals;
let (empty_return_data: felt*) = alloc();
Expand Down Expand Up @@ -104,7 +98,7 @@ namespace ExecutionContext {

let (code_len, code) = IEvm_Contract.code(contract_address=starknet_address);

local ctx: model.ExecutionContext* = new model.ExecutionContext(
return new model.ExecutionContext(
code=code,
code_len=code_len,
calldata=calldata,
Expand All @@ -121,7 +115,6 @@ namespace ExecutionContext {
starknet_address=starknet_address,
evm_address=address,
);
return ctx;
}

// @notice Compute the intrinsic gas cost of the current transaction.
Expand Down Expand Up @@ -162,12 +155,7 @@ namespace ExecutionContext {
// @dev When the execution context is stopped, no more instructions can be executed.
// @param self The pointer to the execution context.
// @return The pointer to the updated execution context.
func stop{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(self: model.ExecutionContext*) -> model.ExecutionContext* {
func stop(self: model.ExecutionContext*) -> model.ExecutionContext* {
return new model.ExecutionContext(
code=self.code,
code_len=self.code_len,
Expand Down Expand Up @@ -199,7 +187,7 @@ namespace ExecutionContext {
alloc_locals;
// Get current pc value
let pc = self.program_counter;
let (local output: felt*) = alloc();
let (output: felt*) = alloc();
// Copy code slice
memcpy(dst=output, src=self.code + pc, len=len);
// Move program counter
Expand All @@ -211,12 +199,9 @@ namespace ExecutionContext {
// @dev The stack is updated with the given stack.
// @param self The pointer to the execution context.
// @param stack The pointer to the new stack.
func update_stack{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(self: model.ExecutionContext*, new_stack: model.Stack*) -> model.ExecutionContext* {
func update_stack(
self: model.ExecutionContext*, new_stack: model.Stack*
) -> model.ExecutionContext* {
return new model.ExecutionContext(
code=self.code,
code_len=self.code_len,
Expand All @@ -240,12 +225,9 @@ namespace ExecutionContext {
// @dev The memory is updated with the given memory.
// @param self The pointer to the execution context.
// @param memory The pointer to the new memory.
func update_memory{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(self: model.ExecutionContext*, new_memory: model.Memory*) -> model.ExecutionContext* {
func update_memory(
self: model.ExecutionContext*, new_memory: model.Memory*
) -> model.ExecutionContext* {
return new model.ExecutionContext(
code=self.code,
code_len=self.code_len,
Expand All @@ -269,12 +251,7 @@ namespace ExecutionContext {
// @dev The memory is updated with the given memory.
// @param self The pointer to the execution context.
// @param memory The pointer to the new memory.
func update_return_data{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(
func update_return_data(
self: model.ExecutionContext*, new_return_data_len: felt, new_return_data: felt*
) -> model.ExecutionContext* {
return new model.ExecutionContext(
Expand Down Expand Up @@ -352,12 +329,7 @@ namespace ExecutionContext {

// @notice Dump the current execution context.
// @dev The execution context is dumped to the debug server if `DEBUG` environment variable is set to `True`.
func dump{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(self: model.ExecutionContext*) {
func dump{range_check_ptr}(self: model.ExecutionContext*) {
let pc = self.program_counter;
let stopped = is_stopped(self);
%{
Expand Down
17 changes: 4 additions & 13 deletions src/kakarot/instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,7 @@ namespace EVMInstructions {
// @param instructions the instruction set
// @param opcode The opcode value
// @param function the function to execute for the specified opcode
func add_instruction{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(instructions: felt*, opcode: felt, function: codeoffset) {
func add_instruction(instructions: felt*, opcode: felt, function: codeoffset) {
assert [instructions + opcode] = cast(function, felt);
return ();
}
Expand All @@ -141,7 +136,8 @@ namespace EVMInstructions {
import logging
logging.info("0x00 - STOP")
%}
return ExecutionContext.stop(ctx_ptr);
let ctx = ExecutionContext.stop(ctx_ptr);
return ctx;
}

// @notice Prepare arguments for the dynamic call.
Expand All @@ -165,12 +161,7 @@ namespace EVMInstructions {

// @notice Generate the instructions set for the EVM.
// @return The instructions set.
func generate_instructions{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}() -> felt* {
func generate_instructions() -> felt* {
alloc_locals;
// Init instructions
let (instructions: felt*) = alloc();
Expand Down
Loading

0 comments on commit 2959f2f

Please sign in to comment.