How to mine a Bitcoin - Tutorials - CryptoTalk.Org Jump to content
sandybay

How to mine a Bitcoin

Recommended Posts

Newly minted Bitcoin are rewards for a mathematical race that maintains the ledger of transactions. Rules for the race and rewards are fixed and inherent in the protocol. It is the decentralised incentive structure to keep the ledger honest and the value scarce that sets Bitcoin apart from other currencies and assets. Herein I provide a detailed explanation and code of how Bitcoins are mined.
 

Blocks

Bitcoin transactions are grouped by timeframe (roughly 10 minutes) and ordered in blocks with meta data that meet protocol requirements. Each block is sequential and agreed by consensus. The first miner to reach meta data requirements for the current block and reach approval through network consensus is awarded the block reward in addition to all transaction fees in that block. See the two figures below to understand the incentive structure for mining.
Bitcoin supply
Bitcoin increasing fees offset decreasing block reward

Presently, the block reward is 12.5 bitcoin. At the current price ( ~ $ 10,000 / btc ), that's more than $ 125,000 every 10 minutes! I hope that sparks your interest. It did for me. The first step in mining is to maintain a bitcoin node, which stores and validates the full ledger. This can be installed from Bitcoin.org. I will use KDB+ to interface with the node and mine Bitcoin as it is the most efficient way to handle data. Full code can be seen at: our Github page.
 

Block templates

A template for the current block can be attained from our local node. (for details, see: Bitcoin.org)

template: .bitcoind.getblocktemplate[template_request][`result]

Here are the details we need:

template[`height] : 605355 // block height to be mined
template[`curtime] : 1574689408 // unix epoch time 
template[`coinbasevalue] : 1274375042 // block reward in satoshis
template[`version] : 536870912 // indicates which validation rules to use
template[`bits] : "1715b23e" // encoded target
template[`target] : "00000000000000000015b23e0000000000000000000000000000000000000000"
template[`previousblockhash] : "000000000000000000042e1de089b9ee56a5aeab5f49dc534f090bfe022cbec8"            
first template[`transactions] : 
data   | "0200000002446b124c95e37465f0d8a229450f6f77cb031b188a7d2f7074b..."
txid   | "9995322b1aa0c4518e031efadc9a51da4831217631010662c36516e776594e17"
            


 

Block headers

Our job as the miner is to construct the required header with this information that hashes to a lower value than the target. The header itself is a concatenated hexadecimal string consisting of the following (from Bitcoin.org😞
Block header

Bytes Name Data Type Description
4 version int32_t The block version number indicates which set of block validation rules to follow.
32 previous block header hash char[32] A SHA256(SHA256()) hash in internal byte order of the previous block’s header. This ensures no previous block can be changed without also changing this block’s header.
32 merkle root hash char[32] A SHA256(SHA256()) hash in internal byte order. The merkle root is derived from the hashes of all transactions included in this block, ensuring that none of those transactions can be modified without modifying the header.
4 time uint32_t The block time is a Unix epoch time when the miner started hashing the header (according to the miner). Must be strictly greater than the median time of the previous 11 blocks. Full nodes will not accept blocks with headers more than two hours in the future according to their clock.
4 nBits uint32_t An encoded version of the target threshold this block’s header hash must be less than or equal to.
4 nonce uint32_t An arbitrary number miners change to modify the header hash in order to produce a hash less than or equal to the target threshold. If all 32-bit values are tested, the time can be updated or the coinbase transaction can be changed and the merkle root updated.


 

Merkle root

"Merkle roots allow for efficient and secure verification of large data structures, and in the case of blockchains, potentially boundless data sets." (coinomi) Merkle tree
Our Merkle root function:

buildmerkleroot:{[leafs] 
    leafcount:  count leafs;
    root:  $[1 = leafcount; first leafs;
        [if [0 < leafcount mod 2; leafs: leafs,enlist last leafs; leafcount: 1+leafcount;]; // if odd number, duplicate last record
        oddindicies: 1+2*til "i"$(leafcount%2);
        evenindicies: 2*til "i"$(leafcount%2);
        hashpairs: leafs[evenindicies] ,' leafs[oddindicies];
        branches:{  doubleSha256Byte x } peach hashpairs;  
        buildmerkleroot[branches] ]  ];
    root };

Order transaction hashes to build the merkle root.

hashlist: {x[`hash]} each transactions;
hashlist: (enlist coinbasetranshash), hashlist;
bytehashlist: hexstring_to_hex peach hashlist;
bytehashlistBigEndian:reverse peach bytehashlist;
Merkleroot: buildmerkleroot[bytehashlistBigEndian]; 
                        

If you are astute, you may have noticed the coinbase transaction hash creep in there.
 

Coinbase transaction

The coinbase transaction is the record of the reward we pay ourselves for mining the block. As miners, we must place the coinbase transaction at the beginning of the transaction list. Format from Bitcoin.org
Coinbase transaction

Bytes Name Data Type Description
4 version int32_t Transaction version number (note, this is signed); currently version 1 or 2. Programs creating transactions using newer consensus rules may use higher version numbers. Version 2 means that BIP 68 applies.
Varies tx_in count compactSize uint Number of inputs in this transaction.
32 hash (null) char[32] A 32-byte null, as a coinbase has no previous outpoint.
4 index (UINT32_MAX) uint32_t 0xffffffff, as a coinbase has no previous outpoint.
Varies script bytes compactSize uint The number of bytes in the coinbase script, up to a maximum of 100 bytes.
Varies (4) height script The block height of this block as required by BIP34. Uses script language: starts with a data-pushing opcode that indicates how many bytes to push to the stack followed by the block height as a little-endian unsigned integer. This script must be as short as possible, otherwise it may be rejected.

