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

Commit 28247a3

Browse files
committed
Update for @ethereumjs/block @ethereumjs/common: lib/service, lib/sync, lib/util
1 parent 7685b1a commit 28247a3

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

lib/service/ethereumservice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Service } from './service'
22
import { FlowControl } from '../net/protocol/flowcontrol'
33
import { Chain } from '../blockchain'
4-
import Common from 'ethereumjs-common'
4+
import Common from '@ethereumjs/common'
55

66
const defaultOptions = {
77
lightserv: false,
8-
common: new Common('mainnet', 'chainstart'),
8+
common: new Common({ chain: 'mainnet', hardfork: 'chainstart' }),
99
minPeers: 3,
1010
timeout: 8000,
1111
interval: 1000,

lib/service/fastethereumservice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class FastEthereumService extends EthereumService {
9696
} else if (message.name === 'GetBlockBodies') {
9797
const hashes = message.data
9898
const blocks = await Promise.all(hashes.map((hash: any) => this.chain.getBlock(hash)))
99-
const bodies: any = blocks.map((block: any) => block.raw.slice(1))
99+
const bodies: any = blocks.map((block: any) => block.raw().slice(1))
100100
;(peer.eth as BoundProtocol).send('BlockBodies', bodies)
101101
} else if (message.name === 'NewBlockHashes') {
102102
this.synchronizer.announced(message.data, peer)

lib/sync/fetcher/blockfetcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fetcher } from './fetcher'
2-
const Block = require('ethereumjs-block')
2+
import { Block, BlockBodyBuffer } from '@ethereumjs/block'
33
import { BN } from 'ethereumjs-util'
44
import { Peer } from '../../net/peer'
55
import { Chain } from '../../blockchain'
@@ -67,8 +67,8 @@ export class BlockFetcher extends Fetcher {
6767
const { first, count } = task
6868
const headers = await peer.eth.getBlockHeaders({ block: first, max: count })
6969
const bodies = await peer.eth.getBlockBodies(headers.map((h: any) => h.hash()))
70-
const blocks = bodies.map(
71-
(body: any, i: number) => new Block([headers[i]].concat(body), { common: this.common })
70+
const blocks = bodies.map(([txsData, unclesData]: BlockBodyBuffer, i: number) =>
71+
Block.fromValuesArray([headers[i].raw(), txsData, unclesData], { common: this.common })
7272
)
7373
return { blocks }
7474
}

lib/sync/fetcher/fetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Readable, Writable } from 'stream'
22
const Heap = require('qheap')
3-
import Common from 'ethereumjs-common'
3+
import Common from '@ethereumjs/common'
44
import { defaultLogger } from '../../logging'
55
import { PeerPool } from '../../net/peerpool'
66

77
const defaultOptions = {
8-
common: new Common('mainnet', 'chainstart'),
8+
common: new Common({ chain: 'mainnet', hardfork: 'chainstart' }),
99
logger: defaultLogger,
1010
timeout: 8000,
1111
interval: 1000,

lib/sync/sync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Peer } from '../net/peer/peer'
22
import { EventEmitter } from 'events'
3-
import Common from 'ethereumjs-common'
3+
import Common from '@ethereumjs/common'
44
import { defaultLogger } from '../logging'
55
import { PeerPool } from '../net/peerpool'
66
import { Chain } from '../blockchain'
77
import { FlowControl } from '../net/protocol'
88

99
const defaultOptions = {
10-
common: new Common('mainnet', 'chainstart'),
10+
common: new Common({ chain: 'mainnet', hardfork: 'chainstart' }),
1111
logger: defaultLogger,
1212
interval: 1000,
1313
minPeers: 3,

lib/util/parse.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Account from 'ethereumjs-account'
2-
const Block = require('ethereumjs-block')
2+
import { BlockHeader } from '@ethereumjs/block'
33
const Trie = require('merkle-patricia-tree/secure')
44
import * as util from 'ethereumjs-util'
55
import * as url from 'url'
@@ -93,16 +93,21 @@ async function parseGethState(alloc: any) {
9393
}
9494

9595
async function parseGethHeader(json: any) {
96-
const header = new Block.Header()
97-
header.gasLimit = json.gasLimit
98-
header.difficulty = json.difficulty
99-
header.extraData = json.extraData
100-
header.number = Buffer.from([])
101-
header.nonce = json.nonce
102-
header.timestamp = json.timestamp
103-
header.mixHash = json.mixHash
104-
header.stateRoot = (await parseGethState(json.alloc)).root
105-
return header
96+
return BlockHeader.fromHeaderData(
97+
{
98+
gasLimit: new util.BN(util.stripHexPrefix(json.gasLimit), 16),
99+
difficulty: new util.BN(util.stripHexPrefix(json.difficulty), 16),
100+
extraData: toBuffer(json.extraData),
101+
number: new util.BN(util.stripHexPrefix(json.number), 16),
102+
nonce: toBuffer(json.nonce),
103+
timestamp: new util.BN(util.stripHexPrefix(json.timestamp), 16),
104+
mixHash: toBuffer(json.mixHash),
105+
stateRoot: (await parseGethState(json.alloc)).root,
106+
},
107+
{
108+
// TODO: Add optional Common param here?
109+
}
110+
)
106111
}
107112

108113
async function parseGethParams(json: any) {

0 commit comments

Comments
 (0)