Skip to content

Commit

Permalink
update to ethereumjs-util 7.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed May 26, 2020
1 parent 4b9a096 commit e9f6396
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 148 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
52 changes: 31 additions & 21 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import Common from 'ethereumjs-common'
import * as utils from 'ethereumjs-util'
import { BN } from 'ethereumjs-util'
import {
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'

const { BN } = require('ethereumjs-util')
import IBN = require('bn.js')

/**
* An object that represents the block header
*/
Expand Down Expand Up @@ -55,35 +65,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 +102,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,23 +125,23 @@ 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)
}

/**
* Returns the canonical difficulty for this block.
*
* @param parentBlock - the parent `Block` of this header
*/
canonicalDifficulty(parentBlock: Block): BN {
canonicalDifficulty(parentBlock: Block): IBN {
const hardfork = this._getHardfork()
const blockTs = new BN(this.timestamp)
const parentTs = new BN(parentBlock.header.timestamp)
Expand All @@ -143,11 +153,11 @@ export class BlockHeader {
let num = new BN(this.number)

// We use a ! here as TS can follow this hardforks-dependent logic, but it always gets assigned
let dif!: BN
let dif!: IBN

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 @@ -252,7 +262,7 @@ export class BlockHeader {
* @param blockchain - the blockchain that this block is validating against
* @param height - If this is an uncle header, this is the height of the block that is including it
*/
async validate(blockchain: Blockchain, height?: BN): Promise<void> {
async validate(blockchain: Blockchain, height?: IBN): Promise<void> {
if (this.isGenesis()) {
return
}
Expand Down Expand Up @@ -283,11 +293,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 +311,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 +357,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
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"
}
}
Loading

0 comments on commit e9f6396

Please sign in to comment.