4
4
from eth_utils import decode_hex
5
5
6
6
from eth import constants
7
+ from eth .abc import MiningChainAPI
7
8
from eth .chains .mainnet import MAINNET_GENESIS_HEADER
8
9
from eth .chains .ropsten import ROPSTEN_GENESIS_HEADER
9
10
from eth .exceptions import (
@@ -38,6 +39,15 @@ def tx(chain, funded_address, funded_address_private_key):
38
39
return new_transaction (vm , from_ , recipient , amount , funded_address_private_key )
39
40
40
41
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
+
41
51
@pytest .mark .xfail (reason = "modification to initial allocation made the block fixture invalid" )
42
52
def test_import_block_validation (valid_chain , funded_address , funded_address_initial_balance ):
43
53
block = rlp .decode (valid_block_rlp , sedes = FrontierBlock )
@@ -63,6 +73,7 @@ def test_import_block(chain, tx):
63
73
else :
64
74
# working on a non-mining chain, so we have to build the block to apply manually
65
75
new_block , receipts , computations = chain .build_block_with_transactions ([tx ])
76
+ assert len (computations ) == 1
66
77
computations [0 ].raise_if_error ()
67
78
68
79
block_import_result = chain .import_block (new_block )
@@ -74,6 +85,48 @@ def test_import_block(chain, tx):
74
85
assert chain .get_canonical_transaction (tx .hash ) == tx
75
86
76
87
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
+
77
130
def test_empty_transaction_lookups (chain ):
78
131
with pytest .raises (TransactionNotFound ):
79
132
chain .get_canonical_transaction (b'\0 ' * 32 )
0 commit comments