From 572d268f2860773562ce83719432c05af47c7528 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Sun, 26 Apr 2015 17:40:35 +0200 Subject: [PATCH] Squashed 'libjsqrc/ethereumjs/' changes from c74c854..3b799d1 3b799d1 fixed typos c5f6379 version 0.3.3 d795d36 fixed trimming call output prefix a2f561f fixed calling contract only with array param 4912ec6 tests for decoding solidity params 2b4fd21 solidity param encoding tests git-subtree-dir: libjsqrc/ethereumjs git-subtree-split: 3b799d128452639463424c657956ee90a28daec6 --- coder.decodeParam.js | 69 ++++++++++++++++++++++----- coder.encodeParam.js | 92 +++++++++++++++++++++++++++++++----- contract.js | 49 +++++++++++++++++++ helpers/FakeHttpProvider.js | 8 +++- helpers/FakeHttpProvider2.js | 27 +++++++++++ 5 files changed, 219 insertions(+), 26 deletions(-) create mode 100644 helpers/FakeHttpProvider2.js diff --git a/coder.decodeParam.js b/coder.decodeParam.js index 0a67bda02..3eea9dd6f 100644 --- a/coder.decodeParam.js +++ b/coder.decodeParam.js @@ -1,23 +1,68 @@ var chai = require('chai'); var assert = chai.assert; var coder = require('../lib/solidity/coder'); +var BigNumber = require('bignumber.js'); +var bn = BigNumber; -var tests = [ - { type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000001', expected: 1}, - { type: 'int', value: '0000000000000000000000000000000000000000000000000000000000000010', expected: 16}, - { type: 'int', value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: -1}, - { type: 'bytes32', value: '6761766f66796f726b0000000000000000000000000000000000000000000000', expected: 'gavofyork'}, - { type: 'bytes', value: '0000000000000000000000000000000000000000000000000000000000000009' + - '6761766f66796f726b0000000000000000000000000000000000000000000000', expected: 'gavofyork'} -]; describe('lib/solidity/coder', function () { describe('decodeParam', function () { - tests.forEach(function (test) { - it('should turn ' + test.value + ' to ' + test.expected, function () { - assert.equal(coder.decodeParam(test.type, test.value), test.expected); + var test = function (t) { + it('should turn ' + t.value + ' to ' + t.expected, function () { + assert.deepEqual(coder.decodeParam(t.type, t.value), t.expected); }); - }); + }; + + + test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int256', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'bytes32', expected: 'gavofyork', value: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'bytes', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000009' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int[]', expected: [new bn(1), new bn(2), new bn(3)], + value: '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + }); +}); + +describe('lib/solidity/coder', function () { + describe('decodeParams', function () { + var test = function (t) { + it('should turn ' + t.values + ' to ' + t.expected, function () { + assert.deepEqual(coder.decodeParams(t.types, t.values), t.expected); + }); + }; + + + test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['bytes32', 'int'], expected: ['gavofyork', new bn(5)], + values: '6761766f66796f726b0000000000000000000000000000000000000000000000' + + '0000000000000000000000000000000000000000000000000000000000000005'}); + test({ types: ['int', 'bytes32'], expected: [new bn(5), 'gavofyork'], + values: '0000000000000000000000000000000000000000000000000000000000000005' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], expected: [new bn(1), 'gavofyork', new bn(2), new bn(3), new bn(4), + [new bn(5), new bn(6), new bn(7)]], + values: '0000000000000000000000000000000000000000000000000000000000000009' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000004' + + '6761766f66796f726b0000000000000000000000000000000000000000000000' + + '0000000000000000000000000000000000000000000000000000000000000005' + + '0000000000000000000000000000000000000000000000000000000000000006' + + '0000000000000000000000000000000000000000000000000000000000000007'}); }); }); diff --git a/coder.encodeParam.js b/coder.encodeParam.js index 1a4df06bf..a9e11ab9e 100644 --- a/coder.encodeParam.js +++ b/coder.encodeParam.js @@ -2,22 +2,90 @@ var chai = require('chai'); var assert = chai.assert; var coder = require('../lib/solidity/coder'); -var tests = [ - { type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}, - { type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}, - { type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}, - { type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'}, - { type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000009' + - '6761766f66796f726b0000000000000000000000000000000000000000000000'} -]; describe('lib/solidity/coder', function () { describe('encodeParam', function () { - tests.forEach(function (test) { - it('should turn ' + test.value + ' to ' + test.expected, function () { - assert.equal(coder.encodeParam(test.type, test.value), test.expected); + var test = function (t) { + it('should turn ' + t.value + ' to ' + t.expected, function () { + assert.equal(coder.encodeParam(t.type, t.value), t.expected); }); - }); + }; + + + test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'bytes32', value: 'gavofyork', expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'bytes', value: 'gavofyork', expected: '0000000000000000000000000000000000000000000000000000000000000009' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ type: 'int[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int256[]', value: [3], expected: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int[]', value: [1,2,3], expected: '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003'}); }); }); + +describe('lib/solidity/coder', function () { + describe('encodeParams', function () { + var test = function (t) { + it('should turn ' + t.values + ' to ' + t.expected, function () { + assert.equal(coder.encodeParams(t.types, t.values), t.expected); + }); + }; + + + test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ types: ['bytes32'], values: ['gavofyork'], expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['bytes'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000009' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ types: ['int256[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ types: ['int256[]'], values: [[1,2,3]], expected: '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ types: ['bytes32', 'int'], values: ['gavofyork', 5], + expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' + + '0000000000000000000000000000000000000000000000000000000000000005'}); + test({ types: ['int', 'bytes32'], values: [5, 'gavofyork'], + expected: '0000000000000000000000000000000000000000000000000000000000000005' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['bytes', 'int'], values: ['gavofyork', 5], + expected: '0000000000000000000000000000000000000000000000000000000000000009' + + '0000000000000000000000000000000000000000000000000000000000000005' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['int', 'bytes'], values: [5, 'gavofyork'], + expected: '0000000000000000000000000000000000000000000000000000000000000009' + + '0000000000000000000000000000000000000000000000000000000000000005' + + '6761766f66796f726b0000000000000000000000000000000000000000000000'}); + test({ types: ['int', 'bytes', 'int', 'int', 'int', 'int[]'], values: [1, 'gavofyork', 2, 3, 4, [5, 6, 7]], + expected: '0000000000000000000000000000000000000000000000000000000000000009' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000004' + + '6761766f66796f726b0000000000000000000000000000000000000000000000' + + '0000000000000000000000000000000000000000000000000000000000000005' + + '0000000000000000000000000000000000000000000000000000000000000006' + + '0000000000000000000000000000000000000000000000000000000000000007'}); + + }); +}); + + diff --git a/contract.js b/contract.js index db9840c52..8a2ea109d 100644 --- a/contract.js +++ b/contract.js @@ -2,7 +2,9 @@ var chai = require('chai'); var assert = chai.assert; var web3 = require('../index'); var FakeHttpProvider = require('./helpers/FakeHttpProvider'); +var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2'); var utils = require('../lib/utils/utils'); +var BigNumber = require('bignumber.js'); var desc = [{ "name": "balance(address)", @@ -27,6 +29,18 @@ var desc = [{ "type": "uint256" }], "outputs": [] +}, { + "name": "testArr(int[])", + "type": "function", + "inputs": [{ + "name": "value", + "type": "int[]" + }], + "constant": true, + "outputs": [{ + "name": "d", + "type": "int" + }] }, { "name":"Changed", "type":"event", @@ -313,5 +327,40 @@ describe('web3.eth.contract', function () { contract.send.sendTransaction(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}); }); + + it('should call testArr method and properly parse result', function () { + 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) + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003', + to: address + }, + 'latest' + ]); + } + step++; + }); + + var Contract = web3.eth.contract(desc); + var contract = new Contract(address); + + var result = contract.testArr([3]); + + assert.deepEqual(new BigNumber(5), result); + }); }); }); diff --git a/helpers/FakeHttpProvider.js b/helpers/FakeHttpProvider.js index 20d1f2580..0b01a171c 100644 --- a/helpers/FakeHttpProvider.js +++ b/helpers/FakeHttpProvider.js @@ -26,7 +26,7 @@ FakeHttpProvider.prototype.send = function (payload) { // imitate plain json object this.validation(JSON.parse(JSON.stringify(payload))); } - return this.response; + return this.getResponse(); }; FakeHttpProvider.prototype.sendAsync = function (payload, callback) { @@ -36,7 +36,7 @@ FakeHttpProvider.prototype.sendAsync = function (payload, callback) { // imitate plain json object this.validation(JSON.parse(JSON.stringify(payload)), callback); } - callback(this.error, this.response); + callback(this.error, this.getResponse()); }; FakeHttpProvider.prototype.injectResponse = function (response) { @@ -56,6 +56,10 @@ FakeHttpProvider.prototype.injectBatchResults = function (results) { }); }; +FakeHttpProvider.prototype.getResponse = function () { + return this.response; +}; + FakeHttpProvider.prototype.injectError = function (error) { this.error = error; }; diff --git a/helpers/FakeHttpProvider2.js b/helpers/FakeHttpProvider2.js new file mode 100644 index 000000000..0f26d84b7 --- /dev/null +++ b/helpers/FakeHttpProvider2.js @@ -0,0 +1,27 @@ +var FakeHttpProvider = require('./FakeHttpProvider'); + +var FakeHttpProvider2 = function () { + this.counter = 0; + this.resultList = []; +}; + +FakeHttpProvider2.prototype = new FakeHttpProvider(); +FakeHttpProvider2.prototype.constructor = FakeHttpProvider2; + +FakeHttpProvider2.prototype.injectResultList = function (list) { + this.resultList = list; +}; + +FakeHttpProvider2.prototype.getResponse = function () { + var result = this.resultList[this.counter]; + this.counter++; + if (result.type === 'batch') { + this.injectBatchResults(result.result); + } else { + this.injectResult(result.result); + } + return this.response; +}; + +module.exports = FakeHttpProvider2; +