@@ -308,74 +308,76 @@ Finally, we can call :func:`~eth.chains.base.MiningChain.apply_transaction` and
308
308
What follows is the complete script that demonstrates how to mine a single block with one simple
309
309
zero value transfer transaction.
310
310
311
- ::
312
-
313
- from eth_keys import keys
314
- from eth_utils import decode_hex
315
- from eth_typing import Address
316
-
317
- from eth.consensus.pow import mine_pow_nonce
318
- from eth import constants, MiningChain
319
- from eth.vm.forks.byzantium import ByzantiumVM
320
- from eth.db.backends.memory import MemoryDB
321
-
322
-
323
- GENESIS_PARAMS = {
324
- 'parent_hash': constants.GENESIS_PARENT_HASH,
325
- 'uncles_hash': constants.EMPTY_UNCLE_HASH,
326
- 'coinbase': constants.ZERO_ADDRESS,
327
- 'transaction_root': constants.BLANK_ROOT_HASH,
328
- 'receipt_root': constants.BLANK_ROOT_HASH,
329
- 'difficulty': 1,
330
- 'block_number': constants.GENESIS_BLOCK_NUMBER,
331
- 'gas_limit': constants.GENESIS_GAS_LIMIT,
332
- 'timestamp': 1514764800,
333
- 'extra_data': constants.GENESIS_EXTRA_DATA,
334
- 'nonce': constants.GENESIS_NONCE
335
- }
336
-
337
- SENDER_PRIVATE_KEY = keys.PrivateKey(
338
- decode_hex('0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8')
339
- )
340
-
341
- SENDER = Address(SENDER_PRIVATE_KEY.public_key.to_canonical_address())
342
-
343
- RECEIVER = Address(b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02')
344
-
345
- klass = MiningChain.configure(
346
- __name__='TestChain',
347
- vm_configuration=(
348
- (constants.GENESIS_BLOCK_NUMBER, ByzantiumVM),
349
- ))
350
-
351
- chain = klass.from_genesis(MemoryDB(), GENESIS_PARAMS)
352
- vm = chain.get_vm()
353
-
354
- nonce = vm.get_transaction_nonce(SENDER)
355
-
356
- tx = vm.create_unsigned_transaction(
357
- nonce=nonce,
358
- gas_price=0,
359
- gas=100000,
360
- to=RECEIVER,
361
- value=0,
362
- data=b'',
363
- )
364
-
365
- signed_tx = tx.as_signed_transaction(SENDER_PRIVATE_KEY)
366
-
367
- chain.apply_transaction(signed_tx)
368
-
369
- # We have to finalize the block first in order to be able read the
370
- # attributes that are important for the PoW algorithm
371
- block = chain.get_vm().finalize_block(chain.get_block())
372
-
373
- # based on mining_hash, block number and difficulty we can perform
374
- # the actual Proof of Work (PoW) mechanism to mine the correct
375
- # nonce and mix_hash for this block
376
- nonce, mix_hash = mine_pow_nonce(
377
- block.number,
378
- block.header.mining_hash,
379
- block.header.difficulty)
380
-
381
- block = chain.mine_block(mix_hash=mix_hash, nonce=nonce)
311
+ .. doctest ::
312
+
313
+ >>> from eth_keys import keys
314
+ >>> from eth_utils import decode_hex
315
+ >>> from eth_typing import Address
316
+ >>> from eth import constants
317
+ >>> from eth.chains.base import MiningChain
318
+ >>> from eth.consensus.pow import mine_pow_nonce
319
+ >>> from eth.vm.forks.byzantium import ByzantiumVM
320
+ >>> from eth.db.backends.memory import MemoryDB
321
+
322
+
323
+ >>> GENESIS_PARAMS = {
324
+ ... ' parent_hash' : constants.GENESIS_PARENT_HASH ,
325
+ ... ' uncles_hash' : constants.EMPTY_UNCLE_HASH ,
326
+ ... ' coinbase' : constants.ZERO_ADDRESS ,
327
+ ... ' transaction_root' : constants.BLANK_ROOT_HASH ,
328
+ ... ' receipt_root' : constants.BLANK_ROOT_HASH ,
329
+ ... ' difficulty' : 1 ,
330
+ ... ' block_number' : constants.GENESIS_BLOCK_NUMBER ,
331
+ ... ' gas_limit' : constants.GENESIS_GAS_LIMIT ,
332
+ ... ' timestamp' : 1514764800 ,
333
+ ... ' extra_data' : constants.GENESIS_EXTRA_DATA ,
334
+ ... ' nonce' : constants.GENESIS_NONCE
335
+ ... }
336
+
337
+ >>> SENDER_PRIVATE_KEY = keys.PrivateKey(
338
+ ... decode_hex(' 0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8' )
339
+ ... )
340
+
341
+ >>> SENDER = Address(SENDER_PRIVATE_KEY .public_key.to_canonical_address())
342
+
343
+ >>> RECEIVER = Address(b ' \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02 ' )
344
+
345
+ >>> klass = MiningChain.configure(
346
+ ... __name__ = ' TestChain' ,
347
+ ... vm_configuration= (
348
+ ... (constants.GENESIS_BLOCK_NUMBER , ByzantiumVM),
349
+ ... ))
350
+
351
+ >>> chain = klass.from_genesis(MemoryDB(), GENESIS_PARAMS )
352
+ >>> vm = chain.get_vm()
353
+
354
+ >>> nonce = vm.state.account_db.get_nonce(SENDER )
355
+
356
+ >>> tx = vm.create_unsigned_transaction(
357
+ ... nonce= nonce,
358
+ ... gas_price= 0 ,
359
+ ... gas= 100000 ,
360
+ ... to= RECEIVER ,
361
+ ... value= 0 ,
362
+ ... data= b ' ' ,
363
+ ... )
364
+
365
+ >>> signed_tx = tx.as_signed_transaction(SENDER_PRIVATE_KEY )
366
+
367
+ >>> chain.apply_transaction(signed_tx)
368
+ (<ByzantiumBlock(#Block #1...)
369
+ >>> # We have to finalize the block first in order to be able read the
370
+ >>> # attributes that are important for the PoW algorithm
371
+ >>> block = chain.get_vm().finalize_block(chain.get_block())
372
+
373
+ >>> # based on mining_hash, block number and difficulty we can perform
374
+ >>> # the actual Proof of Work (PoW) mechanism to mine the correct
375
+ >>> # nonce and mix_hash for this block
376
+ >>> nonce, mix_hash = mine_pow_nonce(
377
+ ... block.number,
378
+ ... block.header.mining_hash,
379
+ ... block.header.difficulty
380
+ ... )
381
+
382
+ >>> chain.mine_block(mix_hash = mix_hash, nonce = nonce)
383
+ <ByzantiumBlock(#Block #1)>
0 commit comments