Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: Altcryptocoin on February 04, 2019, 01:12:28 PM



Title: Bitcoin block creation !!??
Post by: Altcryptocoin on February 04, 2019, 01:12:28 PM
Hello someone would know how a bitcoin block is made it is

I know you have to take all the transactions but I don't know what to do with them afterwards.

and can we create a block with the desired number of transactions?

thank you to you


Title: Re: Bitcoin block creation !!??
Post by: ranochigo on February 04, 2019, 01:16:12 PM
The miner assembles a set of transaction and hashes them. This creates a merkle root which is ultimately included in the block header of the block. With the block header, the hash of the block is obtained by hashing the block header. Only if the hash meets the target of the network, then it would be considered valid.

This doesn't emcompass the whole process but it does roughly cover the idea about how the transactions is used in the block hashing.

The miner has the rights to the number of transactions and which transaction he wishes to include. The least number of transaction is 1 and the maximum is up till the block weight is reached.


Title: Re: Bitcoin block creation !!??
Post by: Altcryptocoin on February 04, 2019, 01:50:48 PM
but what is the actual calculation used?
sha256 (number of transactions + nonce + prev bloc)..............................?


Title: Re: Bitcoin block creation !!??
Post by: ranochigo on February 04, 2019, 01:55:42 PM
but what is the actual calculation used?
sha256 (number of transactions + nonce + prev bloc)..............................?
Bitcoin uses SHA256D. The actual hash is just SHA256(SHA256 (Block Header)). The block header consists of the version, nounce, timestamp, merkle root, hash of previous block and target.


Title: Re: Bitcoin block creation !!??
Post by: Altcryptocoin on February 04, 2019, 02:02:43 PM
sha256(sha256(000000000000000000141653129dd4eb836978acc3c60e10e13ae53788aff716))       

result = next block  ::)


Title: Re: Bitcoin block creation !!??
Post by: ranochigo on February 04, 2019, 02:32:44 PM
sha256(sha256(000000000000000000141653129dd4eb836978acc3c60e10e13ae53788aff716))       

result = next block  ::)
The result you will get is just a (double) hash of the hash of another block header. This won't yield you anything at all.

You need to hash an actual block header (complete with all of the components of the blocks you are mining) for the block to be valid. The network will obtain the block header and hash it. If it doesn't meet the target, then it would be rejected.


Title: Re: Bitcoin block creation !!??
Post by: HeRetiK on February 04, 2019, 03:23:18 PM
but what is the actual calculation used?
sha256 (number of transactions + nonce + prev bloc)..............................?

It's:
SHA256( SHA256( Version + hashPrevBlock + hashMerkleRoot + Time + Target + Nonce ) )

With the Merkle Root being the set of all transactions reduced to a single a hash:
https://www.mycryptopedia.com/merkle-tree-merkle-root-explained/

The Bitcoin wiki has a good description of what is contained in the block header:
https://en.bitcoin.it/wiki/Block_hashing_algorithm


Title: Re: Bitcoin block creation !!??
Post by: Altcryptocoin on February 04, 2019, 03:38:52 PM
but what is the actual calculation used?
sha256 (number of transactions + nonce + prev bloc)..............................?

It's:
SHA256( SHA256( Version + hashPrevBlock + hashMerkleRoot + Time + Target + Nonce ) )

With the Merkle Root being the set of all transactions reduced to a single a hash:
https://www.mycryptopedia.com/merkle-tree-merkle-root-explained/

The Bitcoin wiki has a good description of what is contained in the block header:
https://en.bitcoin.it/wiki/Block_hashing_algorithm


okok thank you



Title: Re: Bitcoin block creation !!??
Post by: Altcryptocoin on February 05, 2019, 01:10:50 PM
Why algo for next Block ??
And Can we find a block 2 months ago


Title: Re: Bitcoin block creation !!??
Post by: bitmover on February 05, 2019, 01:46:54 PM
Why algo for next Block ??
And Can we find a block 2 months ago

You can track any block since Genesis block (2009) using any block explorer, such as blockchain.info
Just Go browsing through blocks and look at timestamp to see the date.


