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

Commit 7685b1a

Browse files
committed
Update for chain refactor: lib/net/protocol and lib/rpc
1 parent fb37f01 commit 7685b1a

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

lib/net/protocol/ethprotocol.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { Chain } from './../../blockchain'
12
import { Message, Protocol } from './protocol'
23
import { BN, bufferToInt } from 'ethereumjs-util'
3-
const Block = require('ethereumjs-block')
4+
import { BlockHeader, BlockHeaderBuffer } from '@ethereumjs/block'
45

56
const messages: Message[] = [
67
{
@@ -29,8 +30,9 @@ const messages: Message[] = [
2930
{
3031
name: 'BlockHeaders',
3132
code: 0x04,
32-
encode: (headers: any[]) => headers.map((h) => h.raw),
33-
decode: (headers: any[]) => headers.map((raw) => new Block.Header(raw)),
33+
encode: (headers: BlockHeader[]) => headers.map((h) => h.raw()),
34+
decode: (headers: BlockHeaderBuffer[]) =>
35+
headers.map((h) => BlockHeader.fromValuesArray(h, {})),
3436
},
3537
{
3638
name: 'GetBlockBodies',
@@ -48,7 +50,7 @@ const messages: Message[] = [
4850
* @memberof module:net/protocol
4951
*/
5052
export class EthProtocol extends Protocol {
51-
private chain: any
53+
private chain: Chain
5254

5355
/**
5456
* Create eth protocol
@@ -107,7 +109,7 @@ export class EthProtocol extends Protocol {
107109
return {
108110
networkId: this.chain.networkId,
109111
td: this.chain.blocks.td.toArrayLike(Buffer),
110-
bestHash: this.chain.blocks.latest.hash(),
112+
bestHash: this.chain.blocks.latest!.hash(),
111113
genesisHash: this.chain.genesis.hash,
112114
}
113115
}

lib/net/protocol/lesprotocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Message, Protocol } from './protocol'
22
import { BN, bufferToInt } from 'ethereumjs-util'
3-
const Block = require('ethereumjs-block')
3+
import { BlockHeader, BlockHeaderBuffer } from '@ethereumjs/block'
44

55
const id = new BN(0)
66

@@ -45,12 +45,12 @@ const messages: Message[] = [
4545
encode: ({ reqId, bv, headers }: any) => [
4646
new BN(reqId).toArrayLike(Buffer),
4747
new BN(bv).toArrayLike(Buffer),
48-
headers.map((h: any) => h.raw),
48+
headers.map((h: any) => h.raw()),
4949
],
5050
decode: ([reqId, bv, headers]: any) => ({
5151
reqId: new BN(reqId),
5252
bv: new BN(bv),
53-
headers: headers.map((raw: Buffer[]) => new Block.Header(raw)),
53+
headers: headers.map((h: BlockHeaderBuffer) => BlockHeader.fromValuesArray(h, {})),
5454
}),
5555
},
5656
]

lib/rpc/modules/eth.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { Chain } from '../../blockchain'
12
import { middleware, validators } from '../validation'
2-
import { toBuffer, bufferToHex } from 'ethereumjs-util'
3+
import { toBuffer, stripHexPrefix, BN } from 'ethereumjs-util'
34

45
/**
56
* eth_* RPC module
67
* @memberof module:rpc/modules
78
*/
89
export class Eth {
9-
private _chain: any
10+
private _chain: Chain
1011
public ethVersion: any
1112

1213
/**
@@ -50,8 +51,7 @@ export class Eth {
5051
async blockNumber(_params = [], cb: (err: Error | null, val?: string) => void) {
5152
try {
5253
const latestHeader = await this._chain.getLatestHeader()
53-
const latestBlockNumber = bufferToHex(latestHeader.number)
54-
cb(null, `${latestBlockNumber}`)
54+
cb(null, `0x${latestHeader.number.toString(16)}`)
5555
} catch (err) {
5656
cb(err)
5757
}
@@ -65,16 +65,15 @@ export class Eth {
6565
* as the second argument
6666
* @return {Promise}
6767
*/
68-
async getBlockByNumber(params: any[] | boolean[], cb: (err: Error | null, val?: any) => void) {
68+
async getBlockByNumber(params: [string, boolean], cb: (err: Error | null, val?: any) => void) {
6969
// eslint-disable-next-line prefer-const
7070
let [blockNumber, includeTransactions] = params
71-
72-
blockNumber = Number.parseInt(blockNumber, 16)
71+
const blockNumberBN = new BN(stripHexPrefix(blockNumber), 16)
7372
try {
74-
const block = await this._chain.getBlock(blockNumber)
75-
const json = block.toJSON(true)
73+
const block = await this._chain.getBlock(blockNumberBN)
74+
const json = block.toJSON()
7675
if (!includeTransactions) {
77-
json.transactions = json.transactions.map((tx: any) => tx.hash)
76+
json.transactions = json.transactions!.map((tx: any) => tx.hash)
7877
}
7978
cb(null, json)
8079
} catch (err) {
@@ -95,11 +94,10 @@ export class Eth {
9594

9695
try {
9796
const block = await this._chain.getBlock(toBuffer(blockHash))
98-
99-
const json = block.toJSON(true)
97+
const json = block.toJSON()
10098

10199
if (!includeTransactions) {
102-
json.transactions = json.transactions.map((tx: any) => tx.hash)
100+
json.transactions = json.transactions!.map((tx: any) => tx.hash)
103101
}
104102
cb(null, json)
105103
} catch (err) {
@@ -115,16 +113,16 @@ export class Eth {
115113
* @return {Promise}
116114
*/
117115
async getBlockTransactionCountByHash(
118-
params: string[],
116+
params: [string],
119117
cb: (err: Error | null, val?: any) => void
120118
) {
121119
const [blockHash] = params
122120

123121
try {
124122
const block = await this._chain.getBlock(toBuffer(blockHash))
125123

126-
const json = block.toJSON(true)
127-
cb(null, `0x${json.transactions.length.toString(16)}`)
124+
const json = block.toJSON()
125+
cb(null, `0x${json.transactions!.length.toString(16)}`)
128126
} catch (err) {
129127
cb(err)
130128
}

0 commit comments

Comments
 (0)