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

VM: Replace BN.js with bigints, Util helpers #1671

Merged
merged 42 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3ae28cc
Replace BN.js with bigints in VM
acolytec3 Jan 26, 2022
b1e0b32
Fix byteLength computation in EXP handler
acolytec3 Jan 27, 2022
3252545
Fix toTwos helper
acolytec3 Jan 27, 2022
d04c929
Compute TWO_POWE256 using math instead of hex
acolytec3 Jan 27, 2022
b063cd7
Update packages/util/src/constants.ts
acolytec3 Jan 27, 2022
8524365
Remove unused variable
acolytec3 Jan 27, 2022
79d1762
Fix exponent byte length calc
acolytec3 Jan 27, 2022
c63db90
Fix exp overflow
acolytec3 Jan 28, 2022
c52b9e4
Fix precompile bigint conversions
acolytec3 Jan 28, 2022
74ce838
Fix more bigint conversions
acolytec3 Jan 28, 2022
69ca2b7
Fix EXP opcode handler
acolytec3 Jan 28, 2022
f058ad4
Fix logic bug in signextend
acolytec3 Jan 28, 2022
3b44909
vm/gas: fix EXTCODECOPY gas
Jan 31, 2022
f217873
vm/gas: fix sha256 gas
jochem-brouwer Jan 31, 2022
0b78dba
vm/gas: sha256 const -> let
jochem-brouwer Jan 31, 2022
02c0d06
vm: lint
jochem-brouwer Jan 31, 2022
8a99248
vm/logic: fix sdiv/smod and fromTwos/toTwos
jochem-brouwer Jan 31, 2022
df382e0
vm/logic: fix SIGNEXTEND
jochem-brouwer Jan 31, 2022
a4e1a94
vm/logic: fix CALLDATALOAD padding
jochem-brouwer Feb 1, 2022
f71290a
vm/logic: remove weird comment
jochem-brouwer Feb 1, 2022
82a692f
Fix SAR opcode handler
acolytec3 Feb 1, 2022
6c59171
Use bufferToBigInt in Push opcode handler
acolytec3 Feb 1, 2022
b3c98d5
use bufferToBigInt everywhere
acolytec3 Feb 1, 2022
bb9247e
Fix missing bufferToBigInt import
acolytec3 Feb 1, 2022
7633ab9
Check for edge case in expmod
acolytec3 Feb 1, 2022
63670b2
Ignore prettier
acolytec3 Feb 3, 2022
29d3440
Update browser tsconfig to es2020 lib
acolytec3 Feb 3, 2022
cf99438
Remove dup ES2020 targets
acolytec3 Feb 4, 2022
83d3552
attempt to dedupe "target" and "lib" tsconfig values
ryanio Feb 4, 2022
a9226f1
Update karma config to target es2020 in parser
acolytec3 Feb 5, 2022
e6ea025
Various test fixes
acolytec3 Feb 7, 2022
ca94dd7
Lint and BN fixes
acolytec3 Feb 7, 2022
993332a
Add bigint helpers to util
acolytec3 Feb 8, 2022
829bd5b
lint fixes
acolytec3 Feb 8, 2022
eed7d57
various bigint helper additions
acolytec3 Feb 9, 2022
65cc36c
Lint fixes
acolytec3 Feb 9, 2022
0cce60f
Fix bnToBigInt
acolytec3 Feb 9, 2022
f25514c
Lint/test fixes
acolytec3 Feb 9, 2022
d7387f4
Switch Xn to BigInt(X)
acolytec3 Feb 22, 2022
05ccc20
lint
acolytec3 Feb 22, 2022
c6b0e70
lint
acolytec3 Feb 22, 2022
1ddcc23
More Xn to BigInt(X) moves
acolytec3 Feb 22, 2022
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
2 changes: 1 addition & 1 deletion config/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'implicit-dependencies', 'prettier'],
env: {
es6: true,
es2020: true,
node: true,
},
ignorePatterns: [
Expand Down
3 changes: 1 addition & 2 deletions config/tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5",
"lib": ["dom", "es5"]
"lib": ["dom", "ES2020"]
}
}
4 changes: 2 additions & 2 deletions config/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"resolveJsonModule": true,
"downlevelIteration": true,
"strict": true,
"target": "ES2017",
"lib": ["es2018"]
"target": "es2020",
"lib": ["ES2020"]
}
}
3 changes: 3 additions & 0 deletions packages/block/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function(config) {
tsconfig: './tsconfig.json',
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
},
},
concurrency: 1,
Expand Down
3 changes: 3 additions & 0 deletions packages/blockchain/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function(config) {
tsconfig: './tsconfig.json',
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
},
},
concurrency: 1,
Expand Down
3 changes: 3 additions & 0 deletions packages/client/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module.exports = function (config) {
karmaTypescriptConfig: {
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
},
resolve: {
alias: {
// Hotfix for `multiformats` client browser build error in Node 16, #1346, 2021-07-12
Expand Down
6 changes: 4 additions & 2 deletions packages/client/lib/miner/miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ export class Miner {
await blockBuilder.addTransaction(txs[index])
} catch (error: any) {
if (error.message === 'tx has a higher gas limit than the remaining gas in the block') {
if (blockBuilder.gasUsed.gt(gasLimit.subn(21000))) {
if (blockBuilder.gasUsed > BigInt(gasLimit.subn(21000).toString(10))) {
// If block has less than 21000 gas remaining, consider it full
blockFull = true
this.config.logger.info(
`Miner: Assembled block full (gasLeft: ${gasLimit.sub(blockBuilder.gasUsed)})`
`Miner: Assembled block full (gasLeft: ${gasLimit.sub(
new BN(blockBuilder.gasUsed.toString(10))
)})`
)
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions packages/client/lib/rpc/modules/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,13 @@ export class Engine {
await blockBuilder.addTransaction(txs[index])
} catch (error: any) {
if (error.message === 'tx has a higher gas limit than the remaining gas in the block') {
if (blockBuilder.gasUsed.gt(gasLimit.subn(21000))) {
if (blockBuilder.gasUsed > BigInt(gasLimit.subn(21000).toString(10))) {
// If block has less than 21000 gas remaining, consider it full
blockFull = true
this.config.logger.info(
`Engine: Assembled block full (gasLeft: ${gasLimit.sub(blockBuilder.gasUsed)})`
// eslint-disable-next-line prettier/prettier
`Engine: Assembled block full (gasLeft: ${BigInt(`0x ${gasLimit.toString('hex')}`) - blockBuilder.gasUsed
})`
)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ export class Eth {
skipBalance: true,
skipBlockGasLimitValidation: true,
})
return bnToHex(gasUsed)
return `0x${gasUsed.toString(16)}`
}

/**
Expand Down Expand Up @@ -745,7 +745,7 @@ export class Eth {
const { gasUsed, createdAddress } = runBlockResult.results[txIndex]
return jsonRpcReceipt(
receipt,
gasUsed,
new BN(gasUsed.toString(10)),
effectiveGasPrice,
block,
tx,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/rpc/eth/estimateGas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tape(`${method}: call with valid arguments`, async (t) => {
const req = params(method, [{ ...estimateTxData, gas: estimateTxData.gasLimit }, 'latest'])
const expectRes = (res: any) => {
const msg = 'should return the correct gas estimate'
t.equal(res.body.result, bnToHex(gasUsed), msg)
t.equal(res.body.result, '0x' + gasUsed.toString(16), msg)
}
await baseRequest(t, server, req, 200, expectRes)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/rpc/eth/getBalance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tape(`${method}: ensure balance deducts after a tx`, async (t) => {
const { amountSpent } = result.results[0]

// verify balance is genesis amount minus amountSpent
const expectedNewBalance = genesisBalance.sub(amountSpent)
const expectedNewBalance = genesisBalance.sub(new BN(amountSpent.toString(10)))
req = params(method, [address.toString(), 'latest'])
expectRes = (res: any) => {
const msg = 'should return the correct balance after a tx'
Expand Down
1 change: 0 additions & 1 deletion packages/client/tsconfig.browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"include": ["browser/index.ts"],
"exclude": ["lib/index.js"],
"compilerOptions": {
"target": "ES2017",
"outDir": "dist.browser",
"typeRoots": ["node_modules/@types", "lib/@types"],
}
Expand Down
1 change: 0 additions & 1 deletion packages/client/tsconfig.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"include": ["bin", "lib"],
"compilerOptions": {
"outDir": "dist",
"lib": ["es2018", "dom"],
"typeRoots": ["node_modules/@types", "lib/@types"],
},
"references": [
Expand Down
3 changes: 3 additions & 0 deletions packages/common/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function(config) {
tsconfig: './tsconfig.json',
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
},
},
concurrency: 1,
Expand Down
1 change: 0 additions & 1 deletion packages/common/tsconfig.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"lib": ["dom"],
"composite": true
},
"include": ["src/**/*.ts", "src/**/*.json"],
Expand Down
3 changes: 3 additions & 0 deletions packages/trie/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = function (config) {
tsconfig: './tsconfig.json',
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
},
},
colors: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/trie/tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "./tsconfig.prod.json",
"extends": "../../config/tsconfig.browser.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"outDir": "./dist.browser",
"target": "es5",
"lib": ["dom", "es5"]
}
}

1 change: 0 additions & 1 deletion packages/trie/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "../../config/tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"lib": ["dom"],
},
"include": ["src/**/*.ts", "test/*.spec.ts"]
}
Expand Down
2 changes: 0 additions & 2 deletions packages/trie/tsconfig.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"target": "ES2017",
"lib": ["dom"],
"composite": true
},
"include": ["src/**/*.ts"],
Expand Down
3 changes: 3 additions & 0 deletions packages/tx/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = function (config) {
tsconfig: './tsconfig.json',
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
acornOptions: {
ecmaVersion: 11
}
},
},
browsers: ['FirefoxHeadless', 'ChromeHeadless'],
Expand Down
18 changes: 18 additions & 0 deletions packages/util/src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,21 @@ export function bufArrToArr(arr: Buffer | NestedBufferArray): Uint8Array | Neste
}
return arr.map((a) => bufArrToArr(a))
}

