<p>Miners don't calculate nonces, they calculate cryptographic hashes <em>using</em> nonces as one of the inputs.</p>

<p>A cryptographic hash is a function that takes as input some arbitrary data and produces as output a unique string.[1]  They always produce the same output string for the same input data.  Here's an example using the sha256 hash function (the basis for the function used in Bitcoin):</p>

<pre><code>$ echo foo | sha256sum
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c  -
$ echo foo | sha256sum
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c  -
$ echo bar | sha256sum
7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730  -
</code></pre>

<p>In the above, "foo" and "bar" are the input and the strings starting with b5bb and 7d86 are the output.</p>

<p>What miners attempt to do is find a hash output that has a certain number of zeros in a certain location (we usually call this the start or front of the hash[2]).  To do this they start with the data they want to add to the ledger and then increment a nonce (or otherwise adjust the block) until they get the desired value.</p>

<p>For example, let's say we want to add a transaction of "Alice pays Bob 10 BTC" to the ledger and the number of zeros we want at the start of the hash is just 1.  We start by doing this:</p>

<pre><code>$ echo "Alice pays Bob 10 BTC | nonce: 0" | sha256sum
39a946f968d6986b2d560bbb0d63af04578352f02f48c1bfd71e535cd1148a6f  -
</code></pre>

<p>As you can see, our first nonce of "0" didn't produce a hash starting with 0.  So we try again:</p>

<pre><code>$ echo "Alice pays Bob 10 BTC | nonce: 1" | sha256sum
dce8dff1c536cdb43861e4ea1bc843e6f110e82768cfcca656d8c5f53cc7fc1d  -
</code></pre>

<p>Again, we don't get the desired result.  So we keep trying over and over until we do get the desired result:</p>

<pre><code>$ echo "Alice pays Bob 10 BTC | nonce: 18" | sha256sum
005b8307be5a9447be13ba6f026e9d3b695bfd5dc6ce633ac928de8cd8634a1f  -
</code></pre>

<p>So we had to do the work of generating a hash 19 times before finding a hash starting with the number zero.  This is called proof of work.</p>

<p>Now the person who found this proof of work can share all of the data---both the transaction and the nonce---to other people, and those people can verify that the input data of "Alice pays Bob 10 BTC | nonce: 18" does equal the a hash with one zero worth of proof of work.</p>

<p>One zero worth of proof of work isn't much (it's 4 bits or odds of 1:15), but real miners deal with much greater amounts and producing all of the work requires consuming a large amount of electricity.  Normally miners are rewarded for consuming that electricity by receiving a reward for protecting transactions with proof of work---but they only receive the reward if the transactions they protect are valid.[3]  Otherwise any proof of work they generate is wasted, meaning the electricity they use is wasted and they'll be less profitable than other miners who protect valid transactions.</p>

<p>For example, in the above example, if Alice didn't have 10 BTC to give to Bob, then that transaction would be invalid and so it wouldn't matter that the hash of the data starts with a zero---users who verify every transaction using what's called a "full node" will simply ignore that transaction and its proof of work.</p>

<p>So in short, what prevents a miner from adding an invalid transaction to the ledger is a combination of users who reject invalid transactions with their full nodes and an economic incentive paid to miners who mine valid transactions.</p>

<p>[1] Hash functions don't always work like this, but that's the ideal.</p>

<p>[2] But that's because Bitcoin displays hashes weirdly.  To any other tool, it'd be the end or back of the hash.</p>

<p>[3] Technically, they only receive the reward if other people <em>think</em> their transactions are valid.  Currently, there are a lot of Bitcoin users who verify every transaction, so it's not possible to sneak in an invalid transaction.  In other cryptocurrencies or a sad future where Bitcoin becomes less secure, that may not be the case</p>

