Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Tym on April 03, 2020, 10:03:45 AM



Title: Blocknumber not part of the header
Post by: Tym on April 03, 2020, 10:03:45 AM
Hello,

compared to Ethereum there is no field "blocknumber" in a bitcoin-header. Only full nodes can verify a blocknumber since they know the whole chain.

My question: Is there some kind of an agreement out there (i.e. a BIP) which tells that the blocknumber should be stored for example in the "extra data"-field within the coinbase transaction?

I want to verify the blocknumber as a client which doesn't have full access to the chain.

I would love to read your answers.

Thanks,
Tym


Title: Re: Blocknumber not part of the header
Post by: nc50lc on April 03, 2020, 10:19:24 AM
My question: Is there some kind of an agreement out there (i.e. a BIP) which tells that the blocknumber should be stored for example in the "extra data"-field within the coinbase transaction?
BIP34 (https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki#Specification), looks like you already knew the answer prior to posting this question because your guess too specific :p
For the "blocknumber", the equivalent must be bitcoin's "block height".


Title: Re: Blocknumber not part of the header
Post by: HCP on April 03, 2020, 10:19:36 AM
I believe the reason that "blocknumber" (aka Height) is not used as an identifier is explained by this:
Since multiple blocks can have the same height during a block chain fork, block height should not be used as a globally unique identifier. Instead, blocks are usually referenced by the hash of their header (often with the byte order reversed, and in hexadecimal).

So, as stated, the unique identifier for a given block is the hash of the header.


Title: Re: Blocknumber not part of the header
Post by: Tym on April 03, 2020, 10:50:40 AM
My question: Is there some kind of an agreement out there (i.e. a BIP) which tells that the blocknumber should be stored for example in the "extra data"-field within the coinbase transaction?
BIP34 (https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki#Specification), looks like you already knew the answer prior to posting this question because your guess too specific :p
For the "blocknumber", the equivalent must be bitcoin's "block height".

Thanks! Thats what i was looking for ;)


Title: Re: Blocknumber not part of the header
Post by: pooya87 on April 04, 2020, 04:31:05 AM
I want to verify the blocknumber as a client which doesn't have full access to the chain.

if you mean as an SPV client then you still have to take additional steps to "verify" the block height because SPV clients only download block headers which does  not contain any block header field. so you have to download the first transaction of the block (aka coinbase transaction) and then decode its signature script to get the height.


Title: Re: Blocknumber not part of the header
Post by: gmaxwell on April 04, 2020, 02:08:17 PM
Only full nodes can verify a blocknumber since they know the whole chain.
In addition to the other excellent responses you've received here, SPV nodes (lite clients) also know the height even without BIP34 because they process all the prior headers too.  If they didn't they'd have no clue about the appropriate difficulty or total work in the chain, and so they couldn't validate the header.


Title: Re: Blocknumber not part of the header
Post by: Tym on April 06, 2020, 02:24:40 PM
if you mean as an SPV client then you still have to take additional steps to "verify" the block height because SPV clients only download block headers which does  not contain any block header field. so you have to download the first transaction of the block (aka coinbase transaction) and then decode its signature script to get the height.

Hello pooya87,

I just read your answer.
You said: "then decode its signature script to get the height"

Would you mind explaining this step more detailed to me?

I'd love to read your answer! Thanks!
Tym


Title: Re: Blocknumber not part of the header
Post by: pooya87 on April 06, 2020, 03:47:34 PM
You said: "then decode its signature script to get the height"
Would you mind explaining this step more detailed to me?

after block number 227,835 all blocks must include the block height in their coinbase transaction. they do it by including the number in their only input's signature script. if you are unfamiliar with transaction structure and scripts read [1] and [2].
numbers exist as bytes inside a script and bytes should be pushed to the stack with the appropriate push OP code. this push must be the first pushed item and the rest can be anything.
example from last block: https://blockchair.com/bitcoin/transaction/02d8cdb103f50532e2f18d9d1f85c016468ee0294908d387e38f80b99410d893
signature script is:
Code:
03348809041f4e8b5e7669702f7777772e6f6b65782e636f6d2ffabe6d6db388905769d4e3720b1e59081407ea75173ba3ed6137d32308591495198155ce020000004204cb9a2a31601215b2ffbeaf1c4e00
decoding this script is like this:
03 -> push following 3 bytes
348809 -> in little endian = 624692
041f... -> the rest can be anything

[1] https://bitcoin.org/en/developer-reference#transactions
[2] https://en.bitcoin.it/wiki/Script


Title: Re: Blocknumber not part of the header
Post by: Tym on April 07, 2020, 07:14:26 AM
Thank you so much @pooya87!
Appreciate it!