From 6d69a531bf48271c09759ed47ac11798b4f4753e Mon Sep 17 00:00:00 2001 From: Jochem Brouwer Date: Thu, 29 Oct 2020 21:52:23 +0100 Subject: [PATCH] blockchain: make tests use getConsecutiveDifficulty --- packages/blockchain/test/index.ts | 46 ++++++++++++++++++------------- packages/blockchain/test/util.ts | 5 ++-- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/blockchain/test/index.ts b/packages/blockchain/test/index.ts index 7156f46c132..90ff5ae656a 100644 --- a/packages/blockchain/test/index.ts +++ b/packages/blockchain/test/index.ts @@ -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, }, } @@ -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, }, } @@ -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 }) @@ -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 }) @@ -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) @@ -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) @@ -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, }, } diff --git a/packages/blockchain/test/util.ts b/packages/blockchain/test/util.ts index b1f3b8d785c..7d9c32f14ba 100644 --- a/packages/blockchain/test/util.ts +++ b/packages/blockchain/test/util.ts @@ -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)