From 710a1463c18dea96bd1f03d7ea62a95702d92d72 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 14 May 2015 14:05:30 +0200 Subject: [PATCH] Squashed 'libjsqrc/ethereumjs/' changes from e908439..16861fc 16861fc version 0.4.2 e4f7dde fixed failing lint 69e7d9d fixed #133 55553d7 add async error test d02a375 Merge branch 'develop' of github.com:ethereum/web3.js into develop ff48da1 fixed callback error issue 471bce1 #133 in progress dc69085 version 0.4.1 2ae0afa critical bugfix 09f2a92 updated examples a0a36e5 Merge branch 'develop' of https://github.com/ethereum/ethereum.js into develop 2b179a8 updated node install test 8f63640 Merge pull request #203 from frozeman/develop 22ef4a1 added estimateGas method cd2e462 version 0.4.0 ae7762a Merge pull request #198 from ethereum/minerMethods d1e1206 removed byte formatter methods 6fd17d9 merged develop 373fd1b Merge pull request #201 from debris/batch 274fc29 Merge pull request #200 from debris/contract_overhaul b2ff2ff Merge branch 'contract_overhaul' into batch 99a3bda removed redundant lines 7cafac9 Merge branch 'develop' into contract_overhaul fa8db32 Merge pull request #202 from debris/async_properties 5f9272c asyncGet -> getAsync cd0df61 gulp 80c0792 async getting properties, remove property setters 744bb9e batch for requests with calls/transacts 8636077 initial support for batch requests a0b9cfb contract async call && transact 1272780 sendTransaction && call async 6afb1f9 Merge branch 'async-contract-calls' of https://github.com/niran/web3.js into contract_overhaul 611f640 asynchronous contract creation, breaking changes e3796bf merged develop edf808a add getWork and submitWork functions and toXBytes utils 9812b01 Merge branch 'blockFilterProposal' of https://github.com/ethereum/ethereum.js into develop 6f466f6 add miner functions 3af8e27 add newblockfilter Proposal e86552f Use async contract calls when a callback is passed git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: 16861fc1944cca721672bb8faa81909d69be2b99 --- abi.formatConstructorParams.js | 106 --------------------------------- async.js | 69 +++++++++++++++++++++ batch.js | 86 ++++++++++++++++++++++++++ contract.js | 100 +++++++++++++++++++++++++------ method.request.js | 23 +++++++ node/app.js | 2 +- node/package.json | 2 +- polling.js | 21 +++++-- web3.eth.blockNumber.js | 20 +++++++ web3.eth.call.js | 41 +++++++++++++ web3.eth.contract.js | 21 +++---- web3.eth.estimateGas.js | 25 ++++++++ web3.eth.filter.js | 10 +++- web3.eth.getWork.js | 16 +++++ web3.eth.submitWork.js | 17 ++++++ 15 files changed, 413 insertions(+), 146 deletions(-) delete mode 100644 abi.formatConstructorParams.js create mode 100644 async.js create mode 100644 batch.js create mode 100644 method.request.js create mode 100644 web3.eth.call.js create mode 100644 web3.eth.estimateGas.js create mode 100644 web3.eth.getWork.js create mode 100644 web3.eth.submitWork.js diff --git a/abi.formatConstructorParams.js b/abi.formatConstructorParams.js deleted file mode 100644 index 9113f02c6..000000000 --- a/abi.formatConstructorParams.js +++ /dev/null @@ -1,106 +0,0 @@ -var chai = require('chai'); -var assert = require('assert'); -var abi = require('../lib/solidity/abi'); - -describe('lib/solidity/abi', function () { - describe('formatConstructorParams', function () { - it('should format uint256 properly', function () { - // given - var description = [{ - "name": "test", - "type": "constructor", - "inputs": [{ - "name": "a", - "type": "uint256" - } - ] - }]; - - // when - var bytes = abi.formatConstructorParams(description, [2]); - - // then - assert.equal(bytes, '0000000000000000000000000000000000000000000000000000000000000002'); - }); - - it('should not find matching constructor', function () { - // given - var description = [{ - "name": "test", - "type": "constructor", - "inputs": [{ - "name": "a", - "type": "uint256" - } - ] - }]; - - // when - var bytes = abi.formatConstructorParams(description, []); - - // then - assert.equal(bytes, ''); - }); - - it('should not find matching constructor2', function () { - // given - var description = [{ - "name": "test", - "type": "constructor", - "inputs": [{ - "name": "a", - "type": "uint256" - } - ] - }]; - - // when - var bytes = abi.formatConstructorParams(description, [1,2]); - - // then - assert.equal(bytes, ''); - }); - - it('should not find matching constructor3', function () { - // given - var description = [{ - "name": "test", - "type": "function", - "inputs": [{ - "name": "a", - "type": "uint256" - } - ] - }]; - - // when - var bytes = abi.formatConstructorParams(description, [2]); - - // then - assert.equal(bytes, ''); - }); - - it('should find matching constructor with multiple args', function () { - // given - var description = [{ - "name": "test", - "type": "constructor", - "inputs": [{ - "name": "a", - "type": "uint256" - }, { - "name": "b", - "type": "uint256" - }] - }]; - - // when - var bytes = abi.formatConstructorParams(description, ['1', '5']); - - // then - assert.equal(bytes, '00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000005'); - }); - }); -}); - - diff --git a/async.js b/async.js new file mode 100644 index 000000000..77497603c --- /dev/null +++ b/async.js @@ -0,0 +1,69 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); + +// use sendTransaction as dummy +var method = 'sendTransaction'; + +var tests = [{ + result: '0xb', + formattedResult: '0xb', + call: 'eth_'+ method +}]; + +describe('async', function () { + tests.forEach(function (test, index) { + it('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, [{}]); + }); + + // when + web3.eth[method]({}, function(error, result){ + + // then + assert.isNull(error); + assert.strictEqual(test.formattedResult, result); + + done(); + }); + + }); + + it('error test: ' + index, function (done) { + + // given + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + provider.injectError({ + message: test.result, + code: -32603 + }); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, test.call); + assert.deepEqual(payload.params, [{}]); + }); + + // when + web3.eth[method]({}, function(error, result){ + + // then + assert.isUndefined(result); + assert.strictEqual(test.formattedResult, error.message); + + done(); + }); + + }); + }); +}); + diff --git a/batch.js b/batch.js new file mode 100644 index 000000000..69ae8fd58 --- /dev/null +++ b/batch.js @@ -0,0 +1,86 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); +var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var bn = require('bignumber.js'); + +describe('lib/web3/batch', function () { + describe('execute', function () { + it('should execute batch request', function (done) { + + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + web3.reset(); + + var result = '0x126'; + var result2 = '0x127'; + provider.injectBatchResults([result, result2]); + + var counter = 0; + var callback = function (err, r) { + counter++; + assert.deepEqual(new bn(result), r); + }; + + var callback2 = function (err, r) { + assert.equal(counter, 1); + assert.deepEqual(new bn(result2), r); + done(); + }; + + var batch = web3.createBatch(); + batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback)); + batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000005', 'latest', callback2)); + batch.execute(); + }); + + it('should execute batch request', function (done) { + + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + web3.reset(); + + var abi = [{ + "name": "balance(address)", + "type": "function", + "inputs": [{ + "name": "who", + "type": "address" + }], + "constant": true, + "outputs": [{ + "name": "value", + "type": "uint256" + }] + }]; + + + var address = '0x0000000000000000000000000000000000000000'; + var result = '0x126'; + var result2 = '0x0000000000000000000000000000000000000000000000000000000000000123'; + var signature = '0x001122334455'; + + // TODO: fix this, maybe in browser sha3? + provider.injectResult(signature); + + var counter = 0; + var callback = function (err, r) { + counter++; + assert.deepEqual(new bn(result), r); + }; + + var callback2 = function (err, r) { + assert.equal(counter, 1); + assert.deepEqual(new bn(result2), r); + done(); + }; + + var batch = web3.createBatch(); + batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback)); + batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2)); + provider.injectBatchResults([result, result2]); + batch.execute(); + }); + }); +}); + diff --git a/contract.js b/contract.js index 0dcaa1003..a46a8cab3 100644 --- a/contract.js +++ b/contract.js @@ -116,8 +116,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); var res = 0; contract.Changed({from: address}).watch(function(err, result) { @@ -155,8 +154,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.balance(address); }); @@ -186,8 +184,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.send(address, 17); }); @@ -218,8 +215,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.balance(address, {from: address, gas: 50000}); @@ -251,8 +247,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.balance.call(address, {from: address, gas: 50000}); @@ -287,8 +282,7 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.send(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}); }); @@ -322,12 +316,48 @@ describe('web3.eth.contract', function () { } }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); + var contract = web3.eth.contract(desc).at(address); contract.send.sendTransaction(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}); }); + it('should explicitly sendTransaction with optional params and call callback without error', function (done) { + var provider = new FakeHttpProvider(); + web3.setProvider(provider); + web3.reset(); + var sha3 = '0x5131231231231231231231'; + var address = '0x1234567890123456789012345678901234567890'; + provider.injectResult(sha3); + var step = 0; + provider.injectValidation(function (payload) { + if (step === 0) { + step = 1; + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, 'web3_sha3'); + assert.equal(payload.params[0], web3.fromAscii('send(address,uint256)')); + } else if (step === 1) { + assert.equal(payload.method, 'eth_sendTransaction'); + assert.deepEqual(payload.params, [{ + data: sha3.slice(0, 10) + + '0000000000000000000000001234567890123456789012345678901234567890' + + '0000000000000000000000000000000000000000000000000000000000000011' , + to: address, + from: address, + gas: '0xc350', + gasPrice: '0xbb8', + value: '0x2710' + }]); + } + }); + + var contract = web3.eth.contract(desc).at(address); + + contract.send.sendTransaction(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}, function (err) { + assert.equal(err, null); + done(); + }); + }); + it('should call testArr method and properly parse result', function () { var provider = new FakeHttpProvider2(); web3.setProvider(provider); @@ -356,12 +386,48 @@ describe('web3.eth.contract', function () { step++; }); - var Contract = web3.eth.contract(desc); - var contract = new Contract(address); - + var contract = web3.eth.contract(desc).at(address); var result = contract.testArr([3]); assert.deepEqual(new BigNumber(5), result); }); + + it('should call testArr method, properly parse result and return the result async', function (done) { + var provider = new FakeHttpProvider2(); + web3.setProvider(provider); + web3.reset(); + var sha3 = '0x5131231231231231231231'; + var address = '0x1234567890123456789012345678901234567890'; + provider.injectResultList([{ + result: sha3 + }, { + result: '0x0000000000000000000000000000000000000000000000000000000000000005' + }]); + var step = 0; + provider.injectValidation(function (payload) { + if (step === 1) { // getting sha3 is first + assert.equal(payload.method, 'eth_call'); + assert.deepEqual(payload.params, [{ + data: sha3.slice(0, 10) + + '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003', + to: address + }, + 'latest' + ]); + } + step++; + }); + + var contract = web3.eth.contract(desc).at(address); + + contract.testArr([3], function (err, result) { + assert.deepEqual(new BigNumber(5), result); + done(); + }); + + }); }); }); + diff --git a/method.request.js b/method.request.js new file mode 100644 index 000000000..00bf52cb7 --- /dev/null +++ b/method.request.js @@ -0,0 +1,23 @@ +var chai = require('chai'); +var assert = chai.assert; +var web3 = require('../index'); + +describe('lib/web3/method', function () { + describe('request', function () { + it('should create proper request', function () { + + var callback = function (err, result) {}; + var expected = { + method: 'eth_getBalance', + callback: callback, + params: ['0x0000000000000000000000000000000000000000', 'latest'], + }; + + var request = web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback); + + expected.format = request.format; + assert.deepEqual(request, expected); + }); + }); +}); + diff --git a/node/app.js b/node/app.js index c3fd489a0..9ec310f44 100644 --- a/node/app.js +++ b/node/app.js @@ -1,4 +1,4 @@ -var web3 = require('ethereum.js'); +var web3 = require('web3'); console.log(web3.version.api); diff --git a/node/package.json b/node/package.json index 4c56b2c10..310ad2e04 100644 --- a/node/package.json +++ b/node/package.json @@ -9,6 +9,6 @@ "author": "", "license": "ISC", "dependencies": { - "ethereum.js": "ethereum/ethereum.js#master" + "web3": "ethereum/web3.js#master" } } diff --git a/polling.js b/polling.js index ea7dd9829..8bd2b041c 100644 --- a/polling.js +++ b/polling.js @@ -6,15 +6,26 @@ var utils = require('../lib/utils/utils'); var tests = [{ protocol: 'eth', - args: ['pending'], + args: ['latest'], firstResult: 1, firstPayload: { method: "eth_newBlockFilter", - params: [ - "pending" - ] + params: [] }, - secondResult: [null], + secondResult: ['0x1234'], + secondPayload: { + method: "eth_getFilterChanges" + } +}, +{ + protocol: 'eth', + args: ['pending'], + firstResult: 1, + firstPayload: { + method: "eth_newPendingTransactionFilter", + params: [] + }, + secondResult: ['0x1234'], secondPayload: { method: "eth_getFilterChanges" } diff --git a/web3.eth.blockNumber.js b/web3.eth.blockNumber.js index dfd73f57e..dbe7f1c44 100644 --- a/web3.eth.blockNumber.js +++ b/web3.eth.blockNumber.js @@ -32,6 +32,26 @@ describe('web3.eth', function () { // then assert.strictEqual(test.formattedResult, result); }); + + it('async get property 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, []); + }); + + // when + web3.eth.getBlockNumber(function (err, result) { + assert.strictEqual(test.formattedResult, result); + done(); + }); + + }); }); }); }); diff --git a/web3.eth.call.js b/web3.eth.call.js new file mode 100644 index 000000000..d79bd64d4 --- /dev/null +++ b/web3.eth.call.js @@ -0,0 +1,41 @@ +var web3 = require('../index'); +var testMethod = require('./helpers/test.method.js'); + +var method = 'call'; + +var tests = [{ + args: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: 11, + gasPrice: 11 + }], + formattedArgs: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: '0xb', + gasPrice: '0xb' + }, 'latest'], + result: '0x31981', + formattedResult: '0x31981', + call: 'eth_'+ method +},{ + args: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: 11, + gasPrice: 11 + }, 11], + formattedArgs: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: '0xb', + gasPrice: '0xb' + }, '0xb'], + result: '0x31981', + formattedResult: '0x31981', + call: 'eth_'+ method +}]; + +testMethod.runTests('eth', method, tests); + diff --git a/web3.eth.contract.js b/web3.eth.contract.js index 651810b69..b089f0243 100644 --- a/web3.eth.contract.js +++ b/web3.eth.contract.js @@ -25,8 +25,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('function', typeof myCon.test); @@ -54,8 +53,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('function', typeof myCon.test); @@ -97,8 +95,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('function', typeof myCon.test); @@ -142,8 +139,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('function', typeof myCon.test); @@ -171,8 +167,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('undefined', typeof myCon.test); @@ -200,8 +195,7 @@ describe('web3.eth.contract', function() { var address = '0x1234567890123456789012345678901234567890'; // when - var Con = contract(description); - var myCon = new Con(address); + var myCon = contract(description).at(address); // then assert.equal('function', typeof myCon.test); @@ -233,8 +227,7 @@ describe('web3.eth.contract', function() { done(); }); - var Con = contract(description); - var myCon = new Con({data: code}, 2); + var myCon = contract(description).new(2, {data: code}); }); }); diff --git a/web3.eth.estimateGas.js b/web3.eth.estimateGas.js new file mode 100644 index 000000000..efa6bf310 --- /dev/null +++ b/web3.eth.estimateGas.js @@ -0,0 +1,25 @@ +var web3 = require('../index'); +var testMethod = require('./helpers/test.method.js'); + +var method = 'estimateGas'; + +var tests = [{ + args: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: 11, + gasPrice: 11 + }], + formattedArgs: [{ + to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', + data: '0x23455654', + gas: '0xb', + gasPrice: '0xb' + }], + result: '0x31981', + formattedResult: 203137, + call: 'eth_'+ method +}]; + +testMethod.runTests('eth', method, tests); + diff --git a/web3.eth.filter.js b/web3.eth.filter.js index 7a355b50c..9f196b349 100644 --- a/web3.eth.filter.js +++ b/web3.eth.filter.js @@ -37,11 +37,17 @@ var tests = [{ formattedResult: '0xf', call: 'eth_newFilter' },{ - args: ['pending'], - formattedArgs: ['pending'], + args: ['latest'], + formattedArgs: [], result: '0xf', formattedResult: '0xf', call: 'eth_newBlockFilter' +},{ + args: ['pending'], + formattedArgs: [], + result: '0xf', + formattedResult: '0xf', + call: 'eth_newPendingTransactionFilter' }]; describe('web3.eth', function () { diff --git a/web3.eth.getWork.js b/web3.eth.getWork.js new file mode 100644 index 000000000..181c1894b --- /dev/null +++ b/web3.eth.getWork.js @@ -0,0 +1,16 @@ +var chai = require('chai'); +var web3 = require('../index'); +var testMethod = require('./helpers/test.method.js'); + +var method = 'getWork'; + +var tests = [{ + args: [], + formattedArgs: [], + result: true, + formattedResult: true, + call: 'eth_'+ method +}]; + +testMethod.runTests('eth', method, tests); + diff --git a/web3.eth.submitWork.js b/web3.eth.submitWork.js new file mode 100644 index 000000000..3751c8073 --- /dev/null +++ b/web3.eth.submitWork.js @@ -0,0 +1,17 @@ +var chai = require('chai'); +var web3 = require('../index'); +var testMethod = require('./helpers/test.method.js'); + +var method = 'submitWork'; + +var tests = [ +{ + args: ['0x567890abcdef5555', '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', '0xcdef1234567890abcdef1234567890abcdef0x1234567890abcf1234567890ab'], + formattedArgs: ['0x567890abcdef5555', '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', '0xcdef1234567890abcdef1234567890abcdef0x1234567890abcf1234567890ab'], + result: true, + formattedResult: true, + call: 'eth_'+ method +}]; + +testMethod.runTests('eth', method, tests); +