<commit_before>import { buf as crc32Buffer } from 'crc-32'
import { chains as chainParams } from './chains'
import { hardforks as hardforkChanges } from './hardforks'
import { Chain } from './types'

interface hardforkOptions {
  /** optional, only allow supported HFs (default: false) */
  onlySupported?: boolean
  /** optional, only active HFs (default: false) */
  onlyActive?: boolean
}

/**
 * Common class to access chain and hardfork parameters
 */
export default class Common {
  private _hardfork: string | null
  private _supportedHardforks: Array<string>
  private _chainParams: Chain

  /**
   * Creates a Common object for a custom chain, based on a standard one. It uses all the [[Chain]]
   * params from [[baseChain]] except the ones overridden in [[customChainParams]].
   *
   * @param baseChain The name (`mainnet`) or id (`1`) of a standard chain used to base the custom
   * chain params on.
   * @param customChainParams The custom parameters of the chain.
   * @param hardfork String identifier ('byzantium') for hardfork (optional)
   * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
   */
  static forCustomChain(
    baseChain: string | number,
    customChainParams: Partial<Chain>,
    hardfork?: string | null,
    supportedHardforks?: Array<string>,
  ): Common {
    const standardChainParams = Common._getChainParams(baseChain)

    return new Common(
      {
        ...standardChainParams,
        ...customChainParams,
      },
      hardfork,
      supportedHardforks,
    )
  }

  private static _getChainParams(chain: string | number): Chain {
    if (typeof chain === 'number') {
      if (chainParams['names'][chain]) {
        return chainParams[chainParams['names'][chain]]
      }

      throw new Error(`Chain with ID ${chain} not supported`)
    }

    if (chainParams[chain]) {
      return chainParams[chain]
    }

    throw new Error(`Chain with name ${chain} not supported`)
  }

  /**
   * @constructor
   * @param chain String ('mainnet') or Number (1) chain
   * @param hardfork String identifier ('byzantium') for hardfork (optional)
   * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
   */
  constructor(
    chain: string | number | object,
    hardfork?: string | null,
    supportedHardforks?: Array<string>,
  ) {
    this._chainParams = this.setChain(chain)
    this._hardfork = null
    this._supportedHardforks = supportedHardforks === undefined ? [] : supportedHardforks
    if (hardfork) {
      this.setHardfork(hardfork)
    }
  }

  /**
   * Sets the chain
   * @param chain String ('mainnet') or Number (1) chain
   *     representation. Or, a Dictionary of chain parameters for a private network.
   * @returns The dictionary with parameters set as chain
   */
  setChain(chain: string | number | object): any {
    if (typeof chain === 'number' || typeof chain === 'string') {
      this._chainParams = Common._getChainParams(chain)
    } else if (typeof chain === 'object') {
      const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
      for (const param of required) {
        if ((<any>chain)[param] === undefined) {
          throw new Error(`Missing required chain parameter: ${param}`)
        }
      }
      this._chainParams = chain as Chain
    } else {
      throw new Error('Wrong input format')
    }
    return this._chainParams
  }

  /**
   * Sets the hardfork to get params for
   * @param hardfork String identifier ('byzantium')
   */
  setHardfork(hardfork: string | null): void {
    if (!this._isSupportedHardfork(hardfork)) {
      throw new Error(`Hardfork ${hardfork} not set as supported in supportedHardforks`)
    }
    let changed = false
    for (const hfChanges of hardforkChanges) {
      if (hfChanges[0] === hardfork) {
        this._hardfork = hardfork
        changed = true
      }
    }
    if (!changed) {
      throw new Error(`Hardfork with name ${hardfork} not supported`)
    }
  }

