Bitcoin Forum
May 22, 2024, 06:44:26 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: When bitcoind starts up, In where and which function difficulty is calculated?  (Read 315 times)
chameleon123 (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 17


View Profile
October 26, 2018, 08:08:16 AM
Last edit: October 26, 2018, 06:14:27 PM by chameleon123
Merited by DarkStar_ (5), ABCbits (2)
 #1

I am trying to understand bitcoin core deeply though debugging it is really a challenge for me because I'm newbie with gdb and not good with reading memory addresses and machine codes and getting variable values from there.

I wonder when starting up bitcoind where difficulty is calculated and how. I read the formula is pdiff/bdiff (current_difficulty). I f changed bdiff where pdiff is calculated?

There is a function in /src/rpc/blockchain.cpp named GetDifficulty but I think it is only used when rpc callings such as "bitcoin-cli getdifficulty" and "bitcoin-cli getblockheader  <hash of block> ". So what happens in the code if bitcoind is typed? Where will it be calculated?

Here is my own private version of bitcoin https://github.com/chameleon1239/bitcoin. I changed some parameters in chainparams.cpp and GetDifficulty function in  blockchain.cpp. but the whole code is untouched.

The problem I have with my code is -addnode command doesn't work properly.

Guys , the deadline is very close so any suggestions will be appreciated.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
October 26, 2018, 06:38:37 PM
Merited by ABCbits (1)
 #2

The difficulty is merely a user facing thing. In reality, it does not calculate the difficulty at all and the difficulty itself is never used anywhere.

The only thing that matters when it comes to the proof of work is that the block hash, when interpreted as a 256 bit little endian integer, is less than or equal to the target value.

chameleon123 (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 17


View Profile
October 27, 2018, 10:45:18 AM
 #3

The difficulty is merely a user facing thing. In reality, it does not calculate the difficulty at all and the difficulty itself is never used anywhere.

The only thing that matters when it comes to the proof of work is that the block hash, when interpreted as a 256 bit little endian integer, is less than or equal to the target value.

So you mean that if I change nBits and consensus.powLimit parameters in chainparams.cpp the whole network should work fine ? but I face a problem when I run two bitcoindd :

Code:
 bitcoind -dns=0 -dnsseed=0 -port=15333 -rpcport=15332 -datadir=/home/.../.bitcoin2 
and
Code:
bitcoind -dns=0 -dnsseed=0 -port=6333 -rpcport=6332 -addnode=127.0.0.1:15332 -debug=rpc

The two servers start up properly but when i type the command "bicoin-cli getblocktemplate" I get

Code:
error code: -10
error message:
Bitcoin is downloading blocks...

The problem is insanely strange to me since when I set those two parameters back to what they have been, The whole thing works fine. Here's my code http:// [url]https://github.com/chameleon1239/bitcoin.git . Would be really thankful if you take a look at it.

achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
October 30, 2018, 12:31:52 AM
 #4

So you mean that if I change nBits and consensus.powLimit parameters in chainparams.cpp the whole network should work fine ? but I face a problem when I run two bitcoindd :
Yes.

The two servers start up properly but when i type the command "bicoin-cli getblocktemplate" I get

Code:
error code: -10
error message:
Bitcoin is downloading blocks...
That error is completely unrelated to the difficulty. The getblocktemplate RPC will refuse to return a valid block template if it thinks it is not fully synced. That will happen if the timestamp of the most recent block is not within two hours of the computer's time.

chameleon123 (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 17


View Profile
October 30, 2018, 06:58:18 AM
 #5

Quote
That error is completely unrelated to the difficulty. The getblocktemplate RPC will refuse to return a valid block template if it thinks it is not fully synced. That will happen if the timestamp of the most recent block is not within two hours of the computer's time.

Thank you so much for the reply. I'm a bit confused a little bit here. The most recent block is my genesis block. If I create a new genesis block and do the compiling and starting up process, same thing will happen. That's why I speculated about difficulty.You say there is no problem with the network and I can create transactions properly?
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3402
Merit: 6642


Just writing some code


View Profile WWW
October 30, 2018, 01:53:12 PM
Merited by chameleon123 (3), ABCbits (1)
 #6

Quote
That error is completely unrelated to the difficulty. The getblocktemplate RPC will refuse to return a valid block template if it thinks it is not fully synced. That will happen if the timestamp of the most recent block is not within two hours of the computer's time.

Thank you so much for the reply. I'm a bit confused a little bit here. The most recent block is my genesis block. If I create a new genesis block and do the compiling and starting up process, same thing will happen. That's why I speculated about difficulty.You say there is no problem with the network and I can create transactions properly?
There are other conditions that can trigger this error, it is not just the timestamp. It may be possible that the fact that there is only the genesis block is what is causing the problem. I think it could be that just having the genesis block even if it is up to date will still cause the node to think it is still in InitialBlockDownload. You can just make the error go away by commenting out or removing these 2 lines in src/rpc/mining.cpp.

chameleon123 (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 17


View Profile
October 30, 2018, 05:55:23 PM
Last edit: November 13, 2018, 03:56:17 PM by chameleon123
 #7

Quote
That error is completely unrelated to the difficulty. The getblocktemplate RPC will refuse to return a valid block template if it thinks it is not fully synced. That will happen if the timestamp of the most recent block is not within two hours of the computer's time.

Thank you so much for the reply. I'm a bit confused a little bit here. The most recent block is my genesis block. If I create a new genesis block and do the compiling and starting up process, same thing will happen. That's why I speculated about difficulty.You say there is no problem with the network and I can create transactions properly?
There are other conditions that can trigger this error, it is not just the timestamp. It may be possible that the fact that there is only the genesis block is what is causing the problem. I think it could be that just having the genesis block even if it is up to date will still cause the node to think it is still in InitialBlockDownload. You can just make the error go away by commenting out or removing these 2 lines in src/rpc/mining.cpp.

Thank you so so much for taking time and finding the solution. I truly appreciate it. I went and saw IsInitialBlockDownload function and read it carefully. The reason is very ambiguous to me.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!