Skip to content
This repository was archived by the owner on Dec 10, 2020. It is now read-only.

Commit 726d2db

Browse files
committed
Clean up chain constructor & initialization
1 parent ab5be31 commit 726d2db

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

lib/blockchain/chain.ts

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface ChainOptions {
2525
/**
2626
* Specify a blockchain which implements the Chain interface
2727
*/
28-
blockchain?: Chain
28+
blockchain?: Blockchain
2929

3030
/**
3131
* Logging provider
@@ -80,14 +80,6 @@ export interface GenesisBlockParams {
8080
[key: string]: Buffer
8181
}
8282

83-
/**
84-
* Default blockchain constructor options
85-
*/
86-
const defaultOptions = {
87-
logger: defaultLogger,
88-
common: new Common({ chain: 'mainnet', hardfork: 'chainstart' }),
89-
}
90-
9183
/**
9284
* Blockchain
9385
* @memberof module:blockchain
@@ -97,7 +89,7 @@ export class Chain extends EventEmitter {
9789
public common: Common
9890
public db: any
9991
public blockchain: Blockchain
100-
public opened = false
92+
public opened: boolean
10193

10294
private _headers: ChainHeaders = {
10395
latest: null,
@@ -115,35 +107,21 @@ export class Chain extends EventEmitter {
115107
* Create new chain
116108
* @param {ChainOptions} options
117109
*/
118-
constructor(options?: ChainOptions) {
110+
constructor(options: ChainOptions = {}) {
119111
super()
120-
options = { ...defaultOptions, ...options }
121-
this.logger = options.logger!
122-
this.common = options.common!
123-
this.db = options.db
124-
125-
//@ts-ignore Blockchain will be set in init
126-
this.blockchain = options.blockchain
127-
this.init()
128-
}
112+
this.logger = options.logger || defaultLogger
113+
this.common = options.common || new Common({ chain: 'mainnet', hardfork: 'chainstart' })
129114

130-
/**
131-
* Initializes blockchain
132-
*/
133-
private init() {
134-
if (!this.blockchain) {
135-
this.blockchain = new Blockchain({
136-
db: this.db,
115+
this.blockchain =
116+
options.blockchain ||
117+
new Blockchain({
118+
db: options.db,
137119
validateBlocks: false,
138120
validatePow: false,
139121
common: this.common,
140122
})
141-
if (!this.db) {
142-
this.db = this.blockchain.db
143-
}
144-
}
145123

146-
this.reset()
124+
this.db = this.blockchain.db
147125
this.opened = false
148126
}
149127

test/rpc/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RPCManager as Manager } from '../../lib/rpc'
66
import * as Logger from '../../lib/logging'
77
import { blockChain } from './blockChainStub'
88
import { Chain } from '../../lib/blockchain/chain'
9+
import Blockchain from '@ethereumjs/blockchain'
910

1011
const config: any = { loglevel: 'error' }
1112
config.logger = Logger.getLogger(config)
@@ -26,7 +27,7 @@ export function createManager(node: any) {
2627
}
2728

2829
export function createNode(nodeConfig?: any) {
29-
const chain = new Chain({ blockchain: (<unknown>blockChain({})) as Chain })
30+
const chain = new Chain({ blockchain: (<unknown>blockChain({})) as Blockchain })
3031
chain.opened = true
3132
const defaultNodeConfig = {
3233
blockchain: chain,

0 commit comments

Comments
 (0)