Skip to content

Commit

Permalink
Convert bn to bigint (#1771)
Browse files Browse the repository at this point in the history
* util: use bigints. Expose new secp constants

* common: use bigints

* tx: use bigints

* lint: fix util, tx, common

* tx: fix isSigned

* update comment block style to be picked up by vscode

* remove doc typo (extra `)

* tx: add isSigned() tests to base.spec.ts to iterate across txTypes

* block: bn to bigint changes wip

block: finish header changes

block: test updates

block: test fixes

Partial difficulty fixes

* block: fix homestead difficulty

* block: fix >= Byzantium difficulty

* BigInt conversion fixes

* block: update st.ok to st.equals

* Update readme to use bigints

* ethash: bn to bigint

* blockchain: wip bn to bigint changes

* devp2p: bn to bigint

* vm: wip remaining bn -> bigint

* vm: more test fixes

* fix runTx negative value args test, ensure balance does not go negative when using skipBalance

* vm: lint:fix

* re-add newline

* Blockchain: small rebase fix

* vm: Fix API tests

* client: bn to bigint updates in source

* client: various fixes

* client: last fixes

* client: integration test fixes

* replace st.ok usage with st.equal

* normalize st.equals to st.equal

* update toType cases

* nits, lint

* fix vm benchmarks

* client: fix fullEthereumService tests

* touch ups, fix miner integration test

* use prefix increment

* client: fix full sync test

* test fixes

* Fix reorg logic bug

* bnTo... function renaming and cleanup

* Goodbye bn.js

* reverse changelog changes

* nits

* remove more BN, more nits

* turn off restrict-plus-operands and remove disable overrides (does not work properly)

* more nits, update package-lock

* fix build errors, re-add @types/bn.js for rlp v2 export to fix build (will be removed in subsequent PR for rlp)

* replace miller-rabin (bn.js) with bigint-crypto-utils isProbablyPrime

* more nits / fixes

* Fix misplaced paren

* Fix miner test

* more nits

* fix buffer to big int

* set expectedTests back to >= comparison

* last fixes

* Removed re-introduced Common methods

Co-authored-by: Paul Miller <paul@paulmillr.com>
Co-authored-by: Ryan Ghods <ryan@ryanio.com>
Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
Co-authored-by: Jochem Brouwer <jochembrouwer96@gmail.com>
  • Loading branch information
5 people authored and g11tech committed Apr 29, 2022
1 parent 5478fe6 commit 9333863
Show file tree
Hide file tree
Showing 175 changed files with 2,773 additions and 2,830 deletions.
1 change: 1 addition & 0 deletions config/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = {
'prettier/prettier': 'error',
'no-redeclare': 'off',
'@typescript-eslint/no-redeclare': ['error'],
'@typescript-eslint/restrict-plus-operands': 'off',
},
parserOptions: {
sourceType: 'module',
Expand Down
13 changes: 6 additions & 7 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ This library supports the creation of [EIP-1559](https://eips.ethereum.org/EIPS/
To instantiate an EIP-1559 block, the hardfork parameter on the `Common` instance needs to be set to `london` (this is now the default hardfork):

```typescript
import { BN } from 'ethereumjs-util'
import { Block } from '@ethereumjs/block'
import Common, { Chain, Hardfork } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })

const block = Block.fromBlockData({
header: {
baseFeePerGas: new BN(10),
gasLimit: new BN(100),
gasUsed: new BN(60)
baseFeePerGas: BigInt(10),
gasLimit: BigInt(100),
gasUsed: BigInt(60)
}
}, { common })

Expand All @@ -85,14 +84,14 @@ block.header.calcNextBaseFee().toNumber() // 11
const blockWithMatchingBaseFee = Block.fromBlockData({
header: {
baseFeePerGas: parentHeader.calcNextBaseFee(),
gasLimit: new BN(100),
gasUsed: new BN(60)
gasLimit: BigInt(100),
gasUsed: BigInt(60)
}
}, { common })

```

EIP-1559 blocks have an extra `baseFeePerGas` field (default: `new BN(7)`) and can encompass `FeeMarketEIP1559Transaction` txs (type `2`) (supported by `@ethereumjs/tx` `v3.2.0` or higher) as well as `Transaction` legacy txs (internal type `0`) and `AccessListEIP2930Transaction` txs (type `1`).
EIP-1559 blocks have an extra `baseFeePerGas` field (default: `BigInt(7)`) and can encompass `FeeMarketEIP1559Transaction` txs (type `2`) (supported by `@ethereumjs/tx` `v3.2.0` or higher) as well as `Transaction` legacy txs (internal type `0`) and `AccessListEIP2930Transaction` txs (type `1`).

## Consensus Types

Expand Down
17 changes: 8 additions & 9 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, rlp, keccak256, KECCAK256_RLP, bufferToHex } from 'ethereumjs-util'
import { rlp, keccak256, KECCAK256_RLP, bufferToHex } from 'ethereumjs-util'
import Common, { ConsensusType } from '@ethereumjs/common'
import {
TransactionFactory,
Expand Down Expand Up @@ -30,7 +30,6 @@ export class Block {
*/
public static fromBlockData(blockData: BlockData = {}, opts?: BlockOptions) {
const { header: headerData, transactions: txsData, uncleHeaders: uhsData } = blockData

const header = BlockHeader.fromHeaderData(headerData, opts)

// parse transactions
Expand Down Expand Up @@ -250,12 +249,12 @@ export class Block {
if (this._common.isActivatedEIP(1559)) {
if (tx.supports(Capability.EIP1559FeeMarket)) {
tx = tx as FeeMarketEIP1559Transaction
if (tx.maxFeePerGas.lt(this.header.baseFeePerGas!)) {
if (tx.maxFeePerGas < this.header.baseFeePerGas!) {
errs.push('tx unable to pay base fee (EIP-1559 tx)')
}
} else {
tx = tx as Transaction
if (tx.gasPrice.lt(this.header.baseFeePerGas!)) {
if (tx.gasPrice < this.header.baseFeePerGas!) {
errs.push('tx unable to pay base fee (non EIP-1559 tx)')
}
}
Expand Down Expand Up @@ -368,7 +367,7 @@ export class Block {
*
* @param parentBlock - the parent of this `Block`
*/
canonicalDifficulty(parentBlock: Block): BN {
canonicalDifficulty(parentBlock: Block): bigint {
return this.header.canonicalDifficulty(parentBlock.header)
}

Expand Down Expand Up @@ -422,11 +421,11 @@ export class Block {
// Check how many blocks we should get in order to validate the uncle.
// In the worst case, we get 8 blocks, in the best case, we only get 1 block.
const canonicalBlockMap: Block[] = []
let lowestUncleNumber = this.header.number.clone()
let lowestUncleNumber = this.header.number

uncleHeaders.map((header) => {
if (header.number.lt(lowestUncleNumber)) {
lowestUncleNumber = header.number.clone()
if (header.number < lowestUncleNumber) {
lowestUncleNumber = header.number
}
})

Expand All @@ -437,7 +436,7 @@ export class Block {
const includedUncles: { [key: string]: boolean } = {}

// Due to the header validation check above, we know that `getBlocks` is between 1 and 8 inclusive.
const getBlocks = this.header.number.clone().sub(lowestUncleNumber).addn(1).toNumber()
const getBlocks = this.header.number - lowestUncleNumber + BigInt(1)

// See Geth: https://github.com/ethereum/go-ethereum/blob/b63bffe8202d46ea10ac8c4f441c582642193ac8/consensus/ethash/consensus.go#L207
// Here we get the necessary blocks from the chain.
Expand Down
8 changes: 3 additions & 5 deletions packages/block/src/clique.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { BN } from 'ethereumjs-util'

// Fixed number of extra-data prefix bytes reserved for signer vanity
export const CLIQUE_EXTRA_VANITY = 32
// Fixed number of extra-data suffix bytes reserved for signer seal
export const CLIQUE_EXTRA_SEAL = 65

// Block difficulty for in-turn signatures
export const CLIQUE_DIFF_INTURN = new BN(2)
// Block difficulty for in-turn signatures
export const CLIQUE_DIFF_NOTURN = new BN(1)
export const CLIQUE_DIFF_INTURN = BigInt(2)
// Block difficulty for out-of-turn signatures
export const CLIQUE_DIFF_NOTURN = BigInt(1)
Loading

0 comments on commit 9333863

Please sign in to comment.