The data-pushing opcode will be 0x03 and the total size four bytes until block 16,777,216 about 300 years from now.
Varies coinbase script None The coinbase field: Arbitrary data not exceeding 100 bytes minus the (4) height bytes. Miners commonly place an extra nonce in this field to update the block header merkle root during hashing.
4 sequence uint32_t Sequence number.
Varies tx_out count compactSize uint Number of outputs in this transaction.
8 value int64_t Number of satoshis to spend. Coinbase transactions spend the block subsidy and collected transaction fees.
1+ pk_script bytes compactSize uint Number of bytes in the pubkey script. Maximum is 10,000 bytes.
Varies pk_script char[] Defines the conditions which must be satisfied to spend this output.
4 lock_time uint32_t A time (Unix epoch time) or block number.
coinbaseversion : "01000000"
coinbasein : "01"
coinbaseprevtx : "0000000000000000000000000000000000000000000000000000000000000000"
coinbaseprevout : "ffffffff";
coinbaseseq  :"00000000";
coinbaseout : "01";
cbpubkey : "866b6c77fd236f9185bb11b4e176328d7f912a4b";
scriptPubKey : "76","A9","14" , cbpubkey, "88", "AC";
coinbasesl : "19";
coinbaselocktime : "00000000";
coinbasesats: littleEndian [ coinbasevalue; 16 ];
coinbasescript : "03" , ( littleEndian [ height; 6 ] ), texttohexstr [ "Reap what you sow." ];
coinbasescriptlen : littleEndian[ "i"$( count coinbasescript ) % 2;2];
coinbasetrans: coinbaseversion, coinbasein, coinbaseprevtx, coinbaseprevout, coinbasescriptlen, coinbasescript, coinbaseseq, coinbaseout, coinbasesats, coinbasesl, scriptPubKey, coinbaselocktime;
coinbasetransbytes : hexstring_to_hex coinbasetrans;
coinbasetranshash : raze string reverse doubleSha256Byte [ coinbasetransbytes ];
                            

Again, for the astute there are a couple of points to note here outside of the format to pay yourself. First is that there are hidden messages encoded in each mined block (see cryptograffiti for examples ). Second is that there is a pubkey script language, using op codes to spend the outputs. This is bitcoin's smart contracts (for details see pubkey scripts). We use a standard Pay-to-Public-Key-Hash opcodes:

Public key script

  1. OP_DUP ( 0x76 ) - Duplicates the top stack item.
  2. OP_HASH160 ( 0xA9 ) - The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
  3. OP_EQUALVERIFY ( 0x88 ) - Returns 1 if the inputs are exactly equal, 0 otherwise, runs OP_VERIFY afterward.
  4. OP_CHECKSIG ( 0xAC ) - The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.

 

Hash race

Now we have the constructs to find a live solution and submit it to the blockchain for reward. The block is now simple to put together:

Block : Version , hashPrevBlock , Merkleroot , Time , Bits, 4#reverse 0x0 vs Nonce, transcounter , raze trans

Except that the header hash value must be less than the extremely improbable target value. To achieve this, miners can vary the Nonce from 0 to 4,294,967,295. They can also update the transaction pool or message text to attain a different merkle root. Miner software and hardware optimisations are geared towards looping through different Nonce value hashes at faster rates, often at great expense ( with setup, electricity and security ). Our script is a portable, energy efficient parallel CPU solution; competitve with pooled resources or a high performance cpu cluster. One run through the entire Nonce range looks like this:

curBlock: 0N! computeData`height;
latBlock: 0N! blockcount[];
i :4200000000;
while[11b ~  (curBlock > latBlock) , i > 0; // check if block is still current 
    .Q.gc[]; // clean memory
    computehash peach i + til 100000000; // process in batches, typically takes 1-2 minutes on a normal modern computer
    i : 0N! i - 100000000;
    latBlock: 0N! blockcount[]; ];

One run through of 4.3 billion Nonces would meet the base rate difficulty of the genesis block a decade ago. The difficulty is self adjusting to average 10 minutes a block and now requires more iterations. In our code, we update the transaction list to create a new merkle root after exhausting the Nonce range. In cases of reward conflict, the longest transaction list wins. Here is the code and functions we use for the continous mining effort:

submit:{[headerHex]headstr:raze string headerHex; blk: headstr ,raze string computeData`trans; h (`.bitcoind.submitblock;blk);0N! "SUCCESS: ", headstr;};
hashcheck:{[finalHash;headerHex]
  if [finalHash[til 7] ~ 0x00000000000000;
  targ: computeData`target;
  if [finalHash[7] <= targ[7];
    finalHashstr: raze string finalHash;
    lz:first where finalHashstr <> "0";
    $[leadingzeros < lz; 
      [ submit[headerHex]; ];
      [ simpletarget: hex_to_int targ[8 9 10 11]
      simplehash: hex_to_int finalHash[8 9 10 11]
      if[ simplehash < simpletarget; submit[headerHex];]
      if [simplehash = simpletarget;
        simpletarget: hex_to_int targ[12 13 14 15]
        simplehash: hex_to_int finalHash[12 13 14 15]
        if[ simplehash < simpletarget; submit[headerHex];]
        if [simplehash = simpletarget;
          simpletarget: hex_to_int targ[16 17 18 19]
          simplehash: hex_to_int finalHash[16 17 18 19]
          if[simplehash < simpletarget; submit[headerHex];]
          if [simplehash = simpletarget;
            simpletarget: hex_to_int targ[20 21 22 23]
            simplehash: hex_to_int finalHash[20 21 22 23]
            if[ simplehash < simpletarget; submit[ headerHex ] ] ] ] ] ] ] ] ] }
              
computehash:{[Nonce]    
    hashcheck[reverse sha2561[ endMerkle , 4#reverse 0x0 vs Nonce; H0; target32 ]; computeData`partialHeader , 4#reverse 0x0 vs Nonce];        
    };    

cycleAttempts: { [ ]
    computeData:: h"loadUpdates[];computeData";
    H0::  sha2560 64#computeData`partialHeader;
    target32:: -4#8#computeData`target;
    leadingzeros::  first where (raze string computeData`target) <> "0";
    endMerkle::  -12#computeData`partialHeader;
    //process 100,000,000 at a time ~ 1 min on a normal modern computer
    // check if block has already been mined between efforts
    curBlock: 0N! computeData`height;
    latBlock: 0N! blockcount[];
    i :4200000000;
    while[11b ~  (curBlock > latBlock) , i > 0;
        .Q.gc[];
        computehash peach i + til 100000000;
        i : 0N! i - 100000000;
        latBlock: 0N! blockcount[];
    ];
    cycleAttempts[];
    };  

cycleAttempts[];    

Perhaps you can see the rabbit hole. I refer the interested to the bitcoin white paper. Please visit our github page for full code. Feedback is welcome at the email below.

  • Useful or interesting 4
  • +1 2
  • +3 1
  • +4 1

Share this post


Link to post
Share on other sites

Theoretically, you could use your computer's CPU to mine for bitcoins, but in practice, this is so slow by today's standards that there isn't any point. You can enhance your bitcoin hash rate by adding graphics hardware to your desktop computer. Graphics cards feature graphical processing units (GPUs).

Share this post


Link to post
Share on other sites

I can not read all the posts made @OP so many material submitted. So as not to make it into the head alone, But see little by little that you really really know a lot about mining.

Share this post


Link to post
Share on other sites
On 1/2/2020 at 6:15 PM, sandybay said:

Newly minted Bitcoin are rewards for a mathematical race that maintains the ledger of transactions. Rules for the race and rewards are fixed and inherent in the protocol. It is the decentralised incentive structure to keep the ledger honest and the value scarce that sets Bitcoin apart from other currencies and assets. Herein I provide a detailed explanation and code of how Bitcoins are mined.
 

Blocks

Bitcoin transactions are grouped by timeframe (roughly 10 minutes) and ordered in blocks with meta data that meet protocol requirements. Each block is sequential and agreed by consensus. The first miner to reach meta data requirements for the current block and reach approval through network consensus is awarded the block reward in addition to all transaction fees in that block. See the two figures below to understand the incentive structure for mining.
Bitcoin supply
Bitcoin increasing fees offset decreasing block reward

Presently, the block reward is 12.5 bitcoin. At the current price ( ~ $ 10,000 / btc ), that's more than $ 125,000 every 10 minutes! I hope that sparks your interest. It did for me. The first step in mining is to maintain a bitcoin node, which stores and validates the full ledger. This can be installed from Bitcoin.org. I will use KDB+ to interface with the node and mine Bitcoin as it is the most efficient way to handle data. Full code can be seen at: our Github page.
 

Block templates

A template for the current block can be attained from our local node. (for details, see: Bitcoin.org)


template: .bitcoind.getblocktemplate[template_request][`result]

Here are the details we need:


template[`height] : 605355 // block height to be mined
template[`curtime] : 1574689408 // unix epoch time 
template[`coinbasevalue] : 1274375042 // block reward in satoshis
template[`version] : 536870912 // indicates which validation rules to use
template[`bits] : "1715b23e" // encoded target
template[`target] : "00000000000000000015b23e0000000000000000000000000000000000000000"
template[`previousblockhash] : "000000000000000000042e1de089b9ee56a5aeab5f49dc534f090bfe022cbec8"            
first template[`transactions] : 
data   | "0200000002446b124c95e37465f0d8a229450f6f77cb031b188a7d2f7074b..."
txid   | "9995322b1aa0c4518e031efadc9a51da4831217631010662c36516e776594e17"
            


 

Block headers

Our job as the miner is to construct the required header with this information that hashes to a lower value than the target. The header itself is a concatenated hexadecimal string consisting of the following (from Bitcoin.org😞
Block header

Bytes Name Data Type Description
4 version int32_t The block version number indicates which set of block validation rules to follow.
32 previous block header hash char[32] A SHA256(SHA256()) hash in internal byte order of the previous block’s header. This ensures no previous block can be changed without also changing this block’s header.
32 merkle root hash char[32] A SHA256(SHA256()) hash in internal byte order. The merkle root is derived from the hashes of all transactions included in this block, ensuring that none of those transactions can be modified without modifying the header.
4 time uint32_t The block time is a Unix epoch time when the miner started hashing the header (according to the miner). Must be strictly greater than the median time of the previous 11 blocks. Full nodes will not accept blocks with headers more than two hours in the future according to their clock.
4 nBits uint32_t An encoded version of the target threshold this block’s header hash must be less than or equal to.
4 nonce uint32_t An arbitrary number miners change to modify the header hash in order to produce a hash less than or equal to the target threshold. If all 32-bit values are tested, the time can be updated or the coinbase transaction can be changed and the merkle root updated.


 

Merkle root

"Merkle roots allow for efficient and secure verification of large data structures, and in the case of blockchains, potentially boundless data sets." (coinomi) Merkle tree
Our Merkle root function:


buildmerkleroot:{[leafs] 
    leafcount:  count leafs;
    root:  $[1 = leafcount; first leafs;
        [if [0 < leafcount mod 2; leafs: leafs,enlist last leafs; leafcount: 1+leafcount;]; // if odd number, duplicate last record
        oddindicies: 1+2*til "i"$(leafcount%2);
        evenindicies: 2*til "i"$(leafcount%2);
        hashpairs: leafs[evenindicies] ,' leafs[oddindicies];
        branches:{  doubleSha256Byte x } peach hashpairs;  
        buildmerkleroot[branches] ]  ];
    root };

Order transaction hashes to build the merkle root.


hashlist: {x[`hash]} each transactions;
hashlist: (enlist coinbasetranshash), hashlist;
bytehashlist: hexstring_to_hex peach hashlist;
bytehashlistBigEndian:reverse peach bytehashlist;
Merkleroot: buildmerkleroot[bytehashlistBigEndian]; 
                        

If you are astute, you may have noticed the coinbase transaction hash creep in there.
 

Coinbase transaction

The coinbase transaction is the record of the reward we pay ourselves for mining the block. As miners, we must place the coinbase transaction at the beginning of the transaction list. Format from Bitcoin.org
Coinbase transaction

Bytes Name Data Type Description
4 version int32_t Transaction version number (note, this is signed); currently version 1 or 2. Programs creating transactions using newer consensus rules may use higher version numbers. Version 2 means that BIP 68 applies.
Varies tx_in count compactSize uint Number of inputs in this transaction.
32 hash (null) char[32] A 32-byte null, as a coinbase has no previous outpoint.
4 index (UINT32_MAX) uint32_t 0xffffffff, as a coinbase has no previous outpoint.
Varies script bytes compactSize uint The number of bytes in the coinbase script, up to a maximum of 100 bytes.
Varies (4) height script The block height of this block as required by BIP34. Uses script language: starts with a data-pushing opcode that indicates how many bytes to push to the stack followed by the block height as a little-endian unsigned integer. This script must be as short as possible, otherwise it may be rejected.

The data-pushing opcode will be 0x03 and the total size four bytes until block 16,777,216 about 300 years from now.
Varies coinbase script None The coinbase field: Arbitrary data not exceeding 100 bytes minus the (4) height bytes. Miners commonly place an extra nonce in this field to update the block header merkle root during hashing.
4 sequence uint32_t Sequence number.
Varies tx_out count compactSize uint Number of outputs in this transaction.
8 value int64_t Number of satoshis to spend. Coinbase transactions spend the block subsidy and collected transaction fees.
1+ pk_script bytes compactSize uint Number of bytes in the pubkey script. Maximum is 10,000 bytes.
Varies pk_script char[] Defines the conditions which must be satisfied to spend this output.
4 lock_time uint32_t A time (Unix epoch time) or block number.

coinbaseversion : "01000000"
coinbasein : "01"
coinbaseprevtx : "0000000000000000000000000000000000000000000000000000000000000000"
coinbaseprevout : "ffffffff";
coinbaseseq  :"00000000";
coinbaseout : "01";
cbpubkey : "866b6c77fd236f9185bb11b4e176328d7f912a4b";
scriptPubKey : "76","A9","14" , cbpubkey, "88", "AC";
coinbasesl : "19";
coinbaselocktime : "00000000";
coinbasesats: littleEndian [ coinbasevalue; 16 ];
coinbasescript : "03" , ( littleEndian [ height; 6 ] ), texttohexstr [ "Reap what you sow." ];
coinbasescriptlen : littleEndian[ "i"$( count coinbasescript ) % 2;2];
coinbasetrans: coinbaseversion, coinbasein, coinbaseprevtx, coinbaseprevout, coinbasescriptlen, coinbasescript, coinbaseseq, coinbaseout, coinbasesats, coinbasesl, scriptPubKey, coinbaselocktime;
coinbasetransbytes : hexstring_to_hex coinbasetrans;
coinbasetranshash : raze string reverse doubleSha256Byte [ coinbasetransbytes ];
                            

Again, for the astute there are a couple of points to note here outside of the format to pay yourself. First is that there are hidden messages encoded in each mined block (see cryptograffiti for examples ). Second is that there is a pubkey script language, using op codes to spend the outputs. This is bitcoin's smart contracts (for details see pubkey scripts). We use a standard Pay-to-Public-Key-Hash opcodes:

Public key script

  1. OP_DUP ( 0x76 ) - Duplicates the top stack item.
  2. OP_HASH160 ( 0xA9 ) - The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
  3. OP_EQUALVERIFY ( 0x88 ) - Returns 1 if the inputs are exactly equal, 0 otherwise, runs OP_VERIFY afterward.
  4. OP_CHECKSIG ( 0xAC ) - The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.

 

Hash race

Now we have the constructs to find a live solution and submit it to the blockchain for reward. The block is now simple to put together:


Block : Version , hashPrevBlock , Merkleroot , Time , Bits, 4#reverse 0x0 vs Nonce, transcounter , raze trans

Except that the header hash value must be less than the extremely improbable target value. To achieve this, miners can vary the Nonce from 0 to 4,294,967,295. They can also update the transaction pool or message text to attain a different merkle root. Miner software and hardware optimisations are geared towards looping through different Nonce value hashes at faster rates, often at great expense ( with setup, electricity and security ). Our script is a portable, energy efficient parallel CPU solution; competitve with pooled resources or a high performance cpu cluster. One run through the entire Nonce range looks like this:


curBlock: 0N! computeData`height;
latBlock: 0N! blockcount[];
i :4200000000;
while[11b ~  (curBlock > latBlock) , i > 0; // check if block is still current 
    .Q.gc[]; // clean memory
    computehash peach i + til 100000000; // process in batches, typically takes 1-2 minutes on a normal modern computer
    i : 0N! i - 100000000;
    latBlock: 0N! blockcount[]; ];

One run through of 4.3 billion Nonces would meet the base rate difficulty of the genesis block a decade ago. The difficulty is self adjusting to average 10 minutes a block and now requires more iterations. In our code, we update the transaction list to create a new merkle root after exhausting the Nonce range. In cases of reward conflict, the longest transaction list wins. Here is the code and functions we use for the continous mining effort:


submit:{[headerHex]headstr:raze string headerHex; blk: headstr ,raze string computeData`trans; h (`.bitcoind.submitblock;blk);0N! "SUCCESS: ", headstr;};
hashcheck:{[finalHash;headerHex]
  if [finalHash[til 7] ~ 0x00000000000000;
  targ: computeData`target;
  if [finalHash[7] <= targ[7];
    finalHashstr: raze string finalHash;
    lz:first where finalHashstr <> "0";
    $[leadingzeros < lz; 
      [ submit[headerHex]; ];
      [ simpletarget: hex_to_int targ[8 9 10 11]
      simplehash: hex_to_int finalHash[8 9 10 11]
      if[ simplehash < simpletarget; submit[headerHex];]
      if [simplehash = simpletarget;
        simpletarget: hex_to_int targ[12 13 14 15]
        simplehash: hex_to_int finalHash[12 13 14 15]
        if[ simplehash < simpletarget; submit[headerHex];]
        if [simplehash = simpletarget;
          simpletarget: hex_to_int targ[16 17 18 19]
          simplehash: hex_to_int finalHash[16 17 18 19]
          if[simplehash < simpletarget; submit[headerHex];]
          if [simplehash = simpletarget;
            simpletarget: hex_to_int targ[20 21 22 23]
            simplehash: hex_to_int finalHash[20 21 22 23]
            if[ simplehash < simpletarget; submit[ headerHex ] ] ] ] ] ] ] ] ] }
              
computehash:{[Nonce]    
    hashcheck[reverse sha2561[ endMerkle , 4#reverse 0x0 vs Nonce; H0; target32 ]; computeData`partialHeader , 4#reverse 0x0 vs Nonce];        
    };    

cycleAttempts: { [ ]
    computeData:: h"loadUpdates[];computeData";
    H0::  sha2560 64#computeData`partialHeader;
    target32:: -4#8#computeData`target;
    leadingzeros::  first where (raze string computeData`target) <> "0";
    endMerkle::  -12#computeData`partialHeader;
    //process 100,000,000 at a time ~ 1 min on a normal modern computer
    // check if block has already been mined between efforts
    curBlock: 0N! computeData`height;
    latBlock: 0N! blockcount[];
    i :4200000000;
    while[11b ~  (curBlock > latBlock) , i > 0;
        .Q.gc[];
        computehash peach i + til 100000000;
        i : 0N! i - 100000000;
        latBlock: 0N! blockcount[];
    ];
    cycleAttempts[];
    };  

cycleAttempts[];    

Perhaps you can see the rabbit hole. I refer the interested to the bitcoin white paper. Please visit our github page for full code. Feedback is welcome at the email below.

You create a long post which make me bored. I know that you work hard to create the post. I read some of this post and learn something about bitcoin mining. 

  • +3 1
  • +5 1

Improve online skills and get more information about online earning https://www.onlinefreemaster.com/

Share this post


Link to post
Share on other sites

Bitcoin mining is the process by which new bitcoins are entered into circulation, but it is also a critical component of the maintenance , we should buy miners and set up to mine btc

Share this post


Link to post
Share on other sites

A rather useful article, but I can say that there are much easier ways to get, but you need to invest a fairly large amount of funds in them.

  • +2 1

Share this post


Link to post
Share on other sites

Bitcoin mining is done by specialized computers. The role of miners is to secure the network and to process every Bitcoin transaction. Miners achieve this by solving a computational problem which allows them to chain together blocks of transactions (hence Bitcoin's famous “blockchain”).

Share this post


Link to post
Share on other sites

This is a very huge information, and explains the mining process with all its precise details. This is a great effort if you did it on your own, but if you have taken it from the internet then you must mention the source, otherwise it can be considered stealing and you will be punished.

Share this post


Link to post
Share on other sites

Your post very informative brother.you are explain everything about bitcoin mining.but honestly I don't understand your post. I think its helpful  for others people.

  • +1 1

Share this post


Link to post
Share on other sites

Dear members thank you for sharing this information but is very very long and you have created a confusion in any member here, because I see your title talk about How to mine a Bitcoin, but in your topic I see full content without explication.

  • Useful or interesting 1

 

468x60.gif.2f5be3d8fc5caf83e400a2cd4d4ca105.gif

Share this post


Link to post
Share on other sites
On 1/2/2020 at 2:15 PM, sandybay said:

Newly minted Bitcoin are rewards for a mathematical race that maintains the ledger of transactions. Rules for the race and rewards are fixed and inherent in the protocol. It is the decentralised incentive structure to keep the ledger honest and the value scarce that sets Bitcoin apart from other currencies and assets. Herein I provide a detailed explanation and code of how Bitcoins are mined.
 

Blocks

Bitcoin transactions are grouped by timeframe (roughly 10 minutes) and ordered in blocks with meta data that meet protocol requirements. Each block is sequential and agreed by consensus. The first miner to reach meta data requirements for the current block and reach approval through network consensus is awarded the block reward in addition to all transaction fees in that block. See the two figures below to understand the incentive structure for mining.
Bitcoin supply
Bitcoin increasing fees offset decreasing block reward

Presently, the block reward is 12.5 bitcoin. At the current price ( ~ $ 10,000 / btc ), that's more than $ 125,000 every 10 minutes! I hope that sparks your interest. It did for me. The first step in mining is to maintain a bitcoin node, which stores and validates the full ledger. This can be installed from Bitcoin.org. I will use KDB+ to interface with the node and mine Bitcoin as it is the most efficient way to handle data. Full code can be seen at: our Github page.
 

Block templates

A template for the current block can be attained from our local node. (for details, see: Bitcoin.org)


template: .bitcoind.getblocktemplate[template_request][`result]

Here are the details we need:


template[`height] : 605355 // block height to be mined
template[`curtime] : 1574689408 // unix epoch time 
template[`coinbasevalue] : 1274375042 // block reward in satoshis
template[`version] : 536870912 // indicates which validation rules to use
template[`bits] : "1715b23e" // encoded target
template[`target] : "00000000000000000015b23e0000000000000000000000000000000000000000"
template[`previousblockhash] : "000000000000000000042e1de089b9ee56a5aeab5f49dc534f090bfe022cbec8"            
first template[`transactions] : 
data   | "0200000002446b124c95e37465f0d8a229450f6f77cb031b188a7d2f7074b..."
txid   | "9995322b1aa0c4518e031efadc9a51da4831217631010662c36516e776594e17"
            


 

Block headers

Our job as the miner is to construct the required header with this information that hashes to a lower value than the target. The header itself is a concatenated hexadecimal string consisting of the following (from Bitcoin.org😞
Block header

Bytes Name Data Type Description
4 version int32_t The block version number indicates which set of block validation rules to follow.
32 previous block header hash char[32] A SHA256(SHA256()) hash in internal byte order of the previous block’s header. This ensures no previous block can be changed without also changing this block’s header.
32 merkle root hash char[32] A SHA256(SHA256()) hash in internal byte order. The merkle root is derived from the hashes of all transactions included in this block, ensuring that none of those transactions can be modified without modifying the header.
4 time uint32_t The block time is a Unix epoch time when the miner started hashing the header (according to the miner). Must be strictly greater than the median time of the previous 11 blocks. Full nodes will not accept blocks with headers more than two hours in the future according to their clock.
4 nBits uint32_t An encoded version of the target threshold this block’s header hash must be less than or equal to.
4 nonce uint32_t An arbitrary number miners change to modify the header hash in order to produce a hash less than or equal to the target threshold. If all 32-bit values are tested, the time can be updated or the coinbase transaction can be changed and the merkle root updated.


 

Merkle root

"Merkle roots allow for efficient and secure verification of large data structures, and in the case of blockchains, potentially boundless data sets." (coinomi) Merkle tree
Our Merkle root function:


buildmerkleroot:{[leafs] 
    leafcount:  count leafs;
    root:  $[1 = leafcount; first leafs;
        [if [0 < leafcount mod 2; leafs: leafs,enlist last leafs; leafcount: 1+leafcount;]; // if odd number, duplicate last record
        oddindicies: 1+2*til "i"$(leafcount%2);
        evenindicies: 2*til "i"$(leafcount%2);
        hashpairs: leafs[evenindicies] ,' leafs[oddindicies];
        branches:{  doubleSha256Byte x } peach hashpairs;  
        buildmerkleroot[branches] ]  ];
    root };

Order transaction hashes to build the merkle root.


hashlist: {x[`hash]} each transactions;
hashlist: (enlist coinbasetranshash), hashlist;
bytehashlist: hexstring_to_hex peach hashlist;
bytehashlistBigEndian:reverse peach bytehashlist;
Merkleroot: buildmerkleroot[bytehashlistBigEndian]; 
                        

If you are astute, you may have noticed the coinbase transaction hash creep in there.
 

Coinbase transaction

The coinbase transaction is the record of the reward we pay ourselves for mining the block. As miners, we must place the coinbase transaction at the beginning of the transaction list. Format from Bitcoin.org
Coinbase transaction

Bytes Name Data Type Description
4 version int32_t Transaction version number (note, this is signed); currently version 1 or 2. Programs creating transactions using newer consensus rules may use higher version numbers. Version 2 means that BIP 68 applies.
Varies tx_in count compactSize uint Number of inputs in this transaction.
32 hash (null) char[32] A 32-byte null, as a coinbase has no previous outpoint.
4 index (UINT32_MAX) uint32_t 0xffffffff, as a coinbase has no previous outpoint.
Varies script bytes compactSize uint The number of bytes in the coinbase script, up to a maximum of 100 bytes.
Varies (4) height script The block height of this block as required by BIP34. Uses script language: starts with a data-pushing opcode that indicates how many bytes to push to the stack followed by the block height as a little-endian unsigned integer. This script must be as short as possible, otherwise it may be rejected.

The data-pushing opcode will be 0x03 and the total size four bytes until block 16,777,216 about 300 years from now.
Varies coinbase script None The coinbase field: Arbitrary data not exceeding 100 bytes minus the (4) height bytes. Miners commonly place an extra nonce in this field to update the block header merkle root during hashing.
4 sequence uint32_t Sequence number.
Varies tx_out count compactSize uint Number of outputs in this transaction.
8 value int64_t Number of satoshis to spend. Coinbase transactions spend the block subsidy and collected transaction fees.
1+ pk_script bytes compactSize uint Number of bytes in the pubkey script. Maximum is 10,000 bytes.
Varies pk_script char[] Defines the conditions which must be satisfied to spend this output.
4 lock_time uint32_t A time (Unix epoch time) or block number.

coinbaseversion : "01000000"
coinbasein : "01"
coinbaseprevtx : "0000000000000000000000000000000000000000000000000000000000000000"
coinbaseprevout : "ffffffff";
coinbaseseq  :"00000000";
coinbaseout : "01";
cbpubkey : "866b6c77fd236f9185bb11b4e176328d7f912a4b";
scriptPubKey : "76","A9","14" , cbpubkey, "88", "AC";
coinbasesl : "19";
coinbaselocktime : "00000000";
coinbasesats: littleEndian [ coinbasevalue; 16 ];
coinbasescript : "03" , ( littleEndian [ height; 6 ] ), texttohexstr [ "Reap what you sow." ];
coinbasescriptlen : littleEndian[ "i"$( count coinbasescript ) % 2;2];
coinbasetrans: coinbaseversion, coinbasein, coinbaseprevtx, coinbaseprevout, coinbasescriptlen, coinbasescript, coinbaseseq, coinbaseout, coinbasesats, coinbasesl, scriptPubKey, coinbaselocktime;
coinbasetransbytes : hexstring_to_hex coinbasetrans;
coinbasetranshash : raze string reverse doubleSha256Byte [ coinbasetransbytes ];
                            

Again, for the astute there are a couple of points to note here outside of the format to pay yourself. First is that there are hidden messages encoded in each mined block (see cryptograffiti for examples ). Second is that there is a pubkey script language, using op codes to spend the outputs. This is bitcoin's smart contracts (for details see pubkey scripts). We use a standard Pay-to-Public-Key-Hash opcodes:

Public key script

  1. OP_DUP ( 0x76 ) - Duplicates the top stack item.
  2. OP_HASH160 ( 0xA9 ) - The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
  3. OP_EQUALVERIFY ( 0x88 ) - Returns 1 if the inputs are exactly equal, 0 otherwise, runs OP_VERIFY afterward.
  4. OP_CHECKSIG ( 0xAC ) - The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.

 

Hash race

Now we have the constructs to find a live solution and submit it to the blockchain for reward. The block is now simple to put together:


Block : Version , hashPrevBlock , Merkleroot , Time , Bits, 4#reverse 0x0 vs Nonce, transcounter , raze trans

Except that the header hash value must be less than the extremely improbable target value. To achieve this, miners can vary the Nonce from 0 to 4,294,967,295. They can also update the transaction pool or message text to attain a different merkle root. Miner software and hardware optimisations are geared towards looping through different Nonce value hashes at faster rates, often at great expense ( with setup, electricity and security ). Our script is a portable, energy efficient parallel CPU solution; competitve with pooled resources or a high performance cpu cluster. One run through the entire Nonce range looks like this:


curBlock: 0N! computeData`height;
latBlock: 0N! blockcount[];
i :4200000000;
while[11b ~  (curBlock > latBlock) , i > 0; // check if block is still current 
    .Q.gc[]; // clean memory
    computehash peach i + til 100000000; // process in batches, typically takes 1-2 minutes on a normal modern computer
    i : 0N! i - 100000000;
    latBlock: 0N! blockcount[]; ];

One run through of 4.3 billion Nonces would meet the base rate difficulty of the genesis block a decade ago. The difficulty is self adjusting to average 10 minutes a block and now requires more iterations. In our code, we update the transaction list to create a new merkle root after exhausting the Nonce range. In cases of reward conflict, the longest transaction list wins. Here is the code and functions we use for the continous mining effort:


submit:{[headerHex]headstr:raze string headerHex; blk: headstr ,raze string computeData`trans; h (`.bitcoind.submitblock;blk);0N! "SUCCESS: ", headstr;};
hashcheck:{[finalHash;headerHex]
  if [finalHash[til 7] ~ 0x00000000000000;
  targ: computeData`target;
  if [finalHash[7] <= targ[7];
    finalHashstr: raze string finalHash;
    lz:first where finalHashstr <> "0";
    $[leadingzeros < lz; 
      [ submit[headerHex]; ];
      [ simpletarget: hex_to_int targ[8 9 10 11]
      simplehash: hex_to_int finalHash[8 9 10 11]
      if[ simplehash < simpletarget; submit[headerHex];]
      if [simplehash = simpletarget;
        simpletarget: hex_to_int targ[12 13 14 15]
        simplehash: hex_to_int finalHash[12 13 14 15]
        if[ simplehash < simpletarget; submit[headerHex];]
        if [simplehash = simpletarget;
          simpletarget: hex_to_int targ[16 17 18 19]
          simplehash: hex_to_int finalHash[16 17 18 19]
          if[simplehash < simpletarget; submit[headerHex];]
          if [simplehash = simpletarget;
            simpletarget: hex_to_int targ[20 21 22 23]
            simplehash: hex_to_int finalHash[20 21 22 23]
            if[ simplehash < simpletarget; submit[ headerHex ] ] ] ] ] ] ] ] ] }
              
computehash:{[Nonce]    
    hashcheck[reverse sha2561[ endMerkle , 4#reverse 0x0 vs Nonce; H0; target32 ]; computeData`partialHeader , 4#reverse 0x0 vs Nonce];        
    };    

cycleAttempts: { [ ]
    computeData:: h"loadUpdates[];computeData";
    H0::  sha2560 64#computeData`partialHeader;
    target32:: -4#8#computeData`target;
    leadingzeros::  first where (raze string computeData`target) <> "0";
    endMerkle::  -12#computeData`partialHeader;
    //process 100,000,000 at a time ~ 1 min on a normal modern computer
    // check if block has already been mined between efforts
    curBlock: 0N! computeData`height;
    latBlock: 0N! blockcount[];
    i :4200000000;
    while[11b ~  (curBlock > latBlock) , i > 0;
        .Q.gc[];
        computehash peach i + til 100000000;
        i : 0N! i - 100000000;
        latBlock: 0N! blockcount[];
    ];
    cycleAttempts[];
    };  

cycleAttempts[];    

Perhaps you can see the rabbit hole. I refer the interested to the bitcoin white paper. Please visit our github page for full code. Feedback is welcome at the email below.

I really like your post as I gain alot of information from it , but we should know that mining may be very bad to the miners if the bitcoin price doesn't increase after halving. 

Share this post


Link to post
Share on other sites

Mining Bitcoin is an action that is carried out by carrying out multiple rounds of the SHA256 hash verification process to validate Bitcoin transactions and make security a prerequisite for public ledgers on the Bitcoin network. The speed at which you mine Bitcoin is assessed in hashes per second. There are various ways to mine Bitcoin, one of them using Cloud Mining.

Share this post


Link to post
Share on other sites

Mining bitcoins by myself will be good but I think I might reconsider it before engaging in the activities because in my country mining consumes lots of electricity and the electricity charges here is very high

Share this post


Link to post
Share on other sites

Mining bitcoin is simple, buy the right equipment from it. Set it up. Get ready for your electricity bill if your country have a less fee in electricity you are lucky. So that's it mate.

Share this post


Link to post
Share on other sites

There are various cloud mining websites through which you can earn free bitcoin with ease. Just create an account start your mining let them on. After some time passed open it and see whether you have rewarded or not. I am using MasterCoin and Indigo for mining these two are the most famous mining sites for now 

Share this post


Link to post
Share on other sites

Thanks for sharing some interesting information. If you want to start cryptocurrency mining you need it. Cheap electricity, crypto MINER (ASIC, GPU ETC )and the place to put it also require internet and a cooling system

Share this post


Link to post
Share on other sites

Actually I have no experience about this metter. But I think Bitcoin is the best successful crypto currency. Bitcoin called mother all of crypto currency

  • +2 1

Share this post


Link to post
Share on other sites

Mining is the process yo solve the complex mathematical problems to get a fruitful output. To start mining first connect yourself with a mining pool to interact with block chain then connect hardware to.pool & process will start.

Share this post


Link to post
Share on other sites

Thanks for sharing information. But minning is contionus process that is cause of hanging mobile or other devices. It is dangerous for all devices. 

Share this post


Link to post
Share on other sites

I know I have no idea about this.  Sorry  However I am very curious to know about this.  Thanks.

Share this post


Link to post
Share on other sites

You must be having your own source if energy that's has no any cost in your mining, also you must have your mining hardware equipment with best performance

Edited by Lubruselu

Share this post


Link to post
Share on other sites

To starting mining bitcoins or other cryptocurrencies your own it's very simple, but I advising you don't do the bitcoin mining its not profitable


Trade on forex without KYC verification, also deposit and withdraw easy with bitcoin.... Just click on this LINK

 

The best forex broker with leverege up to 300× minimum deposit $10 only.

Iqoption.com/signup

Share this post


Link to post
Share on other sites

Mining an entire bitcoin is extremely hard! In fact it would take a lot of cpu power to mine a bitcoin. There are many sites that you can mine from, but it is damaging to your laptop/pc. I suggest that if you don't have the necessary hardware to mine btc that you stay away from it coz it could cause damage that costs more than you male, making it unprofitable.


Looking to earn through posting on forums? Join Beermoneyforum where you can earn up to $12 daily for your posts! Follow this link to get started: https://www.beermoneyforum.com/?inv=126648

A simple, awesome faucet with payouts in BTC and TRX. Join now at https://moremoney.io/?ref=72749

Join this awesome forum for pay per post rewards: https://bizdustry.com/?referrer=5769

Share this post


Link to post
Share on other sites

To mine crypto currency you must have atleast the right equipments and right tools for it like for example GTX or AMD's this are the most actually needed.

Share this post


Link to post
Share on other sites

Mining alone  now is not recommended for the simple reason that it's not economically profitable to mine it, because after the halving the number of Bitcoin will be cut in half than we are producing and besides the mining cost and the difficulty is very high that you will lose a lot of money mining it.

  • +2 1
  • +3 1

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...