utils.py 1007 B

123456789101112131415161718192021222324252627282930
  1. import src.proof_of_work
  2. src.proof_of_work.verify_proof_of_work = lambda b: True
  3. from src.block import *
  4. from src.blockchain import *
  5. from src.crypto import *
  6. from src.transaction import *
  7. from datetime import datetime
  8. def extend_blockchain(chain, trans:list=None, verify_res=True):
  9. ts = datetime.utcfromtimestamp(len(chain.blocks))
  10. new_block = Block.create(chain, trans, ts)
  11. new_block.hash = new_block.get_hash()
  12. new_chain = Blockchain(chain.blocks + [new_block])
  13. assert new_chain.verify_all() == verify_res
  14. return new_chain
  15. def trans_as_input(trans, out_idx=0):
  16. assert len(trans.targets) > out_idx
  17. return TransactionInput(trans.get_hash(), out_idx)
  18. def new_trans(old_trans, out_idx=0):
  19. amount = old_trans.targets[out_idx].amount
  20. key = Signing.generatePrivateKey()
  21. trans = Transaction([trans_as_input(old_trans, out_idx)],
  22. [TransactionTarget(key, amount)])
  23. trans.sign([old_trans.targets[out_idx].recipient_pk])
  24. return trans