From b3b95c5f986dd08cbe8ef919c758b17b2ae88136 Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Mon, 23 Mar 2015 18:50:33 +0100 Subject: [PATCH] add getBlock test --- lib/web3/formatters.js | 2 + lib/web3/property.js | 75 +++++++ test/eth.accounts.js | 38 ++++ test/eth.blockNumber.js | 38 ++++ test/eth.coinbase.js | 38 ++++ test/eth.getBlock.js | 187 ++++++++++++++++++ test/formatters.inputTransactionFormatter.js | 8 +- test/formatters.outputTransactionFormatter.js | 8 +- 8 files changed, 389 insertions(+), 5 deletions(-) create mode 100644 lib/web3/property.js create mode 100644 test/eth.accounts.js create mode 100644 test/eth.blockNumber.js create mode 100644 test/eth.coinbase.js create mode 100644 test/eth.getBlock.js diff --git a/lib/web3/formatters.js b/lib/web3/formatters.js index dbbcc5e..6422720 100644 --- a/lib/web3/formatters.js +++ b/lib/web3/formatters.js @@ -69,6 +69,8 @@ var inputTransactionFormatter = function (options){ * @returns {Object} transaction */ var outputTransactionFormatter = function (tx){ + tx.blockNumber = utils.toDecimal(tx.blockNumber); + tx.transactionIndex = utils.toDecimal(tx.transactionIndex); tx.gas = utils.toDecimal(tx.gas); tx.gasPrice = utils.toBigNumber(tx.gasPrice); tx.value = utils.toBigNumber(tx.value); diff --git a/lib/web3/property.js b/lib/web3/property.js new file mode 100644 index 0000000..202aa37 --- /dev/null +++ b/lib/web3/property.js @@ -0,0 +1,75 @@ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** + * @file method.js + * @author Fabian Vogelsteller + * @date 2015 + */ + +var utils = require('../utils/utils'); +var errors = require('./errors'); + +var Property = function (options) { + this.name = options.name; + this.getter = options.getter; + this.setter = options.setter; + this.outputFormatter = options.outputFormatter; + this.inputFormatter = options.inputFormatter; +}; + +/** + * Should be called to format input args of method + * + * @method formatInput + * @param {Array} + * @return {Array} + */ +Property.prototype.formatInput = function (arg) { + return this.inputFormatter ? this.inputFormatter(arg) : arg; +}; + +/** + * Should be called to format output(result) of method + * + * @method formatOutput + * @param {Object} + * @return {Object} + */ +Property.prototype.formatOutput = function (result) { + return this.outputFormatter && result !== null ? this.outputFormatter(result) : result; +}; + +/** + * Should attach function to method + * + * @method attachToObject + * @param {Object} + * @param {Function} + */ +Property.prototype.attachToObject = function (obj, proto) { + var name = this.name.split('.'); + if (name.length > 1) { + obj[name[0]] = obj[name[0]] || {}; + + Object.defineProperty(obj[name[0]], name[1], proto); + } else { + Object.defineProperty(obj, name[0], proto); + } +}; + +module.exports = Property; + diff --git a/test/eth.accounts.js b/test/eth.accounts.js new file mode 100644 index 0000000..acc1ace --- /dev/null +++ b/test/eth.accounts.js @@ -0,0 +1,38 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +var method = 'accounts'; + +var tests = [{ + result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], + formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], + call: 'eth_'+ method +}]; + +describe('eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('property test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, []); + }); + + // when + var result = web3.eth[method]; + + // then + assert.deepEqual(test.formattedResult, result); + }); + }); + }); +}); + diff --git a/test/eth.blockNumber.js b/test/eth.blockNumber.js new file mode 100644 index 0000000..0a63f83 --- /dev/null +++ b/test/eth.blockNumber.js @@ -0,0 +1,38 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +var method = 'blockNumber'; + +var tests = [{ + result: '0xb', + formattedResult: 11, + call: 'eth_'+ method +}]; + +describe('eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('property test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, []); + }); + + // when + var result = web3.eth[method]; + + // then + assert.strictEqual(test.formattedResult, result); + }); + }); + }); +}); + diff --git a/test/eth.coinbase.js b/test/eth.coinbase.js new file mode 100644 index 0000000..d0dde99 --- /dev/null +++ b/test/eth.coinbase.js @@ -0,0 +1,38 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +var method = 'coinbase'; + +var tests = [{ + result: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', + formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', + call: 'eth_'+ method +}]; + +describe('eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('property test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, []); + }); + + // when + var result = web3.eth[method]; + + // then + assert.deepEqual(test.formattedResult, result); + }); + }); + }); +}); + diff --git a/test/eth.getBlock.js b/test/eth.getBlock.js new file mode 100644 index 0000000..883d213 --- /dev/null +++ b/test/eth.getBlock.js @@ -0,0 +1,187 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var BigNumber = require('bignumber.js'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +var method = 'getBlock'; + +var blockResult = { + "number": "0x1b4", + "hash": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5", + "nonce": "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "logsBloom": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff", + "miner": "0x4e65fda2159562a496f9f3522f89122a3088497a", + "difficulty": "0x027f07", + "totalDifficulty": "0x027f07", + "size": "0x027f07", + "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": "0x9f759", + "minGasPrice": "0x9f759", + "gasUsed": "0x9f759", + "timestamp": "0x54e34e8e", + "transactions": ['0x460cfb8472af2c5fd05b5a2','0x460cfb8472af2c5fd05b5a2'], + "uncles": ["0x460cfb8472af2c5fd05b5a2", "0xd5460cfb8472af2c5fd05b5a2"] +}; +var formattedBlockResult = { + "number": 436, + "hash": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5", + "nonce": "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "logsBloom": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff", + "miner": "0x4e65fda2159562a496f9f3522f89122a3088497a", + "difficulty": new BigNumber(163591), + "totalDifficulty": new BigNumber(163591), + "size": 163591, + "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": 653145, + "minGasPrice": new BigNumber(653145), + "gasUsed": 653145, + "timestamp": 1424182926, + "transactions": ['0x460cfb8472af2c5fd05b5a2','0x460cfb8472af2c5fd05b5a2'], + "uncles": ["0x460cfb8472af2c5fd05b5a2", "0xd5460cfb8472af2c5fd05b5a2"] +}; +var blockResultWithTx = { + "number": "0x1b4", + "hash": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5", + "nonce": "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "logsBloom": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff", + "miner": "0x4e65fda2159562a496f9f3522f89122a3088497a", + "difficulty": "0x027f07", + "totalDifficulty": "0x027f07", + "size": "0x027f07", + "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": "0x9f759", + "minGasPrice": "0x9f759", + "gasUsed": "0x9f759", + "timestamp": "0x54e34e8e", + "transactions": [{ + "status": "mined", + "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", + "nonce":"0x", + "blockHash": "0x6fd9e2a26ab", + "blockNumber": "0x15df", + "transactionIndex": "0x1", + "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", + "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", + "value":"0x7f110", + "gas": "0x7f110", + "gasPrice":"0x09184e72a000", + "input":"0x603880600c6000396000f30060", + }], + "uncles": ["0x460cfb8472af2c5fd05b5a2", "0xd5460cfb8472af2c5fd05b5a2"] +}; +var formattedBlockResultWithTx = { + "number": 436, + "hash": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5", + "nonce": "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2", + "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "logsBloom": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331", + "transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + "stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff", + "miner": "0x4e65fda2159562a496f9f3522f89122a3088497a", + "difficulty": new BigNumber(163591), + "totalDifficulty": new BigNumber(163591), + "size": 163591, + "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": 653145, + "minGasPrice": new BigNumber(653145), + "gasUsed": 653145, + "timestamp": 1424182926, + "transactions": [{ + "status": "mined", + "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", + "nonce":"0x", + "blockHash": "0x6fd9e2a26ab", + "blockNumber": 5599, + "transactionIndex": 1, + "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", + "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", + "value": new BigNumber(520464), + "gas": 520464, + "gasPrice": new BigNumber(10000000000000), + "input":"0x603880600c6000396000f30060", + }], + "uncles": ["0x460cfb8472af2c5fd05b5a2", "0xd5460cfb8472af2c5fd05b5a2"] +}; + +var tests = [{ + args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], + formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', false], + result: blockResult, + formattedResult: formattedBlockResult, + call: 'eth_'+ method + 'ByHash' +},{ + args: [436], + formattedArgs: ['0x1b4', false], + result: blockResult, + formattedResult: formattedBlockResult, + call: 'eth_'+ method + 'ByNumber' +},{ + args: [436, true], + formattedArgs: ['0x1b4', true], + result: blockResultWithTx, + formattedResult: formattedBlockResultWithTx, + call: 'eth_'+ method + 'ByNumber' +}]; + +describe('eth', function () { + describe(method, function () { + tests.forEach(function (test, index) { + it('sync test: ' + index, function () { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, test.formattedArgs); + }); + + // when + var result = web3.eth[method].apply(null, test.args.slice(0)); + + // then + assert.deepEqual(test.formattedResult, result); + }); + + it('async test: ' + index, function (done) { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectResult(test.result); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, test.formattedArgs); + }); + var callback = function (err, result) { + assert.deepEqual(test.formattedResult, result); + done(); + }; + + var args = test.args.slice(0); + args.push(callback); + + // when + web3.eth[method].apply(null, args); + }); + }); + }); +}); + diff --git a/test/formatters.inputTransactionFormatter.js b/test/formatters.inputTransactionFormatter.js index 69b216b..2f7f8c2 100644 --- a/test/formatters.inputTransactionFormatter.js +++ b/test/formatters.inputTransactionFormatter.js @@ -8,19 +8,19 @@ describe('formatters', function () { it('should return the correct value', function () { assert.deepEqual(formatters.inputTransactionFormatter({ - data: '0x34234kjh23kj4234', + data: '0x34234bf23bf4234', value: new BigNumber(100), from: '0x00000', to: '0x00000', gas: 1000, - gasPrice: new BigNumber(1000), + gasPrice: new BigNumber(1000) }), { - data: '0x34234kjh23kj4234', + data: '0x34234bf23bf4234', value: '0x64', from: '0x00000', to: '0x00000', gas: '0x3e8', - gasPrice: '0x3e8', + gasPrice: '0x3e8' }); }); }); diff --git a/test/formatters.outputTransactionFormatter.js b/test/formatters.outputTransactionFormatter.js index f42ed84..b1eaff5 100644 --- a/test/formatters.outputTransactionFormatter.js +++ b/test/formatters.outputTransactionFormatter.js @@ -12,7 +12,10 @@ describe('formatters', function () { to: '0x00000', value: '0x3e8', gas: '0x3e8', - gasPrice: '0x3e8' + gasPrice: '0x3e8', + transactionIndex: '0x1', + blockNumber: '0x3e8', + blockHash: '0x34234bf23bf4234' }), { input: '0x34234kjh23kj4234', from: '0x00000', @@ -20,6 +23,9 @@ describe('formatters', function () { value: new BigNumber(1000), gas: 1000, gasPrice: new BigNumber(1000), + blockNumber: 1000, + blockHash: '0x34234bf23bf4234', + transactionIndex: 1 }); }); });