Author

Topic: difficulty (Read 553 times)

legendary
Activity: 1512
Merit: 1036
January 20, 2013, 06:48:20 AM
#11
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" is a compact form of of the target.

Code:
{
  "hash":"000000000000026e70652663fd4a5e3a1c38cfdf0c1b2666308c3816d9c21321",
  "ver":2,
  "prev_block":"00000000000000e788455adcba11a49a452acd5298e3a26d2db4cb9fcc050d1a",
  "mrkl_root":"b5484c1dd069b73063ea5cc594826b1541a8d5fd069629e9e950bb304789670b",
  "time":1358682853,
  "bits":436545969,
legendary
Activity: 1386
Merit: 1000
January 19, 2013, 06:28:04 PM
#10
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 contains member CBigNum bnChainWork;

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 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?
member
Activity: 112
Merit: 16
January 19, 2013, 06:25:34 PM
#9
This should probably be in mining, I would think, as opposed to newbies.
legendary
Activity: 1386
Merit: 1000
January 19, 2013, 06:05:53 PM
#8
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)
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 19, 2013, 06:03:46 PM
#7
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 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.
donator
Activity: 1218
Merit: 1079
Gerald Davis
January 19, 2013, 06:00:18 PM
#6
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.
legendary
Activity: 1386
Merit: 1000
January 19, 2013, 05:51:24 PM
#5
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 into the block,
so there is no way to reliable determine the amount of work
legendary
Activity: 1050
Merit: 1000
You are WRONG!
January 19, 2013, 05:48:41 PM
#4
the best chain is determined not by length(blocks) but by work(hashs).
legendary
Activity: 1386
Merit: 1000
January 19, 2013, 05:41:24 PM
#3
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
legendary
Activity: 1148
Merit: 1008
If you want to walk on water, get out of the boat
January 19, 2013, 05:39:21 PM
#2
The fact that no client would accept it?
legendary
Activity: 1386
Merit: 1000
January 19, 2013, 05:13:39 PM
#1
What prevent the miners from generating the whole new blockchain with low difficulty along the chain?
Thus the chain can be splitted from the starting block or from any checkpoint...
Jump to: