Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Merge 90540b8 into 40551b0
Browse files Browse the repository at this point in the history
  • Loading branch information
youfoundron committed Feb 6, 2019
2 parents 40551b0 + 90540b8 commit bee0a3b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
4 changes: 2 additions & 2 deletions fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const ethUtil = require('ethereumjs-util')
* @prop {Buffer} s EC signature parameter
*/
module.exports = class FakeTransaction extends Transaction {
constructor (data) {
super(data)
constructor (data, opts) {
super(data, opts)

var self = this

Expand Down
36 changes: 28 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const ethUtil = require('ethereumjs-util')
const fees = require('ethereum-common/params.json')
const Common = require('ethereumjs-common')
const BN = ethUtil.BN

// secp256k1n/2
Expand Down Expand Up @@ -41,10 +41,29 @@ const N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46
* @param {Buffer} data.r EC signature parameter
* @param {Buffer} data.s EC signature parameter
* @param {Number} data.chainId EIP 155 chainId - mainnet: 1, ropsten: 3
*
* @param {Array} opts Options
* @param {String|Number} opts.chain The chain for the block [default: 'mainnet']
* @param {String} opts.hardfork Hardfork for the block [default: null, block number-based behaviour]
* @param {Object} opts.common Alternatively pass a Common instance (ethereumjs-common) instead of setting chain/hardfork directly
* */

class Transaction {
constructor (data) {
constructor (data, opts) {
opts = opts || {}

// instantiate Common class instance based on passed options
if (opts.common) {
if (opts.chain) {
throw new Error('Instantiation with both opts.common and opts.chain parameter not allowed!')
}
this._common = opts.common
} else {
let chain = opts.chain ? opts.chain : 'mainnet'
let hardfork = opts.hardfork ? opts.hardfork : 'byzantium'
this._common = new Common(chain, hardfork)
}

data = data || {}
// Define Properties
const fields = [{
Expand Down Expand Up @@ -133,7 +152,6 @@ class Transaction {

// set chainId
this._chainId = chainId || data.chainId || 0
this._homestead = true
}

/**
Expand Down Expand Up @@ -216,7 +234,7 @@ class Transaction {
verifySignature () {
const msgHash = this.hash(false)
// All transaction signatures whose s-value is greater than secp256k1n/2 are considered invalid.
if (this._homestead && new BN(this.s).cmp(N_DIV_2) === 1) {
if (this._common.gteHardfork('homestead') && new BN(this.s).cmp(N_DIV_2) === 1) {
return false
}

Expand Down Expand Up @@ -254,7 +272,9 @@ class Transaction {
const data = this.raw[5]
const cost = new BN(0)
for (let i = 0; i < data.length; i++) {
data[i] === 0 ? cost.iaddn(fees.txDataZeroGas.v) : cost.iaddn(fees.txDataNonZeroGas.v)
data[i] === 0
? cost.iaddn(this._common.param('gasPrices', 'txDataZero'))
: cost.iaddn(this._common.param('gasPrices', 'txDataNonZero'))
}
return cost
}
Expand All @@ -264,9 +284,9 @@ class Transaction {
* @return {BN}
*/
getBaseFee () {
const fee = this.getDataFee().iaddn(fees.txGas.v)
if (this._homestead && this.toCreationAddress()) {
fee.iaddn(fees.txCreation.v)
const fee = this.getDataFee().iaddn(this._common.param('gasPrices', 'tx'))
if (this._common.gteHardfork('homestead') && this.toCreationAddress()) {
fee.iaddn(this._common.param('gasPrices', 'txCreation'))
}
return fee
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "mjbecze <mb@ethdev.com>",
"license": "MPL-2.0",
"dependencies": {
"ethereum-common": "^0.0.18",
"ethereumjs-common": "^0.6.1",
"ethereumjs-util": "^5.0.0"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions test/fake.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const tape = require('tape')
const utils = require('ethereumjs-util')
const Common = require('ethereumjs-common')
const FakeTransaction = require('../fake.js')

// Use private key 0x0000000000000000000000000000000000000000000000000000000000000001 as 'from' Account
Expand Down Expand Up @@ -61,4 +62,16 @@ tape('[FakeTransaction]: Basic functions', function (t) {
var tx = new FakeTransaction(txDataNoFrom)
st.equal(utils.bufferToHex(tx.from), txData.from)
})

t.test('should throw if common and chain options are passed to constructor', function (st) {
var txData = Object.assign({}, txData)
var txOptsInvalid = {
chain: 'mainnet',
common: new Common('mainnet', 'chainstart')
}
st.plan(1)
st.throws(
() => new FakeTransaction(txData, txOptsInvalid)
)
})
})
8 changes: 3 additions & 5 deletions test/transactionRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const tape = require('tape')
const ethUtil = require('ethereumjs-util')
const argv = require('minimist')(process.argv.slice(2))
const testing = require('ethereumjs-testing')
const common = require('ethereum-common/params.json')

var txTests = testing.getTests('transaction', argv)

Expand Down Expand Up @@ -32,10 +31,9 @@ testing.runTests(function (testData, sst, cb) {

try {
var rawTx = ethUtil.toBuffer(testData.rlp)
var tx = new Tx(rawTx)
if (testData.blocknumber !== String(common.homeSteadForkNumber.v)) {
tx._homestead = false
}
var tx = new Tx(rawTx, {
hardfork: testData.blockNumber >= 1150000 ? 'homestead' : 'chainstart'
})
} catch (e) {
sst.equal(undefined, tTx, 'should not have any fields ')
cb()
Expand Down

0 comments on commit bee0a3b

Please sign in to comment.