Bitcoin Forum

Other => Beginners & Help => Topic started by: ArsenShnurkov on January 19, 2013, 10:43:39 PM



Title: difficulty
Post by: ArsenShnurkov on January 19, 2013, 10:43:39 PM
What prevent the miners from generating the whole new blockchain with low difficulty (https://en.bitcoin.it/wiki/Difficulty) along the chain?
Thus the chain can be splitted from the starting block or from any checkpoint (https://en.bitcoin.it/wiki/Checkpoint_Lockin)...


Title: Re: difficulty
Post by: Gabi on January 19, 2013, 11:09:21 PM
The fact that no client would accept it?


Title: Re: difficulty
Post by: ArsenShnurkov on January 19, 2013, 11:11:24 PM
no client would accept it?

Why? New chain can be made longer than the existing one

1) take the place in the "official" chain where difficulty changes
2) generate 2016 blocks to establish low difficulty
3) generate rest of new chain with low difficulty
4) ship all of this to the network
5) ...
6) profit

UPD:
http://sourceforge.net/mailarchive/message.php?msg_id=28441042


Title: Re: difficulty
Post by: kokjo on January 19, 2013, 11:18:41 PM
the best chain is determined not by length(blocks) but by work(hashs).


Title: Re: difficulty
Post by: ArsenShnurkov on January 19, 2013, 11:21:24 PM
I mean not the length(blocks) but count(blocks in the chain)

the best chain is determined not by length(blocks) but by work(hashs).

I don't understood this. The number of calculated hashes is not included (https://en.bitcoin.it/wiki/Block_hashing_algorithm) into the block,
so there is no way to reliable determine the amount of work


Title: Re: difficulty
Post by: DeathAndTaxes on January 19, 2013, 11:30:18 PM
The "best" chain is the one with the highest combined difficulty.  So a longer but easier chain won't be seen as the "best/longest".

The only way to have the longer chain is to do more work then the current longest chain.


Title: Re: difficulty
Post by: kokjo on January 19, 2013, 11:33:46 PM
the best chain is determined not by length(blocks) but by work(hashs).

I don't understood this. The number of calculated hashes is not included (https://en.bitcoin.it/wiki/Block_hashing_algorithm) into the block,
so there is no way to reliable determine the amount of work

each blockheader contains a field called "bits", the field describes how much of the hash should be 0's, before the block is considered valid. this number is the same as difficulty.

a difficulty adjustment every 2 weeks, adjusts the bits field, depending on the timestamps on the first and last block in that difficulty period. trying to match 2 week on average: if blocks are created too fast, difficulty rises; too slow, difficulty falls.


Title: Re: difficulty
Post by: ArsenShnurkov on January 19, 2013, 11:35:53 PM
The "best" chain is the one with the highest combined difficulty.

Where i can find this check in the code? (in which source code file and function)


Title: Re: difficulty
Post by: 21after2 on January 19, 2013, 11:55:34 PM
This should probably be in mining, I would think, as opposed to newbies.


Title: Re: difficulty
Post by: ArsenShnurkov on January 19, 2013, 11:58:04 PM
I found global variable
CBigNum bnBestChainWork = 0;
in file
https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.cpp#L38

Now I want to understand how the code calculate this "Work"

class CBlockIndex (https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.h#L1592) contains member CBigNum bnChainWork; (https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.h#L1617)

Here
https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.cpp#L1925
https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.cpp#L2537
it is calculated
bnChainWork = (pindexNew->pprev ? pindexNew->pprev->bnChainWork : 0) + pindexNew->GetBlockWork();

https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.h#L1788
Code:
struct CBlockIndexWorkComparator
{
    bool operator()(CBlockIndex *pa, CBlockIndex *pb) {
        if (pa->bnChainWork > pb->bnChainWork) return false;
        if (pa->bnChainWork < pb->bnChainWork) return true;

        if (pa->GetBlockHash() < pb->GetBlockHash()) return false;
        if (pa->GetBlockHash() > pb->GetBlockHash()) return true;

        return false; // identical blocks
    }
};

global set of blocks
https://github.com/bitcoin/bitcoin/blob/0e31ae9818528d52bbd802a8917b7015f8e38ae7/src/main.cpp#L42
set (http://www.cplusplus.com/reference/set/set/)<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid; // may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed

Actually, I don't understood, how operator() is called in set:
http://www.sgi.com/tech/stl/stl_set.h
(I don't know C++ well)
Ok, it's usual technique - http://www.sgi.com/tech/stl/set.html

Where one can read about block's lifecycle with references to code?
How they are received, in which variables they are stored in memory, when they persists to disk and so on?


Title: Re: difficulty
Post by: deepceleron on January 20, 2013, 12:18:20 PM
http://bitcoin.org/bitcoin.pdf

Section 4; Section 11


The actual "strength" of the blockchain is the chain of difficulty targets stored in blocks since the time of the block one would attempt to replace. The SHA256 hashes of blocks are all below the target; finding such a hash is computationally expensive. By definition, other miners would reject a sequence of blocks where the difficulty target is not as expected for the difficulty period of 2016 blocks or the block hashes did not meet the difficulty.

In the actual block, "bits" (https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F) is a compact form of of the target.

Code:
{
  "hash":"000000000000026e70652663fd4a5e3a1c38cfdf0c1b2666308c3816d9c21321",
  "ver":2,
  "prev_block":"00000000000000e788455adcba11a49a452acd5298e3a26d2db4cb9fcc050d1a",
  "mrkl_root":"b5484c1dd069b73063ea5cc594826b1541a8d5fd069629e9e950bb304789670b",
  "time":1358682853,
  "bits":436545969,