Skip to content

Commit f98fad5

Browse files
committed
Teach builder tools how to mine block with transactions
1 parent ebb0c45 commit f98fad5

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

eth/tools/builder/chain/builders.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,14 @@ def mine_block(chain: MiningChainAPI, **kwargs: Any) -> MiningChainAPI:
361361
overridden using keyword arguments.
362362
363363
"""
364+
364365
if not isinstance(chain, MiningChainAPI):
365366
raise ValidationError('`mine_block` may only be used on MiningChain instances')
367+
368+
transactions = kwargs.pop('transactions', ())
369+
for tx in transactions:
370+
chain.apply_transaction(tx)
371+
366372
chain.mine_block(**kwargs)
367373
return chain
368374

newsfragments/1947.internal.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Allow `mine_block` of chain builder tools to take a ``transactions`` parameter.
2+
This makes it easier to model test scenarios that depend on creating blocks
3+
with transactions.

tests/core/builder-tools/test_chain_builder.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import pytest
22

3-
from eth_utils import ValidationError
3+
from eth_utils import ValidationError, to_wei
44

55
from eth.chains.base import (
66
Chain,
77
MiningChain,
88
)
9+
from eth.constants import ZERO_ADDRESS
910
from eth.tools.builder.chain import (
1011
at_block_number,
1112
build,
@@ -19,19 +20,25 @@
1920
mine_block,
2021
mine_blocks,
2122
)
23+
from eth.tools.factories.transaction import new_transaction
2224

2325

24-
MINING_CHAIN_PARAMS = (
25-
MiningChain,
26-
frontier_at(0),
27-
disable_pow_check,
28-
genesis(),
29-
)
26+
@pytest.fixture
27+
def mining_chain_params(funded_address):
28+
return (
29+
MiningChain,
30+
frontier_at(0),
31+
disable_pow_check,
32+
genesis(
33+
params={'gas_limit': 1000000},
34+
state={funded_address: {'balance': to_wei(1000, 'ether')}}
35+
),
36+
)
3037

3138

3239
@pytest.fixture
33-
def mining_chain():
34-
return build(*MINING_CHAIN_PARAMS)
40+
def mining_chain(mining_chain_params):
41+
return build(*mining_chain_params)
3542

3643

3744
REGULAR_CHAIN_PARAMS = (
@@ -46,11 +53,6 @@ def regular_chain():
4653
return build(*REGULAR_CHAIN_PARAMS)
4754

4855

49-
@pytest.fixture(params=(MINING_CHAIN_PARAMS, REGULAR_CHAIN_PARAMS))
50-
def any_chain(request):
51-
return build(*request.param)
52-
53-
5456
def test_chain_builder_build_single_default_block(mining_chain):
5557
chain = build(
5658
mining_chain,
@@ -92,6 +94,26 @@ def test_chain_builder_mine_block_with_parameters(mining_chain):
9294
assert header.extra_data == b'test-setting-extra-data'
9395

9496

97+
def test_chain_builder_mine_block_with_transactions(mining_chain,
98+
funded_address,
99+
funded_address_private_key):
100+
tx = new_transaction(
101+
mining_chain.get_vm(),
102+
from_=funded_address,
103+
to=ZERO_ADDRESS,
104+
private_key=funded_address_private_key,
105+
)
106+
107+
chain = build(
108+
mining_chain,
109+
mine_block(transactions=[tx]),
110+
)
111+
112+
block = chain.get_canonical_block_by_number(1)
113+
assert len(block.transactions) == 1
114+
assert block.transactions[0] == tx
115+
116+
95117
def test_chain_builder_mine_block_only_on_mining_chain(regular_chain):
96118
with pytest.raises(ValidationError, match="MiningChain"):
97119
mine_block()(regular_chain)

0 commit comments

Comments
 (0)