<p>As the name says, it is a chain of blocks.</p>
<p>These blocks are usually stored in blkNNNNN.dat files, with each block prefixed with a magic value and a byte size.</p>
<p>Each block starts with an 80 byte header, containing:</p>
<ul>
<li>version</li>
<li>hash of the previous block</li>
<li>the merkle root</li>
<li>the timestamp</li>
<li>the block difficulty</li>
<li>a nonce
The blockhash is the hash of that 80 byte header.
The 'hash of previous block'  is what makes this a chain of blocks.</li>
</ul>
<p>The block header is followed by a count,
followed by that many transactions.</p>
<p>There are two types of transactions:</p>
<ul>
<li>legacy</li>
<li>segwit</li>
</ul>
<p>Legacy transactions are formatted like this:</p>
<ul>
<li>a version</li>
<li>a count of the number of inputs</li>
<li>the input transactions</li>
<li>a count of the number of outputs</li>
<li>the output transactions</li>
<li>a locktime.
for legacy transactions, the transaction hash is just the hash of the entire transaction.</li>
</ul>
<p>a segwit transaction:</p>
<ul>
<li>a version</li>
<li>a byte 0x00</li>
<li>a byte: witnessflags</li>
<li>a count of the number of inputs</li>
<li>the input transactions</li>
<li>a count of the number of outputs</li>
<li>the output transactions</li>
<li>for each input, a witness</li>
<li>a locktime.
for segwit transactions, the hash is excluding the witnessflag and 0 byte, and witness list.</li>
</ul>
<p>An input is formatted like this:</p>
<ul>
<li>hash of the source transaction</li>
<li>the index of the output in the source transaction</li>
<li>the input script ( aka ScriptSig )</li>
<li>a sequence number</li>
</ul>
<p>An output is formatted like this:</p>
<ul>
<li>a bitcoin value</li>
<li>the output script</li>
</ul>
<p>A witness is formatted like this:</p>
<ul>
<li>a count</li>
<li>a number of values</li>
</ul>
<p>Some values are 32 bit little endian integers:</p>
<ul>
<li>the sequence number</li>
<li>the versions</li>
<li>the output index</li>
<li>the locktime</li>
<li>the timestamp, nonce and difficulty</li>
</ul>
<p>The bitcoin value is stored as a 64 bit little endian integer.</p>
<p>The counts are stored as a special compressed 'varint'.</p>
<p>Scripts and witness values are stored as: count + bytes.</p>
<p>hashes are stored as 32 bytes:</p>
<ul>
<li>the previous block hash</li>
<li>the merkle root</li>
<li>the source transaction hash</li>
</ul>

CC BY-SA 4.0
2022-11-28T23:48:53.560Z
2022-11-28T23:48:53.560Z
Willem Hengeveld
<p>In the Bitcoin blockchain, transactions are stored in a data structure called a Merkle tree, also known as a hash tree. In a Merkle tree, transactions are grouped together into &quot;blocks&quot; and each block is connected to the previous block in the chain, creating a series of blocks that form the blockchain.</p>
<p>The &quot;vout&quot; and &quot;vin&quot; fields that you see on a blockexplorer represent the inputs and outputs of a transaction in the blockchain. These fields are typically encoded in a format called JSON (JavaScript Object Notation) for easy reading and parsing by software applications.</p>
<p>In the Bitcoin node software, the transactions are not typically serialized (converted into a series of bytes) because they are stored and processed in the data structures of the Merkle tree. The specific data structures used will depend on the implementation of the Bitcoin node software, but they are typically some combination of arrays, lists, and hash tables.</p>

CC BY-SA 4.0
2022-12-05T17:58:05.200Z
2022-12-05T17:58:05.200Z
Monstrum
<p>In the bitcoin-core you can see <a href="https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L279" rel="nofollow noreferrer">here</a>. There is a class called <code>CTransaction</code> which has 2 important public members called <code>vin</code> and <code>vout</code>.</p>
<pre><code>/** The basic transaction that is broadcasted on the network and contained in
 * blocks.  A transaction can contain multiple inputs and outputs.
 */
class CTransaction
{
public:
    // Default transaction version.
    static const int32_t CURRENT_VERSION=2;

