Author

Topic: Bitcoin Mining Formula (Bitcoin Pseudo client Documentation Research) (Read 5912 times)

legendary
Activity: 980
Merit: 1003
I'm not just any shaman, I'm a Sha256man
Notes to self:


1. Get block hash
./bitcoind getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f 

2. Get block information (Looking for
/bitcoind getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f                                                                               
{                                                                                                                                                                                                                               
    "hash" : "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",                                                                                                                                                 
    "confirmations" : 206583,                                                                                                                                                                                                   
    "size" : 285,                                                                                                                                                                                                               
    "height" : 0,                                                                                                                                                                                                               
    "version" : 1,                                                                                                                                                                                                               
    "merkleroot" : "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",                                                                                                                                           
    "tx" : [                                                                                                                                                                                                                     
        "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"                                                                                                                                                       
    ],                                                                                                                                                                                                                           
    "time" : 1231006505,                                                                                                                                                                                                         
    "nonce" : 2083236893,                                                                                                                                                                                                       
    "bits" : "1d00ffff",                                                                                                                                                                                                         
    "difficulty" : 1.00000000,                                                                                                                                                                                                   
    "nextblockhash" : "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"                                                                                                                                         
}

3. Get transaction list (Block 0 dosen't have any inputs so no transaction list will be found "As far as im told")
./bitcoind getrawtransaction 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
error: {"code":-5,"message":"No information available about transaction"}   

B
kjj
legendary
Activity: 1302
Merit: 1026
Start with the header.  It is described in that link I posted.  Be careful of endianness issues.  Once you get to the point where you can build a header and get the same hash that the rest of the network sees, you can move on to parsing the block body, which is fairly simple, and hashing the transactions to build the Merkle Tree.

Then you can start building a database of transaction outputs and checking that transaction inputs are valid.  The final step will be checking the signatures in the transactions.  The signatures themselves aren't too bad, but first you need to prepare it in a specific way.

https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer VI pops up in a lot of places, you'll need to know how to read it to do anything
https://en.bitcoin.it/wiki/File:TxBinaryMap.png this map helps you parse transactions.
https://en.bitcoin.it/wiki/Script list of opcodes, needed for parsing scripts
https://en.bitcoin.it/wiki/File:Bitcoin_OpCheckSig_InDetail.png this map is for preparing transactions for signing
https://en.bitcoin.it/wiki/OP_CHECKSIG description of the signing process
https://bitcointalksearch.org/topic/specification-of-blockchain-format-101514 format of the block files, essentially a dump of the block messages minus the command string, also the magic and the blocksize aren't part of the block proper.
legendary
Activity: 980
Merit: 1003
I'm not just any shaman, I'm a Sha256man
...

Great explanation, I send you some Bitcents  Smiley

I might be back for more questions about this when/if i run into some problems doing my own verifying of blocks
kjj
legendary
Activity: 1302
Merit: 1026
First, the nodes don't have hardcoded trusted nodes.  There are DNS seeds that can be used when a new node is bootstrapping and needs a way to find running nodes to connect to.

Each block is composed of a header and a list of transactions.  The transactions are hashed together in a Merkle Tree and the root of that tree is included in the header in such a way that none of the transactions can be changed without changing the hash in the header.  The header also includes the hash of the previous header, proving that it came after the previous block, and also ensuring that the previous block can't be changed without changing the current header.

The nonce field is allowed to be anything at all, provided that it leads to a valid hash.  This means that each miner iterates through all 232 possible nonces for the block candidate they are working on hoping that one of them works out.  If none works, they ask for a new candidate, which means changing the timestamp or Merkle root hash.  Changing the Merkle root means that at least one transaction needs to change (or a new transaction can be added).  The generate transaction is allowed to contain arbitrary data, giving nodes the ability to generate new trees as needed.

The header is then fed into hash=SHA256(SHA256(header)).  SHA256 is expected to have essentially a random distribution, meaning that the output is similar to getting a random number.  The network tracks a target value, and only accepts blocks that are less than the target.  There is no way to design a block header in advance that will have a hash that is below the target value, so miners have to check each one hoping to get lucky.  The current target gives each hash about a 1 in 15 quadrillion chance of being below the target, so miners are checking (in aggregate) about 25 trillion hashes per second.

When a new block is found, every transaction is checked to make sure it is well formed and valid, and that each redeemed output is valid and not previously spent.  The Merkle tree is created and checked to make sure that it matches the root hash in the header.  The timestamp is checked to make sure it is within the allowable range, the difficulty is checked to make sure it agrees with the network, and the header hash is checked to make sure it is below the target value.  If all of the checks are true, and the previous block hash in the header belongs to the block that was the previously highest block, the chain is extended.

When doing the initial download, some checks are abbreviated or skipped on old blocks, but the 2500 most recent blocks always get full verification.
legendary
Activity: 980
Merit: 1003
I'm not just any shaman, I'm a Sha256man
legendary
Activity: 980
Merit: 1003
I'm not just any shaman, I'm a Sha256man
I basically understand that everyone runs a client, and then talks to the trusted hard coded nodes for syncing to the network (Downloading Blocks)
So we take the raw block data: https://en.bitcoin.it/wiki/Genesis_block
Then what steps/formula is applied to the next confirmed block?
I'm mostly curious how miners are always finding a random nonce, but who sets the next nonce and where is the next nonce set in the next block? How are we able to find a random nonce but also be able to confirm that the nonce is true but at the same time NOT know what the nonce of the next block is?

I will give Bitcoin tips to the one that gives me the most informative explanation (I am a coder so Pseudo-codes or real code will help me understand better along with English language explanations)

Thanks mates!
Jump to: