-1

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?

2

3 Answers 3

0

In the bitcoin-core you can see here. 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 and here.

1
  • that is how things are stored inside the bitcoin-core program. that is not the same as 'how they are stored in the blockchain' Commented Nov 28, 2022 at 23:28
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.