github.com/aychain/blockbook@v0.1.1-0.20181121092459-6d1fc7e07c5b/docs/rocksdb.md (about)

     1  # Data storage in RocksDB
     2  
     3  **Blockbook** stores data the key-value store RocksDB. Each index is stored in its own column family.
     4  
     5  >Database content is described in golang pseudo types in the form *(name type)*. 
     6  >
     7  >Operators used in the description: 
     8  >- -> mapping from key to value. 
     9  >- \+ concatenation, 
    10  >- [] array
    11  >
    12  >Types used in the description:
    13  >- []byte - array of bytes
    14  >- uint32 - unsigned integer, stored as array of 4 bytes in big endian*
    15  >- vint, vuint - variable length signed/unsigned int
    16  >- addrDesc - address descriptor, abstraction of an address. In all bitcoin like coins it is output script. Stored as variable length array of bytes.
    17  >- bigInt - unsigned big integer, stored as length of array (1 byte) followed by array of bytes of big int, i.e. *(int_len byte)+(int_value []byte)*. Zero is stored as one byte containing 0.
    18  
    19  **Column families:**
    20  
    21  - **default**
    22  
    23    stores internal state in json format, under the key *internalState*. 
    24    
    25    Most important internal state values are:
    26    - coin - which coin is indexed in DB
    27    - data format version - currently 3
    28    - dbState - closed, open, inconsistent
    29      
    30    Blockbook is on startup checking these values and does not allow to run against wrong coin, data format version and in inconsistent state.
    31  
    32  - **height** 
    33  
    34      maps *block height* to *block hash* and additional data about block
    35      ```
    36      (height uint32) -> (hash [32]byte)+(time uint32)+(nr_txs vuint)+(size vuint)
    37      ```
    38  
    39  - **addresses**
    40  
    41      maps *addrDesc+block height* to  *array of outpoints* (array of transactions with input/output index). Input/output is recognized by the sign of the number, output is positive, input is negative, with operation bitwise complement ^ performed on the number.
    42      ```
    43      (addrDesc []byte)+(height uint32) -> []((txid [32]byte)+(index vint))
    44      ```
    45  
    46  - **addressBalance**
    47  
    48      maps *addrDesc* to *number of transactions*, *sent amount* and *total balance* of given address
    49      ```
    50      (addrDesc []byte) -> (nr_txs vuint)+(sent_amount bigInt)+(balance bigInt)
    51      ```
    52  
    53  - **txAddresses**
    54  
    55      maps *txid* to *block height* and array of *input addrDesc* with *amounts* and array of *output addrDesc* with *amounts*, with flag if output is spent. In case of spent output, *addrDesc_len* is negative (negative sign is achieved by bitwise complement ^).
    56      ```
    57      (txid []byte) -> (height vuint)+
    58                       (nr_inputs vuint)+[]((addrDesc_len vuint)+(addrDesc []byte)+(amount bigInt))+
    59                       (nr_outputs vuint)+[]((addrDesc_len vint)+(addrDesc []byte)+(amount bigInt))
    60      ```
    61  
    62  - **blockTxs**
    63  
    64      maps *block height* to an array of *txids* and *input points* in the block - only last 300 (by default) blocks are kept, the column is used in case of rollback.
    65      ```
    66      (height uint32) -> []((txid [32]byte)+(nr_inputs vuint)+[]((txid [32]byte)+(index vint)))
    67      ```
    68  
    69  - **transactions**
    70  
    71      transaction cache, *txdata* is generated by coin specific parser function PackTx
    72      ```
    73      (txid []byte) -> (txdata []byte)
    74      ```