Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: update prefixedHexString to use literal string template #3348

Merged
merged 14 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions packages/block/src/from-beacon-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import type { ExecutionPayload, VerkleExecutionWitness } from './types.js'
import type { PrefixedHexString } from '@ethereumjs/util'

type BeaconWithdrawal = {
index: string
validator_index: string
address: string
amount: string
index: PrefixedHexString
validator_index: PrefixedHexString
address: PrefixedHexString
amount: PrefixedHexString
}

// Payload json that one gets using the beacon apis
// curl localhost:5052/eth/v2/beacon/blocks/56610 | jq .data.message.body.execution_payload
export type BeaconPayloadJson = {
parent_hash: string
fee_recipient: string
state_root: string
receipts_root: string
logs_bloom: string
prev_randao: string
block_number: string
gas_limit: string
gas_used: string
timestamp: string
extra_data: string
base_fee_per_gas: string
block_hash: string
transactions: string[]
parent_hash: PrefixedHexString
fee_recipient: PrefixedHexString
state_root: PrefixedHexString
receipts_root: PrefixedHexString
logs_bloom: PrefixedHexString
prev_randao: PrefixedHexString
block_number: PrefixedHexString
gas_limit: PrefixedHexString
gas_used: PrefixedHexString
timestamp: PrefixedHexString
extra_data: PrefixedHexString
base_fee_per_gas: PrefixedHexString
block_hash: PrefixedHexString
transactions: PrefixedHexString[]
withdrawals?: BeaconWithdrawal[]
blob_gas_used?: string
excess_blob_gas?: string
parent_beacon_block_root?: string
blob_gas_used?: PrefixedHexString
excess_blob_gas?: PrefixedHexString
parent_beacon_block_root?: PrefixedHexString
// the casing of VerkleExecutionWitness remains same camel case for now
execution_witness?: VerkleExecutionWitness
}
Expand Down
5 changes: 3 additions & 2 deletions packages/block/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import { BIGINT_0, BIGINT_1, TypeOutput, isHexString, toType } from '@ethereumjs

import type { BlockHeaderBytes, HeaderData } from './types.js'
import type { TypedTransaction } from '@ethereumjs/tx'
import type { PrefixedHexString } from '@ethereumjs/util'

/**
* Returns a 0x-prefixed hex number string from a hex string or string integer.
* @param {string} input string to check, convert, and return
*/
export const numberToHex = function (input?: string) {
export const numberToHex = function (input?: string): PrefixedHexString | undefined {
if (input === undefined) return undefined
if (!isHexString(input)) {
const regex = new RegExp(/^\d+$/) // test to make sure input contains only digits
if (!regex.test(input)) {
const msg = `Cannot convert string to hex string. numberToHex only supports 0x-prefixed hex or integer strings but the given string was: ${input}`
throw new Error(msg)
}
return '0x' + parseInt(input, 10).toString(16)
return `0x${parseInt(input, 10).toString(16)}`
}
return input
}
Expand Down
94 changes: 47 additions & 47 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,61 +195,61 @@ export interface JsonBlock {
}

/**
* An object with the block header's data represented as strings.
* An object with the block header's data represented as 0x-prefixed hex strings.
*/
export interface JsonHeader {
parentHash?: string
uncleHash?: string
coinbase?: string
stateRoot?: string
transactionsTrie?: string
receiptTrie?: string
logsBloom?: string
difficulty?: string
number?: string
gasLimit?: string
gasUsed?: string
timestamp?: string
extraData?: string
mixHash?: string
nonce?: string
baseFeePerGas?: string
withdrawalsRoot?: string
blobGasUsed?: string
excessBlobGas?: string
parentBeaconBlockRoot?: string
parentHash?: PrefixedHexString
uncleHash?: PrefixedHexString
coinbase?: PrefixedHexString
stateRoot?: PrefixedHexString
transactionsTrie?: PrefixedHexString
receiptTrie?: PrefixedHexString
logsBloom?: PrefixedHexString
difficulty?: PrefixedHexString
number?: PrefixedHexString
gasLimit?: PrefixedHexString
gasUsed?: PrefixedHexString
timestamp?: PrefixedHexString
extraData?: PrefixedHexString
mixHash?: PrefixedHexString
nonce?: PrefixedHexString
baseFeePerGas?: PrefixedHexString
withdrawalsRoot?: PrefixedHexString
blobGasUsed?: PrefixedHexString
excessBlobGas?: PrefixedHexString
parentBeaconBlockRoot?: PrefixedHexString
}

/*
* Based on https://ethereum.org/en/developers/docs/apis/json-rpc/
*/
export interface JsonRpcBlock {
number: string // the block number. null when pending block.
hash: string // hash of the block. null when pending block.
parentHash: string // hash of the parent block.
mixHash?: string // bit hash which proves combined with the nonce that a sufficient amount of computation has been carried out on this block.
nonce: string // hash of the generated proof-of-work. null when pending block.
sha3Uncles: string // SHA3 of the uncles data in the block.
logsBloom: string // the bloom filter for the logs of the block. null when pending block.
transactionsRoot: string // the root of the transaction trie of the block.
stateRoot: string // the root of the final state trie of the block.
receiptsRoot: string // the root of the receipts trie of the block.
miner: string // the address of the beneficiary to whom the mining rewards were given.
difficulty: string // integer of the difficulty for this block.
totalDifficulty: string // integer of the total difficulty of the chain until this block.
extraData: string // the “extra data” field of this block.
size: string // integer the size of this block in bytes.
gasLimit: string // the maximum gas allowed in this block.
gasUsed: string // the total used gas by all transactions in this block.
timestamp: string // the unix timestamp for when the block was collated.
transactions: Array<JsonRpcTx | string> // Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
uncles: string[] // Array of uncle hashes
baseFeePerGas?: string // If EIP-1559 is enabled for this block, returns the base fee per gas
number: PrefixedHexString // the block number. null when pending block.
hash: PrefixedHexString // hash of the block. null when pending block.
parentHash: PrefixedHexString // hash of the parent block.
mixHash?: PrefixedHexString // bit hash which proves combined with the nonce that a sufficient amount of computation has been carried out on this block.
nonce: PrefixedHexString // hash of the generated proof-of-work. null when pending block.
sha3Uncles: PrefixedHexString // SHA3 of the uncles data in the block.
logsBloom: PrefixedHexString // the bloom filter for the logs of the block. null when pending block.
transactionsRoot: PrefixedHexString // the root of the transaction trie of the block.
stateRoot: PrefixedHexString // the root of the final state trie of the block.
receiptsRoot: PrefixedHexString // the root of the receipts trie of the block.
miner: PrefixedHexString // the address of the beneficiary to whom the mining rewards were given.
difficulty: PrefixedHexString // integer of the difficulty for this block.
totalDifficulty: PrefixedHexString // integer of the total difficulty of the chain until this block.
extraData: PrefixedHexString // the “extra data” field of this block.
size: PrefixedHexString // integer the size of this block in bytes.
gasLimit: PrefixedHexString // the maximum gas allowed in this block.
gasUsed: PrefixedHexString // the total used gas by all transactions in this block.
timestamp: PrefixedHexString // the unix timestamp for when the block was collated.
transactions: Array<JsonRpcTx | PrefixedHexString> // Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
uncles: PrefixedHexString[] // Array of uncle hashes
baseFeePerGas?: PrefixedHexString // If EIP-1559 is enabled for this block, returns the base fee per gas
withdrawals?: Array<JsonRpcWithdrawal> // If EIP-4895 is enabled for this block, array of withdrawals
withdrawalsRoot?: string // If EIP-4895 is enabled for this block, the root of the withdrawal trie of the block.
blobGasUsed?: string // If EIP-4844 is enabled for this block, returns the blob gas used for the block
excessBlobGas?: string // If EIP-4844 is enabled for this block, returns the excess blob gas for the block
parentBeaconBlockRoot?: string // If EIP-4788 is enabled for this block, returns parent beacon block root
withdrawalsRoot?: PrefixedHexString // If EIP-4895 is enabled for this block, the root of the withdrawal trie of the block.
blobGasUsed?: PrefixedHexString // If EIP-4844 is enabled for this block, returns the blob gas used for the block
excessBlobGas?: PrefixedHexString // If EIP-4844 is enabled for this block, returns the excess blob gas for the block
parentBeaconBlockRoot?: PrefixedHexString // If EIP-4788 is enabled for this block, returns parent beacon block root
executionWitness?: VerkleExecutionWitness | null // If Verkle is enabled for this block
}

Expand Down Expand Up @@ -282,5 +282,5 @@ export type ExecutionPayload = {
excessBlobGas?: PrefixedHexString // QUANTITY, 64 Bits
parentBeaconBlockRoot?: PrefixedHexString // QUANTITY, 64 Bits
// VerkleExecutionWitness is already a hex serialized object
executionWitness?: VerkleExecutionWitness | null // QUANTITY, 64 Bits, null imples not available
executionWitness?: VerkleExecutionWitness | null // QUANTITY, 64 Bits, null implies not available
}
4 changes: 2 additions & 2 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import type {
Consensus,
OnBlock,
} from './types.js'
import type { BlockData } from '@ethereumjs/block'
import type { BlockData, HeaderData } from '@ethereumjs/block'
import type { CliqueConfig } from '@ethereumjs/common'
import type { BigIntLike, DB, DBObject, GenesisState } from '@ethereumjs/util'

Expand Down Expand Up @@ -1418,7 +1418,7 @@ export class Blockchain implements BlockchainInterface {
timestamp: common.genesis().timestamp,
})

