Skip to content

Commit

Permalink
Merge pull request #944 from ethereumjs/tx-constructor-improvements
Browse files Browse the repository at this point in the history
Tx Constructor Restructuring
  • Loading branch information
holgerd77 authored Nov 10, 2020
2 parents d7d9123 + 39e37cc commit 23366cc
Show file tree
Hide file tree
Showing 11 changed files with 1,941 additions and 11,032 deletions.
12,827 changes: 1,867 additions & 10,960 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/block/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
extends: "@ethereumjs/eslint-config-defaults",
ignorePatterns: ["test-build", "karma.conf.js"]
ignorePatterns: ["test-build", "karma.conf.js"],
rules: {
"@typescript-eslint/no-unnecessary-condition": "off"
}
}
5 changes: 4 additions & 1 deletion packages/blockchain/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = {
extends: "@ethereumjs/eslint-config-defaults"
extends: "@ethereumjs/eslint-config-defaults",
rules: {
"@typescript-eslint/no-unnecessary-condition": "off"
}
}
5 changes: 4 additions & 1 deletion packages/common/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
extends: "@ethereumjs/eslint-config-defaults",
ignorePatterns: ['scripts'],
ignorePatterns: ["scripts"],
rules: {
"@typescript-eslint/no-unnecessary-condition": "off"
}
}
5 changes: 4 additions & 1 deletion packages/ethash/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
extends: "@ethereumjs/eslint-config-defaults",
ignorePatterns: ['examples']
ignorePatterns: ["examples"],
rules: {
"@typescript-eslint/no-unnecessary-condition": "off"
}
}
5 changes: 4 additions & 1 deletion packages/tx/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
extends: "@ethereumjs/eslint-config-defaults",
ignorePatterns: ["examples", "karma.conf.js", "test-build"]
ignorePatterns: ["examples", "karma.conf.js", "test-build"],
rules: {
"@typescript-eslint/no-unnecessary-condition": "off"
}
}
1 change: 1 addition & 0 deletions packages/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
## 3.0.0-beta.2 - UNRELEASED

