Skip to content

Commit 0df1f45

Browse files
authored
Merge pull request #1089 from ethereumjs/block-tx-copy-common
Block, Tx: always copy Common on instantiation / Fix HF consistency bugs
2 parents a738424 + e5d9cde commit 0df1f45

File tree

22 files changed

+247
-62
lines changed

22 files changed

+247
-62
lines changed

packages/block/src/block.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export class Block {
2525
// parse transactions
2626
const transactions = []
2727
for (const txData of txsData || []) {
28-
const tx = Transaction.fromTxData(txData, opts as TxOptions)
28+
const tx = Transaction.fromTxData(txData, {
29+
...opts,
30+
// Use header common in case of hardforkByBlockNumber being activated
31+
common: header._common,
32+
} as TxOptions)
2933
transactions.push(tx)
3034
}
3135

@@ -34,7 +38,10 @@ export class Block {
3438
for (const uhData of uhsData || []) {
3539
const uh = BlockHeader.fromHeaderData(uhData, {
3640
...opts,
37-
// Disable this option here (all other options carried over), since this overwrites the provided Difficulty to an incorrect value
41+
// Use header common in case of hardforkByBlockNumber being activated
42+
common: header._common,
43+
// Disable this option here (all other options carried over), since this overwrites
44+
// the provided Difficulty to an incorrect value
3845
calcDifficultyFromHeader: undefined,
3946
})
4047
uncleHeaders.push(uh)
@@ -65,7 +72,13 @@ export class Block {
6572
// parse transactions
6673
const transactions = []
6774
for (const txData of txsData || []) {
68-
transactions.push(Transaction.fromValuesArray(txData, opts))
75+
transactions.push(
76+
Transaction.fromValuesArray(txData, {
77+
...opts,
78+
// Use header common in case of hardforkByBlockNumber being activated
79+
common: header._common,
80+
})
81+
)
6982
}
7083

7184
// parse uncle headers
@@ -74,6 +87,8 @@ export class Block {
7487
uncleHeaders.push(
7588
BlockHeader.fromValuesArray(uncleHeaderData, {
7689
...opts,
90+
// Use header common in case of hardforkByBlockNumber being activated
91+
common: header._common,
7792
// Disable this option here (all other options carried over), since this overwrites the provided Difficulty to an incorrect value
7893
calcDifficultyFromHeader: undefined,
7994
})

packages/block/src/header.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ export class BlockHeader {
173173
options: BlockOptions = {}
174174
) {
175175
if (options.common) {
176-
this._common = options.common
176+
this._common = Object.assign(
177+
Object.create(Object.getPrototypeOf(options.common)),
178+
options.common
179+
)
177180
} else {
178181
const chain = 'mainnet' // default
179182
if (options.initWithGenesisHeader) {
@@ -685,6 +688,10 @@ export class BlockHeader {
685688
cliqueSigner(): Address {
686689
this._requireClique('cliqueSigner')
687690
const extraSeal = this.cliqueExtraSeal()
691+
// Reasonable default for default blocks
692+
if (extraSeal.length === 0) {
693+
return Address.zero()
694+
}
688695
const r = extraSeal.slice(0, 32)
689696
const s = extraSeal.slice(32, 64)
690697
const v = bufferToInt(extraSeal.slice(64, 65)) + 27

packages/block/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface BlockOptions {
1313
/**
1414
* A Common object defining the chain and the hardfork a block/block header belongs to.
1515
*
16+
* Object will be internally copied so that tx behavior don't incidentally
17+
* change on future HF changes.
18+
*
1619
* Default: `Common` object set to `mainnet` and the HF currently defined as the default
1720
* hardfork in the `Common` class.
1821
*

packages/block/test/clique.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tape('[Header]: Clique PoA Functionality', function (t) {
8080
t.test('Signing', function (st) {
8181
const cliqueSigner = A.privateKey
8282

83-
const header = BlockHeader.fromHeaderData(
83+
let header = BlockHeader.fromHeaderData(
8484
{ number: 1, extraData: Buffer.alloc(97) },
8585
{ common, freeze: false, cliqueSigner }
8686
)
@@ -89,6 +89,13 @@ tape('[Header]: Clique PoA Functionality', function (t) {
8989
st.ok(header.cliqueVerifySignature([A.address]), 'should verify signature')
9090
st.ok(header.cliqueSigner().equals(A.address), 'should recover the correct signer address')
9191

92+
header = BlockHeader.fromHeaderData({}, { common })
93+
st.deepEqual(
94+
header.cliqueSigner(),
95+
Address.zero(),
96+
'should return zero address on default block'
97+
)
98+
9299
st.end()
93100
})
94101
})

packages/block/test/header.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ tape('[Block]: Header functions', function (t) {
4141
const common = new Common({ chain: 'ropsten', hardfork: 'chainstart' })
4242
let header = BlockHeader.genesis(undefined, { common })
4343
st.ok(header.hash().toString('hex'), 'genesis block should initialize')
44+
st.equal(header._common.hardfork(), 'chainstart', 'should initialize with correct HF provided')
45+
46+
common.setHardfork('byzantium')
47+
st.equal(
48+
header._common.hardfork(),
49+
'chainstart',
50+
'should stay on correct HF if outer common HF changes'
51+
)
4452

4553
header = BlockHeader.fromHeaderData({}, { common })
4654
st.ok(header.hash().toString('hex'), 'default block should initialize')

packages/blockchain/src/db/manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class DBManager {
167167
}
168168
}
169169
const blockData = [header, ...body] as BlockBuffer
170-
const opts = { common: this._common }
170+
const opts = { common: this._common, hardforkByBlockNumber: true }
171171
return Block.fromValuesArray(blockData, opts)
172172
}
173173

@@ -184,7 +184,7 @@ export class DBManager {
184184
*/
185185
async getHeader(blockHash: Buffer, blockNumber: BN) {
186186
const encodedHeader = await this.get(DBTarget.Header, { blockHash, blockNumber })
187-
const opts = { common: this._common }
187+
const opts = { common: this._common, hardforkByBlockNumber: true }
188188
return BlockHeader.fromRLPSerializedHeader(encodedHeader, opts)
189189
}
190190

packages/blockchain/src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,10 @@ export default class Blockchain implements BlockchainInterface {
202202
public static async fromBlocksData(blocksData: BlockData[], opts: BlockchainOptions = {}) {
203203
const blockchain = await Blockchain.create(opts)
204204
for (const blockData of blocksData) {
205-
const common = Object.assign(
206-
Object.create(Object.getPrototypeOf(blockchain._common)),
207-
blockchain._common
208-
)
209-
const block = Block.fromBlockData(blockData, { common, hardforkByBlockNumber: true })
205+
const block = Block.fromBlockData(blockData, {
206+
common: blockchain._common,
207+
hardforkByBlockNumber: true,
208+
})
210209
await blockchain.putBlock(block)
211210
}
212211
return blockchain
@@ -798,7 +797,10 @@ export default class Blockchain implements BlockchainInterface {
798797
await this.runWithLock<void>(async () => {
799798
const block =
800799
item instanceof BlockHeader
801-
? new Block(item, undefined, undefined, { common: this._common })
800+
? new Block(item, undefined, undefined, {
801+
common: this._common,
802+
hardforkByBlockNumber: true,
803+
})
802804
: item
803805
const isGenesis = block.isGenesis()
804806

packages/client/lib/blockchain/chain.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export class Chain extends EventEmitter {
267267
}
268268
await this.open()
269269
blocks = blocks.map((b: Block) =>
270-
Block.fromValuesArray(b.raw(), { common: this.config.chainCommon })
270+
Block.fromValuesArray(b.raw(), {
271+
common: this.config.chainCommon,
272+
hardforkByBlockNumber: true,
273+
})
271274
)
272275
await this.blockchain.putBlocks(blocks)
273276
await this.update()
@@ -302,7 +305,10 @@ export class Chain extends EventEmitter {
302305
}
303306
await this.open()
304307
headers = headers.map((h) =>
305-
BlockHeader.fromValuesArray(h.raw(), { common: this.config.chainCommon })
308+
BlockHeader.fromValuesArray(h.raw(), {
309+
common: this.config.chainCommon,
310+
hardforkByBlockNumber: true,
311+
})
306312
)
307313
await this.blockchain.putHeaders(headers)
308314
await this.update()

packages/client/lib/net/protocol/ethprotocol.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ export class EthProtocol extends Protocol {
6363
name: 'BlockHeaders',
6464
code: 0x04,
6565
encode: (headers: BlockHeader[]) => headers.map((h) => h.raw()),
66-
decode: (headers: BlockHeaderBuffer[]) =>
67-
/* eslint-disable-next-line no-invalid-this */
68-
headers.map((h) => BlockHeader.fromValuesArray(h, { common: this.config.chainCommon })),
66+
decode: (headers: BlockHeaderBuffer[]) => {
67+
return headers.map((h) =>
68+
BlockHeader.fromValuesArray(h, {
69+
hardforkByBlockNumber: true,
70+
common: this.config.chainCommon, // eslint-disable-line no-invalid-this
71+
})
72+
)
73+
},
6974
},
7075
{
7176
name: 'GetBlockBodies',

packages/client/lib/net/protocol/lesprotocol.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ export class LesProtocol extends Protocol {
9191
decode: ([reqId, bv, headers]: any) => ({
9292
reqId: new BN(reqId),
9393
bv: new BN(bv),
94-
headers: headers.map((h: BlockHeaderBuffer) =>
95-
/* eslint-disable-next-line no-invalid-this */
96-
BlockHeader.fromValuesArray(h, { common: this.config.chainCommon })
97-
),
94+
headers: headers.map((h: BlockHeaderBuffer) => {
95+
return BlockHeader.fromValuesArray(h, {
96+
hardforkByBlockNumber: true,
97+
common: this.config.chainCommon, // eslint-disable-line no-invalid-this
98+
})
99+
}),
98100
}),
99101
},
100102
]

0 commit comments

Comments
 (0)