Skip to content

Commit

Permalink
blockchain: use new method to calculate difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Nov 5, 2020
1 parent 515051e commit 7423290
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
39 changes: 22 additions & 17 deletions packages/blockchain/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BN } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Block, BlockHeader } from '@ethereumjs/block'
import { Block, BlockHeader, BlockOptions } from '@ethereumjs/block'
import tape from 'tape'
import Blockchain from '../src'
import { generateBlockchain, generateBlocks, isConsecutive, createTestDB } from './util'
Expand Down Expand Up @@ -81,12 +81,11 @@ tape('blockchain test', (t) => {
header: {
number,
parentHash: lastBlock.hash(),
difficulty: lastBlock.canonicalDifficulty(lastBlock),
timestamp: lastBlock.header.timestamp.addn(1),
gasLimit,
},
}
const block = Block.fromBlockData(blockData)
const block = Block.fromBlockData(blockData, { calcDifficultyFromHeader: lastBlock.header })
await blockchain.putBlock(block)
blocks.push(block)

Expand Down Expand Up @@ -118,12 +117,13 @@ tape('blockchain test', (t) => {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(1),
gasLimit,
},
}
const block = Block.fromBlockData(blockData)
const block = Block.fromBlockData(blockData, {
calcDifficultyFromHeader: genesis.header,
})
blocks.push(block)
await blockchain.putBlock(block)

Expand Down Expand Up @@ -377,11 +377,13 @@ tape('blockchain test', (t) => {
const headerData = {
number: 15,
parentHash: blocks[14].hash(),
difficulty: blocks[14].canonicalDifficulty(blocks[14]),
gasLimit: 8000000,
timestamp: blocks[14].header.timestamp.addn(1),
}
const forkHeader = BlockHeader.fromHeaderData(headerData, { common })
const forkHeader = BlockHeader.fromHeaderData(headerData, {
common,
calcDifficultyFromHeader: blocks[14].header,
})

blockchain._heads['staletest'] = blockchain._headHeader

Expand All @@ -400,11 +402,13 @@ tape('blockchain test', (t) => {
const headerData = {
number: 15,
parentHash: blocks[14].hash(),
difficulty: blocks[14].canonicalDifficulty(blocks[14]),
gasLimit: 8000000,
timestamp: blocks[14].header.timestamp.addn(1),
}
const forkHeader = BlockHeader.fromHeaderData(headerData, { common })
const forkHeader = BlockHeader.fromHeaderData(headerData, {
common,
calcDifficultyFromHeader: blocks[14].header,
})

blockchain._heads['staletest'] = blockchain._headHeader

Expand Down Expand Up @@ -558,11 +562,12 @@ tape('blockchain test', (t) => {
const headerData = {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
gasLimit,
timestamp: genesis.header.timestamp.addn(1),
}
const header = BlockHeader.fromHeaderData(headerData)
const header = BlockHeader.fromHeaderData(headerData, {
calcDifficultyFromHeader: genesis.header,
})
await blockchain.putHeader(header)

blockchain = new Blockchain({
Expand All @@ -586,7 +591,7 @@ tape('blockchain test', (t) => {
})
const gasLimit = 8000000
const common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
const opts = { common }
const opts: BlockOptions = { common }

const genesis = Block.genesis({ header: { gasLimit } }, opts)
await blockchain.putGenesis(genesis)
Expand All @@ -595,30 +600,30 @@ tape('blockchain test', (t) => {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(3),
gasLimit,
},
}
opts.calcDifficultyFromHeader = genesis.header
const block = Block.fromBlockData(blockData, opts)

const headerData1 = {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(1),
gasLimit,
}
opts.calcDifficultyFromHeader = genesis.header
const header1 = BlockHeader.fromHeaderData(headerData1, opts)
const headers = [header1]

const headerData2 = {
number: 2,
parentHash: header1.hash(),
difficulty: header1.canonicalDifficulty(block.header),
timestamp: header1.timestamp.addn(1),
gasLimit,
}
opts.calcDifficultyFromHeader = block.header
const header2 = BlockHeader.fromHeaderData(headerData2, opts)
headers.push(header2)

Expand Down Expand Up @@ -655,7 +660,6 @@ tape('blockchain test', (t) => {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(1),
gasLimit,
},
Expand All @@ -668,9 +672,10 @@ tape('blockchain test', (t) => {

const blocks = [
genesis,
Block.fromBlockData(blockData1, { common }),
Block.fromBlockData(blockData1, { common, calcDifficultyFromHeader: genesis.header }),
Block.fromBlockData(blockData2, {
common: new Common({ chain: 'ropsten', hardfork: 'chainstart' }),
calcDifficultyFromHeader: genesis.header,
}),
]

Expand Down
7 changes: 5 additions & 2 deletions packages/blockchain/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export const generateBlocks = (numberOfBlocks: number, existingBlocks?: Block[])
header: {
number: i,
parentHash: lastBlock.hash(),
difficulty: lastBlock.canonicalDifficulty(lastBlock),
gasLimit,
timestamp: lastBlock.header.timestamp.addn(1),
},
}
const block = Block.fromBlockData(blockData, opts)
const block = Block.fromBlockData(blockData, {
common,
calcDifficultyFromHeader: lastBlock.header,
})
blocks.push(block)
}

Expand Down Expand Up @@ -83,6 +85,7 @@ export const generateConsecutiveBlock = (
},
{
common,
calcDifficultyFromHeader: parentBlock.header,
}
)

Expand Down

0 comments on commit 7423290

Please sign in to comment.