Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save holgerd77/864e22d1f3f2ceefe89ce0b7e80b79af to your computer and use it in GitHub Desktop.
Save holgerd77/864e22d1f3f2ceefe89ce0b7e80b79af to your computer and use it in GitHub Desktop.
ethereumjs-blockchain - test getHead() for a reopened block-containing leveldown DB instance
'use strict'
const test = require('tape')
const Blockchain = require('..')
const Block = require('ethereumjs-block')
const async = require('async')
const ethUtil = require('ethereumjs-util')
const levelup = require('levelup')
const leveldown = require('leveldown')
const memdown = require('memdown')
const testData = require('./testdata.json')
const BN = require('bn.js')
test('blockchain test', function (t) {
var db = levelup('./db', { db: leveldown })
var blockchain = new Blockchain({ db: db })
var genesisBlock
var blocks = []
var forkBlock
blockchain.validate = false
async.series([
function (done) {
blockchain.getHead(function (err, head) {
if (err) return done(err)
console.log(head)
t.ok(true, 'should not crash on getting head of a blockchain without a genesis')
done()
})
},
function addgenesis (done) {
genesisBlock = new Block()
genesisBlock.setGenesisParams()
blockchain.putGenesis(genesisBlock, function (err) {
if (err) return done(err)
t.equals(genesisBlock.hash().toString('hex'), blockchain.meta.genesis.toString('hex'), 'genesis block hash should be correct')
blocks.push(genesisBlock)
done()
})
},
function addBlocks (done) {
function addNextBlock (blockNumber) {
var block = new Block()
block.header.number = ethUtil.toBuffer(blockNumber)
block.header.difficulty = '0xfffffff'
block.header.parentHash = blocks[blockNumber - 1].hash()
blockchain.putBlock(block, function (err) {
if (err) return done(err)
blocks.push(block)
if (blocks.length === 10) {
t.ok(true, 'added 10 blocks')
done()
} else {
addNextBlock(blockNumber + 1)
}
})
}
addNextBlock(1)
}
], function (err) {
if (err) {
t.ok(false, err)
} else {
t.ok(true, 'no errors')
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment