Skip to content

Commit

Permalink
Chain & HF enum usage (#1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonmacro authored Jul 19, 2021
1 parent 7a5af59 commit 8d140c3
Show file tree
Hide file tree
Showing 82 changed files with 444 additions and 394 deletions.
12 changes: 6 additions & 6 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ To instantiate an EIP-1559 block the hardfork parameter on the `Common` instance

```typescript
import { Block } from 'ethereumjs-block'
import Common from '@ethereumjs/common'
const common = new Common({ chain: 'mainnet', hardfork: 'london' })
import Common, { Chain, Hardfork } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const block = Block.fromBlockData({
header: {
//...,
Expand All @@ -85,8 +85,8 @@ An Ethash/PoW block can be instantiated as follows:

```typescript
import { Block } from '@ethereumjs/block'
import Common from '@ethereumjs/common'
const common = new Common({ chain: 'mainnet' })
import Common, { Chain } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet })
console.log(common.consensusType()) // 'pow'
console.log(common.consensusAlgorithm()) // 'ethash'
const block = Block.fromBlockData({}, { common })
Expand All @@ -102,8 +102,8 @@ A clique block can be instantiated as follows:

```typescript
import { Block } from '@ethereumjs/block'
import Common from '@ethereumjs/common'
const common = new Common({ chain: 'goerli' })
import Common, { Chain } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Goerli })
console.log(common.consensusType()) // 'poa'
console.log(common.consensusAlgorithm()) // 'clique'
const block = Block.fromBlockData({}, { common })
Expand Down
6 changes: 3 additions & 3 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import {
Address,
BN,
Expand Down Expand Up @@ -202,9 +202,9 @@ export class BlockHeader {
if (options.common) {
this._common = options.common.copy()
} else {
const chain = 'mainnet' // default
const chain = Chain.Mainnet // default
if (options.initWithGenesisHeader) {
this._common = new Common({ chain, hardfork: 'chainstart' })
this._common = new Common({ chain, hardfork: Hardfork.Chainstart })
} else {
// This initializes on the Common default hardfork
this._common = new Common({ chain })
Expand Down
24 changes: 12 additions & 12 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ tape('[Block]: block functions', function (t) {

t.test('should initialize with null parameters without throwing', function (st) {
st.doesNotThrow(function () {
const common = new Common({ chain: 'ropsten' })
const common = new Common({ chain: Chain.Ropsten })
const opts = { common }
Block.fromBlockData({}, opts)
st.end()
Expand All @@ -72,7 +72,7 @@ tape('[Block]: block functions', function (t) {
t.test(
'should throw when trying to initialize with uncle headers on a PoA network',
function (st) {
const common = new Common({ chain: 'rinkeby' })
const common = new Common({ chain: Chain.Rinkeby })
const uncleBlock = Block.fromBlockData(
{ header: { extraData: Buffer.alloc(117) } },
{ common }
Expand Down Expand Up @@ -100,7 +100,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test block validation on poa chain', async function (st) {
const common = new Common({ chain: 'goerli', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
const blockchain = new Mockchain()
const block = blockFromRpc(testDataFromRpcGoerli, [], { common })

Expand Down Expand Up @@ -158,7 +158,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test transaction validation with legacy tx in london', async function (st) {
const common = new Common({ chain: 'goerli', hardfork: 'london' })
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.London })
const blockRlp = testData.blocks[0].rlp
const block = Block.fromRLPSerializedBlock(blockRlp, { common, freeze: false })
await testTransactionValidation(st, block)
Expand Down Expand Up @@ -469,7 +469,7 @@ tape('[Block]: block functions', function (t) {
const blockchain = new Mockchain()

const common = new Common({ chain: Chain.Mainnet })
common.setHardfork('berlin')
common.setHardfork(Hardfork.Berlin)

const mainnetForkBlock = common.hardforkBlockBN('london')
const rootBlock = Block.fromBlockData({
Expand All @@ -491,7 +491,7 @@ tape('[Block]: block functions', function (t) {
common
)
await blockchain.putBlock(preForkBlock)
common.setHardfork('london')
common.setHardfork(Hardfork.London)
const forkBlock = createBlock(preForkBlock, 'forkBlock', [], common)
await blockchain.putBlock(forkBlock)
const uncleFork = createBlock(forkBlock, 'uncleFork', [], common)
Expand Down Expand Up @@ -565,7 +565,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test isGenesis (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten' })
const common = new Common({ chain: Chain.Ropsten })
const block = Block.fromBlockData({ header: { number: 1 } }, { common })
st.notEqual(block.isGenesis(), true)
const genesisBlock = Block.fromBlockData({ header: { number: 0 } }, { common })
Expand All @@ -587,7 +587,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test genesis hashes (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
const genesis = Block.genesis({}, { common })
st.strictEqual(
genesis.hash().toString('hex'),
Expand All @@ -598,7 +598,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test genesis hashes (rinkeby)', function (st) {
const common = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })
const genesis = Block.genesis({}, { common })
st.strictEqual(
genesis.hash().toString('hex'),
Expand All @@ -609,7 +609,7 @@ tape('[Block]: block functions', function (t) {
})

t.test('should test genesis parameters (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
const genesis = Block.genesis({}, { common })
const ropstenStateRoot = '217b0bbcfb72e2d57e28f33cb361b9983513177755dc3f33ce3e7022ed62b77b'
st.strictEqual(
Expand Down Expand Up @@ -648,7 +648,7 @@ tape('[Block]: block functions', function (t) {
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = Buffer.from('1D4C00', 'hex')

const common = new Common({ chain: 'mainnet', hardfork: 'dao' })
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Dao })
st.throws(
function () {
Block.fromValuesArray(blockData, { common })
Expand All @@ -669,7 +669,7 @@ tape('[Block]: block functions', function (t) {
t.test(
'should set canonical difficulty if I provide a calcDifficultyFromHeader header',
function (st) {
const common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const genesis = Block.genesis({}, { common })

const nextBlockHeaderData = {
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/clique.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import tape from 'tape'
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { BlockHeader } from '../src/header'
import { Address } from 'ethereumjs-util'

tape('[Header]: Clique PoA Functionality', function (t) {
const common = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })

t.test('Header Data', function (st) {
let header = BlockHeader.fromHeaderData({ number: 1 })
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/difficulty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tape from 'tape'
import { BN, toBuffer, bufferToInt } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import Common, { Chain } from '@ethereumjs/common'
import { Block } from '../src'

function isHexPrefixed(str: string) {
Expand Down Expand Up @@ -42,7 +42,7 @@ tape('[Header]: difficulty tests', (t) => {
/* eslint-disable-next-line no-restricted-syntax */
for (const testName in testData) {
const test = testData[testName]
const common = new Common({ chain: 'mainnet', hardfork: hardfork })
const common = new Common({ chain: Chain.Mainnet, hardfork: hardfork })
const parentBlock = Block.fromBlockData(
{
header: {
Expand Down
8 changes: 4 additions & 4 deletions packages/block/test/eip1559block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tape from 'tape'
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { BlockHeader } from '../src/header'
import { BN } from 'ethereumjs-util'
import { Mockchain } from './mockchain'
Expand All @@ -12,8 +12,8 @@ const eip1559BaseFee = require('./testdata/eip1559baseFee.json')

const common = new Common({
eips: [1559],
chain: 'mainnet',
hardfork: 'london',
chain: Chain.Mainnet,
hardfork: Hardfork.London,
})

const blockchain1 = new Mockchain()
Expand Down Expand Up @@ -42,7 +42,7 @@ tape('EIP1559 tests', function (t) {

t.test('Header -> Initialization', async function (st) {
try {
const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Istanbul })
BlockHeader.fromHeaderData(
{
number: new BN(1),
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tape from 'tape'
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { Transaction } from '@ethereumjs/tx'
import blockFromRpc from '../src/from-rpc'
import blockHeaderFromRpc from '../src/header-from-rpc'
Expand Down Expand Up @@ -79,7 +79,7 @@ tape('[fromRPC]:', function (t) {
)

t.test('should create a block from london hardfork', function (st) {
const common = new Common({ chain: 'goerli', hardfork: 'london' })
const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.London })
const block = blockFromRpc(testDataFromRpcGoerliLondon, [], { common })
st.equal(
`0x${block.header.baseFeePerGas?.toString(16)}`,
Expand Down
20 changes: 10 additions & 10 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import tape from 'tape'
import { Address, BN, zeros, KECCAK256_RLP, KECCAK256_RLP_ARRAY, rlp } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { BlockHeader } from '../src/header'
import { Block } from '../src'
import { Mockchain } from './mockchain'
Expand Down Expand Up @@ -39,12 +39,12 @@ tape('[Block]: Header functions', function (t) {
})

t.test('Initialization -> fromHeaderData()', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
let header = BlockHeader.genesis(undefined, { common })
st.ok(header.hash().toString('hex'), 'genesis block should initialize')
st.equal(header._common.hardfork(), 'chainstart', 'should initialize with correct HF provided')

common.setHardfork('byzantium')
common.setHardfork(Hardfork.Byzantium)
st.equal(
header._common.hardfork(),
'chainstart',
Expand Down Expand Up @@ -140,7 +140,7 @@ tape('[Block]: Header functions', function (t) {
})

t.test('Initialization -> Clique Blocks', function (st) {
const common = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })
let header = BlockHeader.genesis(undefined, { common })
st.ok(header.hash().toString('hex'), 'genesis block should initialize')

Expand All @@ -153,7 +153,7 @@ tape('[Block]: Header functions', function (t) {
t.test('should validate extraData', async function (st) {
// PoW
let blockchain = new Mockchain()
let common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
let genesis = Block.genesis({}, { common })
await blockchain.putBlock(genesis)

Expand Down Expand Up @@ -199,7 +199,7 @@ tape('[Block]: Header functions', function (t) {

// PoA
blockchain = new Mockchain()
common = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
common = new Common({ chain: Chain.Rinkeby, hardfork: Hardfork.Chainstart })
genesis = Block.genesis({}, { common })
await blockchain.putBlock(genesis)

Expand Down Expand Up @@ -264,7 +264,7 @@ tape('[Block]: Header functions', function (t) {
t.test('header validation -> poa checks', async function (st) {
const headerData = testData.blocks[0].blockHeader

const common = new Common({ chain: 'goerli' })
const common = new Common({ chain: Chain.Goerli })
const blockchain = new Mockchain()

const block = Block.fromRLPSerializedBlock(testData.genesisRLP, { common })
Expand Down Expand Up @@ -408,23 +408,23 @@ tape('[Block]: Header functions', function (t) {
})

t.test('should test genesis parameters (ropsten)', function (st) {
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Chainstart })
const genesis = BlockHeader.genesis({}, { common })
const ropstenStateRoot = '217b0bbcfb72e2d57e28f33cb361b9983513177755dc3f33ce3e7022ed62b77b'
st.strictEqual(genesis.stateRoot.toString('hex'), ropstenStateRoot, 'genesis stateRoot match')
st.end()
})

t.test('should test hash() function', function (st) {
let common = new Common({ chain: 'mainnet', hardfork: 'chainstart' })
let common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
let header = BlockHeader.fromHeaderData(blocksMainnet[0]['header'], { common })
st.equal(
header.hash().toString('hex'),
'88e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6',
'correct PoW hash (mainnet block 1)'
)

common = new Common({ chain: 'goerli', hardfork: 'chainstart' })
common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
header = BlockHeader.fromHeaderData(blocksGoerli[0]['header'], { common })
st.equal(
header.hash().toString('hex'),
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common from '@ethereumjs/common'
import Common, { Chain } from '@ethereumjs/common'
import { BN, rlp, keccak256 } from 'ethereumjs-util'
import { Block, BlockHeader } from '../src'

Expand All @@ -15,7 +15,7 @@ function createBlock(
common?: Common
): Block {
uncles = uncles ?? []
common = common ?? new Common({ chain: 'mainnet' })
common = common ?? new Common({ chain: Chain.Mainnet })

if (extraData.length > 32) {
throw new Error('extra data graffiti must be 32 bytes or less')
Expand Down
3 changes: 2 additions & 1 deletion packages/blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ This module performs write operations. Making a backup of your data before tryin

```typescript
import Blockchain from '@ethereumjs/blockchain'
import Common, { Chain } from '@ethereumjs/common'

