Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: fevir on November 21, 2016, 01:38:24 PM



Title: How are transactions separated on bit-level in blocks?
Post by: fevir on November 21, 2016, 01:38:24 PM
Hi,

A quick question. Assume that a block body contains several transactions (tx), and none of these transactions are of equal length. On bit level, transactions are represented by a set of 0 and 1. A set of tx will therefore result in a set of 0 and 1. Now, if a block contains multiple tx, how can the receiver of a block distinguish each transaction in the block body?

Thanks.


Title: Re: How are transactions separated on bit-level in blocks?
Post by: achow101 on November 21, 2016, 02:08:01 PM
Transactions have a known calculable size when it is parsed. So you will know that when a transaction ends and the next begins.


Title: Re: How are transactions separated on bit-level in blocks?
Post by: DannyHamilton on November 21, 2016, 03:23:12 PM
Transactions have a known calculable size when it is parsed. So you will know that when a transaction ends and the next begins.

Specifically...

  • Each transaction starts with 32 bits (set of thirty-two 0 and 1 digits) that store a version number.
  • Following that is a "set of 0 and 1" that tell how many bits (actually how many bytes, but you can multiply by 8 to get the quantity of 0 and 1) in the transaction inputs.*
  • Following that is a "set of 0 and 1" representing all the inputs. This set is exactly the amount indicated in the previous "how many bits" value.
  • Following that is a "set of 0 and 1" that tell how many bits (actually how many bytes, but you can multiply by 8 to get the quantity of 0 and 1) in the transaction outputs.*
  • Following that is a "set of 0 and 1" representing all the outputs. This set is exactly the amount indicated in the previous "how many bits" value.
  • Following that is 32 bits that represent the locktime.

* note, there is a special pattern to the first eight 0 and 1 values of the section that "tells how many bits". This pattern allows the program to know when it has read enough 0 and 1 to determine the appropriate value. If this section starts with 11111111, then the next sixty-four 0 and 1 are used to indicate the size of the inputs or outputs. If it starts with 11111110, then the next thirty-two 0 and 1 are used to indicate the size of the inputs or outputs,  If it starts with 11111101, then the next sixteen 0 and 1 are used to indicate the size of the inputs or outputs. If it starts with anything else, then these 8 values are themselves the indication of the size of the inputs or outputs, and there is no need to use any additional digits to determine that size.



Title: Re: How are transactions separated on bit-level in blocks?
Post by: fevir on November 21, 2016, 03:40:33 PM
Thanks, that's the details I was looking for


Title: Re: How are transactions separated on bit-level in blocks?
Post by: Kexkey on November 22, 2016, 01:56:41 AM
Thanks, that's the details I was looking for

Funny you want to have bit-scale details.  Have a look at the easy to understand and technically detailed doc : https://bitcoin.org/en/developer-reference#raw-transaction-format

Kex