  /**
   * Internal helper function to choose between hardfork set and hardfork provided as param
   * @param hardfork Hardfork given to function as a parameter
   * @returns Hardfork chosen to be used
   */
  _chooseHardfork(hardfork?: string | null, onlySupported?: boolean): string {
    onlySupported = onlySupported === undefined ? true : onlySupported
    if (!hardfork) {
      if (!this._hardfork) {
        throw new Error('Method called with neither a hardfork set nor provided by param')
      } else {
        hardfork = this._hardfork
      }<commit_msg>refactor: sets default value on fn signature, removing manual check.<commit_after>import { buf as crc32Buffer } from 'crc-32'
import { chains as chainParams } from './chains'
import { hardforks as hardforkChanges } from './hardforks'
import { Chain } from './types'

interface hardforkOptions {
  /** optional, only allow supported HFs (default: false) */
  onlySupported?: boolean
  /** optional, only active HFs (default: false) */
  onlyActive?: boolean
}

/**
 * Common class to access chain and hardfork parameters
 */
export default class Common {
  private _hardfork: string | null
  private _supportedHardforks: Array<string>
  private _chainParams: Chain

  /**
   * Creates a Common object for a custom chain, based on a standard one. It uses all the [[Chain]]
   * params from [[baseChain]] except the ones overridden in [[customChainParams]].
   *
   * @param baseChain The name (`mainnet`) or id (`1`) of a standard chain used to base the custom
   * chain params on.
   * @param customChainParams The custom parameters of the chain.
   * @param hardfork String identifier ('byzantium') for hardfork (optional)
   * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
   */
  static forCustomChain(
    baseChain: string | number,
    customChainParams: Partial<Chain>,
    hardfork?: string | null,
    supportedHardforks?: Array<string>,
  ): Common {
    const standardChainParams = Common._getChainParams(baseChain)

    return new Common(
      {
        ...standardChainParams,
        ...customChainParams,
      },
      hardfork,
      supportedHardforks,
    )
  }

  private static _getChainParams(chain: string | number): Chain {
    if (typeof chain === 'number') {
      if (chainParams['names'][chain]) {
        return chainParams[chainParams['names'][chain]]
      }

      throw new Error(`Chain with ID ${chain} not supported`)
    }

    if (chainParams[chain]) {
      return chainParams[chain]
    }

    throw new Error(`Chain with name ${chain} not supported`)
  }

  /**
   * @constructor
   * @param chain String ('mainnet') or Number (1) chain
   * @param hardfork String identifier ('byzantium') for hardfork (optional)
   * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
   */
  constructor(
    chain: string | number | object,
    hardfork?: string | null,
    supportedHardforks?: Array<string>,
  ) {
    this._chainParams = this.setChain(chain)
    this._hardfork = null
    this._supportedHardforks = supportedHardforks === undefined ? [] : supportedHardforks
    if (hardfork) {
      this.setHardfork(hardfork)
    }
  }

  /**
   * Sets the chain
   * @param chain String ('mainnet') or Number (1) chain
   *     representation. Or, a Dictionary of chain parameters for a private network.
   * @returns The dictionary with parameters set as chain
   */
  setChain(chain: string | number | object): any {
    if (typeof chain === 'number' || typeof chain === 'string') {
      this._chainParams = Common._getChainParams(chain)
    } else if (typeof chain === 'object') {
      const required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes']
      for (const param of required) {
        if ((<any>chain)[param] === undefined) {
          throw new Error(`Missing required chain parameter: ${param}`)
        }
      }
      this._chainParams = chain as Chain
    } else {
      throw new Error('Wrong input format')
    }
    return this._chainParams
  }

  /**
   * Sets the hardfork to get params for
   * @param hardfork String identifier ('byzantium')
   */
  setHardfork(hardfork: string | null): void {
    if (!this._isSupportedHardfork(hardfork)) {
      throw new Error(`Hardfork ${hardfork} not set as supported in supportedHardforks`)
    }
    let changed = false
    for (const hfChanges of hardforkChanges) {
      if (hfChanges[0] === hardfork) {
        this._hardfork = hardfork
        changed = true
      }
    }
    if (!changed) {
      throw new Error(`Hardfork with name ${hardfork} not supported`)
    }
  }

  /**
   * Internal helper function to choose between hardfork set and hardfork provided as param
   * @param hardfork Hardfork given to function as a parameter
   * @returns Hardfork chosen to be used
   */
  _chooseHardfork(hardfork?: string | null, onlySupported: boolean = true): string {
    if (!hardfork) {
      if (!this._hardfork) {
        throw new Error('Method called with neither a hardfork set nor provided by param')
      } else {
        hardfork = this._hardfork
      }
{'fraction_non_alphanumeric': 0.10697827518104015, 'fraction_numerical': 0.0017555409260478386, 'mean_word_length': 3.098471223021583, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1656551', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2503, 'n_words': 875}