<commit_before>import Common from 'ethereumjs-common'
import * as ethUtil from 'ethereumjs-util'
import { BN, rlp } from 'ethereumjs-util'
import { Transaction } from 'ethereumjs-tx'

import { BlockHeader } from './header'
import { Blockchain, BlockData, NetworkOptions } from './types'

const Trie = require('merkle-patricia-tree')

/**
 * An object that represents the block
 */
export class Block {
  public readonly header: BlockHeader
  public readonly transactions: Transaction[] = []
  public readonly uncleHeaders: BlockHeader[] = []
  public readonly txTrie = new Trie()

  private readonly _common: Common

  /**
   * Creates a new block object
   *
   * @param data - The block's data.
   * @param opts - The network options for this block, and its header, uncle headers and txs.
   */
  constructor(
    data: Buffer | [Buffer[], Buffer[], Buffer[]] | BlockData = {},
    opts: NetworkOptions = {},
  ) {
    if (opts.common) {
      if (opts.chain !== undefined || opts.hardfork !== undefined) {
        throw new Error(
          'Instantiation with both opts.common and opts.chain / opts.hardfork parameter not allowed!',
        )
      }

      this._common = opts.common
    } else {
      const chain = opts.chain ? opts.chain : 'mainnet'
      const hardfork = opts.hardfork ? opts.hardfork : null
      this._common = new Common(chain, hardfork)
    }

    let rawTransactions
    let rawUncleHeaders

    if (Buffer.isBuffer(data)) {
      // We do this to silence a TS error. We know that after this statement, data is
      // a [Buffer[], Buffer[], Buffer[]]
      const dataAsAny = rlp.decode(data) as any
      data = dataAsAny as [Buffer[], Buffer[], Buffer[]]
    }

    if (Array.isArray(data)) {
      this.header = new BlockHeader(data[0], { common: this._common })
      rawTransactions = data[1]
      rawUncleHeaders = data[2]
    } else {
      this.header = new BlockHeader(data.header, { common: this._common })
      rawTransactions = data.transactions || []
      rawUncleHeaders = data.uncleHeaders || []
    }

    // parse uncle headers
    for (let i = 0; i < rawUncleHeaders.length; i++) {
      this.uncleHeaders.push(new BlockHeader(rawUncleHeaders[i], opts))
    }

    // parse transactions
    for (let i = 0; i < rawTransactions.length; i++) {
      const tx = new Transaction(rawTransactions[i], opts)
      this.transactions.push(tx)
    }
  }

  get raw(): [Buffer[], Buffer[], Buffer[]] {
    return this.serialize(false)
  }

  /**
   * Produces a hash the RLP of the block
   */
  hash(): Buffer {
    return this.header.hash()
  }

  /**
   * Determines if this block is the genesis block
   */
  isGenesis(): boolean {
    return this.header.isGenesis()<commit_msg>Add TOODs to document the limitations about Common and initializing the txs<commit_after>import Common from 'ethereumjs-common'
import * as ethUtil from 'ethereumjs-util'
import { BN, rlp } from 'ethereumjs-util'
import { Transaction } from 'ethereumjs-tx'

import { BlockHeader } from './header'
import { Blockchain, BlockData, NetworkOptions } from './types'

const Trie = require('merkle-patricia-tree')

/**
 * An object that represents the block
 */
export class Block {
  public readonly header: BlockHeader
  public readonly transactions: Transaction[] = []
  public readonly uncleHeaders: BlockHeader[] = []
  public readonly txTrie = new Trie()

  private readonly _common: Common

  /**
   * Creates a new block object
   *
   * @param data - The block's data.
   * @param opts - The network options for this block, and its header, uncle headers and txs.
   */
  constructor(
    data: Buffer | [Buffer[], Buffer[], Buffer[]] | BlockData = {},
    opts: NetworkOptions = {},
  ) {
    if (opts.common) {
      if (opts.chain !== undefined || opts.hardfork !== undefined) {
        throw new Error(
          'Instantiation with both opts.common and opts.chain / opts.hardfork parameter not allowed!',
        )
      }

      this._common = opts.common
    } else {
      const chain = opts.chain ? opts.chain : 'mainnet'
      // TODO: Compute the hardfork based on this block's number. It can be implemented right now
      // because the block number is not immutable, so the Common can get out of sync.
      const hardfork = opts.hardfork ? opts.hardfork : null
      this._common = new Common(chain, hardfork)
    }

    let rawTransactions
    let rawUncleHeaders

    if (Buffer.isBuffer(data)) {
      // We do this to silence a TS error. We know that after this statement, data is
      // a [Buffer[], Buffer[], Buffer[]]
      const dataAsAny = rlp.decode(data) as any
      data = dataAsAny as [Buffer[], Buffer[], Buffer[]]
    }

    if (Array.isArray(data)) {
      this.header = new BlockHeader(data[0], { common: this._common })
      rawTransactions = data[1]
      rawUncleHeaders = data[2]
    } else {
      this.header = new BlockHeader(data.header, { common: this._common })
      rawTransactions = data.transactions || []
      rawUncleHeaders = data.uncleHeaders || []
    }

    // parse uncle headers
    for (let i = 0; i < rawUncleHeaders.length; i++) {
      this.uncleHeaders.push(new BlockHeader(rawUncleHeaders[i], opts))
    }

    // parse transactions
    for (let i = 0; i < rawTransactions.length; i++) {
      // TODO: Pass the common object instead of the options. It can't be implemented right now
      // because the hardfork may be `null`. Read the above TODO for more info.
      const tx = new Transaction(rawTransactions[i], opts)
      this.transactions.push(tx)
    }
  }

  get raw(): [Buffer[], Buffer[], Buffer[]] {
    return this.serialize(false)
  }

  /**
   * Produces a hash the RLP of the block
   */
  hash(): Buffer {
    return this.header.hash()
  }

  /**
   * Determines if this block is the genesis block
   */
  isGenesis(): boolean {
    return this.header.isGenesis()
{'fraction_non_alphanumeric': 0.11888945665133709, 'fraction_numerical': 0.00170328734457503, 'mean_word_length': 2.8154645873944117, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2427996', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1729, 'n_words': 566}