Bitcoin Data Model
This is a high-level overview of Bitcoin’s data model. For the full details, refer to https://bitcoin.org/en/developer-reference. While the serialized versions of these structs are used in the bridge’s API, they are parsed by the chain into a more convenient internal representation. See Data Model.
Block Headers
The 80 bytes block header encodes the following information:
Note
as per bip64, blocks with a version number of less than 4 are rejected. As a consequence, blocks that were mined before December 2015 will not successfully parse in the bridge. This is acceptable, because the bridge is not expected to be initialized with such an old block as genesis.
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
4 |
|
i32 |
The block version to follow. |
32 |
|
char[32] |
The double sha256 hash of the previous block header. |
32 |
|
char[32] |
The double sha256 hash of the Merkle root of all transaction hashes in this block. |
4 |
|
u32 |
The block timestamp included by the miner. |
4 |
|
u32 |
The target difficulty threshold, see also the Bitcoin documentation. |
4 |
|
u32 |
The nonce chosen by the miner to meet the target difficulty threshold. |
Transactions
A transaction is broadcasted in a serialized byte format (also called raw format). It consists of a variable size of bytes and has the following format. Both ‘normal’ transaction and transactions segregated witness data are supported.
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
4 |
|
i32 |
Transaction version number. |
0/2 |
|
Option<u8[2]> |
If present, always 0001, and indicates the presence of witness data |
var |
|
uint |
Number of transaction inputs. |
var |
|
List of transaction inputs. |
|
var |
|
uint |
The number of transaction outputs. |
var |
|
List of transaction outputs. |
|
var |
|
A list of witnesses, one for each input; omitted if flag is omitted above. |
|
4 |
|
u32 |
A Unix timestamp OR block number. |
Note
Bitcoin uses the term “CompactSize Unsigned Integers” to refer to variable-length integers, which are used to indicate the number of bytes representing transaction inputs and outputs. See the Developer Reference for more details.
Inputs
Bitcoin’s UTXO model requires a new transaction to spend at least one existing and unspent transaction output as a transaction input. The txIn
type consists of the following bytes. See the reference for further details.
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
36 |
|
outpoint |
The output to be spent consisting of the transaction hash (32 bytes) and the output index (4 bytes). |
var |
|
uint |
Number of bytes in the signature script (max 10,000 bytes). |
var |
|
char[] |
The script satisfying the output’s script. |
4 |
|
u32 |
Sequence number (default |
Outputs
The transaction output has the following format according to the reference.
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
8 |
|
i64 |
Number of satoshis to be spent. |
1+ |
|
uint |
Number of bytes in the script. |
var |
|
char[] |
Spending condition as script. |
Witness
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
var |
|
uint |
The number of witness stack items in this tx_witness. |
var |
|
List of witness stack items making up this tx_witness. |
Witness Stack Item
Bytes |
Parameter |
Type |
Description |
---|---|---|---|
var |
|
uint |
The number of bytes in this witness stack item. |
var |
|
u8[] |
The bytes making up the witness stack item. |