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

feat: add eth_chain_id entrypoint #932

Merged
merged 6 commits into from
Sep 18, 2024

Conversation

bitfalt
Copy link
Contributor

@bitfalt bitfalt commented Sep 12, 2024

Add eth_chain_id entrypoint to the KakarotCore contract. Add a new constant to compute 2**53. Draft PR since replacing of all references is still needed, also want to ask feedback regarding implementation (correct types) to @enitrat.

Pull Request type

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?

Resolves: #923

What is the new behavior?

Add an entrypoint to the KakarotCore contract get the eth_chain_id from the transaction

  • Add a new constant in utils/src/constants.cairo for 2**53
  • Add eth_chain_id entrypoint

Does this introduce a breaking change?

  • Yes
  • No

This change is Reviewable

@bitfalt
Copy link
Contributor Author

bitfalt commented Sep 12, 2024

I've checked the codebase and found 3 places where we can call kakarot.eth_chain_id(). I have some questions regarding how to call the function in some places, and would appreciate any guidance or feedback towards my solution!

chain_id: get_tx_info().unbox().chain_id.try_into().unwrap(),

I have question regarding the code above, since it is doing chain_id: get_tx_info().unbox().chaind_id.try_into().unwrap() which is similar to what the implementation of the function I did.
However, I'd like to know if it would be viable to call eth_chain_id. If the refactor is necessary here, I'd like to know how to call the function, since I'm used to work with dispatchers, but I don't see any address and the only reference to KakarotCore I see is kakarot_state.

let chain_id: u64 = tx_info.chain_id.try_into().unwrap() % POW_2_32.try_into().unwrap();
assert(signature.len() == 5, 'EOA: Invalid signature length');
let signature = deserialize_signature(signature, chain_id)
.expect('EOA: invalid signature');
let mut encoded_tx_data = deserialize_bytes((*outside_execution.calls[0]).calldata)
.expect('conversion to Span<u8> failed')
.span();
let unsigned_transaction = TransactionUnsignedTrait::decode_enveloped(
ref encoded_tx_data
)
.expect('EOA: could not decode tx');
let address = self.Account_evm_address.read();
verify_eth_signature(unsigned_transaction.hash, signature, address);
let kakarot = IKakarotCoreDispatcher { contract_address: self.ownable.owner() };

The code above is easy to refactor, my solution would be move the line 259 (let kakarot = IKakarotCoreDispatcher { contract_address: self.ownable.owner() };) to be above line 243 (let chain_id: u64 = tx_info.chain_id.try_into().unwrap() % POW_2_32.try_into().unwrap();). Then just call the implemented function to set the chain_id. In this way: let chain_id: u64 = kakarot.eth_chain_id().

let kakarot_chain_id: u64 = get_tx_info()
.chain_id
.try_into()
.unwrap() % POW_2_32
.try_into()
.unwrap();

Regarding the code above, I have the same question as the first one I made. I'm thinking that we could call eth_chain_id function in the KakarotCore contract. However, I'm facing the same issue were this is no address, but in this case we have the parameter kakarot_state. However, I still don't really get how to call the function eth_chain_id implemented in KakarotCore without the dispatcher.

Any guidance toward the questions above is appreciated!

@enitrat
Copy link
Collaborator

enitrat commented Sep 13, 2024

regarding 1. yes you should call eth_chain_id

for 1. and 3. the unsafe_new_contract_state function gives you the ContractState object of the contract you call in on - in this case, KakarotCore::ContractState. This means that any functions defined with self: ContractState in the kakarot contract can be called through this object. Make sure you correctly import the IKakarotCore trait, in that defines functions callable on the KakarotCore::ContractState type.

example:

/// Populate an Environment with Starknet syscalls.
pub fn get_env(origin: EthAddress, gas_price: u128) -> Environment {
    let kakarot_state = KakarotCore::unsafe_new_contract_state();
    let kakarot_storage = kakarot_state.snapshot_deref().storage();
    // let chain_id = kakarot_state.eth_chain_id();
    let block_info = get_block_info().unbox();

    // tx.gas_price and env.gas_price have the same values here
    // - this is not always true in EVM transactions
    Environment {
        origin: origin,
        gas_price,
        chain_id: get_tx_info().unbox().chain_id.try_into().unwrap(),
        prevrandao: kakarot_storage.Kakarot_prev_randao.read(),
        block_number: block_info.block_number,
        block_gas_limit: constants::BLOCK_GAS_LIMIT,
        block_timestamp: block_info.block_timestamp,
        coinbase: kakarot_storage.Kakarot_coinbase.read(),
        base_fee: kakarot_storage.Kakarot_base_fee.read(),
        state: Default::default(),
    }
}

@enitrat
Copy link
Collaborator

enitrat commented Sep 13, 2024

@bitfalt can you move the entrypoint to the EthRPC trait to sync with changes made in #937 and rebase on main?

@bitfalt
Copy link
Contributor Author

bitfalt commented Sep 13, 2024

@enitrat sure! I'll modify the code accordingly.

@bitfalt bitfalt marked this pull request as ready for review September 14, 2024 08:05
Copy link
Collaborator

@enitrat enitrat left a comment

Choose a reason for hiding this comment

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

lgtm, just add a unit test in the same file :)

@bitfalt
Copy link
Contributor Author

bitfalt commented Sep 16, 2024

okayy, wouldn't it be better to have the test in crates/contracts/tests/test_kakarot_core.cairo along with all the other tests?

nvm, i just saw how you're doing the other tests, will do the same!

@enitrat
Copy link
Collaborator

enitrat commented Sep 16, 2024

yes, in the same module, under a #[cfg(test)] module, as we're unit testing

@enitrat
Copy link
Collaborator

enitrat commented Sep 17, 2024

@bitfalt there is a merge conflict; can you rebase?

@bitfalt
Copy link
Contributor Author

bitfalt commented Sep 17, 2024

Just rebased everything, I think I didn't mess up the rebase this time. I'm fairly new to doing rebase instead of merge, so it is a bit confusing to me haha. Let me know if there is anything else!

@enitrat enitrat added this pull request to the merge queue Sep 18, 2024
Merged via the queue into kkrt-labs:main with commit 6334f29 Sep 18, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add eth_chain_id entrypoint
2 participants