Skip to content

Commit

Permalink
Util, Monorepo: hexStringToBytes -> prefixedHexStringToBytes (#2830)
Browse files Browse the repository at this point in the history
* monorepo: hexStringToBytes -> prefixedHexStringToBytes (excluding client) [no ci]

* client: hexStringToBytes -> prefixedHexStringToBytes

* statemanager: hexStr -> prefixedHexStr

* monorepo: lint [no ci]

* client: fix test suite

* monorepo: update types, review prefixedHexStrings client

* vm/common: fix tests

* vm/retesteth: fix docs

* util/client: remove unprefixed hex string type

* block; use zeros, vm: fix tests

* wallet: update examples

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
  • Loading branch information
jochem-brouwer and holgerd77 authored Jun 29, 2023
1 parent 9fbc8b0 commit bec76a9
Show file tree
Hide file tree
Showing 90 changed files with 792 additions and 549 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
equalsBytes,
fetchFromProvider,
getProvider,
hexStringToBytes,
intToPrefixedHexString,
isHexPrefixed,
prefixedHexStringToBytes,
} from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak.js'

Expand Down Expand Up @@ -302,7 +302,7 @@ export class Block {
const txs = []
for (const [index, serializedTx] of transactions.entries()) {
try {
const tx = TransactionFactory.fromSerializedData(hexStringToBytes(serializedTx), {
const tx = TransactionFactory.fromSerializedData(prefixedHexStringToBytes(serializedTx), {
common: options?.common,
})
txs.push(tx)
Expand Down Expand Up @@ -330,7 +330,7 @@ export class Block {
// we are not setting setHardfork as common is already set to the correct hf
const block = Block.fromBlockData({ header, transactions: txs, withdrawals }, options)
// Verify blockHash matches payload
if (!equalsBytes(block.hash(), hexStringToBytes(payload.blockHash))) {
if (!equalsBytes(block.hash(), prefixedHexStringToBytes(payload.blockHash))) {
const validationError = `Invalid blockHash, expected: ${
payload.blockHash
}, received: ${bytesToPrefixedHexString(block.hash())}`
Expand Down
43 changes: 23 additions & 20 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
BigIntLike,
BytesLike,
JsonRpcWithdrawal,
PrefixedHexString,
WithdrawalBytes,
WithdrawalData,
} from '@ethereumjs/util'
Expand Down Expand Up @@ -190,29 +191,31 @@ export interface JsonRpcBlock {
excessDataGas?: string // If EIP-4844 is enabled for this block, returns the excess data gas for the block
}

// Note: all these strings are 0x-prefixed
export type WithdrawalV1 = {
index: string // Quantity, 8 Bytes
validatorIndex: string // Quantity, 8 bytes
address: string // DATA, 20 bytes
amount: string // Quantity, 32 bytes
index: PrefixedHexString // Quantity, 8 Bytes
validatorIndex: PrefixedHexString // Quantity, 8 bytes
address: PrefixedHexString // DATA, 20 bytes
amount: PrefixedHexString // Quantity, 32 bytes
}

// Note: all these strings are 0x-prefixed
export type ExecutionPayload = {
parentHash: string // DATA, 32 Bytes
feeRecipient: string // DATA, 20 Bytes
stateRoot: string // DATA, 32 Bytes
receiptsRoot: string // DATA, 32 bytes
logsBloom: string // DATA, 256 Bytes
prevRandao: string // DATA, 32 Bytes
blockNumber: string // QUANTITY, 64 Bits
gasLimit: string // QUANTITY, 64 Bits
gasUsed: string // QUANTITY, 64 Bits
timestamp: string // QUANTITY, 64 Bits
extraData: string // DATA, 0 to 32 Bytes
baseFeePerGas: string // QUANTITY, 256 Bits
blockHash: string // DATA, 32 Bytes
transactions: string[] // Array of DATA - Array of transaction rlp strings,
parentHash: PrefixedHexString // DATA, 32 Bytes
feeRecipient: PrefixedHexString // DATA, 20 Bytes
stateRoot: PrefixedHexString // DATA, 32 Bytes
receiptsRoot: PrefixedHexString // DATA, 32 bytes
logsBloom: PrefixedHexString // DATA, 256 Bytes
prevRandao: PrefixedHexString // DATA, 32 Bytes
blockNumber: PrefixedHexString // QUANTITY, 64 Bits
gasLimit: PrefixedHexString // QUANTITY, 64 Bits
gasUsed: PrefixedHexString // QUANTITY, 64 Bits
timestamp: PrefixedHexString // QUANTITY, 64 Bits
extraData: PrefixedHexString // DATA, 0 to 32 Bytes
baseFeePerGas: PrefixedHexString // QUANTITY, 256 Bits
blockHash: PrefixedHexString // DATA, 32 Bytes
transactions: PrefixedHexString[] // Array of DATA - Array of transaction rlp strings,
withdrawals?: WithdrawalV1[] // Array of withdrawal objects
dataGasUsed?: string // QUANTITY, 64 Bits
excessDataGas?: string // QUANTITY, 64 Bits
dataGasUsed?: PrefixedHexString // QUANTITY, 64 Bits
excessDataGas?: PrefixedHexString // QUANTITY, 64 Bits
}
10 changes: 5 additions & 5 deletions packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { bytesToHex, equalsBytes, hexStringToBytes, toBytes, zeros } from '@ethereumjs/util'
import { bytesToHex, equalsBytes, prefixedHexStringToBytes, toBytes, zeros } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
// explicitly import util, needed for karma-typescript bundling
// eslint-disable-next-line @typescript-eslint/no-unused-vars, simple-import-sort/imports
Expand Down Expand Up @@ -227,8 +227,8 @@ describe('[Block]: block functions', () => {

it('should test genesis hashes (mainnet default)', () => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const rlp = hexStringToBytes(testDataGenesis.test.genesis_rlp_hex)
const hash = hexStringToBytes(testDataGenesis.test.genesis_hash)
const rlp = prefixedHexStringToBytes('0x' + testDataGenesis.test.genesis_rlp_hex)
const hash = prefixedHexStringToBytes('0x' + testDataGenesis.test.genesis_hash)
const block = Block.fromRLPSerializedBlock(rlp, { common })
assert.ok(equalsBytes(block.hash(), hash), 'genesis hash match')
})
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('[Block]: block functions', () => {
it('DAO hardfork', () => {
const blockData = RLP.decode(testDataPreLondon2.blocks[0].rlp) as NestedUint8Array
// Set block number from test block to mainnet DAO fork block 1920000
blockData[0][8] = hexStringToBytes('1D4C00')
blockData[0][8] = prefixedHexStringToBytes('0x1D4C00')

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Dao })
assert.throws(
Expand All @@ -285,7 +285,7 @@ describe('[Block]: block functions', () => {
) // eslint-disable-line

// Set extraData to dao-hard-fork
blockData[0][12] = hexStringToBytes('64616f2d686172642d666f726b')
blockData[0][12] = prefixedHexStringToBytes('0x64616f2d686172642d666f726b')

assert.doesNotThrow(function () {
Block.fromValuesArray(blockData as BlockBytes, { common })
Expand Down
12 changes: 6 additions & 6 deletions packages/block/test/clique.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { Address, hexStringToBytes } from '@ethereumjs/util'
import { Address, prefixedHexStringToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { BlockHeader } from '../src/header.js'
Expand Down Expand Up @@ -78,12 +78,12 @@ describe('[Header]: Clique PoA Functionality', () => {
}

const A: Signer = {
address: new Address(hexStringToBytes('0b90087d864e82a284dca15923f3776de6bb016f')),
privateKey: hexStringToBytes(
'64bf9cc30328b0e42387b3c82c614e6386259136235e20c1357bd11cdee86993'
address: new Address(prefixedHexStringToBytes('0x0b90087d864e82a284dca15923f3776de6bb016f')),
privateKey: prefixedHexStringToBytes(
'0x64bf9cc30328b0e42387b3c82c614e6386259136235e20c1357bd11cdee86993'
),
publicKey: hexStringToBytes(
'40b2ebdf4b53206d2d3d3d59e7e2f13b1ea68305aec71d5d24cefe7f24ecae886d241f9267f04702d7f693655eb7b4aa23f30dcd0c3c5f2b970aad7c8a828195'
publicKey: prefixedHexStringToBytes(
'0x40b2ebdf4b53206d2d3d3d59e7e2f13b1ea68305aec71d5d24cefe7f24ecae886d241f9267f04702d7f693655eb7b4aa23f30dcd0c3c5f2b970aad7c8a828195'
),
}

Expand Down
14 changes: 7 additions & 7 deletions packages/block/test/eip1559block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { FeeMarketEIP1559Transaction } from '@ethereumjs/tx'
import { hexStringToBytes } from '@ethereumjs/util'
import { prefixedHexStringToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { Block } from '../src/block.js'
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('EIP1559 tests', () => {
parentHash: block1.hash(),
timestamp: BigInt(2),
gasLimit: genesis.header.gasLimit * BigInt(2), // Special case on EIP-1559 transition block
baseFeePerGas: hexStringToBytes('342770c0'),
baseFeePerGas: prefixedHexStringToBytes('0x342770c0'),
},
},
{
Expand Down Expand Up @@ -280,7 +280,7 @@ describe('EIP1559 tests', () => {
parentHash: block1.hash(),
timestamp: BigInt(2),
gasLimit: parentGasLimit + parentGasLimit / BigInt(1024) - BigInt(1),
baseFeePerGas: hexStringToBytes('342770c0'),
baseFeePerGas: prefixedHexStringToBytes('0x342770c0'),
},
{
calcDifficultyFromHeader: block1.header,
Expand All @@ -296,7 +296,7 @@ describe('EIP1559 tests', () => {
parentHash: block1.hash(),
timestamp: BigInt(2),
gasLimit: parentGasLimit - parentGasLimit / BigInt(1024) + BigInt(1),
baseFeePerGas: hexStringToBytes('342770c0'),
baseFeePerGas: prefixedHexStringToBytes('0x342770c0'),
},
{
calcDifficultyFromHeader: block1.header,
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('EIP1559 tests', () => {
parentHash: block1.hash(),
timestamp: BigInt(2),
gasLimit: parentGasLimit + parentGasLimit / BigInt(1024),
baseFeePerGas: hexStringToBytes('342770c0'),
baseFeePerGas: prefixedHexStringToBytes('0x342770c0'),
},
{
calcDifficultyFromHeader: block1.header,
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('EIP1559 tests', () => {
parentHash: block1.hash(),
timestamp: BigInt(2),
gasLimit: parentGasLimit - parentGasLimit / BigInt(1024),
baseFeePerGas: hexStringToBytes('342770c0'),
baseFeePerGas: prefixedHexStringToBytes('0x342770c0'),
},
{
calcDifficultyFromHeader: block1.header,
Expand All @@ -414,7 +414,7 @@ describe('EIP1559 tests', () => {
maxPriorityFeePerGas: BigInt(0),
},
{ common }
).sign(hexStringToBytes('46'.repeat(32)))
).sign(prefixedHexStringToBytes('0x' + '46'.repeat(32)))
const block = Block.fromBlockData(
{
header: {
Expand Down
32 changes: 20 additions & 12 deletions packages/block/test/eip4895block.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import { Address, KECCAK256_RLP, Withdrawal, hexStringToBytes } from '@ethereumjs/util'
import {
Address,
KECCAK256_RLP,
Withdrawal,
prefixedHexStringToBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'

import { Block } from '../src/block.js'
Expand Down Expand Up @@ -31,7 +37,9 @@ common.hardforkBlock = function (hardfork: string | undefined) {
describe('EIP4895 tests', () => {
it('should correctly generate withdrawalsRoot', async () => {
// get withdwalsArray
const gethBlockBytesArray = RLP.decode(hexStringToBytes(gethWithdrawals8BlockRlp))
const gethBlockBytesArray = RLP.decode(
prefixedHexStringToBytes('0x' + gethWithdrawals8BlockRlp)
)
const withdrawals = (gethBlockBytesArray[3] as WithdrawalBytes[]).map((wa) =>
Withdrawal.fromValuesArray(wa)
)
Expand All @@ -50,7 +58,7 @@ describe('EIP4895 tests', () => {
() => {
BlockHeader.fromHeaderData(
{
withdrawalsRoot: hexStringToBytes('00'.repeat(32)),
withdrawalsRoot: zeros(32),
},
{
common: earlyCommon,
Expand All @@ -72,7 +80,7 @@ describe('EIP4895 tests', () => {
assert.doesNotThrow(() => {
BlockHeader.fromHeaderData(
{
withdrawalsRoot: hexStringToBytes('00'.repeat(32)),
withdrawalsRoot: zeros(32),
},
{
common,
Expand Down Expand Up @@ -109,7 +117,7 @@ describe('EIP4895 tests', () => {
Block.fromBlockData(
{
header: {
withdrawalsRoot: hexStringToBytes('00'.repeat(32)),
withdrawalsRoot: zeros(32),
},
withdrawals: [],
},
Expand All @@ -121,7 +129,7 @@ describe('EIP4895 tests', () => {
const block = Block.fromBlockData(
{
header: {
withdrawalsRoot: hexStringToBytes('00'.repeat(32)),
withdrawalsRoot: zeros(32),
},
withdrawals: [],
},
Expand Down Expand Up @@ -153,15 +161,15 @@ describe('EIP4895 tests', () => {
const withdrawal = <WithdrawalData>{
index: BigInt(0),
validatorIndex: BigInt(0),
address: new Address(hexStringToBytes('20'.repeat(20))),
address: new Address(prefixedHexStringToBytes('0x' + '20'.repeat(20))),
amount: BigInt(1000),
}

const validBlockWithWithdrawal = Block.fromBlockData(
{
header: {
withdrawalsRoot: hexStringToBytes(
'897ca49edcb278aecab2688bcc2b7b7ee43524cc489672534fee332a172f1718'
withdrawalsRoot: prefixedHexStringToBytes(
'0x897ca49edcb278aecab2688bcc2b7b7ee43524cc489672534fee332a172f1718'
),
},
withdrawals: [withdrawal],
Expand All @@ -178,15 +186,15 @@ describe('EIP4895 tests', () => {
const withdrawal2 = <WithdrawalData>{
index: BigInt(1),
validatorIndex: BigInt(11),
address: new Address(hexStringToBytes('30'.repeat(20))),
address: new Address(prefixedHexStringToBytes('0x' + '30'.repeat(20))),
amount: BigInt(2000),
}

const validBlockWithWithdrawal2 = Block.fromBlockData(
{
header: {
withdrawalsRoot: hexStringToBytes(
'3b514862c42008079d461392e29d5b6775dd5ed370a6c4441ccb8ab742bf2436'
withdrawalsRoot: prefixedHexStringToBytes(
'0x3b514862c42008079d461392e29d5b6775dd5ed370a6c4441ccb8ab742bf2436'
),
},
withdrawals: [withdrawal, withdrawal2],
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { bytesToPrefixedHexString, hexStringToBytes, randomBytes } from '@ethereumjs/util'
import { bytesToPrefixedHexString, prefixedHexStringToBytes, randomBytes } from '@ethereumjs/util'
import { bytesToHex, equalsBytes } from 'ethereum-cryptography/utils'
import { assert, describe, it } from 'vitest'

Expand Down Expand Up @@ -32,7 +32,7 @@ describe('[fromRPC]: block #2924874', () => {

it('should create a block header with the correct hash', () => {
const block = blockHeaderFromRpc(blockData, { common })
const hash = hexStringToBytes(blockData.hash)
const hash = prefixedHexStringToBytes(blockData.hash)
assert.ok(equalsBytes(block.hash(), hash))
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/block/test/header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
bytesToHex,
concatBytes,
equalsBytes,
hexStringToBytes,
prefixedHexStringToBytes,
toBytes,
zeros,
} from '@ethereumjs/util'
Expand Down Expand Up @@ -113,8 +113,8 @@ describe('[Block]: Header functions', () => {
)

header = BlockHeader.fromRLPSerializedHeader(
hexStringToBytes(
'f90214a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000850400000000808213888080a011bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82faa00000000000000000000000000000000000000000000000000000000000000000880000000000000042'
prefixedHexStringToBytes(
'0xf90214a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000850400000000808213888080a011bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82faa00000000000000000000000000000000000000000000000000000000000000000880000000000000042'
),
{ common, setHardfork: false }
)
Expand Down
4 changes: 2 additions & 2 deletions packages/block/test/mergeBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
KECCAK256_RLP,
KECCAK256_RLP_ARRAY,
equalsBytes,
hexStringToBytes,
prefixedHexStringToBytes,
zeros,
} from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('[Header]: Casper PoS / The Merge Functionality', () => {
// Building a header with random values for constants
try {
const headerData = {
uncleHash: hexStringToBytes('123abc'),
uncleHash: prefixedHexStringToBytes('0x123abc'),
}
BlockHeader.fromHeaderData(headerData, { common })
assert.fail('should throw')
Expand Down
4 changes: 2 additions & 2 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
Address,
bytesToHex,
bytesToPrefixedHexString,
hexStringToBytes,
initKZG,
parseGethGenesisState,
prefixedHexStringToBytes,
randomBytes,
short,
toBytes,
Expand Down Expand Up @@ -625,7 +625,7 @@ async function inputAccounts() {
}
} else {
const acc = readFileSync(path.resolve(args.unlock!), 'utf-8').replace(/(\r\n|\n|\r)/gm, '')
const privKey = hexStringToBytes(acc)
const privKey = prefixedHexStringToBytes('0x' + acc) // See docs: acc has to be non-zero prefixed in the file
const derivedAddress = Address.fromPrivateKey(privKey)
accounts.push([derivedAddress, privKey])
}
Expand Down
Loading

0 comments on commit bec76a9

Please sign in to comment.