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

Common: add BN support #1150

Merged
merged 12 commits into from
Mar 16, 2021
968 changes: 650 additions & 318 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ export class BlockHeader {
if (this._common.hardforkIsActiveOnChain('dao')) {
// verify the extraData field.
const blockNumber = this.number
const DAOActivationBlock = new BN(this._common.hardforkBlock('dao'))
const DAOActivationBlock = this._common.hardforkBlockBN('dao')
if (blockNumber.gte(DAOActivationBlock)) {
const drift = blockNumber.sub(DAOActivationBlock)
if (drift.lte(DAO_ForceExtraDataRange)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default class Blockchain implements BlockchainInterface {

if (!genesisBlock) {
const common = new Common({
chain: this._common.chainId(),
chain: this._common.chainIdBN(),
hardfork: 'chainstart',
})
genesisBlock = Block.genesis({}, { common })
Expand Down Expand Up @@ -871,7 +871,7 @@ export default class Blockchain implements BlockchainInterface {
const currentTd = { header: new BN(0), block: new BN(0) }
let dbOps: DBOp[] = []

if (block._common.chainId() !== this._common.chainId()) {
if (!block._common.chainIdBN().eq(this._common.chainIdBN())) {
throw new Error('Chain mismatch while trying to put block or header')
}

Expand Down
5 changes: 2 additions & 3 deletions packages/client/lib/blockchain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,9 @@ export class Chain extends EventEmitter {

/**
* Network ID
* @return {number}
*/
get networkId(): number {
return this.config.chainCommon.networkId()
get networkId(): BN {
return this.config.chainCommon.networkIdBN()
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/net/protocol/ethprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class EthProtocol extends Protocol {
encodeStatus(): any {
// TODO: add latestBlock for more precise ETH/64 forkhash switch
return {
networkId: this.chain.networkId,
networkId: this.chain.networkId.toNumber(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is currently breaking the client CLI start on Yolo v3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked, for RLPx the networkId can actually be removed completely from here, since the devp2p library is not using this passed in networkId any more but using the networkId from Common directly.

Not sure about libp2p though.

td: this.chain.blocks.td.toArrayLike(Buffer),
bestHash: this.chain.blocks.latest!.hash(),
genesisHash: this.chain.genesis.hash,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/net/protocol/lesprotocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class LesProtocol extends Protocol {
}

return {
networkId: this.chain.networkId,
networkId: this.chain.networkId.toNumber(),
headTd: this.chain.headers.td.toArrayLike(Buffer),
headHash: this.chain.headers.latest?.hash(),
headNum: this.chain.headers.latest?.number,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Admin {
const difficulty = latestHeader.difficulty
const genesis = bufferToHex(this._chain.genesis.hash)
const head = bufferToHex(latestHeader.mixHash)
const network = this._chain.networkId
const network = this._chain.networkId.toNumber()

const nodeInfo = {
name: clientName,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/rpc/modules/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Net {
* @param params An empty array
*/
version(_params = []) {
return `${this._chain.config.chainCommon.chainId()}`
return this._chain.config.chainCommon.chainIdBN().toString()
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/sync/fullsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ export class FullSynchronizer extends Synchronizer {
const count = height.sub(first).addn(1)
if (count.lten(0)) return false

const nextForkBlock = this.config.chainCommon.nextHardforkBlock()
const nextForkBlock = this.config.chainCommon.nextHardforkBlockBN()
if (nextForkBlock) {
if (first.gten(nextForkBlock)) {
this.config.chainCommon.setHardforkByBlockNumber(first.toNumber())
if (first.gte(nextForkBlock)) {
this.config.chainCommon.setHardforkByBlockNumber(first)
this.hardfork = this.config.chainCommon.hardfork()
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/blockchain/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tape('[Chain]', (t) => {
t.test('should retrieve chain properties', async (t) => {
const chain = new Chain({ config })
await chain.open()
t.equal(chain.networkId, 1, 'get chain.networkId')
t.ok(chain.networkId.eqn(1), 'get chain.networkId')
t.equal(chain.blocks.td.toString(10), '17179869184', 'get chain.blocks.td')
t.equal(chain.blocks.height.toString(10), '0', 'get chain.blocks.height')
t.equal(
Expand Down
6 changes: 3 additions & 3 deletions packages/client/test/net/protocol/ethprotocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tape('[EthProtocol]', (t) => {
const p = new EthProtocol({ config, chain })
Object.defineProperty(chain, 'networkId', {
get: () => {
return 1
return new BN(1)
},
})
Object.defineProperty(chain, 'blocks', {
Expand Down Expand Up @@ -64,8 +64,8 @@ tape('[EthProtocol]', (t) => {
genesisHash: '0xbb',
})
t.ok(
status.networkId.toNumber() === 1 &&
status.td.toNumber() === 100 &&
status.networkId.eqn(1) &&
status.td.eqn(100) &&
status.bestHash === '0xaa' &&
status.genesisHash === '0xbb',
'decode status'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/net/protocol/lesprotocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tape('[LesProtocol]', (t) => {
const p = new LesProtocol({ config, chain, flow })
Object.defineProperty(chain, 'networkId', {
get: () => {
return 1
return new BN(1)
},
})
Object.defineProperty(chain, 'blocks', {
Expand Down
3 changes: 2 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
},
"homepage": "https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/common#readme",
"dependencies": {
"crc-32": "^1.2.0"
"crc-32": "^1.2.0",
"ethereumjs-util": "^7.0.9"
},
"devDependencies": {
"@ethereumjs/config-coverage": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/chains/yolov3.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yolov3",
"chainId": 34180983699157880,
"networkId": 34180983699157880,
"chainId": "34180983699157880",
"networkId": "34180983699157880",
"defaultHardfork": "berlin",
"consensus": {
"type": "poa",
Expand Down
Loading