/**
* Converts a {@link Buffer} to a {@link bigint}`
*/
export function bufferToBigInt(buf: Buffer) {
const hex = bufferToHex(buf)
if (hex === '0x') {
return BigInt(0)
}
return BigInt(hex)
}

/**
* Converts a {@link bigint} to a {@link Buffer}
*/
export function bigIntToBuffer(num: bigint) {
return toBuffer('0x' + num.toString(16))
}
12 changes: 12 additions & 0 deletions packages/util/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ export const MAX_INTEGER = new BN(
16
)

/**
* The max integer that the evm can handle (2^256-1) as a bigint
*/
export const MAX_INTEGER_BIGINT = BigInt(2) ** BigInt(256) - BigInt(1)

/**
* 2^256
*
* @deprecated bn.js constants are deprecated, please use the newly introduced bigint constants
*/
export const TWO_POW256 = new BN(
'10000000000000000000000000000000000000000000000000000000000000000',
16
)

/**
* 2^256
*/
export const TWO_POW256_BIGINT = BigInt(2) ** BigInt(256)

/**
* Keccak-256 hash of null
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ export function toType<T extends TypeOutput>(
return `0x${output.toString('hex')}` as TypeOutputReturnType[T]
}
}

export const bnToBigInt = (bn: BN) => {
return BigInt(new BN(bn).toString(10))
}

export const bigIntToBN = (num: bigint) => {
return new BN(num.toString(10))
}

export const bigIntToHex = (num: bigint) => {
return '0x' + num.toString(16)
}
14 changes: 14 additions & 0 deletions packages/util/test/bytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
intToBuffer,
intToHex,
validateNoLeadingZeroes,
bufferToBigInt,
bigIntToBuffer,
} from '../src'

tape('zeros function', function (t) {
Expand Down Expand Up @@ -443,3 +445,15 @@ tape('bufArrToArr', function (st) {
st.deepEqual(bufArrToArr(bufArr), uint8Arr)
st.end()
})

tape('bufferToBigInt', (st) => {
const buf = toBuffer('0x123')
st.equal(BigInt(0x123), bufferToBigInt(buf))
st.end()
})

tape('bigIntToBuffer', (st) => {
const num = BigInt(0x123)
st.deepEqual(toBuffer('0x123'), bigIntToBuffer(num))
st.end()
})
6 changes: 6 additions & 0 deletions packages/util/test/constants.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
KECCAK256_RLP_ARRAY,
KECCAK256_RLP_S,
KECCAK256_RLP,
TWO_POW256_BIGINT,
} from '../src'

tape('constants', function (t) {
Expand All @@ -22,6 +23,11 @@ tape('constants', function (t) {
'10000000000000000000000000000000000000000000000000000000000000000'
)

st.equal(
TWO_POW256_BIGINT.toString(16),
'10000000000000000000000000000000000000000000000000000000000000000'
)

st.equal(KECCAK256_NULL_S, 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')

st.equal(
Expand Down
18 changes: 18 additions & 0 deletions packages/util/test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
bnToHex,
bnToUnpaddedBuffer,
toBuffer,
bnToBigInt,
bigIntToBN,
bigIntToHex,
} from '../src'

tape('toType', function (t) {
Expand Down Expand Up @@ -142,3 +145,18 @@ tape('bnToUnpaddedBuffer', function (t) {
st.end()
})
})

tape('bnToBigInt', (st) => {
st.equal(bnToBigInt(new BN(1)), BigInt(1))
st.end()
})

tape('bigIntToBN', (st) => {
st.ok(bigIntToBN(BigInt(1)).eq(new BN(1)))
st.end()
})

tape('bigIntToHex', (st) => {
st.equal(bigIntToHex(BigInt(1)), '0x1')
st.end()
})
5 changes: 2 additions & 3 deletions packages/util/tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "./tsconfig.prod.json",
"extends": "../../config/tsconfig.browser.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"outDir": "./dist.browser",
"target": "es5",
"lib": ["dom", "es5"]
}
}

2 changes: 1 addition & 1 deletion packages/util/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../config/tsconfig.json",
"include": ["src/**/*.ts", "test/**/*.ts"]
"include": ["src/**/*.ts", "test/**/*.ts"],
}
2 changes: 1 addition & 1 deletion packages/util/tsconfig.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"composite": true
"composite": true,
},
"include": ["src/**/*.ts"]
}
4 changes: 2 additions & 2 deletions packages/vm/benchmarks/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Account, Address, toBuffer, bufferToInt, BN } from 'ethereumjs-util'
import { Account, Address, toBuffer, bufferToInt, BN, bnToBigInt } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { StateManager, DefaultStateManager } from '../dist/state'
Expand Down Expand Up @@ -100,7 +100,7 @@ export const verifyResult = (block: Block, result: RunBlockResult) => {
if (!result.logsBloom.equals(block.header.logsBloom)) {
throw new Error('invalid logsBloom')
}
if (!block.header.gasUsed.eq(result.gasUsed)) {
if (!(bnToBigInt(block.header.gasUsed) === result.gasUsed)) {
throw new Error('invalid gasUsed')
}
}
Loading