Skip to content

Commit

Permalink
Merge pull request #748 from ethereumjs/update-ethereumjs-util-to-v7
Browse files Browse the repository at this point in the history
Update `ethereumjs-util` to v7
  • Loading branch information
holgerd77 authored Jun 1, 2020
2 parents 4b9a096 + 9b4c55e commit 09de6f4
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 149 deletions.
6 changes: 3 additions & 3 deletions packages/account/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/ethereumjs/ethereumjs-account#readme",
"dependencies": {
"ethereumjs-util": "^6.0.0",
"ethereumjs-util": "^7.0.2",
"rlp": "^2.2.1",
"safe-buffer": "^5.1.1"
},
Expand All @@ -45,7 +45,7 @@
"@ethereumjs/config-prettier": "^1.1.0",
"@ethereumjs/config-tsc": "^1.1.0",
"@ethereumjs/config-tslint": "^1.1.0",
"@types/bn.js": "^4.11.3",
"@types/bn.js": "^4.11.6",
"@types/node": "^11.9.4",
"@types/tape": "^4.2.33",
"merkle-patricia-tree": "^3.0.0",
Expand All @@ -56,7 +56,7 @@
"tslint": "^5.12.0",
"typedoc": "next",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.2.2",
"typescript": "^3.9.2",
"typestrict": "^1.0.2"
}
}
6 changes: 3 additions & 3 deletions packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
},
"homepage": "https://github.com/ethereumjs/ethereumjs-block#readme",
"dependencies": {
"@types/bn.js": "^4.11.5",
"@types/bn.js": "^4.11.6",
"ethereumjs-common": "^1.5.0",
"ethereumjs-tx": "^2.1.1",
"ethereumjs-util": "^6.1.0",
"ethereumjs-util": "^7.0.2",
"merkle-patricia-tree": "^2.1.2"
},
"devDependencies": {
Expand All @@ -67,7 +67,7 @@
"tslint": "^5.15.0",
"typedoc": "next",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.4.3",
"typescript": "^3.9.2",
"typestrict": "^1.0.2"
}
}
11 changes: 5 additions & 6 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Common from 'ethereumjs-common'
import * as ethUtil from 'ethereumjs-util'
import { BN, rlp } from 'ethereumjs-util'
import { rlp, keccak256, KECCAK256_RLP, baToJSON } from 'ethereumjs-util'
import { Transaction, TransactionOptions } from 'ethereumjs-tx'

import { BlockHeader } from './header'
import { Blockchain, BlockData, ChainOptions } from './types'

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

/**
* An object that represents the block
Expand Down Expand Up @@ -148,7 +147,7 @@ export class Block {
if (this.transactions.length) {
return txT === this.txTrie.root.toString('hex')
} else {
return txT === ethUtil.KECCAK256_RLP.toString('hex')
return txT === KECCAK256_RLP.toString('hex')
}
}

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

return ethUtil.keccak256(raw).toString('hex') === this.header.uncleHash.toString('hex')
return keccak256(raw).toString('hex') === this.header.uncleHash.toString('hex')
}

/**
Expand Down Expand Up @@ -248,7 +247,7 @@ export class Block {
uncleHeaders: this.uncleHeaders.forEach(uh => uh.toJSON(true)),
}
} else {
return ethUtil.baToJSON(this.raw)
return baToJSON(this.raw)
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FakeTransaction, TransactionOptions } from 'ethereumjs-tx'
import * as ethUtil from 'ethereumjs-util'
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
import { Block } from './index'
import { ChainOptions } from './types'

Expand Down Expand Up @@ -34,7 +34,7 @@ export default function blockFromRpc(
for (const _txParams of blockParams.transactions) {
const txParams = normalizeTxParams(_txParams)
// override from address
const fromAddress = ethUtil.toBuffer(txParams.from)
const fromAddress = toBuffer(txParams.from)
delete txParams.from

const tx = new FakeTransaction(txParams, chainOptions as TransactionOptions)
Expand All @@ -43,7 +43,7 @@ export default function blockFromRpc(
return fromAddress
}
// override hash
const txHash = ethUtil.toBuffer(txParams.hash)
const txHash = toBuffer(txParams.hash)
tx.hash = function() {
return txHash
}
Expand All @@ -60,7 +60,7 @@ function normalizeTxParams(_txParams: any) {
txParams.gasLimit = txParams.gasLimit === undefined ? txParams.gas : txParams.gasLimit
txParams.data = txParams.data === undefined ? txParams.input : txParams.data
// strict byte length checking
txParams.to = txParams.to ? ethUtil.setLengthLeft(ethUtil.toBuffer(txParams.to), 20) : null
txParams.to = txParams.to ? setLengthLeft(toBuffer(txParams.to), 20) : null

// v as raw signature value {0,1}
// v is the recovery bit and can be either {0,1} or {27,28}.
Expand Down
6 changes: 3 additions & 3 deletions packages/block/src/header-from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlockHeader } from './header'
import * as ethUtil from 'ethereumjs-util'
import { KECCAK256_NULL, toBuffer } from 'ethereumjs-util'
import { ChainOptions } from './types'

/**
Expand All @@ -16,7 +16,7 @@ export default function blockHeaderFromRpc(blockParams: any, chainOptions?: Chai
coinbase: blockParams.miner,
stateRoot: blockParams.stateRoot,
transactionsTrie: blockParams.transactionsRoot,
receiptTrie: blockParams.receiptRoot || blockParams.receiptsRoot || ethUtil.KECCAK256_NULL,
receiptTrie: blockParams.receiptRoot || blockParams.receiptsRoot || KECCAK256_NULL,
bloom: blockParams.logsBloom,
difficulty: blockParams.difficulty,
number: blockParams.number,
Expand All @@ -32,7 +32,7 @@ export default function blockHeaderFromRpc(blockParams: any, chainOptions?: Chai

// override hash in case something was missing
blockHeader.hash = function() {
return ethUtil.toBuffer(blockParams.hash)
return toBuffer(blockParams.hash)
}

return blockHeader
Expand Down
44 changes: 26 additions & 18 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import Common from 'ethereumjs-common'
import * as utils from 'ethereumjs-util'
import { BN } from 'ethereumjs-util'
import {
BN,
zeros,
KECCAK256_RLP_ARRAY,
KECCAK256_RLP,
toBuffer,
defineProperties,
bufferToInt,
rlphash,
} from 'ethereumjs-util'
import { Blockchain, BlockHeaderData, BufferLike, ChainOptions, PrefixedHexString } from './types'
import { Buffer } from 'buffer'
import { Block } from './block'
Expand Down Expand Up @@ -55,35 +63,35 @@ export class BlockHeader {
{
name: 'parentHash',
length: 32,
default: utils.zeros(32),
default: zeros(32),
},
{
name: 'uncleHash',
default: utils.KECCAK256_RLP_ARRAY,
default: KECCAK256_RLP_ARRAY,
},
{
name: 'coinbase',
length: 20,
default: utils.zeros(20),
default: zeros(20),
},
{
name: 'stateRoot',
length: 32,
default: utils.zeros(32),
default: zeros(32),
},
{
name: 'transactionsTrie',
length: 32,
default: utils.KECCAK256_RLP,
default: KECCAK256_RLP,
},
{
name: 'receiptTrie',
length: 32,
default: utils.KECCAK256_RLP,
default: KECCAK256_RLP,
},
{
name: 'bloom',
default: utils.zeros(256),
default: zeros(256),
},
{
name: 'difficulty',
Expand All @@ -92,7 +100,7 @@ export class BlockHeader {
{
name: 'number',
// TODO: params.homeSteadForkNumber.v left for legacy reasons, replace on future release
default: utils.toBuffer(1150000),
default: toBuffer(1150000),
},
{
name: 'gasLimit',
Expand All @@ -115,15 +123,15 @@ export class BlockHeader {
},
{
name: 'mixHash',
default: utils.zeros(32),
default: zeros(32),
// length: 32
},
{
name: 'nonce',
default: utils.zeros(8), // sha3(42)
default: zeros(8), // sha3(42)
},
]
utils.defineProperties(this, fields, data)
defineProperties(this, fields, data)
}

/**
Expand All @@ -147,7 +155,7 @@ export class BlockHeader {

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

if (utils.bufferToInt(this.number) - utils.bufferToInt(parentBlock.header.number) !== 1) {
if (bufferToInt(this.number) - bufferToInt(parentBlock.header.number) !== 1) {
throw new Error('invalid height')
}

if (utils.bufferToInt(this.timestamp) <= utils.bufferToInt(parentBlock.header.timestamp)) {
if (bufferToInt(this.timestamp) <= bufferToInt(parentBlock.header.timestamp)) {
throw new Error('invalid timestamp')
}

Expand All @@ -301,7 +309,7 @@ export class BlockHeader {
* Returns the hash of the block header.
*/
hash(): Buffer {
return utils.rlphash(this.raw)
return rlphash(this.raw)
}

/**
Expand Down Expand Up @@ -347,7 +355,7 @@ export class BlockHeader {

return commonHardFork !== null
? commonHardFork
: this._common.activeHardfork(utils.bufferToInt(this.number))
: this._common.activeHardfork(bufferToInt(this.number))
}

private async _getBlockByHash(blockchain: Blockchain, hash: Buffer): Promise<Block | undefined> {
Expand Down
7 changes: 4 additions & 3 deletions packages/block/test/difficulty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import * as utils from 'ethereumjs-util'
import { BN } from 'ethereumjs-util'
import { toBuffer } from 'ethereumjs-util'
import { Block } from '../src/block'
import tape = require('tape')

const { BN } = require('ethereumjs-util')

function isHexPrefixed(str: string) {
return str.toLowerCase().startsWith('0x')
}

function normalize(data: any) {
Object.keys(data).forEach(function(i) {
if (i !== 'homestead' && typeof data[i] === 'string') {
data[i] = isHexPrefixed(data[i]) ? new BN(utils.toBuffer(data[i])) : new BN(data[i])
data[i] = isHexPrefixed(data[i]) ? new BN(toBuffer(data[i])) : new BN(data[i])
}
})
}
Expand Down
23 changes: 11 additions & 12 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import tape = require('tape')
import Common from 'ethereumjs-common'
import * as utils from 'ethereumjs-util'
import { rlp } from 'ethereumjs-util'
import { rlp, toBuffer, zeros, KECCAK256_RLP_S, KECCAK256_RLP_ARRAY_S } from 'ethereumjs-util'
import { BlockHeader } from '../src/header'
import { Block } from '../src/block'

tape('[Block]: Header functions', function(t) {
t.test('should create with default constructor', function(st) {
function compareDefaultHeader(st: tape.Test, header: BlockHeader) {
st.deepEqual(header.parentHash, utils.zeros(32))
st.equal(header.uncleHash.toString('hex'), utils.KECCAK256_RLP_ARRAY_S)
st.deepEqual(header.coinbase, utils.zeros(20))
st.deepEqual(header.stateRoot, utils.zeros(32))
st.equal(header.transactionsTrie.toString('hex'), utils.KECCAK256_RLP_S)
st.equal(header.receiptTrie.toString('hex'), utils.KECCAK256_RLP_S)
st.deepEqual(header.bloom, utils.zeros(256))
st.deepEqual(header.parentHash, zeros(32))
st.equal(header.uncleHash.toString('hex'), KECCAK256_RLP_ARRAY_S)
st.deepEqual(header.coinbase, zeros(20))
st.deepEqual(header.stateRoot, zeros(32))
st.equal(header.transactionsTrie.toString('hex'), KECCAK256_RLP_S)
st.equal(header.receiptTrie.toString('hex'), KECCAK256_RLP_S)
st.deepEqual(header.bloom, zeros(256))
st.deepEqual(header.difficulty, Buffer.from([]))
st.deepEqual(header.number, utils.toBuffer(1150000))
st.deepEqual(header.number, toBuffer(1150000))
st.deepEqual(header.gasLimit, Buffer.from('ffffffffffffff', 'hex'))
st.deepEqual(header.gasUsed, Buffer.from([]))
st.deepEqual(header.timestamp, Buffer.from([]))
st.deepEqual(header.extraData, Buffer.from([]))
st.deepEqual(header.mixHash, utils.zeros(32))
st.deepEqual(header.nonce, utils.zeros(8))
st.deepEqual(header.mixHash, zeros(32))
st.deepEqual(header.nonce, zeros(8))
}

let header = new BlockHeader()
Expand Down
3 changes: 1 addition & 2 deletions packages/block/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "@ethereumjs/config-tsc",
"include": ["src/**/*.ts", "test/**/*.ts"],
"compilerOptions": {
"outDir": "test-build",
"esModuleInterop": true
"outDir": "test-build"
}
}
3 changes: 1 addition & 2 deletions packages/block/tsconfig.prod.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@ethereumjs/config-tsc",
"compilerOptions": {
"outDir": "./dist",
"esModuleInterop": true
"outDir": "./dist"
},
"include": ["src/**/*.ts"]
}
6 changes: 3 additions & 3 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"ethashjs": "~0.0.7",
"ethereumjs-block": "~3.0.0",
"ethereumjs-common": "^1.5.0",
"ethereumjs-util": "~6.1.0",
"ethereumjs-util": "^7.0.2",
"flow-stoplight": "^1.0.0",
"level-mem": "^3.0.1",
"lru-cache": "^5.1.1",
Expand All @@ -54,7 +54,7 @@
"@ethereumjs/config-tsc": "^1.1.1",
"@ethereumjs/config-tslint": "^1.1.1",
"@types/async": "^2.4.1",
"@types/bn.js": "^4.11.4",
"@types/bn.js": "^4.11.6",
"@types/lru-cache": "^5.1.0",
"@types/node": "^11.11.4",
"@types/semaphore": "^1.1.0",
Expand All @@ -67,7 +67,7 @@
"tslint": "^5.13.1",
"typedoc": "next",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.3.3333",
"typescript": "^3.9.2",
"typestrict": "^1.0.2"
}
}
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"tslint": "^5.12.0",
"typedoc": "next",
"typedoc-plugin-markdown": "^2.2.17",
"typescript": "^3.2.2",
"typescript": "^3.9.2",
"typestrict": "^1.0.2"
},
"maintainers": [
Expand Down
Loading

0 comments on commit 09de6f4

Please sign in to comment.