const header: BlockData['header'] = {
const header: HeaderData = {
...common.genesis(),
number: 0,
stateRoot,
Expand Down
9 changes: 4 additions & 5 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
randomBytes,
setLengthLeft,
short,
toBytes,
} from '@ethereumjs/util'
import {
keccak256 as keccak256WASM,
Expand Down Expand Up @@ -55,7 +54,7 @@ import type { ClientOpts } from '../src/types'
import type { RPCArgs } from './startRpc'
import type { BlockBytes } from '@ethereumjs/block'
import type { CustomCrypto } from '@ethereumjs/common'
import type { GenesisState } from '@ethereumjs/util'
import type { GenesisState, PrefixedHexString } from '@ethereumjs/util'
import type { AbstractLevel } from 'abstract-level'

type Account = [address: Address, privateKey: Uint8Array]
Expand Down Expand Up @@ -785,11 +784,11 @@ async function inputAccounts() {
if (!isFile) {
for (const addressString of addresses) {
const address = Address.fromString(addressString)
const inputKey = await question(
const inputKey = (await question(
`Please enter the 0x-prefixed private key to unlock ${address}:\n`
)
)) as PrefixedHexString
;(rl as any).history = (rl as any).history.slice(1)
const privKey = toBytes(inputKey)
const privKey = hexToBytes(inputKey)
const derivedAddress = Address.fromPrivateKey(privKey)
if (address.equals(derivedAddress)) {
accounts.push([address, privKey])
Expand Down
13 changes: 7 additions & 6 deletions packages/client/src/rpc/modules/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UNKNOWN_PAYLOAD } from '../../error-code'

import type { Skeleton } from '../../../service'
import type { Block, ExecutionPayload } from '@ethereumjs/block'
import type { PrefixedHexString } from '@ethereumjs/util'

export enum Status {
ACCEPTED = 'ACCEPTED',
Expand All @@ -11,14 +12,14 @@ export enum Status {
VALID = 'VALID',
}

export type Bytes8 = string
export type Bytes20 = string
export type Bytes32 = string
export type Bytes8 = PrefixedHexString
export type Bytes20 = PrefixedHexString
export type Bytes32 = PrefixedHexString
// type Root = Bytes32
export type Blob = Bytes32
export type Bytes48 = string
export type Uint64 = string
export type Uint256 = string
export type Bytes48 = PrefixedHexString
export type Uint64 = PrefixedHexString
export type Uint256 = PrefixedHexString

type WithdrawalV1 = Exclude<ExecutionPayload['withdrawals'], undefined>[number]

Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/rpc/modules/engine/util/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { type ChainCache } from '../types'

import type { Chain } from '../../../../blockchain'
import type { Common } from '@ethereumjs/common'
import type { PrefixedHexString } from '@ethereumjs/util'

/**
* Recursively finds parent blocks starting from the parentHash.
Expand Down Expand Up @@ -77,7 +78,7 @@ export const validHash = async (
hash: Uint8Array,
chain: Chain,
chainCache: ChainCache
): Promise<string | null> => {
): Promise<PrefixedHexString | null> => {
const { remoteBlocks, executedBlocks, invalidBlocks, skeleton } = chainCache
const maxDepth = chain.config.engineParentLookupMaxDepth

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ export class Eth {
if (transaction.gasPrice === undefined && transaction.maxFeePerGas === undefined) {
// If no gas price or maxFeePerGas provided, use current block base fee for gas estimates
if (transaction.type !== undefined && parseInt(transaction.type) === 2) {
transaction.maxFeePerGas = '0x' + block.header.baseFeePerGas?.toString(16)
transaction.maxFeePerGas = `0x${block.header.baseFeePerGas?.toString(16)}`
} else if (block.header.baseFeePerGas !== undefined) {
transaction.gasPrice = '0x' + block.header.baseFeePerGas?.toString(16)
transaction.gasPrice = `0x${block.header.baseFeePerGas?.toString(16)}`
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/client/src/rpc/modules/web3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bytesToHex, toBytes } from '@ethereumjs/util'
import { bytesToHex, hexToBytes, toBytes } from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak'

import { getClientVersion } from '../../util'
Expand All @@ -8,6 +8,7 @@ import { middleware, validators } from '../validation'
import type { EthereumClient } from '../..'
import type { Chain } from '../../blockchain'
import type { Service } from '../../service'
import type { PrefixedHexString } from '@ethereumjs/util'

/**
* web3_* RPC module
Expand Down Expand Up @@ -43,8 +44,8 @@ export class Web3 {
* Returns Keccak-256 (not the standardized SHA3-256) of the given data
* @param params The data to convert into a SHA3 hash
*/
sha3(params: string[]) {
const hexEncodedDigest = bytesToHex(keccak256(toBytes(params[0])))
sha3(params: PrefixedHexString[]): PrefixedHexString {
const hexEncodedDigest = bytesToHex(keccak256(toBytes(hexToBytes(params[0]))))
return hexEncodedDigest
}
}
40 changes: 21 additions & 19 deletions packages/client/src/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import type { PrefixedHexString } from '@ethereumjs/util'

export interface RpcTx {
from?: string
to?: string
gas?: string
gasPrice?: string
value?: string
data?: string
maxPriorityFeePerGas?: string
maxFeePerGas?: string
type?: string
from?: PrefixedHexString
to?: PrefixedHexString
gas?: PrefixedHexString
gasPrice?: PrefixedHexString
value?: PrefixedHexString
data?: PrefixedHexString
maxPriorityFeePerGas?: PrefixedHexString
maxFeePerGas?: PrefixedHexString
type?: PrefixedHexString
}

export interface RpcTxRes {
from: string
to?: string
gas: string
gasPrice: string
value: string
input?: string
data?: string
maxPriorityFeePerGas: string
maxFeePerGas: string
type: string
from: PrefixedHexString
to?: PrefixedHexString
gas: PrefixedHexString
gasPrice: PrefixedHexString
value: PrefixedHexString
input?: PrefixedHexString
data?: PrefixedHexString
maxPriorityFeePerGas: PrefixedHexString
maxFeePerGas: PrefixedHexString
type: PrefixedHexString
}

/**
Expand Down
Loading
Loading