CC BY-SA 4.0
2018-06-04T02:55:14.017Z
What I didn't understand is only one person get a chance to add a new block to the chain right then he can add invalid transaction to append this to chain?after add new block it will be broadcasted to whole network then how other people validate the new block. will they calculate the hash and check with it or how?
sainaveen devarapalli
CC BY-SA 4.0
2018-06-04T08:54:25.493Z
As the answer explains, hash functions "always produce the same output string for the same input data."  So if a miner sends a different set of transactions than the set he used to generate the hash, the hash will change and will no longer have the correct minimum number of leading zeros.  For example, if I change the above "Alice pays Bob 10 BTC | nonce: 18" to Alice paying Charlie 10 BTC, I get a hash starting with `aa0371`, which no full verification node would accept.
David A. Harding
CC BY-SA 4.0
2018-06-05T13:15:28.100Z
So every full node will again do proof of work to check whether difficulty is acheived by comparing hash having leading zeros?Am I correct?
sainaveen devarapalli
CC BY-SA 4.0
2018-06-05T13:22:13.907Z
I'm not sure about your terminology.  Doing a single hash is a little bit of "work."  Doing a lot of hashes to find one with many leading zeros is a lot of work, and because finding it is an unpredictable (random) process, it's proof of all the hashes (work) you did before.  Someone can check that hash is valid by doing just that one last hash, so they aren't redoing the proof of work---they're just checking the proof of work is valid.
David A. Harding
CC BY-SA 4.0
2018-06-06T14:41:54.630Z
Thank you sir.... I got it ... So, if a person did a proof of work by calculating nonce so that others will use that nonce to check whether it is valid or not....
sainaveen devarapalli
CC BY-SA 4.0
2018-06-06T14:44:53.453Z
Sorry ... Last one doubt if the proof of work is valid then which hash they will append to the blockchain ...the hash which is having zeros or the hash they try to make it have leading zeros.
sainaveen devarapalli
CC BY-SA 4.0
2018-06-01T17:34:06.467Z
2018-06-01T17:34:06.467Z
David A. Harding
<p>I read about blockchain in many websites but didn't understand what actually blockchain offers the proof of work.</p>

<p>A transaction will be broadcasted to the every node in the network and then someone will get a chance to add a new block to the existing ledger. How can we trust that on the person who adds the block to ledger is valid i.e he can add invalid transaction and append to the ledger? </p>

<p>He can calculate the hash of existing ledger and invalid transaction and can append to the existing ledger? And what do the miners? By calculating nonce what they will do?  I didn't understand how the update to ledger is happening.</p>

CC BY-SA 4.0
2018-06-02T11:41:10.327Z
Short answer: Every honest full node knows all the rules for determining whether or not a block is valid and ignores all invalid blocks entirely.
David Schwartz
<blockchain>
Why do we trust that the person who got a chance to add block will add a valid transaction block?
sainaveen devarapalli
# Why do we trust that the person who got a chance to add block will add a valid transaction block?

(Asked by: username_0 on 2018-06-01)

I read about blockchain in many websites but didn't understand what actually blockchain offers the proof of work.

A transaction will be broadcasted to the every node in the network and then someone will get a chance to add a new block to the existing ledger. How can we trust that on the person who adds the block to ledger is valid i.e he can add invalid transaction and append to the ledger?

He can calculate the hash of existing ledger and invalid transaction and can append to the existing ledger? And what do the miners? By calculating nonce what they will do? I didn't understand how the update to ledger is happening.

**Tags:** blockchain

> **Comments:**
> **username_1**: Short answer: Every honest full node knows all the rules for determining whether or not a block is valid and ignores all invalid blocks entirely.  

---

## 1 answers

### Answer by username_2 on 2018-06-01. Score: 1

Miners don't calculate nonces, they calculate cryptographic hashes *using* nonces as one of the inputs.

A cryptographic hash is a function that takes as input some arbitrary data and produces as output a unique string.[1] They always produce the same output string for the same input data. Here's an example using the sha256 hash function (the basis for the function used in Bitcoin):

```
$ echo foo | sha256sum
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c  -
$ echo foo | sha256sum
b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c  -
$ echo bar | sha256sum
7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730  -

```

In the above, "foo" and "bar" are the input and the strings starting with b5bb and 7d86 are the output.

What miners attempt to do is find a hash output that has a certain number of zeros in a certain location (we usually call this the start or front of the hash[2]). To do this they start with the data they want to add to the ledger and then increment a nonce (or otherwise adjust the block) until they get the desired value.

