From fd2e4ea60cc461c32710cf14fbab653263251713 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Tue, 4 Apr 2023 11:36:02 -0400 Subject: [PATCH] Buffer to Uint8Array cleanup (#2607) * `Buffer` to `Uint8Array` conversion (#2566) * V7 update to master 1 (#2593) * Added v7 release reference in main README table (#2562) * common: Schedule shanghai on goerli (#2563) * common: Schedule shanghai on goerli * update timestamp * util/tx: Shift ssz back to case dependency free ES2019 compatible version (#2564) * util/tx: Shift ssz back to case dependency free ES2019 compatible version * update package lock * update karma ecma version * VM: some optimization on the bnadd/bnmul precompiles to only copy over the necessary 128 bytes as input for the WASM call (#2568) * EVM: Memory Fix & Other Optimizations (#2570) * EVM: Rename evm debug logger to evm:evm (one for package, one for class), consistency, also, logger will otherwise be left out when run with evm:* * VM: Rename message checkpoint to state checkpoint in debug message (there is a dedicated message checkpoint msg along msg logging) * EVM: CALL/CREATE debug exit msg differentiation * EVM: avoid buffer copy in memory read (performance) * EVM: Rewrite runCall() checkpoint/revert conditional for readability/simplification * EVM: Added EIP check for transient storage checkpointing * EVM: Precompile Debug Logger Improvements (#2572) * EVM: Added general precompile debug logger setup, first ECRECOVER exemplary debug logging * EVM: Added remaining precompile debug loggers * EVM: added error cases to BLS precompile debug log * EVM: Added missing precompile return value debug logs * Small fixes * tx: ensure eip3860 txs can have more than max_initcode_size data if to field is non-empty (#2575) * EVM: Avoid memory.read() Memory Copy (#2573) * EVM: added avoidCopy parameter to memory.read() function, first test on CREATE opcode * EVM: Add direct memory read to all calling opcodes * EVM: Copy over memory on IDENTITY precompile * EVM: remove length checks and return buffer 0-filling in Memory.read() (memory is uncoditionally being extended properly anyhow) * Some optimizations * blockchain: fix merge->clique transition (#2571) * Client: ensure safe/finalized blocks are part of the canonical chain on forkchoiceUpdated (#2577) * client/engine: ensure finalized/safe blocks are in canonical chain * client: engine-api: fix finalized block check * client/tests: fix forkchoice updated test * client: add fcu tests to check if blocks are part of canonical chain * client/engine: ensure payload has a valid timestamp forkchoiceUpdated (#2579) * client/engine: ensure invalid blockhash response matches spec (#2583) * client/engine: delete invalid skeleton blocks (#2584) Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * Setup to dev/test snapsync with sim architecture (#2574) * Setup to dev/test snapsync with sim architecture * modfiy single-run to setup a lodestar<>geth node to snapsync from * setup an ethereumjs inline client and get it to peer with geth * cleanup setup a bit * snapsync run spec * get the snap testdev sim working * finalize the test infra and update usage doc * enhance coverage * Use geth RPC to connect to ethJS * refac wait for snap sync completion --------- Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * client: Add safe and finalized blockoptions to the chain (#2585) * client: Add safe and finalized blockoptions to the chain * fix tests * fix more tests * fix remaining * cleanup * enhance coverage * unset scheduled goerli timestamp based hfs colliding with test * Client: Small Debug Helpers and CLI Improvements (#2586) * Client: new constant MAX_TOLERATED_BLOCK_TIME for execution, added warning for slowly executed blocks * Client -> Execution: NumBlocksPerIteration (default: 50) as an option * Client: only restart RLPx server or log peer stats if max peers is set to be greater than 0 * Apply suggestions from code review Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * Apply suggestions from code review --------- Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> * common: Schedule Shanghai on mainnet! (#2591) * common: Schedule Shanghai on mainnet! * clear hf timestamp for test * VM: Diff-based Touched Accounts Checkpointing (#2581) * VM: Switched to a more efficient diff-based way of touched account checkpointing * VM: move accessed storage inefficient checkpointing problem to berlin, haha * EVM: avoid memory copy in MLOAD opcode function * Remove console.log() in EVM * vmState: ensure touched accounts delete stack gets properly updated on commit * vm/eei: save touched height * vm/vmState: new possible fix for touched accounts * vm/vmState: another attempt to fix touched accounts journaling * vm: add journaling * Check correct journal height on revert --------- Co-authored-by: Jochem Brouwer Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com> --------- Co-authored-by: Holger Drewes Co-authored-by: g11tech Co-authored-by: Jochem Brouwer * First pass - most util changes * Fix most account stuff * Fix account test * Many byte fixes * util: fix constants tests * remaining fixes * Turn off ci jobs * monorepo: bigIntToUnpaddedBuffer -> bigIntToUnpaddedBytes * util: update description of bytes exporT * util: remove unused import * common: use bytesToHex helper instead of toString('hex') * trie: refactor non-test files to Uint8Array * util: add binary string utils * util: remove extra Uint8Array.from * util: remove arrToBufArray util * trie: adjust tests and fix outstanding issues * util: remove binarystring utils and add compareBytes and randomBytes util * common: refactor common with Uint8Array * util: accept 0x-prefixed and non-prefixed hex strings in toBytes * tx: refactor Buffer -> Uint8Array * tx: remove unused import * util: revert toBytes update * block: refactor Buffer -> uint8array * block: adjust import * trie: refactor remaining buffer instances * move devp2p to uint8Array * statemanager: refactor buffer -> uint8array * util: simplify zeros * util: add concatBytesUnsafe * ethash: partial migration * ethash: update examples * ethash: wip fixes * ethash: more WIP * ethash: ensure fnv input is read from mix, not mixView * blockchain: migrate to uint8array * ethash: renable all tests * ethash: fix bytesReverse * Fix miner tests * many hexToBytes moves * most of evm/vm moves * evm: Fix all tests * vm: more fixes * More fixes * vm: fix receipts encoding * vm: fix tester * client: refactor buffer to uint8array * client: additional uint8 adjustments * client: fix most tests * fix remaining client unit tests * reactivate most CI * client: fix les test/protocol * turn client CI on * util: fix name typo * lint * Fix withdrawals * remove buffarraytoarr * Remove bufArrtoArr references * Lint * fix examples * Fix difficulty test * lint * block: update randomBytes import * replace randombytes import * client: fix sim test util * vm: fix example * devp2p: update snappy typing and fix tests * Fix tests * Remove additional buffer references * rustbn fixes * add 0x prefix to precompile address * Remove `node-ip` dependency and buffer references in devp2p * Switch slice to subarray * evm: fix blake2f * Merge fixes * more merge commit fixes * more test fixes * Address all the feedback * fix dns test * Update packages/util/src/bytes.ts Co-authored-by: Jochem Brouwer * Fix return type for baToJSON * util: instantiate hexByByte array * Remove baToJson * rebase fixes * Fix event typing * Revert outdated initcode changes * lint --------- Co-authored-by: Holger Drewes Co-authored-by: g11tech Co-authored-by: Jochem Brouwer Co-authored-by: Gabriel Rocheleau * Devp2p status fix * Remove buffer detritus * Switch db to view * buffer cleanup * Correctly parse heads from DB * Fix encodings * Cast db values to uint8array * Fix db bug in ethash * Remove unused peerId check * Add pow miner test * Finish test * Move pow test to integration tests * client: lint * rename test helper * Add timeout to PoW test * Fix test runner * remove console log --------- Co-authored-by: Holger Drewes Co-authored-by: g11tech Co-authored-by: Jochem Brouwer Co-authored-by: Gabriel Rocheleau --- packages/blockchain/src/db/manager.ts | 13 ++- packages/blockchain/src/db/operation.ts | 2 +- packages/client/lib/config.ts | 2 +- packages/client/lib/execution/level.ts | 2 +- .../client/lib/net/protocol/snapprotocol.ts | 4 +- packages/client/lib/net/server/rlpxserver.ts | 8 +- .../client/lib/sync/fetcher/accountfetcher.ts | 2 +- packages/client/lib/util/metaDBManager.ts | 2 +- packages/client/test/integration/pow.spec.ts | 97 +++++++++++++++++++ .../test/sync/fetcher/storagefetcher.spec.ts | 2 +- packages/devp2p/examples/simple.ts | 2 +- packages/devp2p/src/protocol/eth.ts | 2 +- packages/devp2p/src/rlpx/peer.ts | 4 +- packages/devp2p/src/util.ts | 2 +- packages/ethash/src/index.ts | 3 + packages/ethash/test/miner.spec.ts | 12 +-- packages/evm/src/memory.ts | 2 +- .../evm/src/precompiles/0a-bls12-g1add.ts | 2 +- .../evm/src/precompiles/0b-bls12-g1mul.ts | 2 +- .../evm/src/precompiles/0d-bls12-g2add.ts | 2 +- .../evm/src/precompiles/0e-bls12-g2mul.ts | 2 +- .../evm/src/precompiles/util/bls12_381.ts | 14 +-- packages/trie/benchmarks/engines/level.ts | 2 +- packages/trie/recipes/level-legacy.ts | 2 +- packages/trie/recipes/level.ts | 2 +- packages/tx/src/types.ts | 6 +- packages/util/src/bytes.ts | 2 +- packages/util/src/types.ts | 2 +- packages/util/test/account.spec.ts | 32 +++--- packages/util/test/bytes.spec.ts | 4 +- .../tester/runners/GeneralStateTestsRunner.ts | 2 +- 31 files changed, 170 insertions(+), 67 deletions(-) create mode 100644 packages/client/test/integration/pow.spec.ts diff --git a/packages/blockchain/src/db/manager.ts b/packages/blockchain/src/db/manager.ts index d84911091a..ec099111ba 100644 --- a/packages/blockchain/src/db/manager.ts +++ b/packages/blockchain/src/db/manager.ts @@ -65,7 +65,11 @@ export class DBManager { async getHeads(): Promise<{ [key: string]: Uint8Array }> { const heads = await this.get(DBTarget.Heads) for (const key of Object.keys(heads)) { - heads[key] = Uint8Array.from(heads[key]) + // DB incorrectly stores the `uint8Array` representation of each head hash + // as a JSON object of key value pairs where the key is the array index + // and the value is the uint8 from that index of the original array + // so we convert it back to a Uint8Array before storing the heads + heads[key] = Uint8Array.from(Object.values(heads[key])) } return heads } @@ -213,13 +217,13 @@ export class DBManager { if (this._cache[cacheString] === undefined) { throw new Error(`Invalid cache: ${cacheString}`) } - let value = this._cache[cacheString].get(dbKey) if (!value) { value = await this._db.get(dbKey, dbOpts) - if (value) { - this._cache[cacheString].set(dbKey, value) + if (value !== undefined) { + // Always cast values to Uint8Array since db sometimes returns values as `Buffer` + this._cache[cacheString].set(dbKey, Uint8Array.from(value)) } } @@ -236,7 +240,6 @@ export class DBManager { const convertedOps: DBOpData[] = ops.map((op) => op.baseDBOp) // update the current cache for each operation ops.map((op) => op.updateCache(this._cache)) - return this._db.batch(convertedOps as any) } } diff --git a/packages/blockchain/src/db/operation.ts b/packages/blockchain/src/db/operation.ts index 4a6cfa9c1a..84730ba853 100644 --- a/packages/blockchain/src/db/operation.ts +++ b/packages/blockchain/src/db/operation.ts @@ -119,7 +119,7 @@ export class DBOp { if (operationTarget === DBTarget.Heads) { dbOperation.baseDBOp.valueEncoding = 'json' } else { - dbOperation.baseDBOp.valueEncoding = 'binary' + dbOperation.baseDBOp.valueEncoding = 'view' } return dbOperation diff --git a/packages/client/lib/config.ts b/packages/client/lib/config.ts index 571f64667f..13b2c0e582 100644 --- a/packages/client/lib/config.ts +++ b/packages/client/lib/config.ts @@ -539,7 +539,7 @@ export class Config { static async getClientKey(datadir: string, common: Common) { const networkDir = `${datadir}/${common.chainName()}` const db = this.getConfigDB(networkDir) - const encodingOpts = { keyEncoding: 'utf8', valueEncoding: 'buffer' } + const encodingOpts = { keyEncoding: 'utf8', valueEncoding: 'view' } const dbKey = 'config:client_key' let key try { diff --git a/packages/client/lib/execution/level.ts b/packages/client/lib/execution/level.ts index 08398f739d..78f88a5ad6 100644 --- a/packages/client/lib/execution/level.ts +++ b/packages/client/lib/execution/level.ts @@ -3,7 +3,7 @@ import { MemoryLevel } from 'memory-level' import type { BatchDBOp, DB } from '@ethereumjs/trie' import type { AbstractLevel } from 'abstract-level' -export const ENCODING_OPTS = { keyEncoding: 'buffer', valueEncoding: 'buffer' } +export const ENCODING_OPTS = { keyEncoding: 'view', valueEncoding: 'view' } /** * LevelDB is a thin wrapper around the underlying levelup db, diff --git a/packages/client/lib/net/protocol/snapprotocol.ts b/packages/client/lib/net/protocol/snapprotocol.ts index 6dc738d56c..92a212be08 100644 --- a/packages/client/lib/net/protocol/snapprotocol.ts +++ b/packages/client/lib/net/protocol/snapprotocol.ts @@ -87,8 +87,8 @@ export interface SnapProtocolMethods { slots: StorageData[][] proof: Uint8Array[] }> - getByteCodes: (opts: GetByteCodesOpts) => Promise<{ reqId: bigint; codes: Buffer[] }> - getTrieNodes: (opts: GetTrieNodesOpts) => Promise<{ reqId: bigint; nodes: Buffer[] }> + getByteCodes: (opts: GetByteCodesOpts) => Promise<{ reqId: bigint; codes: Uint8Array[] }> + getTrieNodes: (opts: GetTrieNodesOpts) => Promise<{ reqId: bigint; nodes: Uint8Array[] }> } /** diff --git a/packages/client/lib/net/server/rlpxserver.ts b/packages/client/lib/net/server/rlpxserver.ts index 9d5f90eb6f..7180f2e0d8 100644 --- a/packages/client/lib/net/server/rlpxserver.ts +++ b/packages/client/lib/net/server/rlpxserver.ts @@ -282,13 +282,7 @@ export class RlpxServer extends Server { } }) - this.rlpx.on('peer:error', (rlpxPeer: Devp2pRLPxPeer, error: Error) => { - const peerId = bytesToHex(rlpxPeer.getId() as Uint8Array) - if (peerId === null) { - return this.error(error) - } - this.error(error) - }) + this.rlpx.on('peer:error', (rlpxPeer: Devp2pRLPxPeer, error: Error) => this.error(error)) this.rlpx.on('error', (e: Error) => this.error(e)) diff --git a/packages/client/lib/sync/fetcher/accountfetcher.ts b/packages/client/lib/sync/fetcher/accountfetcher.ts index 00b8eee21c..39179ad670 100644 --- a/packages/client/lib/sync/fetcher/accountfetcher.ts +++ b/packages/client/lib/sync/fetcher/accountfetcher.ts @@ -322,7 +322,7 @@ export class AccountFetcher extends Fetcher // build record of accounts that need storage slots to be fetched const storageRoot: Uint8Array = account.body[2] instanceof Uint8Array ? account.body[2] : Uint8Array.from(account.body[2]) - if (equalsBytes(storageRoot, KECCAK256_RLP)) { + if (!equalsBytes(storageRoot, KECCAK256_RLP)) { storageFetchRequests.push({ accountHash: account.hash, storageRoot, diff --git a/packages/client/lib/util/metaDBManager.ts b/packages/client/lib/util/metaDBManager.ts index c20feb68bd..dc067a6547 100644 --- a/packages/client/lib/util/metaDBManager.ts +++ b/packages/client/lib/util/metaDBManager.ts @@ -4,7 +4,7 @@ import type { Chain } from '../blockchain' import type { Config } from '../config' import type { AbstractLevel } from 'abstract-level' -const encodingOpts = { keyEncoding: 'buffer', valueEncoding: 'buffer' } +const encodingOpts = { keyEncoding: 'view', valueEncoding: 'view' } /** * Number prepended to the db key to avoid collisions diff --git a/packages/client/test/integration/pow.spec.ts b/packages/client/test/integration/pow.spec.ts new file mode 100644 index 0000000000..5ff93dda3c --- /dev/null +++ b/packages/client/test/integration/pow.spec.ts @@ -0,0 +1,97 @@ +import { parseGethGenesisState } from '@ethereumjs/blockchain' +import { Common, Hardfork } from '@ethereumjs/common' +import { Address } from '@ethereumjs/util' +import { hexToBytes } from 'ethereum-cryptography/utils' +import { removeSync } from 'fs-extra' +import * as tape from 'tape' + +import { Config } from '../../lib' +import { createInlineClient } from '../sim/simutils' + +import type { EthereumClient } from '../../lib' + +const pk = hexToBytes('95a602ff1ae30a2243f400dcf002561b9743b2ae9827b1008e3714a5cc1c0cfe') +const minerAddress = Address.fromPrivateKey(pk) + +async function setupPowDevnet(prefundAddress: Address, cleanStart: boolean) { + if (cleanStart) { + removeSync(`datadir/devnet`) + } + const addr = prefundAddress.toString().slice(2) + const consensusConfig = { ethash: true } + + const defaultChainData = { + config: { + chainId: 123456, + homesteadBlock: 0, + eip150Block: 0, + eip150Hash: '0x0000000000000000000000000000000000000000000000000000000000000000', + eip155Block: 0, + eip158Block: 0, + byzantiumBlock: 0, + constantinopleBlock: 0, + petersburgBlock: 0, + istanbulBlock: 0, + berlinBlock: 0, + londonBlock: 0, + ...consensusConfig, + }, + nonce: '0x0', + timestamp: '0x614b3731', + gasLimit: '0x47b760', + difficulty: '0x1', + mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000', + coinbase: '0x0000000000000000000000000000000000000000', + number: '0x0', + gasUsed: '0x0', + parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', + baseFeePerGas: 7, + } + const extraData = '0x' + '0'.repeat(32) + const chainData = { + ...defaultChainData, + extraData, + alloc: { [addr]: { balance: '0x10000000000000000000' } }, + } + const common = Common.fromGethGenesis(chainData, { chain: 'devnet', hardfork: Hardfork.London }) + const customGenesisState = parseGethGenesisState(chainData) + + const config = new Config({ + common, + transports: ['rlpx'], + bootnodes: [], + multiaddrs: [], + discDns: false, + discV4: false, + port: 30304, + maxAccountRange: (BigInt(2) ** BigInt(256) - BigInt(1)) / BigInt(10), + maxFetcherJobs: 10, + datadir: 'devnet', + accounts: [[minerAddress, pk]], + mine: true, + }) + + const client = await createInlineClient(config, common, customGenesisState) + return client +} + +const mineBlockAndstopClient = async (client: EthereumClient, t: tape.Test) => { + await new Promise((resolve) => { + client.config.logger.on('data', (data) => { + if (data.message.includes('Miner: Found PoW solution') === true && client.started) { + t.pass('found a PoW solution') + void client.stop().then(() => { + t.ok(!client.started, 'client stopped successfully') + resolve(undefined) + }) + } + }) + }) +} + +tape('PoW client test', { timeout: 60000 }, async (t) => { + t.plan(3) + const client = await setupPowDevnet(minerAddress, true) + t.ok(client.started, 'client started successfully') + await mineBlockAndstopClient(client, t) +}) diff --git a/packages/client/test/sync/fetcher/storagefetcher.spec.ts b/packages/client/test/sync/fetcher/storagefetcher.spec.ts index 168b2fe0ed..6cc76198c1 100644 --- a/packages/client/test/sync/fetcher/storagefetcher.spec.ts +++ b/packages/client/test/sync/fetcher/storagefetcher.spec.ts @@ -306,7 +306,7 @@ tape('[StorageFetcher]', async (t) => { const dummyStorageRoot = hexToBytes( '39ed8daab7679c0b1b7cf3667c50108185d4d9d1431c24a1c35f696a58277f8f' ) - const dummyOrigin = Buffer.alloc(32) + const dummyOrigin = new Uint8Array(32) try { await fetcher['verifyRangeProof'](dummyStorageRoot, dummyOrigin, { slots, diff --git a/packages/devp2p/examples/simple.ts b/packages/devp2p/examples/simple.ts index 8cf80e3c1c..8a4c2fa870 100644 --- a/packages/devp2p/examples/simple.ts +++ b/packages/devp2p/examples/simple.ts @@ -1,6 +1,6 @@ import { Chain, Common } from '@ethereumjs/common' import chalk from 'chalk' -import { hexToBytes } from 'ethereum-cryptography/utils' +import { bytesToHex, hexToBytes } from 'ethereum-cryptography/utils' import { DPT } from '../src/index' diff --git a/packages/devp2p/src/protocol/eth.ts b/packages/devp2p/src/protocol/eth.ts index 1b88a27b04..74ffc3e8d5 100644 --- a/packages/devp2p/src/protocol/eth.ts +++ b/packages/devp2p/src/protocol/eth.ts @@ -227,7 +227,7 @@ export class ETH extends Protocol { _getStatusString(status: ETH.StatusMsg) { let sStr = `[V:${bytesToInt(status[0] as Uint8Array)}, NID:${bytesToInt( status[1] as Uint8Array - )}, TD:${status[2].length === 0 ? 0 : bytesToInt(status[2] as Uint8Array)}` + )}, TD:${status[2].length === 0 ? 0 : bytesToBigInt(status[2] as Uint8Array).toString()}` sStr += `, BestH:${formatLogId( bytesToHex(status[3] as Uint8Array), this._verbose diff --git a/packages/devp2p/src/rlpx/peer.ts b/packages/devp2p/src/rlpx/peer.ts index 06adf22e37..1df28c1689 100644 --- a/packages/devp2p/src/rlpx/peer.ts +++ b/packages/devp2p/src/rlpx/peer.ts @@ -587,11 +587,11 @@ export class Peer extends EventEmitter { // if (protocolName === 'Peer') { try { - payload = RLP.decode(Uint8Array.from(payload)) + payload = RLP.decode(payload) } catch (e: any) { if (msgCode === PREFIXES.DISCONNECT) { if (compressed) { - payload = RLP.decode(Uint8Array.from(origPayload)) + payload = RLP.decode(origPayload) } else { payload = RLP.decode(snappy.uncompress(payload)) } diff --git a/packages/devp2p/src/util.ts b/packages/devp2p/src/util.ts index fa0afc447c..a744dd96fd 100644 --- a/packages/devp2p/src/util.ts +++ b/packages/devp2p/src/util.ts @@ -126,7 +126,7 @@ export function toNewUint8Array(buf: Uint8Array): Uint8Array { /*************************** ************************************************************/ // Methods borrowed from `node-ip` by Fedor Indutny (https://github.com/indutny/node-ip) -// and modified to use Uint8Arrays instead of Buffers +// and modified to use Uint8Arrays instead of Uint8Arrays export const ipToString = (bytes: Uint8Array, offset?: number, length?: number) => { offset = offset !== undefined ? ~~offset : 0 length = length ?? bytes.length - offset diff --git a/packages/ethash/src/index.ts b/packages/ethash/src/index.ts index 88c7885214..7074101005 100644 --- a/packages/ethash/src/index.ts +++ b/packages/ethash/src/index.ts @@ -318,6 +318,9 @@ export class Ethash { let data try { data = await this.cacheDB!.get(epoc, this.dbOpts) + // Fix uint8Arrays that get stored in DB as JSON dictionary of array indices and values + data.seed = Uint8Array.from(Object.values(data.seed)) + data.cache = data.cache.map((el) => Uint8Array.from(Object.values(el))) } catch (error: any) { if (error.code !== 'LEVEL_NOT_FOUND') { throw error diff --git a/packages/ethash/test/miner.spec.ts b/packages/ethash/test/miner.spec.ts index 5bd1e035cc..2d01dd51c3 100644 --- a/packages/ethash/test/miner.spec.ts +++ b/packages/ethash/test/miner.spec.ts @@ -6,11 +6,11 @@ import * as tape from 'tape' import { Ethash } from '../src' import type { BlockHeader } from '@ethereumjs/block' - +const cacheDb = new MemoryLevel() const common = new Common({ chain: Chain.Ropsten, hardfork: Hardfork.Petersburg }) tape('Check if miner works as expected', async function (t) { - const e = new Ethash(new MemoryLevel()) + const e = new Ethash(cacheDb as any) const block = Block.fromBlockData( { @@ -54,7 +54,7 @@ tape('Check if miner works as expected', async function (t) { }) tape('Check if it is possible to mine Blocks and BlockHeaders', async function (t) { - const e = new Ethash(new MemoryLevel()) + const e = new Ethash(cacheDb as any) const block = Block.fromBlockData( { @@ -82,7 +82,7 @@ tape('Check if it is possible to mine Blocks and BlockHeaders', async function ( }) tape('Check if it is possible to stop the miner', async function (t) { - const e = new Ethash(new MemoryLevel()) + const e = new Ethash(cacheDb as any) const block = Block.fromBlockData( { @@ -104,7 +104,7 @@ tape('Check if it is possible to stop the miner', async function (t) { }) tape('Check if it is possible to stop the miner', async function (t) { - const e = new Ethash(new MemoryLevel()) + const e = new Ethash(cacheDb as any) const block: any = {} @@ -116,7 +116,7 @@ tape('Check if it is possible to stop the miner', async function (t) { }) tape('Should keep common when mining blocks or headers', async function (t) { - const e = new Ethash(new MemoryLevel()) + const e = new Ethash(cacheDb as any) const block = Block.fromBlockData( { diff --git a/packages/evm/src/memory.ts b/packages/evm/src/memory.ts index 85eafd0604..de06282c5d 100644 --- a/packages/evm/src/memory.ts +++ b/packages/evm/src/memory.ts @@ -75,7 +75,7 @@ export class Memory { return loaded } const returnBytes = new Uint8Array(size) - // Copy the stored "buffer" from memory into the return Buffer + // Copy the stored "buffer" from memory into the return Uint8Array returnBytes.set(loaded) return returnBytes diff --git a/packages/evm/src/precompiles/0a-bls12-g1add.ts b/packages/evm/src/precompiles/0a-bls12-g1add.ts index edbd32748c..1388851876 100644 --- a/packages/evm/src/precompiles/0a-bls12-g1add.ts +++ b/packages/evm/src/precompiles/0a-bls12-g1add.ts @@ -57,7 +57,7 @@ export async function precompile0a(opts: PrecompileInput): Promise { } } - // convert input to mcl G1 points, add them, and convert the output to a Buffer. + // convert input to mcl G1 points, add them, and convert the output to a Uint8Array. let mclPoint1 let mclPoint2 try { diff --git a/packages/evm/src/precompiles/0b-bls12-g1mul.ts b/packages/evm/src/precompiles/0b-bls12-g1mul.ts index b6253c48da..b006d88ea1 100644 --- a/packages/evm/src/precompiles/0b-bls12-g1mul.ts +++ b/packages/evm/src/precompiles/0b-bls12-g1mul.ts @@ -59,7 +59,7 @@ export async function precompile0b(opts: PrecompileInput): Promise { } } - // convert input to mcl G1 points, add them, and convert the output to a Buffer. + // convert input to mcl G1 points, add them, and convert the output to a Uint8Array. let mclPoint try { diff --git a/packages/evm/src/precompiles/0d-bls12-g2add.ts b/packages/evm/src/precompiles/0d-bls12-g2add.ts index 3fdb64351c..620bf61673 100644 --- a/packages/evm/src/precompiles/0d-bls12-g2add.ts +++ b/packages/evm/src/precompiles/0d-bls12-g2add.ts @@ -63,7 +63,7 @@ export async function precompile0d(opts: PrecompileInput): Promise { // TODO: verify that point is on G2 - // convert input to mcl G2 points, add them, and convert the output to a Buffer. + // convert input to mcl G2 points, add them, and convert the output to a Uint8Array. let mclPoint1 let mclPoint2 diff --git a/packages/evm/src/precompiles/0e-bls12-g2mul.ts b/packages/evm/src/precompiles/0e-bls12-g2mul.ts index 1b25251d65..546e1964a4 100644 --- a/packages/evm/src/precompiles/0e-bls12-g2mul.ts +++ b/packages/evm/src/precompiles/0e-bls12-g2mul.ts @@ -63,7 +63,7 @@ export async function precompile0e(opts: PrecompileInput): Promise { // TODO: verify that point is on G2 - // convert input to mcl G2 point/Fr point, add them, and convert the output to a Buffer. + // convert input to mcl G2 point/Fr point, add them, and convert the output to a Uint8Array. let mclPoint try { mclPoint = BLS12_381_ToG2Point(opts.data.subarray(0, 256), mcl) diff --git a/packages/evm/src/precompiles/util/bls12_381.ts b/packages/evm/src/precompiles/util/bls12_381.ts index 0fe8b79814..b307a51b6c 100644 --- a/packages/evm/src/precompiles/util/bls12_381.ts +++ b/packages/evm/src/precompiles/util/bls12_381.ts @@ -139,8 +139,8 @@ export const gasDiscountPairs = [ [127, 175], [128, 174], ] -// convert an input Buffer to a mcl G1 point -// this does /NOT/ do any input checks. the input Buffer needs to be of length 128 +// convert an input Uint8Array to a mcl G1 point +// this does /NOT/ do any input checks. the input Uint8Array needs to be of length 128 // it does raise an error if the point is not on the curve. function BLS12_381_ToG1Point(input: Uint8Array, mcl: any): any { const p_x = bytesToHex(input.subarray(16, 64)) @@ -178,7 +178,7 @@ function BLS12_381_ToG1Point(input: Uint8Array, mcl: any): any { } // input: a mcl G1 point -// output: a 128-byte Buffer +// output: a 128-byte Uint8Array function BLS12_381_FromG1Point(input: any): Uint8Array { // TODO: figure out if there is a better way to decode these values. const decodeStr = input.getStr(16) //return a string of pattern "1 " @@ -200,8 +200,8 @@ function BLS12_381_FromG1Point(input: any): Uint8Array { return concatBytesNoTypeCheck(xBuffer, yBuffer) } -// convert an input Buffer to a mcl G2 point -// this does /NOT/ do any input checks. the input Buffer needs to be of length 256 +// convert an input Uint8Array to a mcl G2 point +// this does /NOT/ do any input checks. the input Uint8Array needs to be of length 256 function BLS12_381_ToG2Point(input: Uint8Array, mcl: any): any { const p_x_1 = input.subarray(0, 64) const p_x_2 = input.subarray(64, 128) @@ -251,7 +251,7 @@ function BLS12_381_ToG2Point(input: Uint8Array, mcl: any): any { } // input: a mcl G2 point -// output: a 256-byte Buffer +// output: a 256-byte Uint8Array function BLS12_381_FromG2Point(input: any): Uint8Array { // TODO: figure out if there is a better way to decode these values. const decodeStr = input.getStr(16) //return a string of pattern "1 " @@ -276,7 +276,7 @@ function BLS12_381_FromG2Point(input: any): Uint8Array { return concatBytesNoTypeCheck(xBuffer1, xBuffer2, yBuffer1, yBuffer2) } -// input: a 32-byte hex scalar Buffer +// input: a 32-byte hex scalar Uint8Array // output: a mcl Fr point function BLS12_381_ToFrPoint(input: Uint8Array, mcl: any): any { diff --git a/packages/trie/benchmarks/engines/level.ts b/packages/trie/benchmarks/engines/level.ts index e172c9adfe..0064b15e2c 100644 --- a/packages/trie/benchmarks/engines/level.ts +++ b/packages/trie/benchmarks/engines/level.ts @@ -4,7 +4,7 @@ import { MemoryLevel } from 'memory-level' import type { BatchDBOp, DB } from '../../src/types' import type { AbstractLevel } from 'abstract-level' -export const ENCODING_OPTS = { keyEncoding: 'Uint8Array', valueEncoding: 'Uint8Array' } +export const ENCODING_OPTS = { keyEncoding: 'view', valueEncoding: 'view' } /** * LevelDB is a thin wrapper around the underlying levelup db, diff --git a/packages/trie/recipes/level-legacy.ts b/packages/trie/recipes/level-legacy.ts index 0040f87269..ff37097c8b 100644 --- a/packages/trie/recipes/level-legacy.ts +++ b/packages/trie/recipes/level-legacy.ts @@ -3,7 +3,7 @@ import level from 'level-mem' import type { BatchDBOp, DB } from '@ethereumjs/trie' import type { LevelUp } from 'levelup' -const ENCODING_OPTS = { keyEncoding: 'binary', valueEncoding: 'binary' } +const ENCODING_OPTS = { keyEncoding: 'view', valueEncoding: 'view' } export class LevelDB implements DB { readonly _leveldb: LevelUp diff --git a/packages/trie/recipes/level.ts b/packages/trie/recipes/level.ts index 6a7744a441..6d6814d70d 100644 --- a/packages/trie/recipes/level.ts +++ b/packages/trie/recipes/level.ts @@ -3,7 +3,7 @@ import { MemoryLevel } from 'memory-level' import type { BatchDBOp, DB } from '@ethereumjs/trie' import type { AbstractLevel } from 'abstract-level' -const ENCODING_OPTS = { keyEncoding: 'buffer', valueEncoding: 'buffer' } +const ENCODING_OPTS = { keyEncoding: 'view', valueEncoding: 'view' } export class LevelDB implements DB { readonly _leveldb: AbstractLevel< diff --git a/packages/tx/src/types.ts b/packages/tx/src/types.ts index ebc95b241f..ff1187624f 100644 --- a/packages/tx/src/types.ts +++ b/packages/tx/src/types.ts @@ -258,12 +258,12 @@ export interface BlobEIP4844TxData extends FeeMarketEIP1559TxData { } /** - * Buffer values array for a legacy {@link Transaction} + * Bytes values array for a legacy {@link Transaction} */ export type TxValuesArray = Uint8Array[] /** - * Buffer values array for an {@link AccessListEIP2930Transaction} + * Bytes values array for an {@link AccessListEIP2930Transaction} */ export type AccessListEIP2930ValuesArray = [ Uint8Array, @@ -280,7 +280,7 @@ export type AccessListEIP2930ValuesArray = [ ] /** - * Buffer values array for a {@link FeeMarketEIP1559Transaction} + * Bytes values array for a {@link FeeMarketEIP1559Transaction} */ export type FeeMarketEIP1559ValuesArray = [ Uint8Array, diff --git a/packages/util/src/bytes.ts b/packages/util/src/bytes.ts index e276b85b95..faaec5d480 100644 --- a/packages/util/src/bytes.ts +++ b/packages/util/src/bytes.ts @@ -334,7 +334,7 @@ export const toUtf8 = function (hex: string): string { * * Note: This method is useful for validating that RLP encoded integers comply with the rule that all * integer values encoded to RLP must be in the most compact form and contain no leading zero bytes - * @param values An object containing string keys and Buffer values + * @param values An object containing string keys and Uint8Array values * @throws if any provided value is found to have leading zero bytes */ export const validateNoLeadingZeroes = function (values: { diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 1c4c6e4a4d..144672eb3c 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -12,7 +12,7 @@ import type { ToBytesInputTypes } from './bytes' export type BigIntLike = bigint | PrefixedHexString | number | Uint8Array /* - * A type that represents an input that can be converted to a Buffer. + * A type that represents an input that can be converted to a Uint8Array. */ export type BytesLike = | Uint8Array diff --git a/packages/util/test/account.spec.ts b/packages/util/test/account.spec.ts index 082d3a477b..d2856eb2fa 100644 --- a/packages/util/test/account.spec.ts +++ b/packages/util/test/account.spec.ts @@ -197,7 +197,7 @@ tape('Utility Functions', function (t) { st.notOk(isValidPrivate(hexToBytes(tmp)), 'should fail on too big input') st.notOk( - isValidPrivate(('WRONG_INPUT_TYPE') as Buffer), + isValidPrivate(('WRONG_INPUT_TYPE') as Uint8Array), 'should fail on wrong input type' ) @@ -311,8 +311,8 @@ tape('Utility Functions', function (t) { ) st.throws(function () { - importPublic((pubKey) as Buffer) - }, 'should throw if input is not Buffer') + importPublic((pubKey) as Uint8Array) + }, 'should throw if input is not Uint8Array') st.end() }) @@ -429,12 +429,18 @@ tape('Utility Functions', function (t) { t.test('generateAddress wt.testh non-buffer inputs', function (st) { st.throws(function () { - generateAddress(('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39') as Buffer, toBytes(0)) - }, 'should throw if address is not Buffer') + generateAddress( + ('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39') as Uint8Array, + toBytes(0) + ) + }, 'should throw if address is not Uint8Array') st.throws(function () { - generateAddress(toBytes('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39'), (0) as Buffer) - }, 'should throw if nonce is not Buffer') + generateAddress( + toBytes('0x990ccf8a0de58091c028d6ff76bb235ee67c1c39'), + (0) as Uint8Array + ) + }, 'should throw if nonce is not Uint8Array') st.end() }) @@ -455,16 +461,16 @@ tape('Utility Functions', function (t) { const { address, salt, initCode } = eip1014Testdata[0] st.throws(function () { - generateAddress2((address) as Buffer, toBytes(salt), toBytes(initCode)) - }, 'should throw if address is not Buffer') + generateAddress2((address) as Uint8Array, toBytes(salt), toBytes(initCode)) + }, 'should throw if address is not Uint8Array') st.throws(function () { - generateAddress2(toBytes(address), (salt) as Buffer, toBytes(initCode)) - }, 'should throw if salt is not Buffer') + generateAddress2(toBytes(address), (salt) as Uint8Array, toBytes(initCode)) + }, 'should throw if salt is not Uint8Array') st.throws(function () { - generateAddress2(toBytes(address), toBytes(salt), (initCode) as Buffer) - }, 'should throw if initCode is not Buffer') + generateAddress2(toBytes(address), toBytes(salt), (initCode) as Uint8Array) + }, 'should throw if initCode is not Uint8Array') st.end() }) diff --git a/packages/util/test/bytes.spec.ts b/packages/util/test/bytes.spec.ts index 497ff4a2e0..ec7070f50c 100644 --- a/packages/util/test/bytes.spec.ts +++ b/packages/util/test/bytes.spec.ts @@ -123,7 +123,7 @@ tape('setLengthLeft', function (t) { }) t.test('should throw if input is not a Uint8Array', function (st) { st.throws(function () { - setLengthLeft(([9, 9]) as Buffer, 3) + setLengthLeft(([9, 9]) as Uint8Array, 3) }) st.end() }) @@ -273,7 +273,7 @@ tape('toUtf8', function (t) { tape('toBytes', function (t) { t.test('should work', function (st) { - // Buffer + // Uint8Array st.ok(equalsBytes(toBytes(new Uint8Array(0)), new Uint8Array())) // Array st.ok(equalsBytes(toBytes([]), new Uint8Array())) diff --git a/packages/vm/test/tester/runners/GeneralStateTestsRunner.ts b/packages/vm/test/tester/runners/GeneralStateTestsRunner.ts index d86fe02f53..00ba0b97f0 100644 --- a/packages/vm/test/tester/runners/GeneralStateTestsRunner.ts +++ b/packages/vm/test/tester/runners/GeneralStateTestsRunner.ts @@ -100,7 +100,7 @@ async function runTestCase(options: any, testData: any, t: tape.Test) { } // Even if no txs are ran, coinbase should always be created - const coinbaseAddress = new Address(testData.env.currentCoinbase.slice(2)) + const coinbaseAddress = Address.fromString(testData.env.currentCoinbase) const account = await (vm).eei.getAccount(coinbaseAddress) await (vm).eei.putAccount(coinbaseAddress, account)