This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from ethereumjs/update-official-transaction-t…
…ests Update official transaction tests
- Loading branch information
Showing
4 changed files
with
79 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ | |
"author": "mjbecze <[email protected]>", | ||
"license": "MPL-2.0", | ||
"dependencies": { | ||
"ethereumjs-common": "^0.6.1", | ||
"ethereumjs-util": "^5.0.0" | ||
"ethereumjs-common": "^1.0.0", | ||
"ethereumjs-util": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"async": "^2.0.0", | ||
|
@@ -33,7 +33,7 @@ | |
"contributor": "^0.1.25", | ||
"coveralls": "^2.11.4", | ||
"documentation": "^8.0.0", | ||
"ethereumjs-testing": "0.0.1", | ||
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git#v1.2.5", | ||
"istanbul": "^0.4.1", | ||
"karma": "^1.1.1", | ||
"karma-browserify": "^5.1.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
const Tx = require('../index.js') | ||
const tape = require('tape') | ||
const ethUtil = require('ethereumjs-util') | ||
const Common = require('ethereumjs-common') | ||
const argv = require('minimist')(process.argv.slice(2)) | ||
const testing = require('ethereumjs-testing') | ||
|
||
var txTests = testing.getTests('transaction', argv) | ||
const forkNames = [ | ||
'Byzantium', | ||
'Constantinople', | ||
'EIP150', | ||
'EIP158', | ||
'Frontier', | ||
'Homestead' | ||
] | ||
|
||
const bufferToHex = ethUtil.bufferToHex | ||
const addHexPrefix = ethUtil.addHexPrefix | ||
const stripHexPrefix = ethUtil.stripHexPrefix | ||
const setLength = ethUtil.setLength | ||
|
||
function addPad (v) { | ||
if (v.length % 2 === 1) { | ||
v = '0' + v | ||
} | ||
return v | ||
const forkNameMap = { | ||
Byzantium: 'byzantium', | ||
Constantinople: 'constantinople', | ||
EIP150: 'tangerineWhistle', | ||
EIP158: 'spuriousDragon', | ||
Frontier: 'chainstart', | ||
Homestead: 'homestead' | ||
} | ||
|
||
function normalizeZero (v) { | ||
if (!v || v === '0x') { | ||
return '0x00' | ||
} else { | ||
return v | ||
} | ||
} | ||
tape('TransactionTests', (t) => { | ||
const fileFilterRegex = argv.file ? new RegExp(argv.file + '[^\\w]') : undefined | ||
|
||
testing.runTests(function (testData, sst, cb) { | ||
var tTx = testData.transaction | ||
testing.getTests('TransactionTests', (filename, testName, testData) => { | ||
t.test(testName, (st) => { | ||
const rawTx = ethUtil.toBuffer(testData.rlp) | ||
|
||
try { | ||
var rawTx = ethUtil.toBuffer(testData.rlp) | ||
var tx = new Tx(rawTx, { | ||
hardfork: testData.blockNumber >= 1150000 ? 'homestead' : 'chainstart' | ||
}) | ||
} catch (e) { | ||
sst.equal(undefined, tTx, 'should not have any fields ') | ||
cb() | ||
return | ||
} | ||
let tx | ||
forkNames.forEach(forkName => { | ||
const forkTestData = testData[forkName] | ||
const shouldBeInvalid = Object.keys(forkTestData).length === 0 | ||
try { | ||
tx = new Tx(rawTx, { | ||
hardfork: forkNameMap[forkName], | ||
chain: 1 | ||
}) | ||
|
||
if (tTx && tx.validate()) { | ||
try { | ||
sst.equal(tx._common instanceof Common, true, '_common class attribute') | ||
sst.equal(bufferToHex(tx.data), addHexPrefix(addPad(stripHexPrefix(tTx.data))), 'data') | ||
sst.equal(normalizeZero(bufferToHex(tx.gasLimit)), tTx.gasLimit, 'gasLimit') | ||
sst.equal(normalizeZero(bufferToHex(tx.gasPrice)), tTx.gasPrice, 'gasPrice') | ||
sst.equal(normalizeZero(bufferToHex(tx.nonce)), tTx.nonce, 'nonce') | ||
sst.equal(normalizeZero(bufferToHex(setLength(tx.r, 32))), normalizeZero(bufferToHex(setLength(tTx.r, 32))), 'r') | ||
sst.equal(normalizeZero(bufferToHex(tx.s)), normalizeZero(bufferToHex(tTx.s)), 's') | ||
sst.equal(normalizeZero(bufferToHex(tx.v)), normalizeZero(bufferToHex(tTx.v)), 'v') | ||
sst.equal(bufferToHex(tx.to), addHexPrefix(tTx.to), 'to') | ||
sst.equal(normalizeZero(bufferToHex(tx.value)), tTx.value, 'value') | ||
sst.equal(normalizeZero(bufferToHex(tx.getSenderAddress())), addHexPrefix(testData.sender), "sender's address") | ||
} catch (e) { | ||
sst.fail(e) | ||
} | ||
} else { | ||
sst.equal(undefined, tTx, 'no tx params in test') | ||
} | ||
cb() | ||
}, txTests, tape) | ||
const sender = tx.getSenderAddress().toString('hex') | ||
const hash = tx.hash().toString('hex') | ||
|
||
const validationErrors = tx.validate(true) | ||
const transactionIsValid = validationErrors.length === 0 | ||
const hashAndSenderAreCorrect = forkTestData && (sender === forkTestData.sender && hash === forkTestData.hash) | ||
|
||
if (shouldBeInvalid) { | ||
st.assert(!transactionIsValid, `Transaction should be invalid on ${forkName}`) | ||
} else { | ||
st.assert(hashAndSenderAreCorrect && transactionIsValid, `Transaction should be valid on ${forkName}`) | ||
} | ||
} catch (e) { | ||
if (shouldBeInvalid) { | ||
st.assert(shouldBeInvalid, `Transaction should be invalid on ${forkName}`) | ||
} else { | ||
st.fail(`Transaction should be valid on ${forkName}`) | ||
} | ||
} | ||
}) | ||
st.end() | ||
}) | ||
}, fileFilterRegex) | ||
.then(() => { | ||
t.end() | ||
}) | ||
}) |