<p>To generate the proof of work your program first creates a block template from a set of transactions. The first transaction is normally called a coinbase transaction and it is invented by the miner, it has outputs of the miner's choosing, typically it pays the miner. It has no explicit inputs, the sum of outputs is no more than sum of transaction fees from the other included transactions (if any) plus the current block reward which halves every few years. The program chooses a value for a nonce field in the block and calculates the hash of most of the data described above. If the hash is greater than the target, the miner must vary the nonce or other data in the block and retry.</p>
<p>If the hash is less than the target, the program makes its few peer nodes aware of it's new block. It does this using the <code>inv</code> message type described in the <a href="https://en.bitcoin.it/wiki/Protocol_documentation#inv" rel="nofollow noreferrer">bitcoin protocol documentation</a></p>
<hr />
<blockquote>
<p>suppose I was to run this code [link] and generate proof of work, how do I broadcast that to peers.</p>
</blockquote>
<p>Firstly, that code has the following function. you need to write more appropriate code to populate the parameters including <code>transactions</code></p>
<pre><code>def mine(block_number, transactions, previous_hash, prefix_zeros):
</code></pre>
<p>I doubt that code actually works. The structure seems far too simple.</p>
<ul>
<li>It doesn't create locking scripts</li>
<li>The transaction data is way too simplified. It is conceptual not real.</li>
<li>It doesn't look up Transaction-Ids and output-indices for inputs</li>
<li>It has <code>prefix_zeros</code> but real codes tests for less than target,
not for number of leading zeroes.</li>
<li>etc.</li>
</ul>
<blockquote>
<p>Does this code on github that uses sha256 automatically inform the network?</p>
</blockquote>
<p>No.</p>
<blockquote>
<p>Can I just add the resulting characters into a bitcoin wallet?</p>
</blockquote>
<p>No. It is far more work than that code suggests. Start elsewhere.</p>
<p>Remember</p>
<ul>
<li>Bitcoin wallets don't contain money.</li>
<li>Bitcoin wallets contain a secret number called a private key.</li>
<li>There are many different types of transaction and a mining program
needs to know about them to validate them.</li>
<li>A mining program should collect unconfirmed transactions.</li>
<li>this is all much more complicated than you are yet aware of.</li>
</ul>

CC BY-SA 4.0
2021-06-07T23:48:43.843Z
2021-06-07T23:48:43.843Z
RedGrittyBrick
<p>Can anyone help with some good explanations on next steps for Bitcoin mining.</p>
<p>Hypothetically let's say I create an algorithm that successfully guesses the code to unlock a Bitcoin blockchain</p>
<p>gives me a result such as: 000000000000000000008d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7</p>
<p>I believe the results of this computational effort is called <strong>'Proof Of Work'</strong></p>
<p>Where do I paste this <strong>'Proof Of Work'</strong> on the internet to claim the Bitcoin?</p>
<p>Also can you refer me to any helpful API where I can check my 'Proof Of Work' against already claimed 'Proof Of Work'</p>
<p>A basic video on how I might create such an algorithm can be found here <a href="https://youtu.be/ZhnJ1bkIWWk" rel="nofollow noreferrer">https://youtu.be/ZhnJ1bkIWWk</a> unfortunately it doesn't mention next step.</p>
<p>I am sorry if this seems like a really basic question, after much googling I am still confused.</p>
<p>I would like to try and make edits to such an algorithm and see if I can unlock a blockchain and claim bitcoin reward. I am not asking how to write an algorithm to unlock bitcoin, I would like an explanation of what I do with the result of the algorithm i.e. how do you cash in that lottery ticket?</p>
<p>Any advice appreciated.</p>

