Skip to content

Commit

Permalink
feat: env information as storage vars (#1009)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR: 0.1d

## 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)
- [ ] 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. -->

Resolves #<Issue number>

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1009)
<!-- Reviewable:end -->
  • Loading branch information
enitrat authored Mar 5, 2024
1 parent 08c3268 commit 99a4972
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 12 deletions.
3 changes: 1 addition & 2 deletions blockchain-tests-skip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ filename:
# List of specific tests names to be skipped with folder
testname:
vmTests:
- blockInfo_d1g0v0_Shanghai
- blockInfo_d2g0v0_Shanghai
- sha3_d3g0v0_Shanghai
vmArithmeticTest:
- expPower256Of256_d0g0v0_Shanghai
Expand Down Expand Up @@ -224,6 +222,7 @@ testname:
- randomStatetest605_d0g0v0_Shanghai
- randomStatetest650_d0g0v0_Shanghai
stRandom:
- randomStatetest116_d0g0v0_Shanghai
- randomStatetest189_d0g0v0_Shanghai
- randomStatetest100_d0g0v0_Shanghai
- randomStatetest138_d0g0v0_Shanghai
Expand Down
2 changes: 2 additions & 0 deletions scripts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
logger.setLevel(logging.INFO)
load_dotenv()

# Hardcode block gas limit to 20M
BLOCK_GAS_LIMIT = 20_000_000

NETWORKS = {
"mainnet": {
Expand Down
3 changes: 3 additions & 0 deletions scripts/deploy_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from asyncio import run

from scripts.constants import (
BLOCK_GAS_LIMIT,
DECLARED_CONTRACTS,
ETH_TOKEN_ADDRESS,
EVM_ADDRESS,
Expand Down Expand Up @@ -61,6 +62,7 @@ async def main():
], # externally_owned_account_class_hash
class_hash["proxy"], # account_proxy_class_hash
class_hash["Precompiles"],
BLOCK_GAS_LIMIT,
)

if NETWORK["devnet"]:
Expand All @@ -70,6 +72,7 @@ async def main():
class_hash["contract_account"], # contract_account_class_hash_
class_hash["proxy"], # account_proxy_class_hash
class_hash["Precompiles"],
BLOCK_GAS_LIMIT,
)

dump_deployments(deployments)
Expand Down
12 changes: 9 additions & 3 deletions src/backend/starknet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ from kakarot.storages import (
evm_to_starknet_address,
coinbase,
base_fee,
block_gas_limit,
prev_randao,
)

