Skip to content

Commit

Permalink
Refactor tx lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Jul 17, 2020
1 parent b276092 commit 10895cd
Show file tree
Hide file tree
Showing 63 changed files with 2,064 additions and 2,722 deletions.
28 changes: 18 additions & 10 deletions packages/block/src/block.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseTrie as Trie } from 'merkle-patricia-tree'
import Common from '@ethereumjs/common'
import { BN, rlp, keccak256, KECCAK256_RLP, baToJSON } from 'ethereumjs-util'
import { Transaction, TransactionOptions } from '@ethereumjs/tx'
import { Transaction } from '@ethereumjs/tx'
import { BlockHeader } from './header'
import { Blockchain, BlockData, ChainOptions } from './types'

Expand Down Expand Up @@ -41,8 +41,7 @@ export class Block {
this._common = chainOptions.common
} else {
const chain = chainOptions.chain ? chainOptions.chain : 'mainnet'
// TODO: Compute the hardfork based on this block's number. It can be implemented right now
// because the block number is not immutable, so the Common can get out of sync.
// TODO: Compute the hardfork based on this block's number. It can't be implemented right now because the block number is not immutable, so the Common can get out of sync.
const hardfork = chainOptions.hardfork ? chainOptions.hardfork : null
this._common = new Common(chain, hardfork)
}
Expand All @@ -51,8 +50,7 @@ export class Block {
let rawUncleHeaders

if (Buffer.isBuffer(data)) {
// We do this to silence a TS error. We know that after this statement, data is
// a [Buffer[], Buffer[], Buffer[]]
// We do this to silence a TS error. We know that after this statement, data is a [Buffer[], Buffer[], Buffer[]]
const dataAsAny = rlp.decode(data) as any
data = dataAsAny as [Buffer[], Buffer[], Buffer[]]
}
Expand All @@ -73,10 +71,20 @@ export class Block {
}

// parse transactions

// get common based on block's number
// since hardfork param may not be set
// (see TODO above)
const blockNumber = parseInt(this.header.number.toString('hex'), 16)
const hfs = this._common.activeHardforks(blockNumber)
const hf = hfs[hfs.length - 1].name
const common = new Common(this._common.chainId(), hf)

for (let i = 0; i < rawTransactions.length; i++) {
// TODO: Pass the common object instead of the options. It can't be implemented right now
// because the hardfork may be `null`. Read the above TODO for more info.
const tx = new Transaction(rawTransactions[i], chainOptions as TransactionOptions)
const txData = rawTransactions[i]
const tx = Array.isArray(txData)
? Transaction.fromValuesArray(txData as Buffer[], common)
: Transaction.fromRlpSerializedTx(txData as Buffer, common)
this.transactions.push(tx)
}
}
Expand Down Expand Up @@ -119,7 +127,7 @@ export class Block {
serialize(rlpEncode = true) {
const raw = [
this.header.raw,
this.transactions.map((tx) => tx.raw),
this.transactions.map((tx) => tx.serialize()),
this.uncleHeaders.map((uh) => uh.raw),
]

Expand Down Expand Up @@ -243,7 +251,7 @@ export class Block {
if (labeled) {
return {
header: this.header.toJSON(true),
transactions: this.transactions.map((tx) => tx.toJSON(true)),
transactions: this.transactions.map((tx) => tx.toJSON()),
uncleHeaders: this.uncleHeaders.forEach((uh) => uh.toJSON(true)),
}
} else {
Expand Down
28 changes: 21 additions & 7 deletions packages/block/src/from-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FakeTransaction, TransactionOptions } from '@ethereumjs/tx'
import { Transaction, TxData, Address } from '@ethereumjs/tx'
import Common from '@ethereumjs/common'
import { toBuffer, setLengthLeft } from 'ethereumjs-util'
import { Block, ChainOptions } from './index'

Expand Down Expand Up @@ -32,19 +33,31 @@ export default function blockFromRpc(
if (blockParams.transactions) {
for (const _txParams of blockParams.transactions) {
const txParams = normalizeTxParams(_txParams)

// override from address
const fromAddress = toBuffer(txParams.from)
const fromAddress = new Address(toBuffer(txParams.from))
delete txParams.from

const tx = new FakeTransaction(txParams, chainOptions as TransactionOptions)
tx.from = fromAddress
// get common based on block's number
// since hardfork param may not be set
// (see TODO in block.constructor)
const blockNumber = parseInt(block.header.number.toString('hex'), 16)
const hfs = (block as any)._common.activeHardforks(blockNumber)
const hf = hfs[hfs.length - 1].name
const common = new Common((block as any)._common.chainId(), hf)
console.log('yoyo', hf)

const frozenTx = Transaction.fromTxData(txParams as TxData, common)
const tx = Object.create(frozenTx)

// override getSenderAddress
tx.getSenderAddress = function () {
return fromAddress
}

// override hash
const txHash = toBuffer(txParams.hash)
tx.hash = function () {
return txHash
return toBuffer(txParams.hash)
}

block.transactions.push(tx)
Expand All @@ -55,9 +68,10 @@ export default function blockFromRpc(

function normalizeTxParams(_txParams: any) {
const txParams = Object.assign({}, _txParams)
// hot fix for https://github.com/ethereumjs/ethereumjs-util/issues/40

txParams.gasLimit = txParams.gasLimit === undefined ? txParams.gas : txParams.gasLimit
txParams.data = txParams.data === undefined ? txParams.input : txParams.data

// strict byte length checking
txParams.to = txParams.to ? setLengthLeft(toBuffer(txParams.to), 20) : null

Expand Down
34 changes: 15 additions & 19 deletions packages/tx/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ethereumjs-tx
# @ethereumjs/tx

[![NPM Package][tx-npm-badge]][tx-npm-link]
[![GitHub Issues][tx-issues-badge]][tx-issues-link]
Expand All @@ -10,18 +10,14 @@

# INSTALL

`npm install ethereumjs-tx`
`npm install @ethereumjs/tx`

# USAGE

- [example](https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.ts)
- [Example](./examples/transactions.ts)

```javascript
const EthereumTx = require('@ethereumjs/tx').Transaction
const privateKey = Buffer.from(
'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
'hex',
)
import Transaction from '@ethereumjs/tx'

const txParams = {
nonce: '0x00',
Expand All @@ -32,21 +28,21 @@ const txParams = {
data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057',
}

// The second parameter is not necessary if these values are used
const tx = new EthereumTx(txParams, { chain: 'mainnet', hardfork: 'petersburg' })
tx.sign(privateKey)
const serializedTx = tx.serialize()
```
const tx = Transaction.fromTxData(txParams, new Common('mainnet', 'petersburg'))

# Chain and Hardfork Support
const privateKey = Buffer.from(
'e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109',
'hex',
)

The `Transaction` and `FakeTransaction` constructors receives a second parameter that lets you specify the chain and hardfork
to be used. By default, `mainnet` and `petersburg` will be used.
const signedTx = tx.sign(privateKey)

There are two ways of customizing these. The first one, as shown in the previous section, is by
using an object with `chain` and `hardfork` names. You can see en example of this in [./examples/ropsten-tx.ts](./examples/ropsten-tx.ts).
const serializedTx = signedTx.serialize()
```

# Chain and Hardfork Support

The second option is by passing the option `common` set to an instance of [@ethereumjs/common](https://github.com/ethereumjs/ethereumjs-common)' Common. This is specially useful for custom networks or chains/hardforks not yet supported by `ethereumjs-common`. You can see en example of this in [./examples/custom-chain-tx.ts](./examples/custom-chain-tx.ts).
The `Transaction` constructor receives a second parameter of a [`@ethereumjs/common`](https://github.com/ethereumjs/ethereumjs-vm/blob/master/packages/common) object that lets you specify the chain and hardfork to be used. By default, `mainnet` and `petersburg` will be used.

## MuirGlacier Support

Expand Down
1 change: 0 additions & 1 deletion packages/tx/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

### Modules

* ["fake"](modules/_fake_.md)
* ["index"](modules/_index_.md)
* ["transaction"](modules/_transaction_.md)
* ["types"](modules/_types_.md)
Loading

0 comments on commit 10895cd

Please sign in to comment.