Skip to content

Commit 09de6f4

Browse files
authored
Merge pull request #748 from ethereumjs/update-ethereumjs-util-to-v7
Update `ethereumjs-util` to v7
2 parents 4b9a096 + 9b4c55e commit 09de6f4

File tree

20 files changed

+146
-149
lines changed

20 files changed

+146
-149
lines changed

packages/account/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"homepage": "https://github.com/ethereumjs/ethereumjs-account#readme",
3838
"dependencies": {
39-
"ethereumjs-util": "^6.0.0",
39+
"ethereumjs-util": "^7.0.2",
4040
"rlp": "^2.2.1",
4141
"safe-buffer": "^5.1.1"
4242
},
@@ -45,7 +45,7 @@
4545
"@ethereumjs/config-prettier": "^1.1.0",
4646
"@ethereumjs/config-tsc": "^1.1.0",
4747
"@ethereumjs/config-tslint": "^1.1.0",
48-
"@types/bn.js": "^4.11.3",
48+
"@types/bn.js": "^4.11.6",
4949
"@types/node": "^11.9.4",
5050
"@types/tape": "^4.2.33",
5151
"merkle-patricia-tree": "^3.0.0",
@@ -56,7 +56,7 @@
5656
"tslint": "^5.12.0",
5757
"typedoc": "next",
5858
"typedoc-plugin-markdown": "^2.2.17",
59-
"typescript": "^3.2.2",
59+
"typescript": "^3.9.2",
6060
"typestrict": "^1.0.2"
6161
}
6262
}

packages/block/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
},
4040
"homepage": "https://github.com/ethereumjs/ethereumjs-block#readme",
4141
"dependencies": {
42-
"@types/bn.js": "^4.11.5",
42+
"@types/bn.js": "^4.11.6",
4343
"ethereumjs-common": "^1.5.0",
4444
"ethereumjs-tx": "^2.1.1",
45-
"ethereumjs-util": "^6.1.0",
45+
"ethereumjs-util": "^7.0.2",
4646
"merkle-patricia-tree": "^2.1.2"
4747
},
4848
"devDependencies": {
@@ -67,7 +67,7 @@
6767
"tslint": "^5.15.0",
6868
"typedoc": "next",
6969
"typedoc-plugin-markdown": "^2.2.17",
70-
"typescript": "^3.4.3",
70+
"typescript": "^3.9.2",
7171
"typestrict": "^1.0.2"
7272
}
7373
}

packages/block/src/block.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import Common from 'ethereumjs-common'
2-
import * as ethUtil from 'ethereumjs-util'
3-
import { BN, rlp } from 'ethereumjs-util'
2+
import { rlp, keccak256, KECCAK256_RLP, baToJSON } from 'ethereumjs-util'
43
import { Transaction, TransactionOptions } from 'ethereumjs-tx'
5-
64
import { BlockHeader } from './header'
75
import { Blockchain, BlockData, ChainOptions } from './types'
86

97
const Trie = require('merkle-patricia-tree')
8+
const { BN } = require('ethereumjs-util')
109

