All files / src/services/chaintracker/chaintracks BlockHeaderApi.ts

50% Statements 4/8
0% Branches 0/4
0% Functions 0/4
50% Lines 4/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                57x               57x               57x               57x      
/**
 * These are fields of 80 byte serialized header in order whose double sha256 hash is a block's hash value
 * and the next block's previousHash value.
 *
 * All block hash values and merkleRoot values are 32 byte hex string values with the byte order reversed from the serialized byte order.
 */
export interface BaseBlockHeader {
  /**
   * Block header version value. Serialized length is 4 bytes.
   */
  version: number
  /**
   * Hash of previous block's block header. Serialized length is 32 bytes.
   */
  previousHash: string
  /**
   * Root hash of the merkle tree of all transactions in this block. Serialized length is 32 bytes.
   */
  merkleRoot: string
  /**
   * Block header time value. Serialized length is 4 bytes.
   */
  time: number
  /**
   * Block header bits value. Serialized length is 4 bytes.
   */
  bits: number
  /**
   * Block header nonce value. Serialized length is 4 bytes.
   */
  nonce: number
}
 
/**
 * A `BaseBlockHeader` extended with its computed hash and height in its chain.
 */
export interface BlockHeader extends BaseBlockHeader {
  /**
     * Height of the header, starting from zero.
     */
  height: number
  /**
     * The double sha256 hash of the serialized `BaseBlockHeader` fields.
     */
  hash: string
}
 
/**
 * The "live" portion of the block chain is recent history that can conceivably be subject to reorganizations.
 * The additional fields support tracking orphan blocks, chain forks, and chain reorgs.
 */
export interface LiveBlockHeader extends BlockHeader {
  /**
     * The cummulative chainwork achieved by the addition of this block to the chain.
     * Chainwork only matters in selecting the active chain.
     */
  chainWork: string
  /**
     * True only if this header is currently a chain tip. e.g. There is no header that follows it by previousHash or previousHeaderId.
     */
  isChainTip: boolean
  /**
     * True only if this header is currently on the active chain.
     */
  isActive: boolean
  /**
     * As there may be more than one header with identical height values due to orphan tracking,
     * headers are assigned a unique headerId while part of the "live" portion of the block chain.
     */
  headerId: number
  /**
     * Every header in the "live" portion of the block chain is linked to an ancestor header through
     * both its previousHash and previousHeaderId properties.
     *
     * Due to forks, there may be multiple headers with identical `previousHash` and `previousHeaderId` values.
     * Of these, only one (the header on the active chain) will have `isActive` === true.
     */
  previousHeaderId: number | null
}
 
//
// TYPE GUARDS
//
 
/**
 * Type guard function.
 * @publicbody
 */
export function isLive (header: BlockHeader | LiveBlockHeader): header is LiveBlockHeader {
  return (header as LiveBlockHeader).headerId !== undefined
}
 
/**
 * Type guard function.
 * @publicbody
 */
export function isBaseBlockHeader (header: BaseBlockHeader | BlockHeader | LiveBlockHeader): header is BaseBlockHeader {
  return typeof header.previousHash === 'string'
}
 
/**
 * Type guard function.
 * @publicbody
 */
export function isBlockHeader (header: BaseBlockHeader | BlockHeader | LiveBlockHeader): header is LiveBlockHeader {
  return ('height' in header) && typeof header.previousHash === 'string'
}
 
/**
 * Type guard function.
 * @publicbody
 */
export function isLiveBlockHeader (header: BaseBlockHeader | BlockHeader | LiveBlockHeader): header is LiveBlockHeader {
  return 'chainwork' in header && typeof header.previousHash === 'string'
}