Browse Source

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
cl-refactor
Marek Kotewicz 10 years ago
parent
commit
1c979d7311
  1. 2
      bower.json
  2. 8
      dist/web3-light.js
  3. 6
      dist/web3-light.js.map
  4. 4
      dist/web3-light.min.js
  5. 8
      dist/web3.js
  6. 6
      dist/web3.js.map
  7. 4
      dist/web3.min.js
  8. 2
      lib/version.json
  9. 6
      lib/web3/function.js
  10. 2
      package.js
  11. 2
      package.json
  12. 69
      test/coder.decodeParam.js
  13. 92
      test/coder.encodeParam.js
  14. 49
      test/contract.js
  15. 8
      test/helpers/FakeHttpProvider.js
  16. 27
      test/helpers/FakeHttpProvider2.js

2
bower.json

@ -1,7 +1,7 @@
{ {
"name": "web3", "name": "web3",
"namespace": "ethereum", "namespace": "ethereum",
"version": "0.3.2", "version": "0.3.3",
"description": "Ethereum Compatible JavaScript API", "description": "Ethereum Compatible JavaScript API",
"main": [ "main": [
"./dist/web3.js", "./dist/web3.js",

8
dist/web3-light.js

@ -1394,7 +1394,7 @@ module.exports = {
},{"bignumber.js":"bignumber.js"}],9:[function(require,module,exports){ },{"bignumber.js":"bignumber.js"}],9:[function(require,module,exports){
module.exports={ module.exports={
"version": "0.3.2" "version": "0.3.3"
} }
},{}],10:[function(require,module,exports){ },{}],10:[function(require,module,exports){
@ -2644,7 +2644,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () { SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var options = {}; var options = {};
if (utils.isObject(args[args.length -1])) { if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop(); options = args.pop();
} }
options.to = this._address; options.to = this._address;
@ -2672,7 +2672,9 @@ SolidityFunction.prototype.signature = function () {
SolidityFunction.prototype.call = function () { SolidityFunction.prototype.call = function () {
var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments)); var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
var output = web3.eth.call(payload); var output = web3.eth.call(payload);
return coder.decodeParams(this._outputTypes, output); output = output.length >= 2 ? output.slice(2) : output;
var result = coder.decodeParams(this._outputTypes, output);
return result.length === 1 ? result[0] : result;
}; };
/** /**

6
dist/web3-light.js.map

File diff suppressed because one or more lines are too long

4
dist/web3-light.min.js

File diff suppressed because one or more lines are too long

8
dist/web3.js

@ -1394,7 +1394,7 @@ module.exports = {
},{"bignumber.js":"bignumber.js"}],9:[function(require,module,exports){ },{"bignumber.js":"bignumber.js"}],9:[function(require,module,exports){
module.exports={ module.exports={
"version": "0.3.2" "version": "0.3.3"
} }
},{}],10:[function(require,module,exports){ },{}],10:[function(require,module,exports){
@ -2644,7 +2644,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () { SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var options = {}; var options = {};
if (utils.isObject(args[args.length -1])) { if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop(); options = args.pop();
} }
options.to = this._address; options.to = this._address;
@ -2672,7 +2672,9 @@ SolidityFunction.prototype.signature = function () {
SolidityFunction.prototype.call = function () { SolidityFunction.prototype.call = function () {
var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments)); var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
var output = web3.eth.call(payload); var output = web3.eth.call(payload);
return coder.decodeParams(this._outputTypes, output); output = output.length >= 2 ? output.slice(2) : output;
var result = coder.decodeParams(this._outputTypes, output);
return result.length === 1 ? result[0] : result;
}; };
/** /**

6
dist/web3.js.map

File diff suppressed because one or more lines are too long

4
dist/web3.min.js

File diff suppressed because one or more lines are too long

2
lib/version.json

@ -1,3 +1,3 @@
{ {
"version": "0.3.2" "version": "0.3.3"
} }

6
lib/web3/function.js

@ -49,7 +49,7 @@ var SolidityFunction = function (json, address) {
SolidityFunction.prototype.toPayload = function () { SolidityFunction.prototype.toPayload = function () {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var options = {}; var options = {};
if (utils.isObject(args[args.length -1])) { if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) {
options = args.pop(); options = args.pop();
} }
options.to = this._address; options.to = this._address;
@ -77,7 +77,9 @@ SolidityFunction.prototype.signature = function () {
SolidityFunction.prototype.call = function () { SolidityFunction.prototype.call = function () {
var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments)); var payload = this.toPayload.apply(this, Array.prototype.slice.call(arguments));
var output = web3.eth.call(payload); var output = web3.eth.call(payload);
return coder.decodeParams(this._outputTypes, output); output = output.length >= 2 ? output.slice(2) : output;
var result = coder.decodeParams(this._outputTypes, output);
return result.length === 1 ? result[0] : result;
}; };
/** /**

2
package.js

@ -1,7 +1,7 @@
/* jshint ignore:start */ /* jshint ignore:start */
Package.describe({ Package.describe({
name: 'ethereum:web3', name: 'ethereum:web3',
version: '0.3.2', version: '0.3.3',
summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC', summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC',
git: 'https://github.com/ethereum/ethereum.js', git: 'https://github.com/ethereum/ethereum.js',
// By default, Meteor will default to using README.md for documentation. // By default, Meteor will default to using README.md for documentation.

2
package.json

@ -1,7 +1,7 @@
{ {
"name": "web3", "name": "web3",
"namespace": "ethereum", "namespace": "ethereum",
"version": "0.3.2", "version": "0.3.3",
"description": "Ethereum JavaScript API, middleware to talk to a ethereum node over RPC", "description": "Ethereum JavaScript API, middleware to talk to a ethereum node over RPC",
"main": "./index.js", "main": "./index.js",
"directories": { "directories": {

69
test/coder.decodeParam.js

@ -1,23 +1,68 @@
var chai = require('chai'); var chai = require('chai');
var assert = chai.assert; var assert = chai.assert;
var coder = require('../lib/solidity/coder'); 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('lib/solidity/coder', function () {
describe('decodeParam', function () { describe('decodeParam', function () {
tests.forEach(function (test) { var test = function (t) {
it('should turn ' + test.value + ' to ' + test.expected, function () { it('should turn ' + t.value + ' to ' + t.expected, function () {
assert.equal(coder.decodeParam(test.type, test.value), test.expected); 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'});
}); });
}); });

92
test/coder.encodeParam.js

@ -2,22 +2,90 @@ var chai = require('chai');
var assert = chai.assert; var assert = chai.assert;
var coder = require('../lib/solidity/coder'); 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('lib/solidity/coder', function () {
describe('encodeParam', function () { describe('encodeParam', function () {
tests.forEach(function (test) { var test = function (t) {
it('should turn ' + test.value + ' to ' + test.expected, function () { it('should turn ' + t.value + ' to ' + t.expected, function () {
assert.equal(coder.encodeParam(test.type, test.value), test.expected); 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'});
}); });
}); });

49
test/contract.js

@ -2,7 +2,9 @@ var chai = require('chai');
var assert = chai.assert; var assert = chai.assert;
var web3 = require('../index'); var web3 = require('../index');
var FakeHttpProvider = require('./helpers/FakeHttpProvider'); var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2');
var utils = require('../lib/utils/utils'); var utils = require('../lib/utils/utils');
var BigNumber = require('bignumber.js');
var desc = [{ var desc = [{
"name": "balance(address)", "name": "balance(address)",
@ -27,6 +29,18 @@ var desc = [{
"type": "uint256" "type": "uint256"
}], }],
"outputs": [] "outputs": []
}, {
"name": "testArr(int[])",
"type": "function",
"inputs": [{
"name": "value",
"type": "int[]"
}],
"constant": true,
"outputs": [{
"name": "d",
"type": "int"
}]
}, { }, {
"name":"Changed", "name":"Changed",
"type":"event", "type":"event",
@ -313,5 +327,40 @@ describe('web3.eth.contract', function () {
contract.send.sendTransaction(address, 17, {from: address, gas: 50000, gasPrice: 3000, value: 10000}); 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);
});
}); });
}); });

8
test/helpers/FakeHttpProvider.js

@ -26,7 +26,7 @@ FakeHttpProvider.prototype.send = function (payload) {
// imitate plain json object // imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload))); this.validation(JSON.parse(JSON.stringify(payload)));
} }
return this.response; return this.getResponse();
}; };
FakeHttpProvider.prototype.sendAsync = function (payload, callback) { FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
@ -36,7 +36,7 @@ FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
// imitate plain json object // imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)), callback); this.validation(JSON.parse(JSON.stringify(payload)), callback);
} }
callback(this.error, this.response); callback(this.error, this.getResponse());
}; };
FakeHttpProvider.prototype.injectResponse = function (response) { 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) { FakeHttpProvider.prototype.injectError = function (error) {
this.error = error; this.error = error;
}; };

27
test/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;
Loading…
Cancel
Save