Skip to content

Commit

Permalink
Merge pull request #1398 from ethereumjs/common/consensus-type-enum
Browse files Browse the repository at this point in the history
common: enums for ConsensusType and ConsensusAlgorithm
  • Loading branch information
gabrocheleau authored Aug 11, 2021
2 parents ae96604 + f6bda4e commit 587ee70
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 61 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, rlp, keccak256, KECCAK256_RLP } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import Common, { ConsensusType } from '@ethereumjs/common'
import {
TransactionFactory,
TypedTransaction,
Expand Down Expand Up @@ -146,10 +146,10 @@ export class Block {
this.uncleHeaders = uncleHeaders
this._common = this.header._common
if (uncleHeaders.length > 0) {
if (this._common.consensusType() === 'poa') {
if (this._common.consensusType() === ConsensusType.ProofOfAuthority) {
throw new Error('Block initialization with uncleHeaders on a PoA network is not allowed')
}
if (this._common.consensusType() === 'pos') {
if (this._common.consensusType() === ConsensusType.ProofOfStake) {
throw new Error('Block initialization with uncleHeaders on a PoS network is not allowed')
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common, { Chain, Hardfork } from '@ethereumjs/common'
import Common, { Chain, ConsensusAlgorithm, ConsensusType, Hardfork } from '@ethereumjs/common'
import {
Address,
BN,
Expand Down Expand Up @@ -342,7 +342,7 @@ export class BlockHeader {
}

// Check for constant values for PoS blocks
if (this._common.consensusType() === 'pos') {
if (this._common.consensusType() === ConsensusType.ProofOfStake) {
let error = false
let errorMsg = ''

Expand Down Expand Up @@ -380,10 +380,10 @@ export class BlockHeader {
* @param parentBlockHeader - the header from the parent `Block` of this header
*/
canonicalDifficulty(parentBlockHeader: BlockHeader): BN {
if (this._common.consensusType() !== 'pow') {
if (this._common.consensusType() !== ConsensusType.ProofOfWork) {
throw new Error('difficulty calculation is only supported on PoW chains')
}
if (this._common.consensusAlgorithm() !== 'ethash') {
if (this._common.consensusAlgorithm() !== ConsensusAlgorithm.Ethash) {
throw new Error('difficulty calculation currently only supports the ethash algorithm')
}
const hardfork = this._getHardfork()
Expand Down Expand Up @@ -546,7 +546,7 @@ export class BlockHeader {
}
const hardfork = this._getHardfork()
// Consensus type dependent checks
if (this._common.consensusAlgorithm() === 'ethash') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Ethash) {
// PoW/Ethash
if (
this.extraData.length > this._common.paramByHardfork('vm', 'maxExtraDataSize', hardfork)
Expand All @@ -555,7 +555,7 @@ export class BlockHeader {
throw this._error(msg)
}
}
if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
// PoA/Clique
const minLength = CLIQUE_EXTRA_VANITY + CLIQUE_EXTRA_SEAL
if (!this.cliqueIsEpochTransition()) {
Expand Down Expand Up @@ -602,7 +602,7 @@ export class BlockHeader {
throw new Error('invalid timestamp')
}

if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
const period = this._common.consensusConfig().period
// Timestamp diff between blocks is lower than PERIOD (clique)
if (parentHeader.timestamp.addn(period).gt(this.timestamp)) {
Expand Down Expand Up @@ -733,7 +733,7 @@ export class BlockHeader {
}

private _requireClique(name: string) {
if (this._common.consensusAlgorithm() !== 'clique') {
if (this._common.consensusAlgorithm() !== ConsensusAlgorithm.Clique) {
throw new Error(`BlockHeader.${name}() call only supported for clique PoA networks`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block/test/mergeBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Block } from '../src/block'

const common = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.TheMerge,
hardfork: Hardfork.Merge,
})

function validateMergeHeader(st: tape.Test, header: BlockHeader) {
Expand Down
26 changes: 13 additions & 13 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, { Chain, Hardfork } from '@ethereumjs/common'
import Common, { Chain, ConsensusAlgorithm, ConsensusType, 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 @@ -264,15 +264,15 @@ export default class Blockchain implements BlockchainInterface {
this.dbManager = new DBManager(this.db, this._common)

if (this._validateConsensus) {
if (this._common.consensusType() === 'pow') {
if (this._common.consensusAlgorithm() !== 'ethash') {
if (this._common.consensusType() === ConsensusType.ProofOfWork) {
if (this._common.consensusAlgorithm() !== ConsensusAlgorithm.Ethash) {
throw new Error('consensus validation only supported for pow ethash algorithm')
} else {
this._ethash = new Ethash(this.db)
}
}
if (this._common.consensusType() === 'poa') {
if (this._common.consensusAlgorithm() !== 'clique') {
if (this._common.consensusType() === ConsensusType.ProofOfAuthority) {
if (this._common.consensusAlgorithm() !== ConsensusAlgorithm.Clique) {
throw new Error(
'consensus (signature) validation only supported for poa clique algorithm'
)
Expand Down Expand Up @@ -348,13 +348,13 @@ export default class Blockchain implements BlockchainInterface {

await this.dbManager.batch(dbOps)

if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
await this.cliqueSaveGenesisSigners(genesisBlock)
}
}

// Clique: read current signer states, signer votes, and block signers
if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
this._cliqueLatestSignerStates = await this.dbManager.getCliqueLatestSignerStates()
this._cliqueLatestVotes = await this.dbManager.getCliqueLatestVotes()
this._cliqueLatestBlockSigners = await this.dbManager.getCliqueLatestBlockSigners()
Expand Down Expand Up @@ -433,7 +433,7 @@ export default class Blockchain implements BlockchainInterface {
}

private _requireClique() {
if (this._common.consensusAlgorithm() !== 'clique') {
if (this._common.consensusAlgorithm() !== ConsensusAlgorithm.Clique) {
throw new Error('Function call only supported for clique PoA networks')
}
}
Expand Down Expand Up @@ -902,14 +902,14 @@ export default class Blockchain implements BlockchainInterface {
}

if (this._validateConsensus) {
if (this._common.consensusAlgorithm() === 'ethash') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Ethash) {
const valid = await this._ethash!.verifyPOW(block)
if (!valid) {
throw new Error('invalid POW')
}
}

if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
const valid = header.cliqueVerifySignature(this.cliqueActiveSigners())
if (!valid) {
throw new Error('invalid PoA block signature (clique)')
Expand All @@ -921,7 +921,7 @@ export default class Blockchain implements BlockchainInterface {
}
}

if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
// validate checkpoint signers towards active signers on epoch transition blocks
if (header.cliqueIsEpochTransition()) {
// note: keep votes on epoch transition blocks in case of reorgs.
Expand Down Expand Up @@ -963,7 +963,7 @@ export default class Blockchain implements BlockchainInterface {
let ancientHeaderNumber: undefined | BN
// if total difficulty is higher than current, add it to canonical chain
if (block.isGenesis() || td.gt(currentTd.header)) {
if (this._common.consensusAlgorithm() === 'clique') {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
ancientHeaderNumber = (await this._findAncient(header)).number
}

Expand Down Expand Up @@ -1001,7 +1001,7 @@ export default class Blockchain implements BlockchainInterface {
await this.dbManager.batch(ops)

// Clique: update signer votes and state
if (this._common.consensusAlgorithm() === 'clique' && ancientHeaderNumber) {
if (this._common.consensusAlgorithm() === ConsensusAlgorithm.Clique && ancientHeaderNumber) {
await this._cliqueDeleteSnapshots(ancientHeaderNumber.addn(1))
for (
const number = ancientHeaderNumber.addn(1);
Expand Down
3 changes: 2 additions & 1 deletion packages/client/lib/blockchain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Block, BlockHeader } from '@ethereumjs/block'
import Blockchain from '@ethereumjs/blockchain'
import { ConsensusAlgorithm } from '@ethereumjs/common'
import { BN, toBuffer } from 'ethereumjs-util'
import { Config } from '../config'
import { Event } from '../types'
Expand Down Expand Up @@ -103,7 +104,7 @@ export class Chain {
constructor(options: ChainOptions) {
this.config = options.config
let validateConsensus = false
if (this.config.chainCommon.consensusAlgorithm() === 'clique') {
if (this.config.chainCommon.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
validateConsensus = true
}

Expand Down
2 changes: 1 addition & 1 deletion packages/common/docs/classes/index.default.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ ___
**consensusType**(): `string`

Returns the consensus type of the network
Possible values: "pow"|"poa"
Possible values: "pow"|"poa"|"pos"

#### Returns

Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"forkash": null
},
{
"name": "theMerge",
"name": "merge",
"block": null,
"forkash": null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/kovan.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"forkash": null
},
{
"name": "theMerge",
"name": "merge",
"block": null,
"forkash": null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"forkash": null
},
{
"name": "theMerge",
"name": "merge",
"block": null,
"forkash": null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/rinkeby.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"forkash": null
},
{
"name": "theMerge",
"name": "merge",
"block": null,
"forkash": null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/chains/ropsten.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"forkash": null
},
{
"name": "theMerge",
"name": "merge",
"block": null,
"forkash": null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/hardforks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const hardforks = [
['berlin', require('./berlin.json')],
['london', require('./london.json')],
['shanghai', require('./shanghai.json')],
['theMerge', require('./theMerge.json')],
['merge', require('./merge.json')],
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "theMerge",
"name": "merge",
"comment": "Hardfork to upgrade the consensus mechanism to Proof-of-Stake",
"url": "https://github.com/ethereum/pm/issues/361",
"status": "pre-Draft",
Expand Down
20 changes: 16 additions & 4 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ export enum Hardfork {
Berlin = 'berlin',
London = 'london',
Shanghai = 'shanghai',
TheMerge = 'theMerge',
Merge = 'merge',
}

export enum ConsensusType {
ProofOfStake = 'pos',
ProofOfWork = 'pow',
ProofOfAuthority = 'poa',
}

export enum ConsensusAlgorithm {
Ethash = 'ethash',
Clique = 'clique',
Casper = 'casper',
}

interface BaseOpts {
Expand Down Expand Up @@ -878,7 +890,7 @@ export default class Common extends EventEmitter {
* Returns the hardfork set
* @returns Hardfork name
*/
hardfork(): string {
hardfork(): string | Hardfork {
return this._hardfork
}

Expand Down Expand Up @@ -938,7 +950,7 @@ export default class Common extends EventEmitter {
*
* Note: This value can update along a hardfork.
*/
consensusType(): string {
consensusType(): string | ConsensusType {
const hardfork = this.hardfork()

let value
Expand All @@ -963,7 +975,7 @@ export default class Common extends EventEmitter {
*
* Note: This value can update along a hardfork.
*/
consensusAlgorithm(): string {
consensusAlgorithm(): string | ConsensusAlgorithm {
const hardfork = this.hardfork()

let value
Expand Down
22 changes: 17 additions & 5 deletions packages/common/tests/chains.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tape from 'tape'
import Common, { Chain, Hardfork } from '../src/'
import Common, { Chain, ConsensusAlgorithm, ConsensusType, Hardfork } from '../src/'
import { BN } from 'ethereumjs-util'

tape('[Common/Chains]: Initialization / Chain params', function (t: tape.Test) {
Expand Down Expand Up @@ -104,17 +104,29 @@ tape('[Common/Chains]: Initialization / Chain params', function (t: tape.Test) {
st.equal(c.genesis().hash, hash, 'should return correct genesis hash')
st.equal(c.hardforks()[3]['block'], 2463000, 'should return correct hardfork data')
st.equal(typeof c.bootstrapNodes()[0].port, 'number', 'should return a port as number')
st.equal(c.consensusType(), 'pow', 'should return correct consensus type')
st.equal(c.consensusAlgorithm(), 'ethash', 'should return correct consensus algorithm')
st.equal(c.consensusType(), ConsensusType.ProofOfWork, 'should return correct consensus type')
st.equal(
c.consensusAlgorithm(),
ConsensusAlgorithm.Ethash,
'should return correct consensus algorithm'
)
st.deepEqual(c.consensusConfig(), {}, 'should return empty dictionary for consensus config')

c = new Common({ chain: 'rinkeby', hardfork: 'chainstart' })
hash = '0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177'
st.equal(c.genesis().hash, hash, 'should return correct genesis hash')
st.equal(c.hardforks()[3]['block'], 3, 'should return correct hardfork data')
st.equal(typeof c.bootstrapNodes()[0].port, 'number', 'should return a port as number')
st.equal(c.consensusType(), 'poa', 'should return correct consensus type')
st.equal(c.consensusAlgorithm(), 'clique', 'should return correct consensus algorithm')
st.equal(
c.consensusType(),
ConsensusType.ProofOfAuthority,
'should return correct consensus type'
)
st.equal(
c.consensusAlgorithm(),
ConsensusAlgorithm.Clique,
'should return correct consensus algorithm'
)
st.equal(c.consensusConfig().epoch, 30000, 'should return correct consensus config parameters')
st.end()
})
Expand Down
Loading

0 comments on commit 587ee70

Please sign in to comment.