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

V6 ethereum tester provider cancun support #3338

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3338.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Cancun support to ``EthereumTesterProvider``; update Cancun-related fields in some internal types.
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

extras_require = {
"tester": [
"eth-tester[py-evm]==v0.9.1-b.2",
"eth-tester[py-evm]>=0.9.0b1,<0.10.0b1; python_version <= '3.7'",
"eth-tester[py-evm]>=0.11.0b1,<0.12.0b1; python_version > '3.7'",
"py-geth>=3.14.0",
],
"linter": [
Expand Down
84 changes: 84 additions & 0 deletions tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import sys

from eth_utils import (
to_checksum_address,
Expand Down Expand Up @@ -355,6 +356,47 @@ def test_get_transaction_formatters(w3):
w3.middleware_onion.remove("result_middleware")


@pytest.mark.skipif(
sys.version_info < (3, 8),
reason=(
"There is no version of eth-tester that supports both python 3.7 and blob "
"transactions / Cancun network upgrade."
),
)
def test_eth_send_raw_blob_transaction(w3):
# `eth-tester` account #1's pkey is "0x00000000...01"
acct = w3.eth.account.from_key(f"0x{'00' * 31}01")

text = "We are the music makers and we are the dreamers of dreams."
encoded_text = w3.codec.encode(["string"], [text])
# Blobs contain 4096 32-byte field elements. Subtract the length of the encoded text
# divided into 32-byte chunks from 4096 and pad the rest with zeros.
blob_data = (b"\x00" * 32 * (4096 - len(encoded_text) // 32)) + encoded_text

tx = {
"type": 3,
"chainId": 1337,
"from": acct.address,
"to": "0xb45BEc6eeCA2a09f4689Dd308F550Ad7855051B5",
"value": 0,
"gas": 21000,
"maxFeePerGas": 10**10,
"maxPriorityFeePerGas": 10**10,
"maxFeePerBlobGas": 10**10,
"nonce": w3.eth.get_transaction_count(acct.address),
}

signed = acct.sign_transaction(tx, blobs=[blob_data])

tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
transaction = w3.eth.get_transaction(tx_hash)

assert len(transaction["blobVersionedHashes"]) == 1
assert transaction["blobVersionedHashes"][0] == HexBytes(
"0x0127c38bcad458d932e828b580b9ad97310be01407dfa0ed88118735980a3e9a"
)


# --- async --- #


Expand All @@ -380,3 +422,45 @@ async def test_async_wait_for_transaction_receipt_transaction_indexing_in_progre
assert receipt == {"status": 1}

async_w3.middleware_onion.remove("result_middleware")


@pytest.mark.skipif(
sys.version_info < (3, 8),
reason=(
"There is no version of eth-tester that supports both python 3.7 and blob "
"transactions / Cancun network upgrade."
),
)
@pytest.mark.asyncio
async def test_async_send_raw_blob_transaction(async_w3):
# `eth-tester` account #1's pkey is "0x00000000...01"
acct = async_w3.eth.account.from_key(f"0x{'00' * 31}01")

text = "We are the music makers and we are the dreamers of dreams."
encoded_text = async_w3.codec.encode(["string"], [text])
# Blobs contain 4096 32-byte field elements. Subtract the length of the encoded text
# divided into 32-byte chunks from 4096 and pad the rest with zeros.
blob_data = (b"\x00" * 32 * (4096 - len(encoded_text) // 32)) + encoded_text

tx = {
"type": 3,
"chainId": 1337,
"from": acct.address,
"to": "0xb45BEc6eeCA2a09f4689Dd308F550Ad7855051B5",
"value": 0,
"gas": 21000,
"maxFeePerGas": 10**10,
"maxPriorityFeePerGas": 10**10,
"maxFeePerBlobGas": 10**10,
"nonce": await async_w3.eth.get_transaction_count(acct.address),
}

signed = acct.sign_transaction(tx, blobs=[blob_data])

tx_hash = await async_w3.eth.send_raw_transaction(signed.rawTransaction)
transaction = await async_w3.eth.get_transaction(tx_hash)

assert len(transaction["blobVersionedHashes"]) == 1
assert transaction["blobVersionedHashes"][0] == HexBytes(
"0x0127c38bcad458d932e828b580b9ad97310be01407dfa0ed88118735980a3e9a"
)
8 changes: 8 additions & 0 deletions tests/core/middleware/test_eth_tester_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import sys
from unittest.mock import (
Mock,
)
Expand All @@ -25,6 +26,13 @@ def test_get_transaction_count_formatters(w3, block_number):
assert tx_counts == 0


@pytest.mark.skipif(
sys.version_info < (3, 8),
reason=(
"There is no version of eth-tester that supports both python 3.7 and blob "
"transactions / Cancun network upgrade."
),
)
def test_get_block_formatters(w3):
all_block_keys = BlockData.__annotations__.keys()
all_non_poa_block_keys = set(
Expand Down
9 changes: 9 additions & 0 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def is_hexstr(value: Any) -> bool:
# --- Request Mapping --- #

TRANSACTION_REQUEST_KEY_MAPPING = {
"blobVersionedHashes": "blob_versioned_hashes",
"gasPrice": "gas_price",
"maxFeePerBlobGas": "max_fee_per_blob_gas",
"maxFeePerGas": "max_fee_per_gas",
"maxPriorityFeePerGas": "max_priority_fee_per_gas",
"accessList": "access_list",
Expand Down Expand Up @@ -126,10 +128,12 @@ def is_hexstr(value: Any) -> bool:

TRANSACTION_RESULT_KEY_MAPPING = {
"access_list": "accessList",
"blob_versioned_hashes": "blobVersionedHashes",
"block_hash": "blockHash",
"block_number": "blockNumber",
"chain_id": "chainId",
"gas_price": "gasPrice",
"max_fee_per_blob_gas": "maxFeePerBlobGas",
"max_fee_per_gas": "maxFeePerGas",
"max_priority_fee_per_gas": "maxPriorityFeePerGas",
"transaction_hash": "transactionHash",
Expand Down Expand Up @@ -166,6 +170,8 @@ def is_hexstr(value: Any) -> bool:
"effective_gas_price": "effectiveGasPrice",
"transaction_hash": "transactionHash",
"transaction_index": "transactionIndex",
"blob_gas_used": "blobGasUsed",
"blob_gas_price": "blobGasPrice",
}
receipt_result_remapper = apply_key_map(RECEIPT_RESULT_KEY_MAPPING)

Expand All @@ -188,6 +194,9 @@ def is_hexstr(value: Any) -> bool:
# JSON-RPC spec still says miner
"coinbase": "miner",
"withdrawals_root": "withdrawalsRoot",
"parent_beacon_block_root": "parentBeaconBlockRoot",
"blob_gas_used": "blobGasUsed",
"excess_blob_gas": "excessBlobGas",
}
block_result_remapper = apply_key_map(BLOCK_RESULT_KEY_MAPPING)

Expand Down
5 changes: 5 additions & 0 deletions web3/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ class RPCError(TypedDict):
"TxParams",
{
"accessList": AccessList,
"blobVersionedHashes": Sequence[Union[str, HexStr, bytes, HexBytes]],
"chainId": int,
"data": Union[bytes, HexStr],
# addr or ens
"from": Union[Address, ChecksumAddress, str],
"gas": int,
# legacy pricing
"gasPrice": Wei,
"maxFeePerBlobGas": Union[str, Wei],
# dynamic fee pricing
"maxFeePerGas": Union[str, Wei],
"maxPriorityFeePerGas": Union[str, Wei],
Expand Down Expand Up @@ -224,6 +226,9 @@ class BlockData(TypedDict, total=False):
uncles: Sequence[HexBytes]
withdrawals: Sequence[WithdrawalData]
withdrawalsRoot: HexBytes
parentBeaconBlockRoot: HexBytes
blobGasUsed: int
excessBlobGas: int

# geth_poa_middleware replaces extraData w/ proofOfAuthorityData
proofOfAuthorityData: HexBytes
Expand Down