namespace Starknet {
Expand Down Expand Up @@ -108,18 +110,22 @@ namespace Starknet {
let (tx_info) = get_tx_info();
let (coinbase_) = coinbase.read();
let (base_fee_) = base_fee.read();
let (block_gas_limit_) = block_gas_limit.read();
let (prev_randao_) = prev_randao.read();
let (block_hashes) = alloc();
// TODO: fix how blockhashes are retrieved
memset(block_hashes, 0, 256 * 2);

// TODO: fix hardcoded coinbase evm address
// No idea why this is required - but trying to pass prev_randao_ directly causes bugs.
let prev_randao_ = Uint256(low=prev_randao_.low, high=prev_randao_.high);

return new model.Environment(
origin=origin,
gas_price=gas_price,
chain_id=tx_info.chain_id,
prev_randao=Uint256(0, 0),
prev_randao=prev_randao_,
block_number=block_number,
block_gas_limit=Constants.BLOCK_GAS_LIMIT,
block_gas_limit=block_gas_limit_,
block_timestamp=block_timestamp,
block_hashes=cast(block_hashes, Uint256*),
coinbase=coinbase_,
Expand Down
4 changes: 0 additions & 4 deletions src/kakarot/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ from kakarot.gas import Gas
// @title Constants file.
// @notice This file contains global constants.
namespace Constants {
// BLOCK
// Hardcode block gas limit to 20M
const BLOCK_GAS_LIMIT = 20000000;

// STACK
const STACK_MAX_DEPTH = 1024;

Expand Down
38 changes: 38 additions & 0 deletions src/kakarot/kakarot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr
externally_owned_account_class_hash: felt,
account_proxy_class_hash: felt,
precompiles_class_hash: felt,
block_gas_limit: felt,
) {
return Kakarot.constructor(
owner,
Expand All @@ -37,6 +38,7 @@ func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr
externally_owned_account_class_hash,
account_proxy_class_hash,
precompiles_class_hash,
block_gas_limit,
);
}

Expand Down Expand Up @@ -109,6 +111,42 @@ func get_coinbase{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_pt
return Kakarot.get_coinbase();
}

// @notice Sets the prev randao
// @param prev_randao_ The new prev randao.
@external
func set_prev_randao{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
prev_randao_: Uint256
) {
return Kakarot.set_prev_randao(prev_randao_);
}

// @notice Get the prev randao.
// @return prev_randao The current prev randao.
@view
func get_prev_randao{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
prev_randao: Uint256
) {
return Kakarot.get_prev_randao();
}

// @notice Sets the block gas limit.
// @param gas_limit_ The new block gas limit.
@external
func set_block_gas_limit{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
gas_limit_: felt
) {
return Kakarot.set_block_gas_limit(gas_limit_);
}

// @notice Get the block gas limit.
// @return gas_limit The current block gas limit.
@view
func get_block_gas_limit{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
block_gas_limit: felt
) {
return Kakarot.get_block_gas_limit();
}

// @notice Compute the starknet address of a contract given its EVM address
// @param evm_address The EVM address of the contract
// @return contract_address The starknet address of the contract
Expand Down
42 changes: 42 additions & 0 deletions src/kakarot/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ from kakarot.storages import (
native_token_address,
precompiles_class_hash,
coinbase,
prev_randao,
block_gas_limit,
)
from kakarot.interpreter import Interpreter
from kakarot.instructions.system_operations import CreateHelper
Expand All @@ -43,6 +45,7 @@ namespace Kakarot {
externally_owned_account_class_hash_,
account_proxy_class_hash_,
precompiles_class_hash_,
block_gas_limit_,
) {
Ownable.initializer(owner);
native_token_address.write(native_token_address_);
Expand All @@ -51,6 +54,7 @@ namespace Kakarot {
account_proxy_class_hash.write(account_proxy_class_hash_);
precompiles_class_hash.write(precompiles_class_hash_);
coinbase.write(0xCA40796aFB5472abaeD28907D5ED6FC74c04954a);
block_gas_limit.write(block_gas_limit_);
return ();
}

Expand Down Expand Up @@ -172,6 +176,44 @@ namespace Kakarot {
return (coinbase_,);
}

// @notice Set the prev_randao.
// @param prev_randao_ The new prev_randao.
func set_prev_randao{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
prev_randao_: Uint256
) {
Ownable.assert_only_owner();
prev_randao.write(prev_randao_);
return ();
}

// @notice Get the prev_randao.
// @return prev_randao The current prev_randao.
func get_prev_randao{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
prev_randao: Uint256
) {
let (prev_randao_) = prev_randao.read();
return (prev_randao_,);
}

// @notice Set the block gas limit.
// @param block_gas_limit_ The new block gas limit.
func set_block_gas_limit{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
block_gas_limit_: felt
) {
Ownable.assert_only_owner();
block_gas_limit.write(block_gas_limit_);
return ();
}

// @notice Get the block gas limit.
// @return block_gas_limit The current block gas limit.
func get_block_gas_limit{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
block_gas_limit: felt
) {
let (block_gas_limit_) = block_gas_limit.read();
return (block_gas_limit_,);
}

// @notice Deploy a new externally owned account.
// @param evm_contract_address The evm address that is mapped to the newly deployed starknet contract address.
// @return starknet_contract_address The newly deployed starknet contract address.
Expand Down
10 changes: 10 additions & 0 deletions src/kakarot/storages.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

%lang starknet

from starkware.cairo.common.uint256 import Uint256

@storage_var
func precompiles_class_hash() -> (res: felt) {
}
Expand Down Expand Up @@ -33,3 +35,11 @@ func coinbase() -> (res: felt) {
@storage_var
func base_fee() -> (res: felt) {
}

@storage_var
func prev_randao() -> (res: Uint256) {
}

@storage_var
func block_gas_limit() -> (res: felt) {
}
1 change: 0 additions & 1 deletion tests/end_to_end/test_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ async def test_execute(
calldata=hex_string_to_bytes_array(params["calldata"]),
access_list=[],
)

assert result.success == params["success"]
assert result.stack_values[: result.stack_size] == (
[
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/EVM.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ from kakarot.storages import (
account_proxy_class_hash,
precompiles_class_hash,
coinbase,
block_gas_limit,
)
from backend.starknet import Starknet, Internals as StarknetInternals
from utils.dict import dict_keys, dict_values
Expand All @@ -35,12 +36,14 @@ func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr
contract_account_class_hash_: felt,
account_proxy_class_hash_: felt,
precompiles_class_hash_: felt,
block_gas_limit_: felt,
) {
native_token_address.write(native_token_address_);
contract_account_class_hash.write(contract_account_class_hash_);
account_proxy_class_hash.write(account_proxy_class_hash_);
precompiles_class_hash.write(precompiles_class_hash_);
coinbase.write(0xCA40796aFB5472abaeD28907D5ED6FC74c04954a);
block_gas_limit.write(block_gas_limit_);
return ();
}

Expand Down
1 change: 1 addition & 0 deletions tests/src/kakarot/instructions/test_block_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestBlockInformation:
],
)
@SyscallHandler.patch("coinbase", 0xCA40796AFB5472ABAED28907D5ED6FC74C04954A)
@SyscallHandler.patch("block_gas_limit", BLOCK_GAS_LIMIT)
@patch.object(SyscallHandler, "block_timestamp", 0x1234)
def test__exec_block_information(self, cairo_run, opcode, expected_result):
output = cairo_run("test__exec_block_information", opcode=opcode)
Expand Down
6 changes: 4 additions & 2 deletions tests/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from enum import IntEnum
from time import time

from scripts.constants import BLOCK_GAS_LIMIT

BLOCK_GAS_LIMIT = BLOCK_GAS_LIMIT

CHAIN_ID = int.from_bytes(b"KKRT", "big") # KKRT (0x4b4b5254) in ASCII

# Amount of funds to pre-fund the account with
Expand All @@ -13,8 +17,6 @@
MOCK_COINBASE_ADDRESS = (
0x388CA486B82E20CC81965D056B4CDCAACDFFE0CF08E20ED8BA10EA97A487004
)
# Hardcode block gas limit to 20M
BLOCK_GAS_LIMIT = 20_000_000

# STACK
STACK_MAX_DEPTH = 1024
Expand Down

0 comments on commit 99a4972

Please sign in to comment.