Skip to content

Commit

Permalink
blockchain: make tests use getConsecutiveDifficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Oct 29, 2020
1 parent 14b0e0e commit 6d69a53
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
46 changes: 27 additions & 19 deletions packages/blockchain/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ tape('blockchain test', (t) => {

const addNextBlock = async (number: number) => {
const lastBlock = blocks[number - 1]
const timestamp = lastBlock.header.timestamp.addn(1)
const blockData = {
header: {
number,
parentHash: lastBlock.hash(),
difficulty: lastBlock.canonicalDifficulty(lastBlock),
timestamp: lastBlock.header.timestamp.addn(1),
difficulty: lastBlock.getConsecutiveCanonicalDifficulty(timestamp),
timestamp,
gasLimit,
},
}
Expand Down Expand Up @@ -113,13 +114,13 @@ tape('blockchain test', (t) => {
const genesis = Block.genesis({ header: { gasLimit } })
blocks.push(genesis)
await blockchain.putGenesis(genesis)

const timestamp = genesis.header.timestamp.addn(1)
const blockData = {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(1),
difficulty: genesis.getConsecutiveCanonicalDifficulty(timestamp),
timestamp,
gasLimit,
},
}
Expand Down Expand Up @@ -374,12 +375,13 @@ tape('blockchain test', (t) => {
await blockchain.putBlocks(blocks.slice(1))

const common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
const timestamp = blocks[14].header.timestamp.addn(1)
const headerData = {
number: 15,
parentHash: blocks[14].hash(),
difficulty: blocks[14].canonicalDifficulty(blocks[14]),
difficulty: blocks[14].getConsecutiveCanonicalDifficulty(timestamp),
gasLimit: 8000000,
timestamp: blocks[14].header.timestamp.addn(1),
timestamp,
}
const forkHeader = BlockHeader.fromHeaderData(headerData, { common })

Expand All @@ -397,12 +399,13 @@ tape('blockchain test', (t) => {
st.error(error, 'no error')

const common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
const timestamp = blocks[14].header.timestamp.addn(1)
const headerData = {
number: 15,
parentHash: blocks[14].hash(),
difficulty: blocks[14].canonicalDifficulty(blocks[14]),
difficulty: blocks[14].getConsecutiveCanonicalDifficulty(timestamp),
gasLimit: 8000000,
timestamp: blocks[14].header.timestamp.addn(1),
timestamp,
}
const forkHeader = BlockHeader.fromHeaderData(headerData, { common })

Expand Down Expand Up @@ -555,12 +558,13 @@ tape('blockchain test', (t) => {
const genesis = Block.genesis({ header: { gasLimit } })
await blockchain.putGenesis(genesis)

const timestamp = genesis.header.timestamp.addn(1)
const headerData = {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
difficulty: genesis.getConsecutiveCanonicalDifficulty(timestamp),
gasLimit,
timestamp: genesis.header.timestamp.addn(1),
timestamp,
}
const header = BlockHeader.fromHeaderData(headerData)
await blockchain.putHeader(header)
Expand Down Expand Up @@ -591,32 +595,35 @@ tape('blockchain test', (t) => {
const genesis = Block.genesis({ header: { gasLimit } }, opts)
await blockchain.putGenesis(genesis)

let timestamp = genesis.header.timestamp.addn(3)
const blockData = {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(3),
difficulty: genesis.getConsecutiveCanonicalDifficulty(timestamp),
timestamp,
gasLimit,
},
}
const block = Block.fromBlockData(blockData, opts)

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

timestamp = header1.timestamp.addn(1)
const headerData2 = {
number: 2,
parentHash: header1.hash(),
difficulty: header1.canonicalDifficulty(block.header),
timestamp: header1.timestamp.addn(1),
difficulty: header1.getConsecutiveCanonicalDifficulty(timestamp),
timestamp,
gasLimit,
}
const header2 = BlockHeader.fromHeaderData(headerData2, opts)
Expand Down Expand Up @@ -651,12 +658,13 @@ tape('blockchain test', (t) => {

const genesis = Block.genesis({ header: { gasLimit } }, { common })

const timestamp = genesis.header.timestamp.addn(1)
const blockData1 = {
header: {
number: 1,
parentHash: genesis.hash(),
difficulty: genesis.canonicalDifficulty(genesis),
timestamp: genesis.header.timestamp.addn(1),
difficulty: genesis.getConsecutiveCanonicalDifficulty(timestamp),
timestamp,
gasLimit,
},
}
Expand Down
5 changes: 3 additions & 2 deletions packages/blockchain/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ export const generateBlocks = (numberOfBlocks: number, existingBlocks?: Block[])

for (let i = blocks.length; i < numberOfBlocks; i++) {
const lastBlock = blocks[i - 1]
const timestamp = lastBlock.header.timestamp.addn(1)
const blockData = {
header: {
number: i,
parentHash: lastBlock.hash(),
difficulty: lastBlock.canonicalDifficulty(lastBlock),
difficulty: lastBlock.getConsecutiveCanonicalDifficulty(timestamp),
gasLimit,
timestamp: lastBlock.header.timestamp.addn(1),
timestamp,
},
}
const block = Block.fromBlockData(blockData, opts)
Expand Down

0 comments on commit 6d69a53

Please sign in to comment.