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

Update EthereumTesterProvider to support cancun #3332

Merged
merged 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3332.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.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

extras_require = {
"tester": [
"eth-tester[py-evm]==v0.10.0-b.3",
"eth-tester[py-evm]>=0.11.0b1,<0.12.0b1",
"py-geth>=4.1.0",
],
"linter": [
Expand Down
69 changes: 69 additions & 0 deletions tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,40 @@ def test_get_transaction_formatters(w3, request_mocker):
assert received_tx == expected


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 @@ -355,3 +389,38 @@ async def test_async_wait_for_transaction_receipt_transaction_indexing_in_progre
):
receipt = await async_w3.eth.wait_for_transaction_receipt(f"0x{'00' * 32}")
assert receipt == {"status": 1}


@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"
)
9 changes: 9 additions & 0 deletions web3/providers/eth_tester/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,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 @@ -123,10 +125,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 @@ -164,6 +168,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 @@ -186,6 +192,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 @@ -166,13 +166,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 @@ -221,6 +223,9 @@ class BlockData(TypedDict, total=False):
uncles: Sequence[HexBytes]
withdrawals: Sequence[WithdrawalData]
withdrawalsRoot: HexBytes
parentBeaconBlockRoot: HexBytes
blobGasUsed: int
excessBlobGas: int

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