<ul>
<li>CheckBlock() --> Checks the info <em>inside a block</em> </li>
<li>AcceptBlock() --> Additional checks + write to disk</li>
<li>ProcessNewBlock() --> Calls <strong>both</strong> of the above and also a function called ActivateBestChain(), which decides whether propagation to the network is warranted, among other things.</li>
<li>ConnectBlock() and DisconnectBlock() Append the block in question to the end of the blockchain (or take it off from there), linking it with the previous block's hash.</li>
</ul>

<p>Bitcoin needs to be industrial-strength code, so expect to see checks everywhere, for anything that could possibly happen (i.e., >1 coinbase transactions in a block, etc.). Moreover, some database operations are atomic, there are decisions when to propagate new blocks to the network, and to further complicate matters, the source code itself is the best documentation out there (slowly changing though, Andreas' Book is just a start).  </p>

<p>Let's start with CheckBlock(), which is called by other functions:</p>

<p>Its sequence of operations/function calls are: </p>

<ul>
<li>CheckBlockHeader() --> checks mostly that the PoW is there;</li>
<li>fCheckMerkleRoot --> checks correctness of Merkle Root &amp; that there are no duplicate transactions </li>
<li>Checks size of the block, whether 1st transaction is coinbase, whether there are other (obviously invalid) coinbase transactions, <em>and only then</em> it checks the transactions and whether the maximum number of sig_ops has been surpassed (which prevents 'a "rogue miner" from creating an incredibly-expensive-to-validate block."');</li>
</ul>

<p>AcceptBlock() will make its own checks and write to disk;</p>

<p>So, in sum, CheckBlock() makes sure that there are no shenanigans within a particular block, AcceptBlock() will write to disk. ProcessNewBlock() calls both CheckBlock() and AcceptBlock(), here's its outline: </p>

<ul>
<li>Call to CheckBlock(); verifying it's ok;</li>
<li>Call to AcceptBlock(); as you can see from the comment prior to the call, AcceptBlock() will write it to disk;</li>
<li>Calls and Checks whether ActivateBestChain() is successful</li>
</ul>

<p>This leaves out ConnectBlock() and DisconnectBlock()...</p>

<p>ConnectBlock() and DisconnectBlock() Append the block in question to the end of the blockchain (or take it off from there).  These are complex functions, in my opinion they are ripe for a slow refactoring, but see the final note:</p>

<p>Final note:  I am <strong>not</strong> a bitcoin-core developer; if one could edit and help out here, I am sure this answer could be considerably improved.   I know one thing, though: the more you study the code, the better your understanding and appreciation of the industrial strength of this magic internet money.</p>

CC BY-SA 3.0
2015-05-08T13:15:30.787Z
2015-05-08T13:21:47.650Z
2015-05-08T13:21:47.650Z
linhares
<p>In the <a href="https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp" rel="nofollow">main.cpp bitcoin code</a>, there are a few different functions for managing blocks.</p>

<pre><code>ProcessNewBlock()
AcceptBlock()
CheckBlock()
ConnectBlock()
DisconnectBlock()
</code></pre>

<p>What is a good summary for what each of these methods do, and the differences between them?</p>

<bitcoin-core-development>
Which block() method does what?
morsecoder
# Which block() method does what?

(Asked by: username_0 on 2014-12-10)

In the [main.cpp bitcoin code](https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp), there are a few different functions for managing blocks.

```
ProcessNewBlock()
AcceptBlock()
CheckBlock()
ConnectBlock()
DisconnectBlock()

```

What is a good summary for what each of these methods do, and the differences between them?

**Tags:** bitcoin-core-development


---

## 1 answers

### Answer by username_1 on 2015-05-08. Score: 4 | Accepted answer

* CheckBlock() --> Checks the info *inside a block*
* AcceptBlock() --> Additional checks + write to disk
* ProcessNewBlock() --> Calls **both** of the above and also a function called ActivateBestChain(), which decides whether propagation to the network is warranted, among other things.
* ConnectBlock() and DisconnectBlock() Append the block in question to the end of the blockchain (or take it off from there), linking it with the previous block's hash.

Bitcoin needs to be industrial-strength code, so expect to see checks everywhere, for anything that could possibly happen (i.e., >1 coinbase transactions in a block, etc.). Moreover, some database operations are atomic, there are decisions when to propagate new blocks to the network, and to further complicate matters, the source code itself is the best documentation out there (slowly changing though, Andreas' Book is just a start).

Let's start with CheckBlock(), which is called by other functions:

Its sequence of operations/function calls are:

* CheckBlockHeader() --> checks mostly that the PoW is there;
* fCheckMerkleRoot --> checks correctness of Merkle Root & that there are no duplicate transactions
* Checks size of the block, whether 1st transaction is coinbase, whether there are other (obviously invalid) coinbase transactions, *and only then* it checks the transactions and whether the maximum number of sig\_ops has been surpassed (which prevents 'a "rogue miner" from creating an incredibly-expensive-to-validate block."');

AcceptBlock() will make its own checks and write to disk;

So, in sum, CheckBlock() makes sure that there are no shenanigans within a particular block, AcceptBlock() will write to disk. ProcessNewBlock() calls both CheckBlock() and AcceptBlock(), here's its outline:

* Call to CheckBlock(); verifying it's ok;
* Call to AcceptBlock(); as you can see from the comment prior to the call, AcceptBlock() will write it to disk;
* Calls and Checks whether ActivateBestChain() is successful

This leaves out ConnectBlock() and DisconnectBlock()...

ConnectBlock() and DisconnectBlock() Append the block in question to the end of the blockchain (or take it off from there). These are complex functions, in my opinion they are ripe for a slow refactoring, but see the final note:

Final note: I am **not** a bitcoin-core developer; if one could edit and help out here, I am sure this answer could be considerably improved. I know one thing, though: the more you study the code, the better your understanding and appreciation of the industrial strength of this magic internet money.


---