Title: Re: Bitcoin block creation !!??
Post by: Altcryptocoin on February 05, 2019, 02:01:45 PM
Code:
from hashlib import sha256
import hashlib
version = "00000020"
previoushash = "823a1913f9e43541545befa600134906819ea60ecfe71e000000000000000000"
merkleroot = "f717f1770bfcf49e72e58f28f1b7c53247a8235933fea24823687655009265ec"
timestamp = "3983595c"
bits = "15683017"
nonce = "f2a164a0"
header = (str(version) + str(previoushash) + str(merkleroot) + str(timestamp) + str(bits) + str(nonce)).decode('hex')
          
print sha256(sha256(header).digest()).digest()[::-1].encode('hex')

for next block

version = "00000020"                                                                                                                              OK
previoushash = "0000000000000000000a93736d47dbfecc769c598931b56a63f2c7547a700229"   inversed                OK
merkle root = sha256(transaction(s))                                                                                                         OK
timestamp = in the future                                                                                                                        OK
bits = random ???                                                                                                                               NO OK
nonce = random ???                                                                                                                            NO OK


create new block possible with genesis :
python genesis.py -z "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks" -n 2083236893 -t 1231006505


Title: Re: Bitcoin block creation !!??
Post by: jackg on February 06, 2019, 01:48:02 AM
Isn't it just the merkle root you hash?

Edit: it's just the merkle root that is hashed!

It blocks act as a merkle tree then to get that hash you do a double hash of the txid of the first transaction.
So you hash the hex digest as a sha256(sha256(genesisTxIDHex+genesisTxIDHex)).

If there's an odd number of nodes like in this case, the last node (incidentally the first) must be duplicated.


Title: Re: Bitcoin block creation !!??
Post by: HeRetiK on February 06, 2019, 09:30:07 AM
bits = random ???                                                                                                                               NO OK
nonce = random ???                                                                                                                            NO OK

bits is not random, it's the current difficulty target:
https://en.bitcoin.it/wiki/Target

This will stay the same on the next block unless the difficulty changes, which happens every 2016 blocks (ie. roughly every 2 weeks):
https://en.bitcoin.it/wiki/Difficulty

The nonce is the value that miners are searching for when mining a new block (ie. miners are looking for a nonce that leads to a block hash that is below the difficulty target as denoted by the bit field).


Isn't it just the merkle root you hash?

Edit: it's just the merkle root that is hashed!

What do you mean? The block hash is definitely more than just the merkle root.


Title: Re: Bitcoin block creation !!??
Post by: jackg on February 06, 2019, 02:39:14 PM
@HeRetiK how is it? A header is just an appendage to show what the block is compatible.

I found a bitcoin wiki article on it yesterday but its gone now..
At the bottom of miner cpp it can also be seen that we hash the merkle root:
pblock->hashMerkleRoot

I don't think there's any other reason to do this unless that's what generates the merkle root but if it did it should be called findMerkleRoot.


Title: Re: Bitcoin block creation !!??
Post by: HeRetiK on February 06, 2019, 10:11:42 PM
@HeRetiK how is it? A header is just an appendage to show what the block is compatible.

I found a bitcoin wiki article on it yesterday but its gone now..
At the bottom of miner cpp it can also be seen that we hash the merkle root:
pblock->hashMerkleRoot

I don't think there's any other reason to do this unless that's what generates the merkle root but if it did it should be called findMerkleRoot.

If you look closely you'll find that hashMerkleRoot is not the only member being set though :)


Here you see the components of the block header as mentioned further upthread:

Code:
class CBlockHeader
{
public:
    // header
    int32_t nVersion;
    uint256 hashPrevBlock;
    uint256 hashMerkleRoot;
    uint32_t nTime;
    uint32_t nBits;
    uint32_t nNonce;

...with hashMerkleRoot being only one of various members set during the mining / block building process.


For example you can see the hash of the previous block, timestamp, difficulty target and an initial nonce being set here:

Code:
    // Fill in header
    pblock->hashPrevBlock  = pindexPrev->GetBlockHash();
    UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
    pblock->nBits          = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
    pblock->nNonce         = 0;


And the version being set here:

Code:
pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());


And for good reason: The nonce nNonce is required to give miners a wide range of values that can be tried during the mining process. The target nBits allows the block to be verifiable in itself and seals the current network target within the blockchain. The timestamp nTime is required to calculate the upcoming difficulty change and makes time warp attacks more difficult. The hash of the previous block hashPrevBlock is necessary for ordering and puts the chain in blockchain. And needless to say hashMerkleRoot ensures transaction integrity. Accordingly you want to ensure the integrity of all of these parameters, hence they are hashed as part of the block header.