CC BY-SA 4.0
2021-06-07T13:14:35.493Z
Does this answer your question? [What exactly is Mining?](https://bitcoin.stackexchange.com/questions/148/what-exactly-is-mining). You broadcast to a few peers  your new block, the first transaction in that block is one you created that pays you the block reward + mining fees. Spending that is  deferred for 100 blocks.
RedGrittyBrick
CC BY-SA 4.0
2021-06-07T23:20:43.973Z
It was an interesting question and set of responses, but I am looking for more practical advice, suppose I was to run this code [link](https://github.com/codebasics/cool_python_apps/blob/main/2_bitcoin_mining/bitcoin_mining.py) and generate proof of work, how do I broadcast that to peers. Does this code on github that uses sha256 automatically inform the network? Can I just add the resulting characters into a bitcoin wallet?
Christopher
CC BY-SA 4.0
2021-06-09T06:07:38.420Z
The code you link doesn't implement bitcoin at all, just a toy scheme that is partly similar to a few small parts. And even if you used code that is correct but still in python it would run only on CPUs, and today CPU mining is uncompetitive by many orders of magnitude. To 'win' a block with python code you would need to own many times more computers than exist in the world and buy probably most of the electric power; I consider that impractical.
dave_thompson_085
CC BY-SA 4.0
2021-06-11T09:05:34.160Z
Thanks for the answers. Why the down votes? I was new to bitcoin.stackexchange asking for practical advice on how to create a proof of work with Python, instead I got links with theory.
Christopher
<mining-theory><mining-reward>
How Do I unlock a Bitcoin Blockchain with my Proof Of Work?
Christopher
# How Do I unlock a Bitcoin Blockchain with my Proof Of Work?

(Asked by: username_0 on 2021-06-07)

Can anyone help with some good explanations on next steps for Bitcoin mining.

Hypothetically let's say I create an algorithm that successfully guesses the code to unlock a Bitcoin blockchain

gives me a result such as: 000000000000000000008d0cff17edbe038f81208fecf9a66be9a2b8321c6ec7

I believe the results of this computational effort is called **'Proof Of Work'**

Where do I paste this **'Proof Of Work'** on the internet to claim the Bitcoin?

Also can you refer me to any helpful API where I can check my 'Proof Of Work' against already claimed 'Proof Of Work'

A basic video on how I might create such an algorithm can be found here <https://youtu.be/ZhnJ1bkIWWk> unfortunately it doesn't mention next step.

I am sorry if this seems like a really basic question, after much googling I am still confused.

I would like to try and make edits to such an algorithm and see if I can unlock a blockchain and claim bitcoin reward. I am not asking how to write an algorithm to unlock bitcoin, I would like an explanation of what I do with the result of the algorithm i.e. how do you cash in that lottery ticket?

Any advice appreciated.

**Tags:** mining-theory, mining-reward

> **Comments:**
> **username_1**: Does this answer your question? [What exactly is Mining?](https://bitcoin.stackexchange.com/questions/148/what-exactly-is-mining). You broadcast to a few peers  your new block, the first transaction in that block is one you created that pays you the block reward + mining fees. Spending that is  deferred for 100 blocks.  
> **username_0**: It was an interesting question and set of responses, but I am looking for more practical advice, suppose I was to run this code [link](https://github.com/codebasics/cool_python_apps/blob/main/2_bitcoin_mining/bitcoin_mining.py) and generate proof of work, how do I broadcast that to peers. Does this code on github that uses sha256 automatically inform the network? Can I just add the resulting characters into a bitcoin wallet?  
> **username_2**: The code you link doesn't implement bitcoin at all, just a toy scheme that is partly similar to a few small parts. And even if you used code that is correct but still in python it would run only on CPUs, and today CPU mining is uncompetitive by many orders of magnitude. To 'win' a block with python code you would need to own many times more computers than exist in the world and buy probably most of the electric power; I consider that impractical.  
> **username_0**: Thanks for the answers. Why the down votes? I was new to bitcoin.stackexchange asking for practical advice on how to create a proof of work with Python, instead I got links with theory.  

---

## 1 answers

### Answer by username_1 on 2021-06-07. Score: 1

To generate the proof of work your program first creates a block template from a set of transactions. The first transaction is normally called a coinbase transaction and it is invented by the miner, it has outputs of the miner's choosing, typically it pays the miner. It has no explicit inputs, the sum of outputs is no more than sum of transaction fees from the other included transactions (if any) plus the current block reward which halves every few years. The program chooses a value for a nonce field in the block and calculates the hash of most of the data described above. If the hash is greater than the target, the miner must vary the nonce or other data in the block and retry.

If the hash is less than the target, the program makes its few peer nodes aware of it's new block. It does this using the `inv` message type described in the [bitcoin protocol documentation](https://en.bitcoin.it/wiki/Protocol_documentation#inv)

---

> suppose I was to run this code [link] and generate proof of work, how do I broadcast that to peers.

Firstly, that code has the following function. you need to write more appropriate code to populate the parameters including `transactions`

```
def mine(block_number, transactions, previous_hash, prefix_zeros):

```

I doubt that code actually works. The structure seems far too simple.

* It doesn't create locking scripts
* The transaction data is way too simplified. It is conceptual not real.
* It doesn't look up Transaction-Ids and output-indices for inputs
* It has `prefix_zeros` but real codes tests for less than target,
  not for number of leading zeroes.
* etc.

> Does this code on github that uses sha256 automatically inform the network?

No.

> Can I just add the resulting characters into a bitcoin wallet?

No. It is far more work than that code suggests. Start elsewhere.

Remember

* Bitcoin wallets don't contain money.
* Bitcoin wallets contain a secret number called a private key.
* There are many different types of transaction and a mining program
  needs to know about them to validate them.
* A mining program should collect unconfirmed transactions.
* this is all much more complicated than you are yet aware of.


---

