@@ -67,20 +67,23 @@ def calc_difficulty(parent, timestamp, config=default_config):
67
67
68
68
# Given a parent state, initialize a block with the given arguments
69
69
def mk_block_from_prevstate (chain , state = None , timestamp = None ,
70
- coinbase = b'\x35 ' * 20 , extra_data = 'moo ha ha says the laughing cow.' ):
70
+ coinbase = b'\x35 ' * 20 , extra_data = b 'moo ha ha says the laughing cow.' ):
71
71
state = state or chain .state
72
- blk = Block (BlockHeader ())
73
72
now = timestamp or chain .time ()
74
- blk .header .number = state .prev_headers [0 ].number + 1
75
- blk .header .difficulty = calc_difficulty (
76
- state .prev_headers [0 ], now , chain .config )
77
- blk .header .gas_limit = calc_gaslimit (state .prev_headers [0 ], chain .config )
78
- blk .header .timestamp = max (now , state .prev_headers [0 ].timestamp + 1 )
79
- blk .header .prevhash = state .prev_headers [0 ].hash
80
- blk .header .coinbase = coinbase
81
- blk .header .extra_data = extra_data
82
- blk .header .bloom = 0
83
- blk .transactions = []
73
+
74
+ blk = Block (
75
+ header = BlockHeader (
76
+ number = state .prev_headers [0 ].number + 1 ,
77
+ difficulty = calc_difficulty (state .prev_headers [0 ], now , chain .config ),
78
+ gas_limit = calc_gaslimit (state .prev_headers [0 ], chain .config ),
79
+ timestamp = max (now , state .prev_headers [0 ].timestamp + 1 ),
80
+ prevhash = state .prev_headers [0 ].hash ,
81
+ coinbase = coinbase ,
82
+ extra_data = extra_data ,
83
+ bloom = 0
84
+ ),
85
+ transactions = []
86
+ )
84
87
return blk
85
88
86
89
@@ -122,7 +125,7 @@ def validate_header(state, header):
122
125
# Add transactions
123
126
def add_transactions (state , block , txqueue , min_gasprice = 0 ):
124
127
if not txqueue :
125
- return
128
+ return block
126
129
pre_txs = len (block .transactions )
127
130
log .info ('Adding transactions, %d in txqueue, %d dunkles' %
128
131
(len (txqueue .txs ), pre_txs ))
@@ -133,12 +136,15 @@ def add_transactions(state, block, txqueue, min_gasprice=0):
133
136
break
134
137
try :
135
138
apply_transaction (state , tx )
136
- block .transactions .append (tx )
139
+ block = block .copy (
140
+ transactions = block .transactions + (tx ,)
141
+ )
137
142
except (InsufficientBalance , BlockGasLimitReached , InsufficientStartGas ,
138
143
InvalidNonce , UnsignedTransaction ) as e :
139
144
log .error (e )
140
145
log .info ('Added %d transactions' % (len (block .transactions ) - pre_txs ))
141
146
147
+ return block
142
148
143
149
# Validate that the transaction list root is correct
144
150
def validate_transaction_tree (state , block ):
@@ -151,13 +157,19 @@ def validate_transaction_tree(state, block):
151
157
152
158
# Set state root, receipt root, etc
153
159
def set_execution_results (state , block ):
154
- block .header .receipts_root = mk_receipt_sha (state .receipts )
155
- block .header .tx_list_root = mk_transaction_sha (block .transactions )
156
160
state .commit ()
157
- block .header .state_root = state .trie .root_hash
158
- block .header .gas_used = state .gas_used
159
- block .header .bloom = state .bloom
161
+
162
+ block = block .copy (
163
+ header = block .header .copy (
164
+ receipts_root = mk_receipt_sha (state .receipts ),
165
+ tx_list_root = mk_transaction_sha (block .transactions ),
166
+ state_root = state .trie .root_hash ,
167
+ gas_used = state .gas_used ,
168
+ bloom = state .bloom
169
+ )
170
+ )
160
171
log .info ('Block pre-sealed, %d gas used' % state .gas_used )
172
+ return block
161
173
162
174
163
175
# Verify state root, receipt root, etc
0 commit comments