const level = require('level')

const gethDbPath = './chaindata' // Add your own path here. It will get modified, see remarks.

const common = new Common({ chain: 'ropsten' })
const common = new Common({ chain: Chain.Ropsten })
const db = level(gethDbPath)
// Use the safe static constructor which awaits the init method
const blockchain = Blockchain.create({ common, db })
Expand Down
8 changes: 4 additions & 4 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Semaphore from 'semaphore-async-await'
import { Address, BN, rlp } from 'ethereumjs-util'
import { Block, BlockData, BlockHeader } from '@ethereumjs/block'
import Ethash from '@ethereumjs/ethash'
import Common from '@ethereumjs/common'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import { DBManager } from './db/manager'
import { DBOp, DBSetBlockOrHeader, DBSetTD, DBSetHashToNumber, DBSaveLookups } from './db/helpers'
import { DBTarget } from './db/operation'
Expand Down Expand Up @@ -248,8 +248,8 @@ export default class Blockchain implements BlockchainInterface {
if (opts.common) {
this._common = opts.common
} else {
const DEFAULT_CHAIN = 'mainnet'
const DEFAULT_HARDFORK = 'chainstart'
const DEFAULT_CHAIN = Chain.Mainnet
const DEFAULT_HARDFORK = Hardfork.Chainstart
this._common = new Common({
chain: DEFAULT_CHAIN,
hardfork: DEFAULT_HARDFORK,
Expand Down Expand Up @@ -324,7 +324,7 @@ export default class Blockchain implements BlockchainInterface {

if (!genesisBlock) {
const common = this._common.copy()
common.setHardfork('chainstart')
common.setHardfork(Hardfork.Chainstart)
genesisBlock = Block.genesis({}, { common })
}

Expand Down
Loading

0 comments on commit 8d140c3

Please sign in to comment.