Bitcoin Forum
May 09, 2024, 07:22:14 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Generate block by the help of cgminer and getblocktemplate  (Read 569 times)
kapam (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 1


View Profile
October 30, 2017, 10:58:41 AM
Merited by ABCbits (1)
 #1

Hi there,

I want to analyze relation between “difficulty” and mining speed in my own private network. I mean I want to see how difficulty influence on the generation rate of producing block.
I ran the bitcoin and cgminer at the same time. Cgminer can see the bitcoin network which is already running. When I generate a block by “bitcoin-cli –datadir=”a given node directory” geneate 1” command, I can obviously see that cgminer informed about it and update the blockchain height. Since bitcoin in regtest mode generate block immediately and without mining, I have decided to generate a transaction by these following commands:

Add=Bitcoin-cli –datadir=first_node_path getnewaddress
Bitcoin-cli –datadir=second_node_path sendtoaddress $Add 10

I expect this command generate a block but it is not true and no block was generated after this commands.
So I tried to generate it by the help of “getblocktemplate” command. However, I don’t know it is correct or not for my goal. Above that I don’t know how I can make a block by the output of the command.

In the nutshell, I don't know how mine a block in regtest mode bitcoin network.

I will appreciate for any hint.
I can share the shell scripts which I wrote to run a given regtest network and generate template blocks.

1715282534
Hero Member
*
Offline Offline

Posts: 1715282534

View Profile Personal Message (Offline)

Ignore
1715282534
Reply with quote  #2

1715282534
Report to moderator
1715282534
Hero Member
*
Offline Offline

Posts: 1715282534

View Profile Personal Message (Offline)

Ignore
1715282534
Reply with quote  #2

1715282534
Report to moderator
1715282534
Hero Member
*
Offline Offline

Posts: 1715282534

View Profile Personal Message (Offline)

Ignore
1715282534
Reply with quote  #2

1715282534
Report to moderator
"This isn't the kind of software where we can leave so many unresolved bugs that we need a tracker for them." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6631


Just writing some code


View Profile WWW
October 30, 2017, 03:18:10 PM
Merited by ABCbits (2)
 #2

Hi there,

I want to analyze relation between “difficulty” and mining speed in my own private network. I mean I want to see how difficulty influence on the generation rate of producing block.
I ran the bitcoin and cgminer at the same time. Cgminer can see the bitcoin network which is already running. When I generate a block by “bitcoin-cli –datadir=”a given node directory” geneate 1” command, I can obviously see that cgminer informed about it and update the blockchain height. Since bitcoin in regtest mode generate block immediately and without mining,
You actually were mining. Bitcoin Core contains mining code for regtest. The difficulty of regtest is low enough that a CPU can mine a regtest block nearly instantaneously. The generate and generatetoaddress commands will perform mining operations.

I have decided to generate a transaction by these following commands:

Add=Bitcoin-cli –datadir=first_node_path getnewaddress
Bitcoin-cli –datadir=second_node_path sendtoaddress $Add 10

I expect this command generate a block but it is not true and no block was generated after this commands.
As there shouldn't be. Those are not mining commands, so blocks will not be mined after they are run. Blocks are not found for every single transaction.

To mine on the regtest network, you just need to use Bitcoin Core's generate or generatetoaddress commands.

kapam (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 1


View Profile
October 31, 2017, 10:56:29 AM
 #3

Thanks for time and answering Smiley

At first I wrote sth like this
#!/bin/bash


for j in $(seq 1 50);
do
   for i in $(seq 0 9);
   do
      bitcoin-cli -datadir=$i generate 2 #&>/dev/null
   done
done

I changed the difficulty in the GetDifficulty() function in the following file.
~/bitcoin/src/rpc/blockchain.cpp.

I set a huge big number such as 99999999999999999.

double GetDifficulty(const CBlockIndex* blockindex)
{
    return 99999999999999999;
    // the same as original. without change
}


but nothing changed after recomiple and run it again.

I changed it to sth like this

#!/bin/bash

TotalNodes=$((10))

for j in $(seq 1 100);
do
   buyerNode=$(($RANDOM%$TotalNodes))
   sellerNode=$(($RANDOM%$TotalNodes))

   if test $buyerNode -ne $sellerNode; then

      sellerBalance=$(bitcoin-cli -datadir=$sellerNode getbalance)

      sellerBalance=${sellerBalance%.*}
      echo "seller balance: $sellerBalance"

      sellerBalance=$(( sellerBalance - 1 ))
      if test $sellerBalance -ge 0; then
         bitcoin-cli -datadir=$sellerNode generatetoaddress 1 ${Address[$buyerNode]}
      fi
   fi
done


however, I have not seen any changes and the block made almost immediately.
then I decided to use 'getblocktemplate' to try to solve it by cgminer. the point is the template is truly uncompleted and transaction field is empty.

I simply want to know the difference between block generation speed in two cases; when difficulty is set to 1 and when is changed to 1.0e+15.

difficulty= 1.0 --> block generation speed = ?
difficulty= 1.0e+15 --> block generation speed = ?



achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6631


Just writing some code


View Profile WWW
October 31, 2017, 03:34:28 PM
Merited by ABCbits (1)
 #4

I changed the difficulty in the GetDifficulty() function in the following file.
~/bitcoin/src/rpc/blockchain.cpp.

I set a huge big number such as 99999999999999999.
That is not at all how difficulty works and not where it is changed. What you changed was the output of an RPC command, not any actual internal behavior.

First of all, Bitcoin does not actually use the difficulty number in mining. It uses a 256 bit integer called the target and a block's hash must be less than that target value. That target value is in turn used to calculate the difficulty value for us humans to understand, but the difficulty value itself is not used in Bitcoin anywhere.

I simply want to know the difference between block generation speed in two cases; when difficulty is set to 1 and when is changed to 1.0e+15.
Do change the difficulty, you will need to change this value to false: https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L281 and modify these two functions: https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L49 and https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L13 to give you a target that matches the difficulty that you want to have.

kapam (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 1


View Profile
November 01, 2017, 03:11:45 PM
 #5

Thanks for detailed hint;

I have read the mentioned functions and target concepts. target is in turn related to nBits parameter while generating a block. the nBits parameter is also used in some other functions such as CheckProofOfWork in pow.cpp https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L74 file. this is indeed the member of  CBlockIndex class; https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L213.

 apparently the main function is BlockAssembler::CreateNewBlock in https://github.com/bitcoin/bitcoin/blob/master/src/miner.cpp#L109

Still I am in doubt how much I must deep to the code for my purposes.  Sad Huh


kapam (OP)
Newbie
*
Offline Offline

Activity: 5
Merit: 1


View Profile
November 01, 2017, 03:18:10 PM
 #6

the other point is there are many constant numbers in the code that seem magic number to me.  Huh
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!