Skip to content

Commit

Permalink
develop: ci fix, some last backwards compatibility removals (#1834)
Browse files Browse the repository at this point in the history
* move ethash depBrowserWorkaround fix for bigint-crypto-utils to blockchain karma.conf.js
  (karma has trouble with the default browser esm import without es6 transform, so it is easier to tell it to use umd)

* new DefaultStateManager will init a trie if not supplied, so we don't need to init one here

* remove backwards compatibility note (we will keep this property)

* trie: remove old backwards compat methods

* client: do undefined check first

* vm: undo doc change

* client: improve receipt size calculation

* vm: resolve more todos

* vm: more hardfork enum usage

* dedupe receipt rlp encoding

* lint

* runCall, runCode: add more param typedocs, simplify

* also add karma workaround to vm
  • Loading branch information
ryanio authored and g11tech committed Apr 29, 2022
1 parent e6dc50f commit 245d3b6
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 123 deletions.
11 changes: 8 additions & 3 deletions packages/blockchain/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function(config) {
module.exports = function (config) {
config.set({
browserNoActivityTimeout: 60000,
frameworks: ['karma-typescript', 'tap'],
Expand All @@ -11,8 +11,13 @@ module.exports = function(config) {
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
ecmaVersion: 11,
},
resolve: {
alias: {
'bigint-crypto-utils': '../../node_modules/bigint-crypto-utils/dist/bundles/umd.js',
},
},
},
},
concurrency: 1,
Expand Down
16 changes: 3 additions & 13 deletions packages/client/lib/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
BlockBodyBuffer,
} from '@ethereumjs/block'
import { TransactionFactory, TypedTransaction } from '@ethereumjs/tx'
import { bigIntToBuffer, bufferToBigInt, bufferToInt, intToBuffer, rlp } from 'ethereumjs-util'
import { encodeReceipt } from '@ethereumjs/vm/dist/runBlock'
import { bigIntToBuffer, bufferToBigInt, bufferToInt, rlp } from 'ethereumjs-util'
import { Chain } from './../../blockchain'
import { Message, Protocol, ProtocolOptions } from './protocol'
import type { TxReceiptWithType } from '../../execution/receipt'
Expand Down Expand Up @@ -230,18 +231,7 @@ export class EthProtocol extends Protocol {
encode: ({ reqId, receipts }: { reqId: bigint; receipts: TxReceiptWithType[] }) => {
const serializedReceipts = []
for (const receipt of receipts) {
let encodedReceipt = rlp.encode([
(receipt as PreByzantiumTxReceipt).stateRoot ??
(receipt as PostByzantiumTxReceipt).status,
bigIntToBuffer(receipt.gasUsed),
receipt.bitvector,
receipt.logs,
])
if (receipt.txType > 0) {
// Serialize receipt according to EIP-2718:
// `typed-receipt = tx-type || receipt-data`
encodedReceipt = Buffer.concat([intToBuffer(receipt.txType), encodedReceipt])
}
const encodedReceipt = encodeReceipt(receipt, receipt.txType)
serializedReceipts.push(encodedReceipt)
}
return [bigIntToBuffer(reqId), serializedReceipts]
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/service/fullethereumservice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hardfork } from '@ethereumjs/common'
import { bigIntToBuffer } from 'ethereumjs-util'
import { encodeReceipt } from '@ethereumjs/vm/dist/runBlock'
import { EthereumService, EthereumServiceOptions } from './ethereumservice'
import { TxPool } from './txpool'
import { FullSynchronizer } from '../sync/fullsync'
Expand Down Expand Up @@ -215,10 +215,10 @@ export class FullEthereumService extends EthereumService {
let receiptsSize = 0
for (const hash of hashes) {
const blockReceipts = await receiptsManager.getReceipts(hash, true, true)
blockReceipts.forEach((r) => (r.gasUsed = bigIntToBuffer(r.gasUsed) as any))
if (!blockReceipts) continue
receipts.push(...blockReceipts)
receiptsSize += Buffer.byteLength(JSON.stringify(blockReceipts))
const receiptsBuffer = Buffer.concat(receipts.map((r) => encodeReceipt(r, r.txType)))
receiptsSize += Buffer.byteLength(receiptsBuffer)
// From spec: The recommended soft limit for Receipts responses is 2 MiB.
if (receiptsSize >= 2097152) {
break
Expand Down
2 changes: 0 additions & 2 deletions packages/ethash/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"types": "dist/index.d.ts",
"browser": "dist.browser/index.js",
"scripts": {
"postinstall": "npm run depBrowserWorkaround",
"depBrowserWorkaround": "npx json -I -f ../../node_modules/bigint-crypto-utils/package.json -e \"this.browser='./dist/bundles/umd.js'\"",
"build": "npm run build:node && npm run build:browser",
"build:node": "../../config/cli/ts-build.sh node",
"build:browser": "../../config/cli/ts-build.sh browser",
Expand Down
25 changes: 3 additions & 22 deletions packages/trie/src/baseTrie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Trie {
*/
async checkRoot(root: Buffer): Promise<boolean> {
try {
const value = await this._lookupNode(root)
const value = await this.lookupNode(root)
return value !== null
} catch (error: any) {
if (error.message == 'Missing node in DB') {
Expand Down Expand Up @@ -244,7 +244,7 @@ export class Trie {
}
}

// Resolve if _walkTrie finishes without finding any nodes
// Resolve if walkTrie finishes without finding any nodes
resolve({ node: null, remaining: [], stack })
})
}
Expand All @@ -259,16 +259,6 @@ export class Trie {
await WalkController.newWalk(onFound, this, root)
}

/**
* @hidden
* Backwards compatibility
* @param root -
* @param onFound -
*/
async _walkTrie(root: Buffer, onFound: FoundNodeFunction): Promise<void> {
await this.walkTrie(root, onFound)
}

/**
* Creates the initial node from an empty tree.
* @private
Expand Down Expand Up @@ -298,15 +288,6 @@ export class Trie {
return foundNode
}

/**
* @hidden
* Backwards compatibility
* @param node The node hash to lookup from the DB
*/
async _lookupNode(node: Buffer | Buffer[]): Promise<TrieNode | null> {
return this.lookupNode(node)
}

/**
* Updates a node.
* @private
Expand Down Expand Up @@ -517,7 +498,7 @@ export class Trie {
const branchNodeKey = branchNodes[0][0]

// look up node
const foundNode = await this._lookupNode(branchNode)
const foundNode = await this.lookupNode(branchNode)
if (foundNode) {
key = processBranchNode(
key,
Expand Down
4 changes: 2 additions & 2 deletions packages/trie/src/util/walkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class WalkController {
this.reject = reject
let node
try {
node = await this.trie._lookupNode(root)
node = await this.trie.lookupNode(root)
} catch (error: any) {
return this.reject(error)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ export class WalkController {
async (taskFinishedCallback: Function) => {
let childNode
try {
childNode = await this.trie._lookupNode(nodeRef)
childNode = await this.trie.lookupNode(nodeRef)
} catch (error: any) {
return this.reject(error)
}
Expand Down
9 changes: 7 additions & 2 deletions packages/vm/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ module.exports = function (config) {
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
ecmaVersion: 11,
},
resolve: {
alias: {
'bigint-crypto-utils': '../../node_modules/bigint-crypto-utils/dist/bundles/umd.js',
},
},
},
},

Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/buildBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class BlockBuilder {
const receiptTrie = new Trie()
for (const [i, txResult] of this.transactionResults.entries()) {
const tx = this.transactions[i]
const encodedReceipt = encodeReceipt(tx, txResult.receipt)
const encodedReceipt = encodeReceipt(txResult.receipt, tx.type)
await receiptTrie.put(rlp.encode(i), encodedReceipt)
}
return receiptTrie.root
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface ExecResult {
*/
gas?: bigint
/**
* Amount of gas the transaction used to run
* Amount of gas the code used to run
*/
gasUsed: bigint
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/src/evm/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export default class Interpreter {
address: this._eei._env.address,
account: this._eei._env.contract,
stateManager: this._runState.stateManager,
memory: this._runState.memory._store, // Return underlying array for backwards-compatibility
memory: this._runState.memory._store,
memoryWordCount: this._runState.memoryWordCount,
codeAddress: this._eei._env.codeAddress,
}
Expand Down
16 changes: 8 additions & 8 deletions packages/vm/src/evm/opcodes/codes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common from '@ethereumjs/common'
import Common, { Hardfork } from '@ethereumjs/common'
import { CustomOpcode } from '../types'
import { getFullname } from './util'
import { AsyncDynamicGasHandler, dynamicGasHandlers, SyncDynamicGasHandler } from './gas'
Expand Down Expand Up @@ -203,15 +203,15 @@ const opcodes: OpcodeEntry = {
// If the base gas cost of any of the operations change, then these should also be added to this list.
// If there are context variables changed (such as "warm slot reads") which are not the base gas fees,
// Then this does not have to be added.
const hardforkOpcodes: { hardforkName: string; opcodes: OpcodeEntry }[] = [
const hardforkOpcodes: { hardfork: Hardfork; opcodes: OpcodeEntry }[] = [
{
hardforkName: 'homestead',
hardfork: Hardfork.Homestead,
opcodes: {
0xf4: { name: 'DELEGATECALL', isAsync: true, dynamicGas: true }, // EIP 7
},
},
{
hardforkName: 'tangerineWhistle',
hardfork: Hardfork.TangerineWhistle,
opcodes: {
0x54: { name: 'SLOAD', isAsync: true, dynamicGas: true },
0xf1: { name: 'CALL', isAsync: true, dynamicGas: true },
Expand All @@ -224,7 +224,7 @@ const hardforkOpcodes: { hardforkName: string; opcodes: OpcodeEntry }[] = [
},
},
{
hardforkName: 'byzantium',
hardfork: Hardfork.Byzantium,
opcodes: {
0xfd: { name: 'REVERT', isAsync: false, dynamicGas: true }, // EIP 140
0xfa: { name: 'STATICCALL', isAsync: true, dynamicGas: true }, // EIP 214
Expand All @@ -233,7 +233,7 @@ const hardforkOpcodes: { hardforkName: string; opcodes: OpcodeEntry }[] = [
},
},
{
hardforkName: 'constantinople',
hardfork: Hardfork.Constantinople,
opcodes: {
0x1b: { name: 'SHL', isAsync: false, dynamicGas: false }, // EIP 145
0x1c: { name: 'SHR', isAsync: false, dynamicGas: false }, // EIP 145
Expand All @@ -243,7 +243,7 @@ const hardforkOpcodes: { hardforkName: string; opcodes: OpcodeEntry }[] = [
},
},
{
hardforkName: 'istanbul',
hardfork: Hardfork.Istanbul,
opcodes: {
0x46: { name: 'CHAINID', isAsync: false, dynamicGas: false }, // EIP 1344
0x47: { name: 'SELFBALANCE', isAsync: false, dynamicGas: false }, // EIP 1884
Expand Down Expand Up @@ -330,7 +330,7 @@ export function getOpcodesForHF(common: Common, customOpcodes?: CustomOpcode[]):
const dynamicGasHandlersCopy = new Map(dynamicGasHandlers)

for (let fork = 0; fork < hardforkOpcodes.length; fork++) {
if (common.gteHardfork(hardforkOpcodes[fork].hardforkName)) {
if (common.gteHardfork(hardforkOpcodes[fork].hardfork)) {
opcodeBuilder = { ...opcodeBuilder, ...hardforkOpcodes[fork].opcodes }
}
}
Expand Down
25 changes: 12 additions & 13 deletions packages/vm/src/evm/opcodes/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Address, bigIntToBuffer, setLengthLeft } from 'ethereumjs-util'
import { ERROR } from '../../exceptions'
import { RunState } from '../interpreter'
import Common from '@ethereumjs/common'
import Common, { Hardfork } from '@ethereumjs/common'
import { updateSstoreGasEIP1283 } from './EIP1283'
import { updateSstoreGasEIP2200 } from './EIP2200'
import { accessAddressEIP2929, accessStorageEIP2929 } from './EIP2929'
Expand Down Expand Up @@ -201,18 +201,17 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
value = bigIntToBuffer(val)
}

// TODO: Replace getContractStorage with EEI method
const currentStorage = setLengthLeftStorage(await runState.eei.storageLoad(keyBuf))
const originalStorage = setLengthLeftStorage(await runState.eei.storageLoad(keyBuf, true))
if (common.hardfork() === 'constantinople') {
if (common.hardfork() === Hardfork.Constantinople) {
gas += updateSstoreGasEIP1283(
runState,
currentStorage,
originalStorage,
setLengthLeftStorage(value),
common
)
} else if (common.gteHardfork('istanbul')) {
} else if (common.gteHardfork(Hardfork.Istanbul)) {
gas += updateSstoreGasEIP2200(
runState,
currentStorage,
Expand Down Expand Up @@ -300,7 +299,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
gas += BigInt(common.param('gasPrices', 'callValueTransfer'))
}

if (common.gteHardfork('spuriousDragon')) {
if (common.gteHardfork(Hardfork.SpuriousDragon)) {
// We are at or after Spurious Dragon
// Call new account gas: account is DEAD and we transfer nonzero value
if ((await runState.eei.isAccountEmpty(toAddress)) && !(value === BigInt(0))) {
Expand Down Expand Up @@ -329,9 +328,9 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
}

if (value !== BigInt(0)) {
// TODO: Don't use private attr directly
runState.eei._gasLeft += BigInt(common.param('gasPrices', 'callStipend'))
gasLimit += BigInt(common.param('gasPrices', 'callStipend'))
const callStipend = BigInt(common.param('gasPrices', 'callStipend'))
runState.eei.addStipend(callStipend)
gasLimit += callStipend
}

runState.messageGasLimit = gasLimit
Expand Down Expand Up @@ -368,9 +367,9 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
trap(ERROR.OUT_OF_GAS)
}
if (value !== BigInt(0)) {
// TODO: Don't use private attr directly
runState.eei._gasLeft += BigInt(common.param('gasPrices', 'callStipend'))
gasLimit += BigInt(common.param('gasPrices', 'callStipend'))
const callStipend = BigInt(common.param('gasPrices', 'callStipend'))
runState.eei.addStipend(callStipend)
gasLimit += callStipend
}

runState.messageGasLimit = gasLimit
Expand Down Expand Up @@ -543,7 +542,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami

const selfdestructToAddress = new Address(addressToBuffer(selfdestructToaddressBigInt))
let deductGas = false
if (common.gteHardfork('spuriousDragon')) {
if (common.gteHardfork(Hardfork.SpuriousDragon)) {
// EIP-161: State Trie Clearing
const balance = await runState.eei.getExternalBalance(runState.eei.getAddress())
if (balance > BigInt(0)) {
Expand All @@ -554,7 +553,7 @@ export const dynamicGasHandlers: Map<number, AsyncDynamicGasHandler | SyncDynami
deductGas = true
}
}
} else if (common.gteHardfork('tangerineWhistle')) {
} else if (common.gteHardfork(Hardfork.TangerineWhistle)) {
// EIP-150 (Tangerine Whistle) gas semantics
const exists = await runState.stateManager.accountExists(selfdestructToAddress)
if (!exists) {
Expand Down
5 changes: 2 additions & 3 deletions packages/vm/src/evm/opcodes/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Common from '@ethereumjs/common'
import Common, { Hardfork } from '@ethereumjs/common'
import { keccak256, setLengthRight, setLengthLeft, bigIntToBuffer } from 'ethereumjs-util'
import { ERROR, VmError } from './../../exceptions'
import { RunState } from './../interpreter'
Expand Down Expand Up @@ -147,8 +147,7 @@ export function maxCallGas(
runState: RunState,
common: Common
): bigint {
const isTangerineWhistleOrLater = common.gteHardfork('tangerineWhistle')
if (isTangerineWhistleOrLater) {
if (common.gteHardfork(Hardfork.TangerineWhistle)) {
const gasAllowed = gasLeft - gasLeft / BigInt(64)
return gasLimit > gasAllowed ? gasAllowed : gasLimit
} else {
Expand Down
Loading

0 comments on commit 245d3b6

Please sign in to comment.