Guest User

Untitled

a guest
Jul 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. from eth_keys import keys
  2. from eth_utils import decode_hex
  3. from eth_typing import Address
  4.  
  5. from eth.consensus.pow import mine_pow_nonce
  6. from eth import constants, chains
  7. from eth.vm.forks.byzantium import ByzantiumVM
  8. from eth.db.backends.memory import MemoryDB
  9.  
  10.  
  11. GENESIS_PARAMS = {
  12. 'parent_hash': constants.GENESIS_PARENT_HASH,
  13. 'uncles_hash': constants.EMPTY_UNCLE_HASH,
  14. 'coinbase': constants.ZERO_ADDRESS,
  15. 'transaction_root': constants.BLANK_ROOT_HASH,
  16. 'receipt_root': constants.BLANK_ROOT_HASH,
  17. 'difficulty': 1,
  18. 'block_number': constants.GENESIS_BLOCK_NUMBER,
  19. 'gas_limit': constants.GENESIS_GAS_LIMIT,
  20. 'timestamp': 1514764800,
  21. 'extra_data': constants.GENESIS_EXTRA_DATA,
  22. 'nonce': constants.GENESIS_NONCE
  23. }
  24.  
  25. SENDER_PRIVATE_KEY = keys.PrivateKey(
  26. decode_hex('0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8')
  27. )
  28.  
  29. SENDER = Address(SENDER_PRIVATE_KEY.public_key.to_canonical_address())
  30.  
  31. RECEIVER = Address(b'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x02')
  32.  
  33. GENESIS_STATE = {
  34. SENDER: {
  35. "balance" : 10**20,
  36. "nonce" : 0,
  37. "code" : b"",
  38. "storage" : {}
  39. }
  40. }
  41.  
  42. klass = chains.base.MiningChain.configure(
  43. __name__='TestChain',
  44. vm_configuration=(
  45. (constants.GENESIS_BLOCK_NUMBER, ByzantiumVM),
  46. ))
  47.  
  48. chain = klass.from_genesis(MemoryDB(), GENESIS_PARAMS, GENESIS_STATE)
  49.  
  50.  
  51.  
  52. ######### Tx1 ###########################
  53. # nonce = vm.get_transaction_nonce(SENDER)
  54. vm = chain.get_vm()
  55. nonce = vm.state.account_db.get_nonce(SENDER)
  56.  
  57. tx1 = vm.create_unsigned_transaction(
  58. nonce=nonce,
  59. gas_price=0,
  60. gas=100000,
  61. to=RECEIVER,
  62. value=0,
  63. data=b'',
  64. )
  65.  
  66. signed_tx1 = tx1.as_signed_transaction(SENDER_PRIVATE_KEY)
  67.  
  68. chain.apply_transaction(signed_tx1)
  69.  
  70. # We have to finalize the block first in order to be able read the
  71. # attributes that are important for the PoW algorithm
  72. block = chain.get_vm().finalize_block(chain.get_block())
  73.  
  74. # based on mining_hash, block number and difficulty we can perform
  75. # the actual Proof of Work (PoW) mechanism to mine the correct
  76. # nonce and mix_hash for this block
  77. nonce, mix_hash = mine_pow_nonce(
  78. block.number,
  79. block.header.mining_hash,
  80. block.header.difficulty)
  81.  
  82. block = chain.mine_block(mix_hash=mix_hash, nonce=nonce)
  83.  
  84. print("BLOCK1 SENDER BALANCE : {}".format(vm.state.account_db.get_balance(SENDER)))
  85. print("BLOCK1 RECEIVER BALANCE : {}".format(vm.state.account_db.get_balance(RECEIVER)))
  86.  
  87. ######### Tx2 ###########################
  88. # nonce = vm.get_transaction_nonce(SENDER)
  89. vm = chain.get_vm()
  90. nonce = vm.state.account_db.get_nonce(SENDER)
  91.  
  92. tx2 = vm.create_unsigned_transaction(
  93. nonce=nonce,
  94. gas_price=0,
  95. gas=100000,
  96. to=RECEIVER,
  97. value=10**18,
  98. data=b'',
  99. )
  100.  
  101. signed_tx2 = tx2.as_signed_transaction(SENDER_PRIVATE_KEY)
  102.  
  103. chain.apply_transaction(signed_tx2)
  104.  
  105. # We have to finalize the block first in order to be able read the
  106. # attributes that are important for the PoW algorithm
  107. block = chain.get_vm().finalize_block(chain.get_block())
  108.  
  109. # based on mining_hash, block number and difficulty we can perform
  110. # the actual Proof of Work (PoW) mechanism to mine the correct
  111. # nonce and mix_hash for this block
  112. nonce, mix_hash = mine_pow_nonce(
  113. block.number,
  114. block.header.mining_hash,
  115. block.header.difficulty)
  116.  
  117. block = chain.mine_block(mix_hash=mix_hash, nonce=nonce)
  118. vm = chain.get_vm()
  119.  
  120. print("BLOCK2 SENDER BALANCE : {}".format(vm.state.account_db.get_balance(SENDER)))
  121. print("BLOCK2 RECEIVER BALANCE : {}".format(vm.state.account_db.get_balance(RECEIVER)))
Add Comment
Please, Sign In to add comment