For example, let's say we want to add a transaction of "Alice pays Bob 10 BTC" to the ledger and the number of zeros we want at the start of the hash is just 1. We start by doing this:

```
$ echo "Alice pays Bob 10 BTC | nonce: 0" | sha256sum
39a946f968d6986b2d560bbb0d63af04578352f02f48c1bfd71e535cd1148a6f  -

```

As you can see, our first nonce of "0" didn't produce a hash starting with 0. So we try again:

```
$ echo "Alice pays Bob 10 BTC | nonce: 1" | sha256sum
dce8dff1c536cdb43861e4ea1bc843e6f110e82768cfcca656d8c5f53cc7fc1d  -

```

Again, we don't get the desired result. So we keep trying over and over until we do get the desired result:

```
$ echo "Alice pays Bob 10 BTC | nonce: 18" | sha256sum
005b8307be5a9447be13ba6f026e9d3b695bfd5dc6ce633ac928de8cd8634a1f  -

```

So we had to do the work of generating a hash 19 times before finding a hash starting with the number zero. This is called proof of work.

Now the person who found this proof of work can share all of the data---both the transaction and the nonce---to other people, and those people can verify that the input data of "Alice pays Bob 10 BTC | nonce: 18" does equal the a hash with one zero worth of proof of work.

One zero worth of proof of work isn't much (it's 4 bits or odds of 1:15), but real miners deal with much greater amounts and producing all of the work requires consuming a large amount of electricity. Normally miners are rewarded for consuming that electricity by receiving a reward for protecting transactions with proof of work---but they only receive the reward if the transactions they protect are valid.[3] Otherwise any proof of work they generate is wasted, meaning the electricity they use is wasted and they'll be less profitable than other miners who protect valid transactions.

For example, in the above example, if Alice didn't have 10 BTC to give to Bob, then that transaction would be invalid and so it wouldn't matter that the hash of the data starts with a zero---users who verify every transaction using what's called a "full node" will simply ignore that transaction and its proof of work.

So in short, what prevents a miner from adding an invalid transaction to the ledger is a combination of users who reject invalid transactions with their full nodes and an economic incentive paid to miners who mine valid transactions.

[1] Hash functions don't always work like this, but that's the ideal.

[2] But that's because Bitcoin displays hashes weirdly. To any other tool, it'd be the end or back of the hash.

[3] Technically, they only receive the reward if other people *think* their transactions are valid. Currently, there are a lot of Bitcoin users who verify every transaction, so it's not possible to sneak in an invalid transaction. In other cryptocurrencies or a sad future where Bitcoin becomes less secure, that may not be the case

> **Comments:**
> **username_0**: What I didn't understand is only one person get a chance to add a new block to the chain right then he can add invalid transaction to append this to chain?after add new block it will be broadcasted to whole network then how other people validate the new block. will they calculate the hash and check with it or how?  
> **username_2**: As the answer explains, hash functions "always produce the same output string for the same input data."  So if a miner sends a different set of transactions than the set he used to generate the hash, the hash will change and will no longer have the correct minimum number of leading zeros.  For example, if I change the above "Alice pays Bob 10 BTC | nonce: 18" to Alice paying Charlie 10 BTC, I get a hash starting with `aa0371`, which no full verification node would accept.  
> **username_0**: So every full node will again do proof of work to check whether difficulty is acheived by comparing hash having leading zeros?Am I correct?  
> **username_2**: I'm not sure about your terminology.  Doing a single hash is a little bit of "work."  Doing a lot of hashes to find one with many leading zeros is a lot of work, and because finding it is an unpredictable (random) process, it's proof of all the hashes (work) you did before.  Someone can check that hash is valid by doing just that one last hash, so they aren't redoing the proof of work---they're just checking the proof of work is valid.  
> **username_0**: Thank you sir.... I got it ... So, if a person did a proof of work by calculating nonce so that others will use that nonce to check whether it is valid or not....  
> **username_0**: Sorry ... Last one doubt if the proof of work is valid then which hash they will append to the blockchain ...the hash which is having zeros or the hash they try to make it have leading zeros.  

---