- Added `freeze` option to allow for transaction freeze deactivation (e.g. to allow for subclassing tx and adding additional parameters), see PR [#941](https://github.com/ethereumjs/ethereumjs-vm/pull/941)
- **Breaking:** Reworked constructor to take in data as a `TxData` typed dictionary instead of single values, the `Tx.fromTxData()` factory method becomes an alias for the constructor with this change, see PR [#944](https://github.com/ethereumjs/ethereumjs-vm/pull/944)

## 3.0.0-beta.1 - 2020-10-22

Expand Down
104 changes: 43 additions & 61 deletions packages/tx/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,7 @@ export default class Transaction {
public readonly s?: BN

public static fromTxData(txData: TxData, opts?: TxOptions) {
const { nonce, gasLimit, gasPrice, to, value, data, v, r, s } = txData

return new Transaction(
new BN(toBuffer(nonce)),
new BN(toBuffer(gasPrice)),
new BN(toBuffer(gasLimit)),
to ? new Address(toBuffer(to)) : undefined,
new BN(toBuffer(value)),
toBuffer(data),
new BN(toBuffer(v)),
new BN(toBuffer(r)),
new BN(toBuffer(s)),
opts
)
return new Transaction(txData, opts)
}

public static fromRlpSerializedTx(serialized: Buffer, opts?: TxOptions) {
Expand All @@ -73,15 +60,17 @@ export default class Transaction {
const [nonce, gasPrice, gasLimit, to, value, data, v, r, s] = values

return new Transaction(
new BN(nonce),
new BN(gasPrice),
new BN(gasLimit),
to && to.length > 0 ? new Address(to) : undefined,
new BN(value),
data || Buffer.from([]),
v ? new BN(v) : undefined,
r ? new BN(r) : undefined,
s ? new BN(s) : undefined,
{
nonce: new BN(nonce),
gasPrice: new BN(gasPrice),
gasLimit: new BN(gasLimit),
to: to && to.length > 0 ? new Address(to) : undefined,
value: new BN(value),
data: data || Buffer.from([]),
v: v ? new BN(v) : undefined,
r: r ? new BN(r) : undefined,
s: s ? new BN(s) : undefined,
},
opts
)
}
Expand All @@ -91,25 +80,26 @@ export default class Transaction {
* Use the static factory methods to assist in creating a Transaction object from varying data types.
* @note Transaction objects implement EIP155 by default. To disable it, pass in an `@ethereumjs/common` object set before EIP155 activation (i.e. before Spurious Dragon).
*/
constructor(
nonce: BN,
gasPrice: BN,
gasLimit: BN,
to: Address | undefined,
value: BN,
data: Buffer,
v?: BN,
r?: BN,
s?: BN,
opts?: TxOptions
) {
constructor(txData: TxData, opts?: TxOptions) {
const { nonce, gasPrice, gasLimit, to, value, data, v, r, s } = txData

this.nonce = new BN(toBuffer(nonce))
this.gasPrice = new BN(toBuffer(gasPrice))
this.gasLimit = new BN(toBuffer(gasLimit))
this.to = to ? new Address(toBuffer(to)) : undefined
this.value = new BN(toBuffer(value))
this.data = toBuffer(data)
this.v = new BN(toBuffer(v))
this.r = new BN(toBuffer(r))
this.s = new BN(toBuffer(s))

const validateCannotExceedMaxInteger = {
nonce,
gasPrice,
gasLimit,
value,
r,
s,
nonce: this.nonce,
gasPrice: this.gasPrice,
gasLimit: this.gasLimit,
value: this.value,
r: this.r,
s: this.s,
}
for (const [key, value] of Object.entries(validateCannotExceedMaxInteger)) {
if (value && value.gt(MAX_INTEGER)) {
Expand All @@ -124,17 +114,7 @@ export default class Transaction {
this.common = new Common({ chain: DEFAULT_CHAIN })
}

this._validateTxV(v)

this.nonce = nonce
this.gasPrice = gasPrice
this.gasLimit = gasLimit
this.to = to
this.value = value
this.data = data
this.v = v
this.r = r
this.s = s
this._validateTxV(this.v)

const freeze = opts?.freeze ?? true
if (freeze) {
Expand Down Expand Up @@ -264,15 +244,17 @@ export default class Transaction {
}

return new Transaction(
this.nonce,
this.gasPrice,
this.gasLimit,
this.to,
this.value,
this.data,
new BN(v),
new BN(r),
new BN(s),
{
nonce: this.nonce,
gasPrice: this.gasPrice,
gasLimit: this.gasLimit,
to: this.to,
value: this.value,
data: this.data,
v: new BN(v),
r: new BN(r),
s: new BN(s),
},
opts
)
}
Expand Down
9 changes: 5 additions & 4 deletions packages/vm/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module.exports = {
extends: "@ethereumjs/eslint-config-defaults",
ignorePatterns: ["scripts", "examples", "karma.conf.js"],
rules: {
'@typescript-eslint/no-use-before-define': 'off',
'no-invalid-this': 'off',
'no-restricted-syntax': 'off',
'no-console': 'off' // supress the console.log warnings in ./tests/
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-unnecessary-condition": "off",
"no-invalid-this": "off",
"no-restricted-syntax": "off",
"no-console": "off" // supress the console.log warnings in ./tests/
}
}
2 changes: 1 addition & 1 deletion packages/vm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/vm/tests/api/runBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ tape('should fail when tx gas limit higher than block gas limit', async (t) => {

const gasLimit = new BN(Buffer.from('3fefba', 'hex'))
const opts = { common: block._common }
block.transactions[0] = new Transaction(nonce, gasPrice, gasLimit, to, value, data, v, r, s, opts)
block.transactions[0] = new Transaction(
{ nonce, gasPrice, gasLimit, to, value, data, v, r, s },
opts
)

await suite.p
.runBlock({ block, skipBlockValidation: true })
Expand Down

0 comments on commit 23366cc

Please sign in to comment.