1110
/**
1211
* An object that represents the block
@@ -148,7 +147,7 @@ export class Block {
148147
if (this.transactions.length) {
149148
return txT === this.txTrie.root.toString('hex')
150149
} else {
151-
return txT === ethUtil.KECCAK256_RLP.toString('hex')
150+
return txT === KECCAK256_RLP.toString('hex')
152151
}
153152
}
154153

@@ -209,7 +208,7 @@ export class Block {
209208
validateUnclesHash(): boolean {
210209
const raw = rlp.encode(this.uncleHeaders.map(uh => uh.raw))
211210

212-
return ethUtil.keccak256(raw).toString('hex') === this.header.uncleHash.toString('hex')
211+
return keccak256(raw).toString('hex') === this.header.uncleHash.toString('hex')
213212
}
214213

215214
/**
@@ -248,7 +247,7 @@ export class Block {
248247
uncleHeaders: this.uncleHeaders.forEach(uh => uh.toJSON(true)),
249248
}
250249
} else {
251-
return ethUtil.baToJSON(this.raw)
250+
return baToJSON(this.raw)
252251
}
253252
}
254253

packages/block/src/from-rpc.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FakeTransaction, TransactionOptions } from 'ethereumjs-tx'
2-
import * as ethUtil from 'ethereumjs-util'
2+
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
33
import { Block } from './index'
44
import { ChainOptions } from './types'
55

@@ -34,7 +34,7 @@ export default function blockFromRpc(
3434
for (const _txParams of blockParams.transactions) {
3535
const txParams = normalizeTxParams(_txParams)
3636
// override from address
37-
const fromAddress = ethUtil.toBuffer(txParams.from)
37+
const fromAddress = toBuffer(txParams.from)
3838
delete txParams.from
3939

4040
const tx = new FakeTransaction(txParams, chainOptions as TransactionOptions)
@@ -43,7 +43,7 @@ export default function blockFromRpc(
4343
return fromAddress
4444
}
4545
// override hash
46-
const txHash = ethUtil.toBuffer(txParams.hash)
46+
const txHash = toBuffer(txParams.hash)
4747
tx.hash = function() {
4848
return txHash
4949
}
@@ -60,7 +60,7 @@ function normalizeTxParams(_txParams: any) {
6060
txParams.gasLimit = txParams.gasLimit === undefined ? txParams.gas : txParams.gasLimit
6161
txParams.data = txParams.data === undefined ? txParams.input : txParams.data
6262
// strict byte length checking
63-
txParams.to = txParams.to ? ethUtil.setLengthLeft(ethUtil.toBuffer(txParams.to), 20) : null
63+
txParams.to = txParams.to ? setLengthLeft(toBuffer(txParams.to), 20) : null
6464

6565
// v as raw signature value {0,1}
6666
// v is the recovery bit and can be either {0,1} or {27,28}.

packages/block/src/header-from-rpc.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BlockHeader } from './header'
2-
import * as ethUtil from 'ethereumjs-util'
2+
import { KECCAK256_NULL, toBuffer } from 'ethereumjs-util'
33
import { ChainOptions } from './types'
44

55
/**
@@ -16,7 +16,7 @@ export default function blockHeaderFromRpc(blockParams: any, chainOptions?: Chai
1616
coinbase: blockParams.miner,
1717
stateRoot: blockParams.stateRoot,
1818
transactionsTrie: blockParams.transactionsRoot,
19-
receiptTrie: blockParams.receiptRoot || blockParams.receiptsRoot || ethUtil.KECCAK256_NULL,
19+
receiptTrie: blockParams.receiptRoot || blockParams.receiptsRoot || KECCAK256_NULL,
2020
bloom: blockParams.logsBloom,
2121
difficulty: blockParams.difficulty,
2222
number: blockParams.number,
@@ -32,7 +32,7 @@ export default function blockHeaderFromRpc(blockParams: any, chainOptions?: Chai
3232

3333
// override hash in case something was missing
3434
blockHeader.hash = function() {
35-
return ethUtil.toBuffer(blockParams.hash)
35+
return toBuffer(blockParams.hash)
3636
}
3737

3838
return blockHeader

packages/block/src/header.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import Common from 'ethereumjs-common'
2-
import * as utils from 'ethereumjs-util'
3-
import { BN } from 'ethereumjs-util'
2+
import {
3+
BN,
4+
zeros,
5+
KECCAK256_RLP_ARRAY,
6+
KECCAK256_RLP,
7+
toBuffer,
8+
defineProperties,
9+
bufferToInt,
10+
rlphash,
11+
} from 'ethereumjs-util'
412
import { Blockchain, BlockHeaderData, BufferLike, ChainOptions, PrefixedHexString } from './types'
513
import { Buffer } from 'buffer'
614
import { Block } from './block'
@@ -55,35 +63,35 @@ export class BlockHeader {
5563
{
5664
name: 'parentHash',
5765
length: 32,
58-
default: utils.zeros(32),
66+
default: zeros(32),
5967
},
6068
{
6169
name: 'uncleHash',
62-
default: utils.KECCAK256_RLP_ARRAY,
70+
default: KECCAK256_RLP_ARRAY,
6371
},
6472
{
6573
name: 'coinbase',
6674
length: 20,
67-
default: utils.zeros(20),
75+
default: zeros(20),
6876
},
6977
{
7078
name: 'stateRoot',
7179
length: 32,
72-
default: utils.zeros(32),
80+
default: zeros(32),
7381
},
7482
{
7583
name: 'transactionsTrie',
7684
length: 32,
77-
default: utils.KECCAK256_RLP,
85+
default: KECCAK256_RLP,
7886
},
7987
{
8088
name: 'receiptTrie',
8189
length: 32,
82-
default: utils.KECCAK256_RLP,
90+
default: KECCAK256_RLP,
8391
},
8492
{
8593
name: 'bloom',
86-
default: utils.zeros(256),
94+
default: zeros(256),
8795
},
8896
{
8997
name: 'difficulty',
@@ -92,7 +100,7 @@ export class BlockHeader {
92100
{
93101
name: 'number',
94102
// TODO: params.homeSteadForkNumber.v left for legacy reasons, replace on future release
95-
default: utils.toBuffer(1150000),
103+
default: toBuffer(1150000),
96104
},
97105
{
98106
name: 'gasLimit',
@@ -115,15 +123,15 @@ export class BlockHeader {
115123
},
116124
{
117125
name: 'mixHash',
118-
default: utils.zeros(32),
126+
default: zeros(32),
119127
// length: 32
120128
},
121129
{
122130
name: 'nonce',
123-
default: utils.zeros(8), // sha3(42)
131+
default: zeros(8), // sha3(42)
124132
},
125133
]
126-
utils.defineProperties(this, fields, data)
134+
defineProperties(this, fields, data)
127135
}
128136

129137
/**
@@ -147,7 +155,7 @@ export class BlockHeader {
147155

148156
if (this._common.hardforkGteHardfork(hardfork, 'byzantium')) {
149157
// max((2 if len(parent.uncles) else 1) - ((timestamp - parent.timestamp) // 9), -99) (EIP100)
150-
const uncleAddend = parentBlock.header.uncleHash.equals(utils.KECCAK256_RLP_ARRAY) ? 1 : 2
158+
const uncleAddend = parentBlock.header.uncleHash.equals(KECCAK256_RLP_ARRAY) ? 1 : 2
151159
let a = blockTs
152160
.sub(parentTs)
153161
.idivn(9)
@@ -283,11 +291,11 @@ export class BlockHeader {
283291
throw new Error('invalid gas limit')
284292
}
285293

286-
if (utils.bufferToInt(this.number) - utils.bufferToInt(parentBlock.header.number) !== 1) {
294+
if (bufferToInt(this.number) - bufferToInt(parentBlock.header.number) !== 1) {
287295
throw new Error('invalid height')
288296
}
289297

290-
if (utils.bufferToInt(this.timestamp) <= utils.bufferToInt(parentBlock.header.timestamp)) {
298+
if (bufferToInt(this.timestamp) <= bufferToInt(parentBlock.header.timestamp)) {
291299
throw new Error('invalid timestamp')
292300
}
293301

@@ -301,7 +309,7 @@ export class BlockHeader {
301309
* Returns the hash of the block header.
302310
*/
303311
hash(): Buffer {
304-
return utils.rlphash(this.raw)
312+
return rlphash(this.raw)
305313
}
306314

307315
/**
@@ -347,7 +355,7 @@ export class BlockHeader {
347355

348356
return commonHardFork !== null
349357
? commonHardFork
350-
: this._common.activeHardfork(utils.bufferToInt(this.number))
358+
: this._common.activeHardfork(bufferToInt(this.number))
351359
}
352360

353361
private async _getBlockByHash(blockchain: Blockchain, hash: Buffer): Promise<Block | undefined> {

packages/block/test/difficulty.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import * as utils from 'ethereumjs-util'
2-
import { BN } from 'ethereumjs-util'
1+
import { toBuffer } from 'ethereumjs-util'
32
import { Block } from '../src/block'
43
import tape = require('tape')
54

5+
const { BN } = require('ethereumjs-util')
6+
67
function isHexPrefixed(str: string) {
78
return str.toLowerCase().startsWith('0x')
89
}
910

1011
function normalize(data: any) {
1112
Object.keys(data).forEach(function(i) {
1213
if (i !== 'homestead' && typeof data[i] === 'string') {
13-
data[i] = isHexPrefixed(data[i]) ? new BN(utils.toBuffer(data[i])) : new BN(data[i])
14+
data[i] = isHexPrefixed(data[i]) ? new BN(toBuffer(data[i])) : new BN(data[i])
1415
}
1516
})
1617
}

packages/block/test/header.spec.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import tape = require('tape')
22
import Common from 'ethereumjs-common'
3-
import * as utils from 'ethereumjs-util'
4-
import { rlp } from 'ethereumjs-util'
3+
import { rlp, toBuffer, zeros, KECCAK256_RLP_S, KECCAK256_RLP_ARRAY_S } from 'ethereumjs-util'
54
import { BlockHeader } from '../src/header'
65
import { Block } from '../src/block'
76

87
tape('[Block]: Header functions', function(t) {
98
t.test('should create with default constructor', function(st) {
109
function compareDefaultHeader(st: tape.Test, header: BlockHeader) {
11-
st.deepEqual(header.parentHash, utils.zeros(32))
12-
st.equal(header.uncleHash.toString('hex'), utils.KECCAK256_RLP_ARRAY_S)
13-
st.deepEqual(header.coinbase, utils.zeros(20))
14-
st.deepEqual(header.stateRoot, utils.zeros(32))
15-
st.equal(header.transactionsTrie.toString('hex'), utils.KECCAK256_RLP_S)
16-
st.equal(header.receiptTrie.toString('hex'), utils.KECCAK256_RLP_S)
17-
st.deepEqual(header.bloom, utils.zeros(256))
10+
st.deepEqual(header.parentHash, zeros(32))
11+
st.equal(header.uncleHash.toString('hex'), KECCAK256_RLP_ARRAY_S)
12+
st.deepEqual(header.coinbase, zeros(20))
13+
st.deepEqual(header.stateRoot, zeros(32))
14+
st.equal(header.transactionsTrie.toString('hex'), KECCAK256_RLP_S)
15+
st.equal(header.receiptTrie.toString('hex'), KECCAK256_RLP_S)
16+
st.deepEqual(header.bloom, zeros(256))
1817
st.deepEqual(header.difficulty, Buffer.from([]))
19-
st.deepEqual(header.number, utils.toBuffer(1150000))
18+
st.deepEqual(header.number, toBuffer(1150000))
2019
st.deepEqual(header.gasLimit, Buffer.from('ffffffffffffff', 'hex'))
2120
st.deepEqual(header.gasUsed, Buffer.from([]))
2221
st.deepEqual(header.timestamp, Buffer.from([]))
2322
st.deepEqual(header.extraData, Buffer.from([]))
24-
st.deepEqual(header.mixHash, utils.zeros(32))
25-
st.deepEqual(header.nonce, utils.zeros(8))
23+
st.deepEqual(header.mixHash, zeros(32))
24+
st.deepEqual(header.nonce, zeros(8))
2625
}
2726

2827
let header = new BlockHeader()

packages/block/tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "@ethereumjs/config-tsc",
33
"include": ["src/**/*.ts", "test/**/*.ts"],
44
"compilerOptions": {
5-
"outDir": "test-build",
6-
"esModuleInterop": true
5+
"outDir": "test-build"
76
}
87
}

