Skip to content

Commit

Permalink
Fix rlp encode unsigned for legacy tx (#1462)
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:

## 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:

- [x] 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?

Data is not parsed and can lead to unexpected behavior.

## What is the new behavior?

Added some basic parsing, I wanted initially to define proper models and
use execution specs models, but it appears to be
overkill for now.

<!-- 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/1462)
<!-- Reviewable:end -->

---------

Co-authored-by: Oba <obatirou@gmail.com>
  • Loading branch information
ClementWalter and obatirou authored Oct 3, 2024
1 parent 886d039 commit 8ca25c1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 49 deletions.
2 changes: 1 addition & 1 deletion kakarot_scripts/deploy_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ async def main():
# %% Starknet Deployments
class_hash = get_declarations()
starknet_deployments = get_starknet_deployments()
evm_deployments = get_evm_deployments()

if NETWORK["type"] is not NetworkType.PROD:
starknet_deployments["EVM"] = await deploy_starknet(
Expand Down Expand Up @@ -125,6 +124,7 @@ async def main():
dump_deployments(starknet_deployments)

# %% Pre-EIP155 deployments
evm_deployments = get_evm_deployments()
evm_deployments["Multicall3"] = await deploy_with_presigned_tx(
MULTICALL3_DEPLOYER,
MULTICALL3_SIGNED_TX,
Expand Down
39 changes: 3 additions & 36 deletions kakarot_scripts/utils/kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union, cast

import rlp
import uvloop
from async_lru import alru_cache
from eth_abi import decode
from eth_abi.exceptions import InsufficientDataBytes
Expand Down Expand Up @@ -178,32 +179,8 @@ async def get_contract(
return contract


def get_contract_sync(
contract_app: str,
contract_name: str,
address=None,
caller_eoa: Optional[Account] = None,
) -> Web3Contract:

artifacts = get_solidity_artifacts(contract_app, contract_name)

contract = cast(
Web3Contract,
WEB3.eth.contract(
address=to_checksum_address(address) if address is not None else address,
abi=artifacts["abi"],
bytecode=artifacts["bytecode"]["object"],
),
)
contract.bytecode_runtime = HexBytes(artifacts["bytecode_runtime"]["object"])

try:
for fun in contract.functions:
setattr(contract, fun, MethodType(_wrap_kakarot(fun, caller_eoa), contract))
except NoABIFunctionsFound:
pass
contract.events.parse_events = MethodType(_parse_events, contract.events)
return contract
def get_contract_sync(*args, **kwargs) -> Web3Contract:
return uvloop.run(get_contract(*args, **kwargs))


@alru_cache()
Expand Down Expand Up @@ -303,16 +280,6 @@ async def deploy(
return contract


async def deploy_details(
contract_app: str, contract_name: str, *args, **kwargs
) -> Web3Contract:
contract = await deploy(contract_app, contract_name, *args, **kwargs)
return {
"address": int(contract.address, 16),
"starknet_address": contract.starknet_address,
}


def dump_deployments(deployments):
json.dump(
{
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ dev-dependencies = [
"openzeppelin-cairo-contracts>=0.6.1",
"pandas>=1.5.1",
"py-ecc>=7.0.0",
"pycryptodome>=3.16.0",
"pyperclip>=1.8.2",
"pysha3>=1.0.2",
"pytest-asyncio==0.21.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/src/kakarot/accounts/test_account_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_should_raise_when_read_bytecode_zellic_issue_1279(
),
):
with cairo_error(message="Value is not empty"):
output_len, output = cairo_run("test__bytecode")
cairo_run("test__bytecode")

class TestNonce:
@SyscallHandler.patch("Ownable_owner", 0xDEAD)
Expand Down
35 changes: 27 additions & 8 deletions tests/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,26 @@
)


def rlp_encode_signed_data(tx: dict) -> bytes:
def to_int(v: Union[str, int]) -> int:
if isinstance(v, str):
if v.startswith("0x"):
return int(v, 16)
return int(v)
return v


def to_bytes(v: Union[str, bytes, list[int]]) -> bytes:
if isinstance(v, bytes):
return v
elif isinstance(v, str):
if v.startswith("0x"):
return bytes.fromhex(v[2:])
return v.encode()
else:
return bytes(v)


def rlp_encode_signed_data(tx: dict):
if "type" in tx:
typed_transaction = TypedTransaction.from_dict(tx)

Expand All @@ -38,13 +57,13 @@ def rlp_encode_signed_data(tx: dict) -> bytes:
]
else:
legacy_tx = [
tx["nonce"],
tx["gasPrice"],
tx["gas"] if "gas" in tx else tx["gasLimit"],
bytes.fromhex(f"{int(tx['to'], 16):040x}"),
tx["value"],
tx["data"],
] + ([tx["chainId"], 0, 0] if "chainId" in tx else [])
to_int(tx["nonce"]),
to_int(tx["gasPrice"]),
to_int(tx["gas"] if "gas" in tx else tx["gasLimit"]),
bytes.fromhex(f"{to_int(tx['to']):040x}") if tx["to"] else b"",
to_int(tx["value"]),
to_bytes(tx["data"]),
] + ([to_int(tx["chainId"]), 0, 0] if "chainId" in tx else [])

return rlp.encode(legacy_tx)

Expand Down
2 changes: 0 additions & 2 deletions uv.lock

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

0 comments on commit 8ca25c1

Please sign in to comment.