A blockchain written in Python 3.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

block.py 876B

1234567891011121314151617181920212223
  1. import hashlib
  2. import jsonpickle
  3. class Block(object):
  4. def __init__(self, _index, _timestamp, _data = [], _prevHash = '', _merkleHash = ''):
  5. self.index, self.timestamp, self.data, self.prevHash, self.merkleHash, self.nonce, self.hash = _index, _timestamp, _data, _prevHash, _merkleHash, 0, ''
  6. def calculateHash(self):
  7. hash = ''.join([str(self.index), str(self.prevHash), str(self.merkleHash), str(self.timestamp), jsonpickle.encode(self.data), str(self.nonce)])
  8. hash = hash.encode('utf-8')
  9. return hashlib.sha256(hash).hexdigest()
  10. def mine(self, _difficulty):
  11. s = []
  12. for i in range(0, _difficulty):
  13. s.append('0')
  14. s = ''.join(s)
  15. while self.hash[0:_difficulty] != s:
  16. self.nonce+=1
  17. self.hash = self.calculateHash()
  18. print("[Info] Block mined: ", self.hash)