Created
May 18, 2020 19:17
-
-
Save bajcmartinez/5dedf1e69c689dcb4996d726dbb31f94 to your computer and use it in GitHub Desktop.
From Zero to Blockchain in Python - Part 1 - Mine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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