packages/block/tsconfig.prod.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"extends": "@ethereumjs/config-tsc",
33
"compilerOptions": {
4-
"outDir": "./dist",
5-
"esModuleInterop": true
4+
"outDir": "./dist"
65
},
76
"include": ["src/**/*.ts"]
87
}

packages/blockchain/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"ethashjs": "~0.0.7",
4242
"ethereumjs-block": "~3.0.0",
4343
"ethereumjs-common": "^1.5.0",
44-
"ethereumjs-util": "~6.1.0",
44+
"ethereumjs-util": "^7.0.2",
4545
"flow-stoplight": "^1.0.0",
4646
"level-mem": "^3.0.1",
4747
"lru-cache": "^5.1.1",
@@ -54,7 +54,7 @@
5454
"@ethereumjs/config-tsc": "^1.1.1",
5555
"@ethereumjs/config-tslint": "^1.1.1",
5656
"@types/async": "^2.4.1",
57-
"@types/bn.js": "^4.11.4",
57+
"@types/bn.js": "^4.11.6",
5858
"@types/lru-cache": "^5.1.0",
5959
"@types/node": "^11.11.4",
6060
"@types/semaphore": "^1.1.0",
@@ -67,7 +67,7 @@
6767
"tslint": "^5.13.1",
6868
"typedoc": "next",
6969
"typedoc-plugin-markdown": "^2.2.17",
70-
"typescript": "^3.3.3333",
70+
"typescript": "^3.9.2",
7171
"typestrict": "^1.0.2"
7272
}
7373
}

packages/common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"tslint": "^5.12.0",
5959
"typedoc": "next",
6060
"typedoc-plugin-markdown": "^2.2.17",
61-
"typescript": "^3.2.2",
61+
"typescript": "^3.9.2",
6262
"typestrict": "^1.0.2"
6363
},
6464
"maintainers": [

0 commit comments

Comments
 (0)