Skip to content

Instantly share code, notes, and snippets.

@bajcmartinez
Created May 18, 2020 19:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bajcmartinez/5dedf1e69c689dcb4996d726dbb31f94 to your computer and use it in GitHub Desktop.
Save bajcmartinez/5dedf1e69c689dcb4996d726dbb31f94 to your computer and use it in GitHub Desktop.
From Zero to Blockchain in Python - Part 1 - Mine
def mine(self, reward_address):
"""
Mines a new block into the chain
:param reward_address: <str> address where the reward coin will be transferred to
:return: result of the mining attempt and the new block
"""
last_block = self.last_block
index = last_block.index + 1
previous_hash = last_block.hash
# Let's start with the heavy duty, generating the proof of work
nonce = self.generate_proof_of_work(last_block)
# In the next step we will create a new transaction to reward the miner
# In this particular case, the miner will receive coins that are just "created", so there is no sender
self.create_transaction(
sender="0",
recipient=reward_address,
amount=1,
)
# Add the block to the new chain
block = Block(index, self.__current_transactions, nonce, previous_hash)
if self.add_block(block):
return block
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment