44from eth_utils import decode_hex
55
66from eth import constants
7+ from eth .abc import MiningChainAPI
78from eth .chains .mainnet import MAINNET_GENESIS_HEADER
89from eth .chains .ropsten import ROPSTEN_GENESIS_HEADER
910from eth .exceptions import (
@@ -38,6 +39,15 @@ def tx(chain, funded_address, funded_address_private_key):
3839 return new_transaction (vm , from_ , recipient , amount , funded_address_private_key )
3940
4041
42+ @pytest .fixture ()
43+ def tx2 (chain , funded_address , funded_address_private_key ):
44+ recipient = b'\x88 ' * 20
45+ amount = 100
46+ vm = chain .get_vm ()
47+ from_ = funded_address
48+ return new_transaction (vm , from_ , recipient , amount , funded_address_private_key , nonce = 1 )
49+
50+
4151@pytest .mark .xfail (reason = "modification to initial allocation made the block fixture invalid" )
4252def test_import_block_validation (valid_chain , funded_address , funded_address_initial_balance ):
4353 block = rlp .decode (valid_block_rlp , sedes = FrontierBlock )
@@ -63,6 +73,7 @@ def test_import_block(chain, tx):
6373 else :
6474 # working on a non-mining chain, so we have to build the block to apply manually
6575 new_block , receipts , computations = chain .build_block_with_transactions ([tx ])
76+ assert len (computations ) == 1
6677 computations [0 ].raise_if_error ()
6778
6879 block_import_result = chain .import_block (new_block )
@@ -74,6 +85,48 @@ def test_import_block(chain, tx):
7485 assert chain .get_canonical_transaction (tx .hash ) == tx
7586
7687
88+ def test_mine_all (chain , tx , tx2 , funded_address ):
89+ if hasattr (chain , 'mine_all' ):
90+ start_balance = chain .get_vm ().state .get_balance (funded_address )
91+
92+ mine_result = chain .mine_all ([tx , tx2 ])
93+ block = mine_result [0 ].imported_block
94+
95+ assert block .transactions == (tx , tx2 )
96+ assert chain .get_block_by_hash (block .hash ) == block
97+ assert chain .get_canonical_block_by_number (block .number ) == block
98+ assert chain .get_canonical_transaction (tx .hash ) == tx
99+
100+ end_balance = chain .get_vm ().state .get_balance (funded_address )
101+ expected_spend = 2 * (100 + 21000 * 10 ) # sent + gas * gasPrice
102+
103+ assert start_balance - end_balance == expected_spend
104+ elif isinstance (chain , MiningChainAPI ):
105+ raise AssertionError () # Mining chains should have the 'mine_all' method
106+
107+
108+ def test_mine_all_uncle (chain , tx , tx2 , funded_address ):
109+ if hasattr (chain , 'mine_all' ):
110+ starting_tip = chain .get_canonical_head ()
111+ canonical = chain .mine_all ([tx ])
112+ uncled = chain .mine_all ([], parent_header = starting_tip )
113+ uncled_header = uncled [0 ].imported_block .header
114+
115+ new_tip = chain .mine_all (
116+ [tx2 ],
117+ parent_header = canonical [0 ].imported_block .header ,
118+ uncles = [uncled_header ],
119+ )
120+
121+ block = new_tip [0 ].imported_block
122+
123+ assert block .transactions == (tx2 ,)
124+ assert chain .get_block_by_hash (block .hash ) == block
125+ assert block .uncles == (uncled_header ,)
126+ elif isinstance (chain , MiningChainAPI ):
127+ raise AssertionError () # Mining chains should have the 'mine_all' method
128+
129+
77130def test_empty_transaction_lookups (chain ):
78131 with pytest .raises (TransactionNotFound ):
79132 chain .get_canonical_transaction (b'\0 ' * 32 )
0 commit comments