Last active
May 10, 2022 00:57
-
-
Save aunyks/0f29a18de1750977b9baa7404da09562 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # ...blockchain | |
| # ...Block class definition | |
| miner_address = "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi" | |
| def proof_of_work(last_proof): | |
| # Create a variable that we will use to find | |
| # our next proof of work | |
| incrementor = last_proof + 1 | |
| # Keep incrementing the incrementor until | |
| # it's equal to a number divisible by 9 | |
| # and the proof of work of the previous | |
| # block in the chain | |
| while not (incrementor % 9 == 0 and incrementor % last_proof == 0): | |
| incrementor += 1 | |
| # Once that number is found, | |
| # we can return it as a proof | |
| # of our work | |
| return incrementor | |
| @node.route('/mine', methods = ['GET']) | |
| def mine(): | |
| # Get the last proof of work | |
| last_block = blockchain[len(blockchain) - 1] | |
| last_proof = last_block.data['proof-of-work'] | |
| # Find the proof of work for | |
| # the current block being mined | |
| # Note: The program will hang here until a new | |
| # proof of work is found | |
| proof = proof_of_work(last_proof) | |
| # Once we find a valid proof of work, | |
| # we know we can mine a block so | |
| # we reward the miner by adding a transaction | |
| this_nodes_transactions.append( | |
| { "from": "network", "to": miner_address, "amount": 1 } | |
| ) | |
| # Now we can gather the data needed | |
| # to create the new block | |
| new_block_data = { | |
| "proof-of-work": proof, | |
| "transactions": list(this_nodes_transactions) | |
| } | |
| new_block_index = last_block.index + 1 | |
| new_block_timestamp = this_timestamp = date.datetime.now() | |
| last_block_hash = last_block.hash | |
| # Empty transaction list | |
| this_nodes_transactions[:] = [] | |
| # Now create the | |
| # new block! | |
| mined_block = Block( | |
| new_block_index, | |
| new_block_timestamp, | |
| new_block_data, | |
| last_block_hash | |
| ) | |
| blockchain.append(mined_block) | |
| # Let the client know we mined a block | |
| return json.dumps({ | |
| "index": new_block_index, | |
| "timestamp": str(new_block_timestamp), | |
| "data": new_block_data, | |
| "hash": last_block_hash | |
| }) + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
File "snakecoin-pow.py", line 21, in
@node.route('/mine', methods = ['GET'])
NameError: name 'node' is not defined
johns-imac:~
why the error?