<commit_before>import VM from '../../'

import Account from 'ethereumjs-account'
import * as utils from 'ethereumjs-util'
import { promisify } from 'util'
import PStateManager from '../../lib/state/promisified'
import { toBuffer } from 'ethereumjs-util'

const Block = require('ethereumjs-block')
const Blockchain = require('ethereumjs-blockchain')
const BlockHeader = require('ethereumjs-block/header.js')
const testData = require('./test-data')
const level = require('level')

async function main() {
  const hardfork = testData.network.toLowerCase()

  const blockchain = new Blockchain({
    hardfork,
    // This flag can be control whether the blocks are validated. This includes:
    //    * Verifying PoW
    //    * Validating each blocks's difficulty, uncles, tries, header and uncles.
    validate: true,
  })

  // When verifying PoW, setting this cache improves the performance of subsequent runs of this
  // script. It has no effect if the blockchain is initialized with `validate: false`.
  setEthashCache(blockchain)

  const vm = new VM({
    blockchain: blockchain,
    hardfork,
  })

  await setupPreConditions(vm, testData)

  await setGenesisBlock(blockchain, hardfork)

  await putBlocks(blockchain, hardfork, testData)

  await vm.runBlockchain(blockchain)

  const blockchainHead = await promisify(vm.blockchain.getHead.bind(vm.blockchain))()

  console.log('--- Finished processing the BlockChain ---')
  console.log('New head:', '0x' + blockchainHead.hash().toString('hex'))
  console.log('Expected:', testData.lastblockhash)
}

function setEthashCache(blockchain: any) {
  if (blockchain.validate) {
    blockchain.ethash.cacheDB = level('./.cachedb')
  }
}

async function setupPreConditions(vm: VM, testData: any) {
  const psm = new PStateManager(vm.stateManager)
  for (const address of Object.keys(testData.pre)) {
    const addressBuf = utils.toBuffer(address)

    const acctData = testData.pre[address]
    const account = new Account({
      nonce: acctData.nonce,
      balance: acctData.balance,
    })

    await psm.putAccount(addressBuf, account)

    for (const hexStorageKey of Object.keys(acctData.storage)) {
      const val = utils.toBuffer(acctData.storage[hexStorageKey])
      const storageKey = utils.setLength(utils.toBuffer(hexStorageKey), 32)

      await psm.putContractStorage(addressBuf, storageKey, val)
    }

    const codeBuf = utils.toBuffer(acctData.code)

    await psm.putContractCode(addressBuf, codeBuf)
  }

  // This forces a cache flush, which is necessary here
  await psm.getStateRoot()
}

async function setGenesisBlock(blockchain: any, hardfork: string) {
  const genesisBlock = new Block({ hardfork })
  genesisBlock.header = new BlockHeader(testData.genesisBlockHeader, { hardfork })

  await promisify(blockchain.putGenesis.bind(blockchain))(genesisBlock)
}

async function putBlocks(blockchain: any, hardfork: string, testData: any) {
  for (const blockData of testData.blocks) {
    const block = new Block(utils.toBuffer(blockData.rlp), { hardfork })
    await promisify(blockchain.putBlock.bind(blockchain))(block)
  }
}

main()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(1)
  })<commit_msg>Use checkpoint and commit in run-blockchain example<commit_after>import VM from '../../'

import Account from 'ethereumjs-account'
import * as utils from 'ethereumjs-util'
import { promisify } from 'util'
import PStateManager from '../../lib/state/promisified'

const Block = require('ethereumjs-block')
const Blockchain = require('ethereumjs-blockchain')
const BlockHeader = require('ethereumjs-block/header.js')
const testData = require('./test-data')
const level = require('level')

async function main() {
  const hardfork = testData.network.toLowerCase()

  const blockchain = new Blockchain({
    hardfork,
    // This flag can be control whether the blocks are validated. This includes:
    //    * Verifying PoW
    //    * Validating each blocks's difficulty, uncles, tries, header and uncles.
    validate: true,
  })

  // When verifying PoW, setting this cache improves the performance of subsequent runs of this
  // script. It has no effect if the blockchain is initialized with `validate: false`.
  setEthashCache(blockchain)

  const vm = new VM({
    blockchain: blockchain,
    hardfork,
  })

  await setupPreConditions(vm, testData)

  await setGenesisBlock(blockchain, hardfork)

  await putBlocks(blockchain, hardfork, testData)

  await vm.runBlockchain(blockchain)

  const blockchainHead = await promisify(vm.blockchain.getHead.bind(vm.blockchain))()

  console.log('--- Finished processing the BlockChain ---')
  console.log('New head:', '0x' + blockchainHead.hash().toString('hex'))
  console.log('Expected:', testData.lastblockhash)
}

function setEthashCache(blockchain: any) {
  if (blockchain.validate) {
    blockchain.ethash.cacheDB = level('./.cachedb')
  }
}

async function setupPreConditions(vm: VM, testData: any) {
  const psm = new PStateManager(vm.stateManager)

  await psm.checkpoint()

  for (const address of Object.keys(testData.pre)) {
    const addressBuf = utils.toBuffer(address)

    const acctData = testData.pre[address]
    const account = new Account({
      nonce: acctData.nonce,
      balance: acctData.balance,
    })

    await psm.putAccount(addressBuf, account)

    for (const hexStorageKey of Object.keys(acctData.storage)) {
      const val = utils.toBuffer(acctData.storage[hexStorageKey])
      const storageKey = utils.setLength(utils.toBuffer(hexStorageKey), 32)

      await psm.putContractStorage(addressBuf, storageKey, val)
    }

    const codeBuf = utils.toBuffer(acctData.code)

    await psm.putContractCode(addressBuf, codeBuf)
  }

  await psm.commit()
}

async function setGenesisBlock(blockchain: any, hardfork: string) {
  const genesisBlock = new Block({ hardfork })
  genesisBlock.header = new BlockHeader(testData.genesisBlockHeader, { hardfork })

  await promisify(blockchain.putGenesis.bind(blockchain))(genesisBlock)
}

async function putBlocks(blockchain: any, hardfork: string, testData: any) {
  for (const blockData of testData.blocks) {
    const block = new Block(utils.toBuffer(blockData.rlp), { hardfork })
    await promisify(blockchain.putBlock.bind(blockchain))(block)
  }
}

main()
  .then(() => process.exit(0))
  .catch(err => {
    console.error(err)
    process.exit(1)
  })
{'fraction_non_alphanumeric': 0.11597374179431072, 'fraction_numerical': 0.0015629884338855893, 'mean_word_length': 4.160483870967742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3081460', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1969, 'n_words': 499}