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

Commit

Permalink
Merge pull request #138 from ethereumjs/114
Browse files Browse the repository at this point in the history
Update Travis to run on Node v8 and v10; Add tests for inherited FakeTransaction functions
  • Loading branch information
holgerd77 committed Apr 16, 2019
2 parents 8e425ec + 37cc36f commit 80eb355
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Icon
Network Trash Folder
Temporary Items
.apdisk
.idea


### Node ###
Expand Down Expand Up @@ -76,4 +77,3 @@ jspm_packages

# Yarn Integrity file
.yarn-integrity

10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "6"
- "8"
- "10"
env:
- CXX=g++-4.8
addons:
Expand All @@ -21,16 +21,16 @@ matrix:
fast_finish: true
include:
- os: linux
node_js: "6"
node_js: "8"
env: CXX=g++-4.8 TEST_SUITE=coveralls
- os: linux
node_js: "6"
node_js: "8"
env: CXX=g++-4.8 TEST_SUITE=lint
- os: linux
node_js: "8"
node_js: "10"
env: CXX=g++-4.8 TEST_SUITE=coveralls
- os: linux
node_js: "8"
node_js: "10"
env: CXX=g++-4.8 TEST_SUITE=lint
# - os: linux
# node_js: "4"
Expand Down
83 changes: 83 additions & 0 deletions test/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var txData = {
tape('[FakeTransaction]: Basic functions', function (t) {
t.test('instantiate with from / create a hash', function (st) {
st.plan(3)

var tx = new FakeTransaction(txData)
var hash = tx.hash()
var cmpHash = Buffer.from('f74b039f6361c4351a99a7c6a10867369fe6701731d85dc07c15671ac1c1b648', 'hex')
Expand Down Expand Up @@ -74,4 +75,86 @@ tape('[FakeTransaction]: Basic functions', function (t) {
() => new FakeTransaction(txData, txOptsInvalid)
)
})

t.test('should return toCreationAddress', st => {
const tx = new FakeTransaction(txData)
const txNoTo = new FakeTransaction({ ...txData, to: '' })
st.plan(2)
st.equal(tx.toCreationAddress(), false, 'tx is not "to" creation address')
st.equal(txNoTo.toCreationAddress(), true, 'tx is "to" creation address')
})

t.test('should return getChainId', st => {
const tx = new FakeTransaction(txData)
const txWithV = new FakeTransaction({ ...txData, v: '0x28' })
const txWithChainId = new FakeTransaction({ ...txData, chainId: 4 })
st.plan(3)
st.equal(tx.getChainId(), 0, 'should return correct chainId')
st.equal(txWithV.getChainId(), 2, 'should return correct chainId')
st.equal(txWithChainId.getChainId(), 4, 'should return correct chainId')
})

t.test('should getSenderAddress and getSenderPublicKey', st => {
const tx = new FakeTransaction(txData)
st.plan(2)
st.equal(tx._from.toString('hex'), '7e5f4552091a69125d5dfcb7b8c2659029395bdf', 'this._from is set in FakeTransaction')
st.equal(tx.getSenderAddress().toString('hex'), '7e5f4552091a69125d5dfcb7b8c2659029395bdf', 'should return correct address')
})

t.test('should verifySignature', st => {
const tx = new FakeTransaction(txData)
const txWithWrongSignature = new FakeTransaction({
...txData,
r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex')
})
st.plan(2)
st.true(tx.verifySignature(), 'signature is valid')
st.false(txWithWrongSignature.verifySignature(), 'signature is not valid')
})

t.test('should sign', st => {
const tx = new FakeTransaction(txData)
tx.sign(Buffer.from('164122e5d39e9814ca723a749253663bafb07f6af91704d9754c361eb315f0c1', 'hex'))
st.plan(3)
st.equal(tx.r.toString('hex'), 'c10062450d68caa5a688e2b6930f34f8302064afe6e1ba7f6ca459115a31d3b8', 'r should be valid')
st.equal(tx.s.toString('hex'), '31718e6bf821a98d35b0d9cd66ea86f91f420c3c4658f60c607222de925d222a', 's should be valid')
st.equal(tx.v.toString('hex'), '1c', 'v should be valid')
})

t.test('should getDataFee', st => {
const tx = new FakeTransaction({...txData, data: '0x00000001'})
st.plan(1)
st.equal(tx.getDataFee().toString(), '80', 'data fee should be correct')
})

t.test('should getBaseFee', st => {
const tx = new FakeTransaction({...txData, data: '0x00000001'})
st.plan(1)
st.equal(tx.getBaseFee().toString(), '21080', 'base fee should be correct')
})

t.test('should getUpfrontCost', st => {
const tx = new FakeTransaction({...txData, gasLimit: '0x6464', gasPrice: '0x2'})
st.plan(1)
st.equal(tx.getUpfrontCost().toString(), '51400', 'base fee should be correct')
})

t.test('should validate', st => {
const tx = new FakeTransaction(txData)
const txWithWrongSignature = new FakeTransaction({
...txData,
r: Buffer.from('abcd1558260ac737ea6d800906c6d085a801e5e0f0952bf93978d6fa468fbdff', 'hex')
})
const txWithLowLimit = new FakeTransaction({
...txData,
gasLimit: '0x1'
})
st.plan(6)
st.true(tx.validate(), 'tx should be valid')
st.false(txWithWrongSignature.validate(), 'tx should be invalid')
st.false(txWithLowLimit.validate(), 'tx should be invalid')
st.equal(tx.validate(true), '', 'tx should return no errors')
st.equal(txWithWrongSignature.validate(true), 'Invalid Signature', 'tx should return correct error')
st.equal(txWithLowLimit.validate(true), 'gas limit is too low. Need at least 21464', 'tx should return correct error')
})
})

0 comments on commit 80eb355

Please sign in to comment.