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

Update common resources dependency #130

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
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
37 changes: 31 additions & 6 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,33 @@ 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'
let supportedHardforks = [
'byzantium',
youfoundron marked this conversation as resolved.
Show resolved Hide resolved
'constantinople'
]
this._common = new Common(chain, hardfork, supportedHardforks)
}

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

// set chainId
this._chainId = chainId || data.chainId || 0
this._homestead = true
this._homestead = this._common.gteHardfork('homestead')
youfoundron marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -254,7 +277,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 +289,9 @@ class Transaction {
* @return {BN}
*/
getBaseFee () {
const fee = this.getDataFee().iaddn(fees.txGas.v)
const fee = this.getDataFee().iaddn(this._common.param('gasPrices', 'tx'))
if (this._homestead && this.toCreationAddress()) {
fee.iaddn(fees.txCreation.v)
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 <[email protected]>",
"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)
)
})
})
4 changes: 0 additions & 4 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 @@ -33,9 +32,6 @@ 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)) {
youfoundron marked this conversation as resolved.
Show resolved Hide resolved
tx._homestead = false
}
} catch (e) {
sst.equal(undefined, tTx, 'should not have any fields ')
cb()
Expand Down