    // The local variables are made const to prevent unintended modification
    // without updating the cached hash value. However, CTransaction is not
    // actually immutable; deserialization and assignment are implemented,
    // and bypass the constness. This is safe, as they update the entire
    // structure, including the hash.
    const std::vector&lt;CTxIn&gt; vin;
    const std::vector&lt;CTxOut&gt; vout;
</code></pre>
<p>As you can see their data type is <code>std::vector</code> of <code>CTxin</code> and <code>CTxout</code>. You can find their declaration respectively <a href="https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L65" rel="nofollow noreferrer">here</a> and <a href="https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L148" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2022-11-28T23:28:50.643Z
that is how things are stored inside the bitcoin-core program.  that is not the same as 'how they are stored in the blockchain'
Willem Hengeveld
CC BY-SA 4.0
2022-07-01T13:19:54.793Z
2022-07-01T13:19:54.793Z
Amir reza Riahi
<p>In what data structure are bitcoin transactions stored in the blockchain? That <code>vout</code> and <code>vin</code> thing shown on blockexplorers is in json format. So, in the code, what data structure is used since in the bitcoin node application the details are not serialised?</p>

CC BY-SA 4.0
2022-06-27T08:53:48.157Z
Does this answer your question? [How is data in a blockchain stored? Structure of block and blockchain](https://bitcoin.stackexchange.com/questions/98788/how-is-data-in-a-blockchain-stored-structure-of-block-and-blockchain) or [What are the components of a raw block](https://bitcoin.stackexchange.com/q/114108/13866)
RedGrittyBrick
CC BY-SA 4.0
2022-12-05T16:29:17.057Z
Could you clarify whether you mean how it is stored on disk or how it is stored when loaded in memory?
Murch
<bitcoin-core><transactions><storage-footprint>
What data structure is used to store the bitcoin transactions in the blockchain?
Athul A
# What data structure is used to store the bitcoin transactions in the blockchain?

(Asked by: username_0 on 2022-06-27)

In what data structure are bitcoin transactions stored in the blockchain? That `vout` and `vin` thing shown on blockexplorers is in json format. So, in the code, what data structure is used since in the bitcoin node application the details are not serialised?

**Tags:** bitcoin-core, transactions, storage-footprint

> **Comments:**
> **username_1**: Does this answer your question? [How is data in a blockchain stored? Structure of block and blockchain](https://bitcoin.stackexchange.com/questions/98788/how-is-data-in-a-blockchain-stored-structure-of-block-and-blockchain) or [What are the components of a raw block](https://bitcoin.stackexchange.com/q/114108/13866)  
> **username_2**: Could you clarify whether you mean how it is stored on disk or how it is stored when loaded in memory?  

---

## 3 answers

### Answer by username_3 on 2022-11-28. Score: 0

As the name says, it is a chain of blocks.

These blocks are usually stored in blkNNNNN.dat files, with each block prefixed with a magic value and a byte size.

Each block starts with an 80 byte header, containing:

* version
* hash of the previous block
* the merkle root
* the timestamp
* the block difficulty
* a nonce
  The blockhash is the hash of that 80 byte header.
  The 'hash of previous block' is what makes this a chain of blocks.

The block header is followed by a count,
followed by that many transactions.

There are two types of transactions:

* legacy
* segwit

Legacy transactions are formatted like this:

* a version
* a count of the number of inputs
* the input transactions
* a count of the number of outputs
* the output transactions
* a locktime.
  for legacy transactions, the transaction hash is just the hash of the entire transaction.

a segwit transaction:

* a version
* a byte 0x00
* a byte: witnessflags
* a count of the number of inputs
* the input transactions
* a count of the number of outputs
* the output transactions
* for each input, a witness
* a locktime.
  for segwit transactions, the hash is excluding the witnessflag and 0 byte, and witness list.

An input is formatted like this:

* hash of the source transaction
* the index of the output in the source transaction
* the input script ( aka ScriptSig )
* a sequence number

An output is formatted like this:

* a bitcoin value
* the output script

A witness is formatted like this:

* a count
* a number of values

Some values are 32 bit little endian integers:

* the sequence number
* the versions
* the output index
* the locktime
* the timestamp, nonce and difficulty

The bitcoin value is stored as a 64 bit little endian integer.

The counts are stored as a special compressed 'varint'.

Scripts and witness values are stored as: count + bytes.

hashes are stored as 32 bytes:

* the previous block hash
* the merkle root
* the source transaction hash


---

### Answer by username_4 on 2022-12-05. Score: 0

In the Bitcoin blockchain, transactions are stored in a data structure called a Merkle tree, also known as a hash tree. In a Merkle tree, transactions are grouped together into "blocks" and each block is connected to the previous block in the chain, creating a series of blocks that form the blockchain.

The "vout" and "vin" fields that you see on a blockexplorer represent the inputs and outputs of a transaction in the blockchain. These fields are typically encoded in a format called JSON (JavaScript Object Notation) for easy reading and parsing by software applications.

In the Bitcoin node software, the transactions are not typically serialized (converted into a series of bytes) because they are stored and processed in the data structures of the Merkle tree. The specific data structures used will depend on the implementation of the Bitcoin node software, but they are typically some combination of arrays, lists, and hash tables.


---

### Answer by username_5 on 2022-07-01. Score: 0

In the bitcoin-core you can see [here](https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L279). There is a class called `CTransaction` which has 2 important public members called `vin` and `vout`.

```
/** The basic transaction that is broadcasted on the network and contained in
 * blocks.  A transaction can contain multiple inputs and outputs.
 */
class CTransaction
{
public:
    // Default transaction version.
    static const int32_t CURRENT_VERSION=2;

    // The local variables are made const to prevent unintended modification
    // without updating the cached hash value. However, CTransaction is not
    // actually immutable; deserialization and assignment are implemented,
    // and bypass the constness. This is safe, as they update the entire
    // structure, including the hash.
    const std::vector<CTxIn> vin;
    const std::vector<CTxOut> vout;

```

As you can see their data type is `std::vector` of `CTxin` and `CTxout`. You can find their declaration respectively [here](https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L65) and [here](https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L148).

> **Comments:**
> **username_3**: that is how things are stored inside the bitcoin-core program.  that is not the same as 'how they are stored in the blockchain'  

---

