Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c053761
block -> refactor: reworked header class with static factory instanti…
holgerd77 Sep 21, 2020
ff46c55
block -> refactoring: added new static factory helpers to block class
holgerd77 Sep 21, 2020
dd228bc
block -> refactor: fix build errors, remove unused imports, unpad num…
jochem-brouwer Sep 24, 2020
608d804
block -> rename Header to BlockHeader
jochem-brouwer Sep 28, 2020
17a33a8
block/tx -> fix block tests
jochem-brouwer Sep 28, 2020
72b5b78
block -> enforce BNs on fields which are interpreted as numbers
jochem-brouwer Sep 28, 2020
8adcdf9
block -> edge case in toBN
jochem-brouwer Sep 29, 2020
8987003
ethash -> make ethash compatible with block
jochem-brouwer Sep 29, 2020
5f5e9f2
Merge branch 'master' into refactor-block-library
ryanio Oct 7, 2020
5351fb8
have validateTransactions return a string[] (https://github.com/ether…
ryanio Oct 7, 2020
ce1dac1
let => const
ryanio Oct 7, 2020
526f986
set default param to resolve js runtime check
ryanio Oct 7, 2020
75689e6
continue refactoring and simplifying methods
ryanio Oct 8, 2020
8923f71
api updates
ryanio Oct 8, 2020
6a2c193
continuing work
ryanio Oct 8, 2020
381f5e1
inline buffer validations. add checks for extraData, mixHash and nonce
ryanio Oct 8, 2020
7eecf80
various fixups
ryanio Oct 8, 2020
395c6f8
continuing various work
ryanio Oct 9, 2020
be5c8d2
continuing work and refactoring
ryanio Oct 9, 2020
7fa486d
Merge branch 'master' into refactor-block-library
ryanio Oct 9, 2020
91d45d7
re-add timestamp to genesis (for rinkeby)
ryanio Oct 9, 2020
7e788c3
last fixups
ryanio Oct 9, 2020
bc459e8
update readme, benchmarks
ryanio Oct 9, 2020
ea8a401
update vm readme, simplify validate
ryanio Oct 10, 2020
b694010
fix timestamp validation
ryanio Oct 10, 2020
1f66378
use native eq
ryanio Oct 10, 2020
9f9bab0
make blockchain optional in block.validate()
ryanio Oct 10, 2020
7ce9132
fixups
ryanio Oct 10, 2020
a5d3d14
remove BLOCK_difficulty_GivenAsList from skip list (https://github.co…
ryanio Oct 12, 2020
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
Prev Previous commit
Next Next commit
continuing work
  • Loading branch information
ryanio committed Oct 8, 2020
commit 6a2c193c59600dc51fb6d5135a0c90d60ed0c267
18 changes: 11 additions & 7 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, rlp, keccak256, KECCAK256_RLP, baToJSON } from 'ethereumjs-util'
import { BN, rlp, keccak256, KECCAK256_RLP } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { Transaction, TxOptions } from '@ethereumjs/tx'
import { BlockHeader } from './header'
import { Blockchain, BlockData, BlockOptions } from './types'
import { BlockData, BlockOptions, JsonBlock, Blockchain } from './types'

/**
* An object that represents the block
Expand Down Expand Up @@ -86,11 +86,15 @@ export class Block {
* This constructor takes the values, validates them, assigns them and freezes the object.
* Use the static factory methods to assist in creating a Block object from varying data types and options.
*/
constructor(header: BlockHeader, transactions: Transaction[], uncleHeaders: BlockHeader[]) {
this.header = header
constructor(
header?: BlockHeader,
transactions: Transaction[] = [],
uncleHeaders: BlockHeader[] = [],
opts: BlockOptions = {},
) {
this.header = header || BlockHeader.fromHeaderData({}, opts)
this.transactions = transactions
this.uncleHeaders = uncleHeaders

this._common = this.header._common

Object.freeze(this)
Copy link
Member Author

Choose a reason for hiding this comment

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

For understanding on further implications: does this Object.freeze() mean we can't do further modifications on e.g. the Common instance passed?

So would a subsequent call (e.g. in a VM context) common.setHardfork('byzantium') (or whatever) throw in this context or would this still work?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll have to give it a test, but I think the common object can still update, it just can't be set to a totally new instance (e.g. block._common = newCommon would fail)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I think this is the case according to here:

Note that values that are objects can still be modified, unless they are also frozen.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze

Expand Down Expand Up @@ -231,11 +235,11 @@ export class Block {
/**
* Returns the block in JSON format.
*/
toJSON() {
toJSON(): JsonBlock {
return {
header: this.header.toJSON(),
transactions: this.transactions.map((tx) => tx.toJSON()),
uncleHeaders: this.uncleHeaders.forEach((uh) => uh.toJSON()),
uncleHeaders: this.uncleHeaders.map((uh) => uh.toJSON()),
}
}

Expand Down
6 changes: 4 additions & 2 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class BlockHeader {

public readonly _common: Common

public static fromHeaderData(headerData: HeaderData, opts: BlockOptions = {}) {
public static fromHeaderData(headerData: HeaderData = {}, opts: BlockOptions = {}) {
const {
parentHash,
uncleHash,
Expand Down Expand Up @@ -132,6 +132,7 @@ export class BlockHeader {
* This constructor takes the values, validates them, assigns them and freezes the object.
* Use the public static factory methods to assist in creating a Header object from
* varying data types.
* For a default empty header, use `BlockHeader.fromHeaderData()`.
*/
constructor(
parentHash: Buffer,
Expand Down Expand Up @@ -182,6 +183,7 @@ export class BlockHeader {
if (options.hardforkByBlockNumber) {
this._common.setHardforkByBlockNumber(this.number.toNumber())
}

if (options.initWithGenesisHeader) {
if (this._common.hardfork() !== 'chainstart') {
throw new Error(
Expand Down Expand Up @@ -231,7 +233,7 @@ export class BlockHeader {
)
let num = toBN(this.number)

// We use a ! here as TS can follow this hardforks-dependent logic, but it always gets assigned
// We use a ! here as TS cannot follow this hardfork-dependent logic, but it always gets assigned
let dif!: BN

if (this._common.hardforkGteHardfork(hardfork, 'byzantium')) {
Expand Down
8 changes: 4 additions & 4 deletions packages/block/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Common from '@ethereumjs/common'
import { TxData } from '@ethereumjs/tx'
import { TxData, JsonTx } from '@ethereumjs/tx'
import { Block } from './block'
import { Address, BN } from 'ethereumjs-util'

Expand Down Expand Up @@ -96,9 +96,9 @@ export interface JsonBlock {
/**
* Header data for the block
*/
header?: string
transactions?: string
uncleHeaders?: string
header?: JsonHeader
transactions?: JsonTx[]
uncleHeaders?: JsonHeader[]
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/block/test/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ tape('[Block]: block functions', function (t) {
t.test('should test toJSON', function (st) {
const block = Block.fromRLPSerializedBlock(testData2.blocks[2].rlp)
st.equal(typeof block.toJSON(), 'object')
st.equal(typeof block.toJSON(true), 'object')
st.end()
})

Expand Down
6 changes: 3 additions & 3 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ export default class Blockchain implements BlockchainInterface {
* @hidden
*/
async _putBlockOrHeader(item: Block | BlockHeader, isGenesis?: boolean) {
const block = item instanceof BlockHeader ? new Block(item, [], []) : item
const block = item instanceof BlockHeader ? new Block(item) : item

const header = block.header
const hash = block.hash()
const number = new BN(header.number)
const td = new BN(header.difficulty)
const number = header.number
const td = header.difficulty
const currentTd = { header: new BN(0), block: new BN(0) }
const dbOps: DBOp[] = []

Expand Down
Loading