Skip to content

Commit

Permalink
block -> refactor: fix build errors, remove unused imports, unpad num…
Browse files Browse the repository at this point in the history
…ber-value buffers in header
  • Loading branch information
jochem-brouwer committed Sep 24, 2020
1 parent ff46c55 commit dd228bc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
15 changes: 7 additions & 8 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import { BN, rlp, keccak256, KECCAK256_RLP, baToJSON } from 'ethereumjs-util'
import Common from '@ethereumjs/common'
import { BN, rlp, keccak256, KECCAK256_RLP, baToJSON, bufferToInt } from 'ethereumjs-util'
import { Transaction, TransactionOptions } from '@ethereumjs/tx'
import { Transaction, TxOptions } from '@ethereumjs/tx'
import { Header } from './header'
import { Blockchain, BlockData, BlockOptions, HeaderData } from './types'
import { Blockchain, BlockData, BlockOptions } from './types'

/**
* An object that represents the block
Expand Down Expand Up @@ -32,7 +31,7 @@ export class Block {
// parse transactions
let transactions = []
for (const txData of txsData) {
transactions.push(new Transaction(txData, opts))
transactions.push(Transaction.fromTxData(txData, opts as TxOptions))
}

// parse uncle headers
Expand Down Expand Up @@ -70,7 +69,7 @@ export class Block {
// parse transactions
let transactions = []
for (const txData of txsData) {
transactions.push(new Transaction(txData, opts))
transactions.push(Transaction.fromRlpSerializedTx(txData, opts as TxOptions))
}

// parse uncle headers
Expand Down Expand Up @@ -135,9 +134,9 @@ export class Block {
serialize(rlpEncode: false): [Buffer[], Buffer[], Buffer[]]
serialize(rlpEncode = true) {
const raw = [
this.header.raw,
this.header.raw(),
this.transactions.map((tx) => tx.serialize()),
this.uncleHeaders.map((uh) => uh.raw),
this.uncleHeaders.map((uh) => uh.raw()),
]

return rlpEncode ? rlp.encode(raw) : raw
Expand Down Expand Up @@ -216,7 +215,7 @@ export class Block {
* Validates the uncle's hash
*/
validateUnclesHash(): boolean {
const raw = rlp.encode(this.uncleHeaders.map((uh) => uh.raw))
const raw = rlp.encode(this.uncleHeaders.map((uh) => uh.raw()))

return keccak256(raw).equals(this.header.uncleHash)
}
Expand Down
19 changes: 9 additions & 10 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ export default function blockFromRpc(blockParams: any, uncles?: any[], options?:

const header = blockHeaderFromRpc(blockParams, options)

const block = new Block(
{
header: header.toJSON(true),
transactions: [],
uncleHeaders: uncles.map((uh) => blockHeaderFromRpc(uh, options).toJSON(true)),
},
options,
)
const transactions: TxData[] = []

if (blockParams.transactions) {
const txOpts = { common: (<any>block)._common }
const txOpts = { common: header._common }

for (const _txParams of blockParams.transactions) {
const txParams = normalizeTxParams(_txParams)
Expand All @@ -47,10 +40,16 @@ export default function blockFromRpc(blockParams: any, uncles?: any[], options?:
return toBuffer(txParams.hash)
}

block.transactions.push(fakeTx)
transactions.push(fakeTx)
}
}

const block = Block.fromBlockData({
header,
transactions,
uncleHeaders: uncles.map((uh) => blockHeaderFromRpc(uh, options)),
})

return block
}

Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/header-from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BlockHeader } from './header'
import { Header } from './header'
import { KECCAK256_NULL, toBuffer } from 'ethereumjs-util'
import { BlockOptions } from './types'

Expand All @@ -9,7 +9,7 @@ import { BlockOptions } from './types'
* @param chainOptions - An object describing the blockchain
*/
export default function blockHeaderFromRpc(blockParams: any, options?: BlockOptions) {
const blockHeader = new BlockHeader(
const blockHeader = Header.fromHeaderData(
{
parentHash: blockParams.parentHash,
uncleHash: blockParams.sha3Uncles,
Expand Down
47 changes: 37 additions & 10 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import {
unpadBuffer,
bufferToInt,
rlphash,
unpadArray,
} from 'ethereumjs-util'
import { Blockchain, HeaderData, BufferLike, BlockOptions, PrefixedHexString } from './types'
import { Blockchain, HeaderData, BlockOptions } from './types'
import { Buffer } from 'buffer'
import { Block } from './block'
import { checkBufferLength } from './util'
import { Options } from 'lru-cache'
import { statSync } from 'fs'

/**
* An object that represents the block header
Expand Down Expand Up @@ -200,6 +197,14 @@ export class Header {
this.stateRoot = this._common.genesis().stateRoot
this.number = toBuffer(0)
}

// Unpad all fields which should be interpreted as numbers
this.timestamp = unpadBuffer(this.timestamp)
this.difficulty = unpadBuffer(this.difficulty)
this.gasLimit = unpadBuffer(this.gasLimit)
this.number = unpadBuffer(this.number)
this.timestamp = unpadBuffer(this.timestamp)

this._checkDAOExtraData()

Object.freeze(this)
Expand Down Expand Up @@ -378,7 +383,15 @@ export class Header {
* Returns the hash of the block header.
*/
hash(): Buffer {
const values: Buffer[] = [
const values: Buffer[] = this.raw()
return rlphash(values)
}

/**
* Returns a Buffer Array of the raw Buffers in this header, in order
*/
raw(): Buffer[] {
return [
this.parentHash,
this.uncleHash,
this.coinbase,
Expand All @@ -387,15 +400,14 @@ export class Header {
this.receiptTrie,
this.bloom,
this.difficulty,
unpadBuffer(this.number),
this.number,
this.gasLimit,
this.gasUsed,
this.timestamp,
this.extraData,
this.mixHash,
this.nonce,
]
return rlphash(values)
}

/**
Expand All @@ -415,12 +427,27 @@ export class Header {

/**
* Returns the block header in JSON format
*
* @see {@link https://github.com/ethereumjs/ethereumjs-util/blob/master/docs/index.md#defineproperties|ethereumjs-util}
*/
toJSON(_labels: boolean = false): { [key: string]: string } | string[] {
// Note: This never gets executed, defineProperties overwrites it.
return {}
/*return Object.create({
parentHash: this.parentHash,
uncleHash: this.uncleHash,
coinbase: this.coinbase,
stateRoot: this.stateRoot,
transactionsTrie: this.transactionsTrie,
receiptTrie: this.receiptTrie,
bloom: this.bloom,
difficulty: this.difficulty,
number: this.number,
gasLimit: this.gasLimit,
gasUsed: this.gasUsed,
timestamp: this.timestamp,
extraData: this.extraData,
mixHash: this.mixHash,
nonce: this.nonce,
})*/
return {} //TODO: FIXME
}

private _getHardfork(): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/block/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Block } from './block'
export { BlockHeader } from './header'
export { Header } from './header'
export * from './types'

0 comments on commit dd228bc

Please sign in to comment.