From 65158d39b0632226c168b9a3415365ca8f072cbf Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 4 Feb 2015 15:05:47 -0800 Subject: [PATCH] Filtering --- cmd/mist/assets/examples/abi.html | 2 +- cmd/mist/assets/examples/coin.html | 22 +- cmd/mist/assets/examples/info.html | 5 + .../assets/ext/ethereum.js/dist/ethereum.js | 1518 +++++++++++------ core/block_processor.go | 13 +- core/chain_manager.go | 3 +- core/filter.go | 7 + core/types/common.go | 8 +- event/filter/old_filter.go | 2 +- miner/worker.go | 13 +- rpc/http/server.go | 4 +- rpc/message.go | 1 + rpc/util.go | 5 +- 13 files changed, 1044 insertions(+), 559 deletions(-) diff --git a/cmd/mist/assets/examples/abi.html b/cmd/mist/assets/examples/abi.html index 8d172482c9..8170e88b06 100644 --- a/cmd/mist/assets/examples/abi.html +++ b/cmd/mist/assets/examples/abi.html @@ -21,7 +21,7 @@ }]; var address = web3.eth.transact({ data: "0x603880600c6000396000f3006001600060e060020a600035048063c6888fa114601857005b6021600435602b565b8060005260206000f35b600081600702905091905056", - gasprice: "1000000000000000", + gasPrice: "1000000000000000", gas: "10000", }); var contract = web3.eth.contract(address, desc); diff --git a/cmd/mist/assets/examples/coin.html b/cmd/mist/assets/examples/coin.html index edeabe5e86..070ac94a6c 100644 --- a/cmd/mist/assets/examples/coin.html +++ b/cmd/mist/assets/examples/coin.html @@ -32,17 +32,19 @@ web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8080')); var desc = [{ "name": "balance(address)", + "type": "function", "inputs": [{ "name": "who", "type": "address" }], - "const": true, + "constant": true, "outputs": [{ "name": "value", "type": "uint256" }] }, { "name": "send(address,uint256)", + "type": "function", "inputs": [{ "name": "to", "type": "address" @@ -51,21 +53,31 @@ "type": "uint256" }], "outputs": [] + }, { + "name":"Changed", + "type":"event", + "inputs": [ + {"name":"to","type":"address","indexed":false}, + {"name":"amount","type":"uint256","indexed":true}, + ], }]; - var address = web3.db.get("jevcoin", "address"); + var address = "";//web3.db.get("jevcoin", "address"); if( address.length == 0 ) { - var code = "0x60056011565b60ae8060356000396000f35b64174876e800600033600160a060020a031660005260205260406000208190555056006001600060e060020a600035048063d0679d34146022578063e3d670d714603457005b602e6004356024356047565b60006000f35b603d600435608d565b8060005260206000f35b80600083600160a060020a0316600052602052604060002090815401908190555080600033600160a060020a031660005260205260406000209081540390819055505050565b6000600082600160a060020a0316600052602052604060002054905091905056"; + var code = "0x60056011565b60b88060356000396000f35b64e8d4a51000600033600160a060020a0316600052602052604060002081905550560060e060020a6000350480637bb98a68146028578063d0679d34146034578063e3d670d714604657005b602e60b3565b60006000f35b60406004356024356059565b60006000f35b604f6004356091565b8060005260206000f35b8060005281600160a060020a03167fb52dda022b6c1a1f40905a85f257f689aa5d69d850e49cf939d688fbe5af594660206000a25050565b6000600082600160a060020a03166000526020526040600020549050919050565b5b60008156"; address = web3.eth.transact({ data: code, - gasprice: "1000000000000000", + gasPrice: "1000000000000000", gas: "10000", }); web3.db.put("jevcoin", "address", address); } var contract = web3.eth.contract(address, desc); - + contract.Changed({to: "0xaabb"}).changed(function(e) { + console.log("e: " + JSON.stringify(e)); + }); + contract.transact({gas: "10000", gasprice: eth.gasPrice}).send( "0xaa", 10000 ); function reflesh() { document.querySelector("#balance").innerHTML = contract.call().balance(eth.coinbase); diff --git a/cmd/mist/assets/examples/info.html b/cmd/mist/assets/examples/info.html index c4df8ea649..daad8c7065 100644 --- a/cmd/mist/assets/examples/info.html +++ b/cmd/mist/assets/examples/info.html @@ -30,6 +30,10 @@ + + Balance + + Gas price @@ -63,6 +67,7 @@ document.querySelector("#peer_count").innerHTML = eth.peerCount; document.querySelector("#default_block").innerHTML = eth.defaultBlock; document.querySelector("#accounts").innerHTML = eth.accounts; + document.querySelector("#balance").innerHTML = web3.toEth(eth.balanceAt(eth.accounts[0])); document.querySelector("#gas_price").innerHTML = eth.gasPrice; document.querySelector("#mining").innerHTML = eth.mining; document.querySelector("#listening").innerHTML = eth.listening; diff --git a/cmd/mist/assets/ext/ethereum.js/dist/ethereum.js b/cmd/mist/assets/ext/ethereum.js/dist/ethereum.js index 6e6c5020cc..2ecee387d3 100644 --- a/cmd/mist/assets/ext/ethereum.js/dist/ethereum.js +++ b/cmd/mist/assets/ext/ethereum.js/dist/ethereum.js @@ -22,175 +22,57 @@ require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof requ * @date 2014 */ -// TODO: is these line is supposed to be here? -if ("build" !== 'build') {/* - var BigNumber = require('bignumber.js'); // jshint ignore:line -*/} - -var web3 = require('./web3'); // jshint ignore:line - -BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN }); - -var ETH_PADDING = 32; - -/// method signature length in bytes -var ETH_METHOD_SIGNATURE_LENGTH = 4; - -/// Finds first index of array element matching pattern -/// @param array -/// @param callback pattern -/// @returns index of element -var findIndex = function (array, callback) { - var end = false; - var i = 0; - for (; i < array.length && !end; i++) { - end = callback(array[i]); - } - return end ? i - 1 : -1; -}; - -/// @returns a function that is used as a pattern for 'findIndex' -var findMethodIndex = function (json, methodName) { - return findIndex(json, function (method) { - return method.name === methodName; - }); -}; - -/// @returns method with given method name -var getMethodWithName = function (json, methodName) { - var index = findMethodIndex(json, methodName); - if (index === -1) { - console.error('method ' + methodName + ' not found in the abi'); - return undefined; - } - return json[index]; -}; - -/// @param string string to be padded -/// @param number of characters that result string should have -/// @param sign, by default 0 -/// @returns right aligned string -var padLeft = function (string, chars, sign) { - return new Array(chars - string.length + 1).join(sign ? sign : "0") + string; -}; - -/// @param expected type prefix (string) -/// @returns function which checks if type has matching prefix. if yes, returns true, otherwise false -var prefixedType = function (prefix) { - return function (type) { - return type.indexOf(prefix) === 0; - }; -}; - -/// @param expected type name (string) -/// @returns function which checks if type is matching expected one. if yes, returns true, otherwise false -var namedType = function (name) { - return function (type) { - return name === type; - }; +var web3 = require('./web3'); +var utils = require('./utils'); +var types = require('./types'); +var c = require('./const'); +var f = require('./formatters'); + +var displayTypeError = function (type) { + console.error('parser does not support type: ' + type); }; +/// This method should be called if we want to check if givent type is an array type +/// @returns true if it is, otherwise false var arrayType = function (type) { return type.slice(-2) === '[]'; }; -/// Formats input value to byte representation of int -/// If value is negative, return it's two's complement -/// If the value is floating point, round it down -/// @returns right-aligned byte representation of int -var formatInputInt = function (value) { - var padding = ETH_PADDING * 2; - if (value instanceof BigNumber || typeof value === 'number') { - if (typeof value === 'number') - value = new BigNumber(value); - value = value.round(); - - if (value.lessThan(0)) - value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1); - value = value.toString(16); - } - else if (value.indexOf('0x') === 0) - value = value.substr(2); - else if (typeof value === 'string') - value = formatInputInt(new BigNumber(value)); - else - value = (+value).toString(16); - return padLeft(value, padding); -}; - -/// Formats input value to byte representation of string -/// @returns left-algined byte representation of string -var formatInputString = function (value) { - return web3.fromAscii(value, ETH_PADDING).substr(2); -}; - -/// Formats input value to byte representation of bool -/// @returns right-aligned byte representation bool -var formatInputBool = function (value) { - return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0'); -}; - -/// Formats input value to byte representation of real -/// Values are multiplied by 2^m and encoded as integers -/// @returns byte representation of real -var formatInputReal = function (value) { - return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128))); -}; - var dynamicTypeBytes = function (type, value) { // TODO: decide what to do with array of strings if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length. - return formatInputInt(value.length); + return f.formatInputInt(value.length); return ""; }; -/// Setups input formatters for solidity types -/// @returns an array of input formatters -var setupInputTypes = function () { - - return [ - { type: prefixedType('uint'), format: formatInputInt }, - { type: prefixedType('int'), format: formatInputInt }, - { type: prefixedType('hash'), format: formatInputInt }, - { type: prefixedType('string'), format: formatInputString }, - { type: prefixedType('real'), format: formatInputReal }, - { type: prefixedType('ureal'), format: formatInputReal }, - { type: namedType('address'), format: formatInputInt }, - { type: namedType('bool'), format: formatInputBool } - ]; -}; - -var inputTypes = setupInputTypes(); +var inputTypes = types.inputTypes(); /// Formats input params to bytes -/// @param contract json abi -/// @param name of the method that we want to use +/// @param abi contract method inputs /// @param array of params that will be formatted to bytes /// @returns bytes representation of input params -var toAbiInput = function (json, methodName, params) { +var formatInput = function (inputs, params) { var bytes = ""; - - var method = getMethodWithName(json, methodName); - var padding = ETH_PADDING * 2; + var padding = c.ETH_PADDING * 2; /// first we iterate in search for dynamic - method.inputs.forEach(function (input, index) { + inputs.forEach(function (input, index) { bytes += dynamicTypeBytes(input.type, params[index]); }); - method.inputs.forEach(function (input, i) { + inputs.forEach(function (input, i) { var typeMatch = false; for (var j = 0; j < inputTypes.length && !typeMatch; j++) { - typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]); + typeMatch = inputTypes[j].type(inputs[i].type, params[i]); } if (!typeMatch) { - console.error('input parser does not support type: ' + method.inputs[i].type); + displayTypeError(inputs[i].type); } var formatter = inputTypes[j - 1].format; var toAppend = ""; - if (arrayType(method.inputs[i].type)) + if (arrayType(inputs[i].type)) toAppend = params[i].reduce(function (acc, curr) { return acc + formatter(curr); }, ""); @@ -202,118 +84,44 @@ var toAbiInput = function (json, methodName, params) { return bytes; }; -/// Check if input value is negative -/// @param value is hex format -/// @returns true if it is negative, otherwise false -var signedIsNegative = function (value) { - return (new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1)) === '1'; -}; - -/// Formats input right-aligned input bytes to int -/// @returns right-aligned input bytes formatted to int -var formatOutputInt = function (value) { - value = value || "0"; - // check if it's negative number - // it it is, return two's complement - if (signedIsNegative(value)) { - return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1); - } - return new BigNumber(value, 16); -}; - -/// Formats big right-aligned input bytes to uint -/// @returns right-aligned input bytes formatted to uint -var formatOutputUInt = function (value) { - value = value || "0"; - return new BigNumber(value, 16); -}; - -/// @returns input bytes formatted to real -var formatOutputReal = function (value) { - return formatOutputInt(value).dividedBy(new BigNumber(2).pow(128)); -}; - -/// @returns input bytes formatted to ureal -var formatOutputUReal = function (value) { - return formatOutputUInt(value).dividedBy(new BigNumber(2).pow(128)); -}; - -/// @returns right-aligned input bytes formatted to hex -var formatOutputHash = function (value) { - return "0x" + value; -}; - -/// @returns right-aligned input bytes formatted to bool -var formatOutputBool = function (value) { - return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false; -}; - -/// @returns left-aligned input bytes formatted to ascii string -var formatOutputString = function (value) { - return web3.toAscii(value); -}; - -/// @returns right-aligned input bytes formatted to address -var formatOutputAddress = function (value) { - return "0x" + value.slice(value.length - 40, value.length); -}; - var dynamicBytesLength = function (type) { if (arrayType(type) || type === 'string') // only string itself that is dynamic; stringX is static length. - return ETH_PADDING * 2; + return c.ETH_PADDING * 2; return 0; }; -/// Setups output formaters for solidity types -/// @returns an array of output formatters -var setupOutputTypes = function () { - - return [ - { type: prefixedType('uint'), format: formatOutputUInt }, - { type: prefixedType('int'), format: formatOutputInt }, - { type: prefixedType('hash'), format: formatOutputHash }, - { type: prefixedType('string'), format: formatOutputString }, - { type: prefixedType('real'), format: formatOutputReal }, - { type: prefixedType('ureal'), format: formatOutputUReal }, - { type: namedType('address'), format: formatOutputAddress }, - { type: namedType('bool'), format: formatOutputBool } - ]; -}; - -var outputTypes = setupOutputTypes(); +var outputTypes = types.outputTypes(); /// Formats output bytes back to param list -/// @param contract json abi -/// @param name of the method that we want to use +/// @param contract abi method outputs /// @param bytes representtion of output /// @returns array of output params -var fromAbiOutput = function (json, methodName, output) { +var formatOutput = function (outs, output) { output = output.slice(2); var result = []; - var method = getMethodWithName(json, methodName); - var padding = ETH_PADDING * 2; + var padding = c.ETH_PADDING * 2; - var dynamicPartLength = method.outputs.reduce(function (acc, curr) { + var dynamicPartLength = outs.reduce(function (acc, curr) { return acc + dynamicBytesLength(curr.type); }, 0); var dynamicPart = output.slice(0, dynamicPartLength); output = output.slice(dynamicPartLength); - method.outputs.forEach(function (out, i) { + outs.forEach(function (out, i) { var typeMatch = false; for (var j = 0; j < outputTypes.length && !typeMatch; j++) { - typeMatch = outputTypes[j].type(method.outputs[i].type); + typeMatch = outputTypes[j].type(outs[i].type); } if (!typeMatch) { - console.error('output parser does not support type: ' + method.outputs[i].type); + displayTypeError(outs[i].type); } var formatter = outputTypes[j - 1].format; - if (arrayType(method.outputs[i].type)) { - var size = formatOutputUInt(dynamicPart.slice(0, padding)); + if (arrayType(outs[i].type)) { + var size = f.formatOutputUInt(dynamicPart.slice(0, padding)); dynamicPart = dynamicPart.slice(padding); var array = []; for (var k = 0; k < size; k++) { @@ -322,7 +130,7 @@ var fromAbiOutput = function (json, methodName, output) { } result.push(array); } - else if (prefixedType('string')(method.outputs[i].type)) { + else if (types.prefixedType('string')(outs[i].type)) { dynamicPart = dynamicPart.slice(padding); result.push(formatter(output.slice(0, padding))); output = output.slice(padding); @@ -335,30 +143,18 @@ var fromAbiOutput = function (json, methodName, output) { return result; }; -/// @returns display name for method eg. multiply(uint256) -> multiply -var methodDisplayName = function (method) { - var length = method.indexOf('('); - return length !== -1 ? method.substr(0, length) : method; -}; - -/// @returns overloaded part of method's name -var methodTypeName = function (method) { - /// TODO: make it not vulnerable - var length = method.indexOf('('); - return length !== -1 ? method.substr(length + 1, method.length - 1 - (length + 1)) : ""; -}; - /// @param json abi for contract /// @returns input parser object for given json abi +/// TODO: refactor creating the parser, do not double logic from contract var inputParser = function (json) { var parser = {}; json.forEach(function (method) { - var displayName = methodDisplayName(method.name); - var typeName = methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function () { var params = Array.prototype.slice.call(arguments); - return toAbiInput(json, method.name, params); + return formatInput(method.inputs, params); }; if (parser[displayName] === undefined) { @@ -377,11 +173,11 @@ var outputParser = function (json) { var parser = {}; json.forEach(function (method) { - var displayName = methodDisplayName(method.name); - var typeName = methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function (output) { - return fromAbiOutput(json, method.name, output); + return formatOutput(method.outputs, output); }; if (parser[displayName] === undefined) { @@ -394,23 +190,85 @@ var outputParser = function (json) { return parser; }; -/// @param method name for which we want to get method signature -/// @returns (promise) contract method signature for method with given name -var methodSignature = function (name) { - return web3.sha3(web3.fromAscii(name)).slice(0, 2 + ETH_METHOD_SIGNATURE_LENGTH * 2); +/// @param function/event name for which we want to get signature +/// @returns signature of function/event with given name +var signatureFromAscii = function (name) { + return web3.sha3(web3.fromAscii(name)).slice(0, 2 + c.ETH_SIGNATURE_LENGTH * 2); +}; + +var eventSignatureFromAscii = function (name) { + return web3.sha3(web3.fromAscii(name)); }; module.exports = { inputParser: inputParser, outputParser: outputParser, - methodSignature: methodSignature, - methodDisplayName: methodDisplayName, - methodTypeName: methodTypeName, - getMethodWithName: getMethodWithName + formatInput: formatInput, + formatOutput: formatOutput, + signatureFromAscii: signatureFromAscii, + eventSignatureFromAscii: eventSignatureFromAscii +}; + + +},{"./const":2,"./formatters":6,"./types":11,"./utils":12,"./web3":13}],2:[function(require,module,exports){ +/* + 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 const.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +/// required to define ETH_BIGNUMBER_ROUNDING_MODE +if ("build" !== 'build') {/* + var BigNumber = require('bignumber.js'); // jshint ignore:line +*/} + +var ETH_UNITS = [ + 'wei', + 'Kwei', + 'Mwei', + 'Gwei', + 'szabo', + 'finney', + 'ether', + 'grand', + 'Mether', + 'Gether', + 'Tether', + 'Pether', + 'Eether', + 'Zether', + 'Yether', + 'Nether', + 'Dether', + 'Vether', + 'Uether' +]; + +module.exports = { + ETH_PADDING: 32, + ETH_SIGNATURE_LENGTH: 4, + ETH_UNITS: ETH_UNITS, + ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN } }; -},{"./web3":7}],2:[function(require,module,exports){ +},{}],3:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -433,98 +291,78 @@ module.exports = { * @date 2014 */ -var web3 = require('./web3'); // jshint ignore:line +var web3 = require('./web3'); var abi = require('./abi'); +var utils = require('./utils'); +var eventImpl = require('./event'); + +var exportNatspecGlobals = function (vars) { + // it's used byt natspec.js + // TODO: figure out better way to solve this + web3._currentContractAbi = vars.abi; + web3._currentContractAddress = vars.address; + web3._currentContractMethodName = vars.method; + web3._currentContractMethodParams = vars.params; +}; -/** - * This method should be called when we want to call / transact some solidity method from javascript - * it returns an object which has same methods available as solidity contract description - * usage example: - * - * var abi = [{ - * name: 'myMethod', - * inputs: [{ name: 'a', type: 'string' }], - * outputs: [{name: 'd', type: 'string' }] - * }]; // contract abi - * - * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object - * - * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default) - * myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit) - * myContract.transact().myMethod('this is test string param for transact'); // myMethod transact - * - * @param address - address of the contract, which should be called - * @param desc - abi json description of the contract, which is being created - * @returns contract object - */ +var addFunctionRelatedPropertiesToContract = function (contract) { + + contract.call = function (options) { + contract._isTransact = false; + contract._options = options; + return contract; + }; -var contract = function (address, desc) { + contract.transact = function (options) { + contract._isTransact = true; + contract._options = options; + return contract; + }; - desc.forEach(function (method) { - // workaround for invalid assumption that method.name is the full anonymous prototype of the method. - // it's not. it's just the name. the rest of the code assumes it's actually the anonymous - // prototype, so we make it so as a workaround. - if (method.name.indexOf('(') === -1) { - var displayName = method.name; - var typeName = method.inputs.map(function(i){return i.type; }).join(); - method.name = displayName + '(' + typeName + ')'; - } + contract._options = {}; + ['gas', 'gasPrice', 'value', 'from'].forEach(function(p) { + contract[p] = function (v) { + contract._options[p] = v; + return contract; + }; }); +}; + +var addFunctionsToContract = function (contract, desc, address) { var inputParser = abi.inputParser(desc); var outputParser = abi.outputParser(desc); - var result = {}; - - result.call = function (options) { - result._isTransact = false; - result._options = options; - return result; - }; - - result.transact = function (options) { - result._isTransact = true; - result._options = options; - return result; - }; - - result._options = {}; - ['gas', 'gasPrice', 'value', 'from'].forEach(function(p) { - result[p] = function (v) { - result._options[p] = v; - return result; - }; - }); + // create contract functions + utils.filterFunctions(desc).forEach(function (method) { - - desc.forEach(function (method) { - - var displayName = abi.methodDisplayName(method.name); - var typeName = abi.methodTypeName(method.name); + var displayName = utils.extractDisplayName(method.name); + var typeName = utils.extractTypeName(method.name); var impl = function () { var params = Array.prototype.slice.call(arguments); - var signature = abi.methodSignature(method.name); + var signature = abi.signatureFromAscii(method.name); var parsed = inputParser[displayName][typeName].apply(null, params); - var options = result._options || {}; + var options = contract._options || {}; options.to = address; options.data = signature + parsed; - var isTransact = result._isTransact === true || (result._isTransact !== false && !method.constant); + var isTransact = contract._isTransact === true || (contract._isTransact !== false && !method.constant); var collapse = options.collapse !== false; // reset - result._options = {}; - result._isTransact = null; + contract._options = {}; + contract._isTransact = null; if (isTransact) { - // it's used byt natspec.js - // TODO: figure out better way to solve this - web3._currentContractAbi = desc; - web3._currentContractAddress = address; - web3._currentContractMethodName = method.name; - web3._currentContractMethodParams = params; + + exportNatspecGlobals({ + abi: desc, + address: address, + method: method.name, + params: params + }); // transactions do not have any output, cause we do not know, when they will be processed web3.eth.transact(options); @@ -543,21 +381,509 @@ var contract = function (address, desc) { return ret; }; - if (result[displayName] === undefined) { - result[displayName] = impl; + if (contract[displayName] === undefined) { + contract[displayName] = impl; } - result[displayName][typeName] = impl; + contract[displayName][typeName] = impl; + }); +}; + +var addEventRelatedPropertiesToContract = function (contract, desc, address) { + contract.address = address; + contract._onWatchEventResult = function (data) { + var matchingEvent = event.getMatchingEvent(utils.filterEvents(desc)); + var parser = eventImpl.outputParser(matchingEvent); + return parser(data); + }; + + Object.defineProperty(contract, 'topic', { + get: function() { + return utils.filterEvents(desc).map(function (e) { + return abi.eventSignatureFromAscii(e.name); + }); + } + }); + +}; + +var addEventsToContract = function (contract, desc, address) { + // create contract events + utils.filterEvents(desc).forEach(function (e) { + + var impl = function () { + var params = Array.prototype.slice.call(arguments); + var signature = abi.eventSignatureFromAscii(e.name); + var event = eventImpl.inputParser(address, signature, e); + var o = event.apply(null, params); + o._onWatchEventResult = function (data) { + var parser = eventImpl.outputParser(e); + return parser(data); + }; + return web3.eth.watch(o); + }; + + // this property should be used by eth.filter to check if object is an event + impl._isEvent = true; + + var displayName = utils.extractDisplayName(e.name); + var typeName = utils.extractTypeName(e.name); + + if (contract[displayName] === undefined) { + contract[displayName] = impl; + } + + contract[displayName][typeName] = impl; + + }); +}; + + +/** + * This method should be called when we want to call / transact some solidity method from javascript + * it returns an object which has same methods available as solidity contract description + * usage example: + * + * var abi = [{ + * name: 'myMethod', + * inputs: [{ name: 'a', type: 'string' }], + * outputs: [{name: 'd', type: 'string' }] + * }]; // contract abi + * + * var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object + * + * myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default) + * myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit) + * myContract.transact().myMethod('this is test string param for transact'); // myMethod transact + * + * @param address - address of the contract, which should be called + * @param desc - abi json description of the contract, which is being created + * @returns contract object + */ + +var contract = function (address, desc) { + // workaround for invalid assumption that method.name is the full anonymous prototype of the method. + // it's not. it's just the name. the rest of the code assumes it's actually the anonymous + // prototype, so we make it so as a workaround. + // TODO: we may not want to modify input params, maybe use copy instead? + desc.forEach(function (method) { + if (method.name.indexOf('(') === -1) { + var displayName = method.name; + var typeName = method.inputs.map(function(i){return i.type; }).join(); + method.name = displayName + '(' + typeName + ')'; + } }); + var result = {}; + addFunctionRelatedPropertiesToContract(result); + addFunctionsToContract(result, desc, address); + addEventRelatedPropertiesToContract(result, desc, address); + addEventsToContract(result, desc, address); + return result; }; -module.exports = contract; - +module.exports = contract; + + +},{"./abi":1,"./event":4,"./utils":12,"./web3":13}],4:[function(require,module,exports){ +/* + 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 event.js + * @authors: + * Marek Kotewicz + * @date 2014 + */ + +var abi = require('./abi'); +var utils = require('./utils'); + +/// filter inputs array && returns only indexed (or not) inputs +/// @param inputs array +/// @param bool if result should be an array of indexed params on not +/// @returns array of (not?) indexed params +var filterInputs = function (inputs, indexed) { + return inputs.filter(function (current) { + return current.indexed === indexed; + }); +}; + +var inputWithName = function (inputs, name) { + var index = utils.findIndex(inputs, function (input) { + return input.name === name; + }); + + if (index === -1) { + console.error('indexed param with name ' + name + ' not found'); + return undefined; + } + return inputs[index]; +}; + +var indexedParamsToTopics = function (event, indexed) { + // sort keys? + return Object.keys(indexed).map(function (key) { + var inputs = [inputWithName(filterInputs(event.inputs, true), key)]; + + var value = indexed[key]; + if (value instanceof Array) { + return value.map(function (v) { + return abi.formatInput(inputs, [v]); + }); + } + return abi.formatInput(inputs, [value]); + }); +}; + +var inputParser = function (address, signature, event) { + + // valid options are 'earliest', 'latest', 'offset' and 'max', as defined for 'eth.watch' + return function (indexed, options) { + var o = options || {}; + o.address = address; + o.topic = []; + o.topic.push(signature); + if (indexed) { + o.topic = o.topic.concat(indexedParamsToTopics(event, indexed)); + } + return o; + }; +}; + +var getArgumentsObject = function (inputs, indexed, notIndexed) { + var indexedCopy = indexed.slice(); + var notIndexedCopy = notIndexed.slice(); + return inputs.reduce(function (acc, current) { + var value; + if (current.indexed) + value = indexed.splice(0, 1)[0]; + else + value = notIndexed.splice(0, 1)[0]; + + acc[current.name] = value; + return acc; + }, {}); +}; + +var outputParser = function (event) { + + return function (output) { + var result = { + event: utils.extractDisplayName(event.name), + number: output.number, + args: {} + }; + + if (!output.topic) { + return result; + } + + var indexedOutputs = filterInputs(event.inputs, true); + var indexedData = "0x" + output.topic.slice(1, output.topic.length).map(function (topic) { return topic.slice(2); }).join(""); + var indexedRes = abi.formatOutput(indexedOutputs, indexedData); + + var notIndexedOutputs = filterInputs(event.inputs, false); + var notIndexedRes = abi.formatOutput(notIndexedOutputs, output.data); + + result.args = getArgumentsObject(event.inputs, indexedRes, notIndexedRes); + + return result; + }; +}; + +var getMatchingEvent = function (events, payload) { + for (var i = 0; i < events.length; i++) { + var signature = abi.eventSignatureFromAscii(events[i].name); + if (signature === payload.topic[0]) { + return events[i]; + } + } + return undefined; +}; + + +module.exports = { + inputParser: inputParser, + outputParser: outputParser, + getMatchingEvent: getMatchingEvent +}; + + +},{"./abi":1,"./utils":12}],5:[function(require,module,exports){ +/* + 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 filter.js + * @authors: + * Jeffrey Wilcke + * Marek Kotewicz + * Marian Oancea + * Gav Wood + * @date 2014 + */ + +var web3 = require('./web3'); // jshint ignore:line + +/// should be used when we want to watch something +/// it's using inner polling mechanism and is notified about changes +/// TODO: change 'options' name cause it may be not the best matching one, since we have events +var Filter = function(options, impl) { + + if (typeof options !== "string") { + + // topics property is deprecated, warn about it! + if (options.topics) { + console.warn('"topics" is deprecated, use "topic" instead'); + } + + this._onWatchResult = options._onWatchEventResult; + + // evaluate lazy properties + options = { + to: options.to, + topic: options.topic, + earliest: options.earliest, + latest: options.latest, + max: options.max, + skip: options.skip, + address: options.address + }; + + } + + this.impl = impl; + this.callbacks = []; + + this.id = impl.newFilter(options); + web3.provider.startPolling({method: impl.changed, params: [this.id]}, this.id, this.trigger.bind(this)); +}; + +/// alias for changed* +Filter.prototype.arrived = function(callback) { + this.changed(callback); +}; +Filter.prototype.happened = function(callback) { + this.changed(callback); +}; + +/// gets called when there is new eth/shh message +Filter.prototype.changed = function(callback) { + this.callbacks.push(callback); +}; + +/// trigger calling new message from people +Filter.prototype.trigger = function(messages) { + for (var i = 0; i < this.callbacks.length; i++) { + for (var j = 0; j < messages.length; j++) { + var message = this._onWatchResult ? this._onWatchResult(messages[j]) : messages[j]; + this.callbacks[i].call(this, message); + } + } +}; + +/// should be called to uninstall current filter +Filter.prototype.uninstall = function() { + this.impl.uninstallFilter(this.id); + web3.provider.stopPolling(this.id); +}; + +/// should be called to manually trigger getting latest messages from the client +Filter.prototype.messages = function() { + return this.impl.getMessages(this.id); +}; + +/// alias for messages +Filter.prototype.logs = function () { + return this.messages(); +}; + +module.exports = Filter; + +},{"./web3":13}],6:[function(require,module,exports){ +/* + 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 formatters.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +if ("build" !== 'build') {/* + var BigNumber = require('bignumber.js'); // jshint ignore:line +*/} + +var utils = require('./utils'); +var c = require('./const'); + +/// @param string string to be padded +/// @param number of characters that result string should have +/// @param sign, by default 0 +/// @returns right aligned string +var padLeft = function (string, chars, sign) { + return new Array(chars - string.length + 1).join(sign ? sign : "0") + string; +}; + +/// Formats input value to byte representation of int +/// If value is negative, return it's two's complement +/// If the value is floating point, round it down +/// @returns right-aligned byte representation of int +var formatInputInt = function (value) { + var padding = c.ETH_PADDING * 2; + if (value instanceof BigNumber || typeof value === 'number') { + if (typeof value === 'number') + value = new BigNumber(value); + BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE); + value = value.round(); + + if (value.lessThan(0)) + value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1); + value = value.toString(16); + } + else if (value.indexOf('0x') === 0) + value = value.substr(2); + else if (typeof value === 'string') + value = formatInputInt(new BigNumber(value)); + else + value = (+value).toString(16); + return padLeft(value, padding); +}; + +/// Formats input value to byte representation of string +/// @returns left-algined byte representation of string +var formatInputString = function (value) { + return utils.fromAscii(value, c.ETH_PADDING).substr(2); +}; + +/// Formats input value to byte representation of bool +/// @returns right-aligned byte representation bool +var formatInputBool = function (value) { + return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0'); +}; + +/// Formats input value to byte representation of real +/// Values are multiplied by 2^m and encoded as integers +/// @returns byte representation of real +var formatInputReal = function (value) { + return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128))); +}; + + +/// Check if input value is negative +/// @param value is hex format +/// @returns true if it is negative, otherwise false +var signedIsNegative = function (value) { + return (new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1)) === '1'; +}; + +/// Formats input right-aligned input bytes to int +/// @returns right-aligned input bytes formatted to int +var formatOutputInt = function (value) { + value = value || "0"; + // check if it's negative number + // it it is, return two's complement + if (signedIsNegative(value)) { + return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1); + } + return new BigNumber(value, 16); +}; + +/// Formats big right-aligned input bytes to uint +/// @returns right-aligned input bytes formatted to uint +var formatOutputUInt = function (value) { + value = value || "0"; + return new BigNumber(value, 16); +}; + +/// @returns input bytes formatted to real +var formatOutputReal = function (value) { + return formatOutputInt(value).dividedBy(new BigNumber(2).pow(128)); +}; + +/// @returns input bytes formatted to ureal +var formatOutputUReal = function (value) { + return formatOutputUInt(value).dividedBy(new BigNumber(2).pow(128)); +}; + +/// @returns right-aligned input bytes formatted to hex +var formatOutputHash = function (value) { + return "0x" + value; +}; + +/// @returns right-aligned input bytes formatted to bool +var formatOutputBool = function (value) { + return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false; +}; + +/// @returns left-aligned input bytes formatted to ascii string +var formatOutputString = function (value) { + return utils.toAscii(value); +}; + +/// @returns right-aligned input bytes formatted to address +var formatOutputAddress = function (value) { + return "0x" + value.slice(value.length - 40, value.length); +}; + + +module.exports = { + formatInputInt: formatInputInt, + formatInputString: formatInputString, + formatInputBool: formatInputBool, + formatInputReal: formatInputReal, + formatOutputInt: formatOutputInt, + formatOutputUInt: formatOutputUInt, + formatOutputReal: formatOutputReal, + formatOutputUReal: formatOutputUReal, + formatOutputHash: formatOutputHash, + formatOutputBool: formatOutputBool, + formatOutputString: formatOutputString, + formatOutputAddress: formatOutputAddress +}; + -},{"./abi":1,"./web3":7}],3:[function(require,module,exports){ +},{"./const":2,"./utils":12}],7:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -574,65 +900,38 @@ module.exports = contract; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file filter.js +/** @file httpsync.js * @authors: - * Jeffrey Wilcke * Marek Kotewicz * Marian Oancea - * Gav Wood * @date 2014 */ -var web3 = require('./web3'); // jshint ignore:line - -/// should be used when we want to watch something -/// it's using inner polling mechanism and is notified about changes -var Filter = function(options, impl) { - this.impl = impl; - this.callbacks = []; - - this.id = impl.newFilter(options); - web3.provider.startPolling({call: impl.changed, args: [this.id]}, this.id, this.trigger.bind(this)); -}; - -/// alias for changed* -Filter.prototype.arrived = function(callback) { - this.changed(callback); -}; - -/// gets called when there is new eth/shh message -Filter.prototype.changed = function(callback) { - this.callbacks.push(callback); -}; - -/// trigger calling new message from people -Filter.prototype.trigger = function(messages) { - for (var i = 0; i < this.callbacks.length; i++) { - for (var j = 0; j < messages.length; j++) { - this.callbacks[i].call(this, messages[j]); - } - } -}; +if ("build" !== 'build') {/* + var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line +*/} -/// should be called to uninstall current filter -Filter.prototype.uninstall = function() { - this.impl.uninstallFilter(this.id); - web3.provider.stopPolling(this.id); +var HttpSyncProvider = function (host) { + this.handlers = []; + this.host = host || 'http://localhost:8080'; }; -/// should be called to manually trigger getting latest messages from the client -Filter.prototype.messages = function() { - return this.impl.getMessages(this.id); +HttpSyncProvider.prototype.send = function (payload) { + //var data = formatJsonRpcObject(payload); + + var request = new XMLHttpRequest(); + request.open('POST', this.host, false); + request.send(JSON.stringify(payload)); + + // check request.status + var result = request.responseText; + return JSON.parse(result); }; -/// alias for messages -Filter.prototype.logs = function () { - return this.messages(); -}; +module.exports = HttpSyncProvider; -module.exports = Filter; -},{"./web3":7}],4:[function(require,module,exports){ +},{}],8:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -649,62 +948,57 @@ module.exports = Filter; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file httpsync.js +/** @file jsonrpc.js * @authors: * Marek Kotewicz - * Marian Oancea - * @date 2014 + * @date 2015 */ -if ("build" !== 'build') {/* - var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line -*/} +var messageId = 1; -var HttpSyncProvider = function (host) { - this.handlers = []; - this.host = host || 'http://localhost:8080'; -}; +/// Should be called to valid json create payload object +/// @param method of jsonrpc call, required +/// @param params, an array of method params, optional +/// @returns valid jsonrpc payload object +var toPayload = function (method, params) { + if (!method) + console.error('jsonrpc method should be specified!'); -/// Transforms inner message to proper jsonrpc object -/// @param inner message object -/// @returns jsonrpc object -function formatJsonRpcObject(object) { return { jsonrpc: '2.0', - method: object.call, - params: object.args, - id: object._id - }; -} + method: method, + params: params || [], + id: messageId++ + }; +}; -/// Transforms jsonrpc object to inner message -/// @param incoming jsonrpc message -/// @returns inner message object -function formatJsonRpcMessage(message) { - var object = JSON.parse(message); +/// Should be called to check if jsonrpc response is valid +/// @returns true if response is valid, otherwise false +var isValidResponse = function (response) { + return !!response && + !response.error && + response.jsonrpc === '2.0' && + typeof response.id === 'number' && + response.result !== undefined; // only undefined is not valid json object +}; - return { - _id: object.id, - data: object.result, - error: object.error - }; -} +/// Should be called to create batch payload object +/// @param messages, an array of objects with method (required) and params (optional) fields +var toBatchPayload = function (messages) { + return messages.map(function (message) { + return toPayload(message.method, message.params); + }); +}; -HttpSyncProvider.prototype.send = function (payload) { - var data = formatJsonRpcObject(payload); - - var request = new XMLHttpRequest(); - request.open('POST', this.host, false); - request.send(JSON.stringify(data)); - - // check request.status - return request.responseText; +module.exports = { + toPayload: toPayload, + isValidResponse: isValidResponse, + toBatchPayload: toBatchPayload }; -module.exports = HttpSyncProvider; -},{}],5:[function(require,module,exports){ +},{}],9:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -730,7 +1024,9 @@ module.exports = HttpSyncProvider; * @date 2014 */ -var web3 = require('./web3'); // jshint ignore:line +var web3 = require('./web3'); +var jsonrpc = require('./jsonrpc'); + /** * Provider manager object prototype @@ -744,25 +1040,35 @@ var web3 = require('./web3'); // jshint ignore:line var ProviderManager = function() { this.polls = []; this.provider = undefined; - this.id = 1; var self = this; var poll = function () { if (self.provider) { - self.polls.forEach(function (data) { - data.data._id = self.id; - self.id++; - var result = self.provider.send(data.data); - - result = JSON.parse(result); + var pollsBatch = self.polls.map(function (data) { + return data.data; + }); + + var payload = jsonrpc.toBatchPayload(pollsBatch); + var results = self.provider.send(payload); + + self.polls.forEach(function (data, index) { + var result = results[index]; + if (!jsonrpc.isValidResponse(result)) { + console.log(result); + return; + } + + result = result.result; // dont call the callback if result is not an array, or empty one - if (result.error || !(result.result instanceof Array) || result.result.length === 0) { + if (!(result instanceof Array) || result.length === 0) { return; } - data.callback(result.result); + data.callback(result); + }); + } setTimeout(poll, 1000); }; @@ -770,22 +1076,19 @@ var ProviderManager = function() { }; /// sends outgoing requests +/// @params data - an object with at least 'method' property ProviderManager.prototype.send = function(data) { - - data.args = data.args || []; - data._id = this.id++; + var payload = jsonrpc.toPayload(data.method, data.params); if (this.provider === undefined) { console.error('provider is not set'); return null; } - //TODO: handle error here? - var result = this.provider.send(data); - result = JSON.parse(result); + var result = this.provider.send(payload); - if (result.error) { - console.log(result.error); + if (!jsonrpc.isValidResponse(result)) { + console.log(result); return null; } @@ -816,7 +1119,7 @@ ProviderManager.prototype.stopPolling = function (pollId) { module.exports = ProviderManager; -},{"./web3":7}],6:[function(require,module,exports){ +},{"./jsonrpc":8,"./web3":13}],10:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -844,13 +1147,239 @@ var QtSyncProvider = function () { }; QtSyncProvider.prototype.send = function (payload) { - return navigator.qt.callMethod(JSON.stringify(payload)); + var result = navigator.qt.callMethod(JSON.stringify(payload)); + return JSON.parse(result); }; module.exports = QtSyncProvider; -},{}],7:[function(require,module,exports){ +},{}],11:[function(require,module,exports){ +/* + 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 types.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var f = require('./formatters'); + +/// @param expected type prefix (string) +/// @returns function which checks if type has matching prefix. if yes, returns true, otherwise false +var prefixedType = function (prefix) { + return function (type) { + return type.indexOf(prefix) === 0; + }; +}; + +/// @param expected type name (string) +/// @returns function which checks if type is matching expected one. if yes, returns true, otherwise false +var namedType = function (name) { + return function (type) { + return name === type; + }; +}; + +/// Setups input formatters for solidity types +/// @returns an array of input formatters +var inputTypes = function () { + + return [ + { type: prefixedType('uint'), format: f.formatInputInt }, + { type: prefixedType('int'), format: f.formatInputInt }, + { type: prefixedType('hash'), format: f.formatInputInt }, + { type: prefixedType('string'), format: f.formatInputString }, + { type: prefixedType('real'), format: f.formatInputReal }, + { type: prefixedType('ureal'), format: f.formatInputReal }, + { type: namedType('address'), format: f.formatInputInt }, + { type: namedType('bool'), format: f.formatInputBool } + ]; +}; + +/// Setups output formaters for solidity types +/// @returns an array of output formatters +var outputTypes = function () { + + return [ + { type: prefixedType('uint'), format: f.formatOutputUInt }, + { type: prefixedType('int'), format: f.formatOutputInt }, + { type: prefixedType('hash'), format: f.formatOutputHash }, + { type: prefixedType('string'), format: f.formatOutputString }, + { type: prefixedType('real'), format: f.formatOutputReal }, + { type: prefixedType('ureal'), format: f.formatOutputUReal }, + { type: namedType('address'), format: f.formatOutputAddress }, + { type: namedType('bool'), format: f.formatOutputBool } + ]; +}; + +module.exports = { + prefixedType: prefixedType, + namedType: namedType, + inputTypes: inputTypes, + outputTypes: outputTypes +}; + + +},{"./formatters":6}],12:[function(require,module,exports){ +/* + 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 utils.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var c = require('./const'); + +/// Finds first index of array element matching pattern +/// @param array +/// @param callback pattern +/// @returns index of element +var findIndex = function (array, callback) { + var end = false; + var i = 0; + for (; i < array.length && !end; i++) { + end = callback(array[i]); + } + return end ? i - 1 : -1; +}; + +/// @returns ascii string representation of hex value prefixed with 0x +var toAscii = function(hex) { +// Find termination + var str = ""; + var i = 0, l = hex.length; + if (hex.substring(0, 2) === '0x') { + i = 2; + } + for (; i < l; i+=2) { + var code = parseInt(hex.substr(i, 2), 16); + if (code === 0) { + break; + } + + str += String.fromCharCode(code); + } + + return str; +}; + +var toHex = function(str) { + var hex = ""; + for(var i = 0; i < str.length; i++) { + var n = str.charCodeAt(i).toString(16); + hex += n.length < 2 ? '0' + n : n; + } + + return hex; +}; + +/// @returns hex representation (prefixed by 0x) of ascii string +var fromAscii = function(str, pad) { + pad = pad === undefined ? 0 : pad; + var hex = toHex(str); + while (hex.length < pad*2) + hex += "00"; + return "0x" + hex; +}; + +/// @returns display name for function/event eg. multiply(uint256) -> multiply +var extractDisplayName = function (name) { + var length = name.indexOf('('); + return length !== -1 ? name.substr(0, length) : name; +}; + +/// @returns overloaded part of function/event name +var extractTypeName = function (name) { + /// TODO: make it invulnerable + var length = name.indexOf('('); + return length !== -1 ? name.substr(length + 1, name.length - 1 - (length + 1)).replace(' ', '') : ""; +}; + +/// Filters all function from input abi +/// @returns abi array with filtered objects of type 'function' +var filterFunctions = function (json) { + return json.filter(function (current) { + return current.type === 'function'; + }); +}; + +/// Filters all events form input abi +/// @returns abi array with filtered objects of type 'event' +var filterEvents = function (json) { + return json.filter(function (current) { + return current.type === 'event'; + }); +}; + +/// used to transform value/string to eth string +/// TODO: use BigNumber.js to parse int +/// TODO: add tests for it! +var toEth = function (str) { + var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str; + var unit = 0; + var units = c.ETH_UNITS; + while (val > 3000 && unit < units.length - 1) + { + val /= 1000; + unit++; + } + var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2); + var replaceFunction = function($0, $1, $2) { + return $1 + ',' + $2; + }; + + while (true) { + var o = s; + s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction); + if (o === s) + break; + } + return s + ' ' + units[unit]; +}; + +module.exports = { + findIndex: findIndex, + toAscii: toAscii, + fromAscii: fromAscii, + extractDisplayName: extractDisplayName, + extractTypeName: extractTypeName, + filterFunctions: filterFunctions, + filterEvents: filterEvents, + toEth: toEth +}; + + +},{"./const":2}],13:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -880,27 +1409,7 @@ if ("build" !== 'build') {/* var BigNumber = require('bignumber.js'); */} -var ETH_UNITS = [ - 'wei', - 'Kwei', - 'Mwei', - 'Gwei', - 'szabo', - 'finney', - 'ether', - 'grand', - 'Mether', - 'Gether', - 'Tether', - 'Pether', - 'Eether', - 'Zether', - 'Yether', - 'Nether', - 'Dether', - 'Vether', - 'Uether' -]; +var utils = require('./utils'); /// @returns an array of objects describing web3 api methods var web3Methods = function () { @@ -1009,8 +1518,8 @@ var setupMethods = function (obj, methods) { var args = Array.prototype.slice.call(arguments); var call = typeof method.call === 'function' ? method.call(args) : method.call; return web3.provider.send({ - call: call, - args: args + method: call, + params: args }); }; }); @@ -1023,15 +1532,15 @@ var setupProperties = function (obj, properties) { var proto = {}; proto.get = function () { return web3.provider.send({ - call: property.getter + method: property.getter }); }; if (property.setter) { proto.set = function (val) { return web3.provider.send({ - call: property.setter, - args: [val] + method: property.setter, + params: [val] }); }; } @@ -1045,43 +1554,11 @@ var web3 = { _events: {}, providers: {}, - toHex: function(str) { - var hex = ""; - for(var i = 0; i < str.length; i++) { - var n = str.charCodeAt(i).toString(16); - hex += n.length < 2 ? '0' + n : n; - } - - return hex; - }, - /// @returns ascii string representation of hex value prefixed with 0x - toAscii: function(hex) { - // Find termination - var str = ""; - var i = 0, l = hex.length; - if (hex.substring(0, 2) === '0x') - i = 2; - for(; i < l; i+=2) { - var code = parseInt(hex.substr(i, 2), 16); - if(code === 0) { - break; - } - - str += String.fromCharCode(code); - } - - return str; - }, + toAscii: utils.toAscii, /// @returns hex representation (prefixed by 0x) of ascii string - fromAscii: function(str, pad) { - pad = pad === undefined ? 0 : pad; - var hex = this.toHex(str); - while(hex.length < pad*2) - hex += "00"; - return "0x" + hex; - }, + fromAscii: utils.fromAscii, /// @returns decimal representaton of hex value prefixed by 0x toDecimal: function (val) { @@ -1096,29 +1573,7 @@ var web3 = { }, /// used to transform value/string to eth string - /// TODO: use BigNumber.js to parse int - toEth: function(str) { - var val = typeof str === "string" ? str.indexOf('0x') === 0 ? parseInt(str.substr(2), 16) : parseInt(str) : str; - var unit = 0; - var units = ETH_UNITS; - while (val > 3000 && unit < units.length - 1) - { - val /= 1000; - unit++; - } - var s = val.toString().length < val.toFixed(2).length ? val.toString() : val.toFixed(2); - var replaceFunction = function($0, $1, $2) { - return $1 + ',' + $2; - }; - - while (true) { - var o = s; - s = s.replace(/(\d)(\d\d\d[\.\,])/, replaceFunction); - if (o === s) - break; - } - return s + ' ' + units[unit]; - }, + toEth: utils.toEth, /// eth object prototype eth: { @@ -1131,8 +1586,15 @@ var web3 = { return ret; }; }, - watch: function (params) { - return new web3.filter(params, ethWatch); + + /// @param filter may be a string, object or event + /// @param indexed is optional, this is an object with optional event indexed params + /// @param options is optional, this is an object with optional event options ('max'...) + watch: function (filter, indexed, options) { + if (filter._isEvent) { + return filter(indexed, options); + } + return new web3.filter(filter, ethWatch); } }, @@ -1141,15 +1603,12 @@ var web3 = { /// shh object prototype shh: { - watch: function (params) { - return new web3.filter(params, shhWatch); + + /// @param filter may be a string, object or event + watch: function (filter, indexed) { + return new web3.filter(filter, shhWatch); } }, - - /// @returns true if provider is installed - haveProvider: function() { - return !!web3.provider.provider; - } }; /// setups all api methods @@ -1172,14 +1631,13 @@ var shhWatch = { setupMethods(shhWatch, shhWatchMethods()); web3.setProvider = function(provider) { - //provider.onmessage = messageHandler; // there will be no async calls, to remove web3.provider.set(provider); }; module.exports = web3; -},{}],"web3":[function(require,module,exports){ +},{"./utils":12}],"web3":[function(require,module,exports){ var web3 = require('./lib/web3'); var ProviderManager = require('./lib/providermanager'); web3.provider = new ProviderManager(); @@ -1192,7 +1650,7 @@ web3.abi = require('./lib/abi'); module.exports = web3; -},{"./lib/abi":1,"./lib/contract":2,"./lib/filter":3,"./lib/httpsync":4,"./lib/providermanager":5,"./lib/qtsync":6,"./lib/web3":7}]},{},["web3"]) +},{"./lib/abi":1,"./lib/contract":3,"./lib/filter":5,"./lib/httpsync":7,"./lib/providermanager":9,"./lib/qtsync":10,"./lib/web3":13}]},{},["web3"]) //# sourceMappingURL=ethereum.js.map diff --git a/core/block_processor.go b/core/block_processor.go index 20a651e846..d59d7fecaa 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -110,6 +110,8 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, state go self.eventMux.Post(TxPostEvent{tx}) } + go self.eventMux.Post(state.Logs()) + return receipt, txGas, err } @@ -155,25 +157,25 @@ done: return receipts, handled, unhandled, erroneous, err } -func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, msgs state.Messages, err error) { +func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, err error) { // Processing a blocks may never happen simultaneously sm.mutex.Lock() defer sm.mutex.Unlock() header := block.Header() if sm.bc.HasBlock(header.Hash()) { - return nil, nil, &KnownBlockError{header.Number, header.Hash()} + return nil, &KnownBlockError{header.Number, header.Hash()} } if !sm.bc.HasBlock(header.ParentHash) { - return nil, nil, ParentError(header.ParentHash) + return nil, ParentError(header.ParentHash) } parent := sm.bc.GetBlock(header.ParentHash) return sm.ProcessWithParent(block, parent) } -func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big.Int, messages state.Messages, err error) { +func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big.Int, err error) { sm.lastAttemptedBlock = block state := state.New(parent.Root(), sm.db) @@ -227,7 +229,6 @@ func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big state.Sync() // Set the block hashes for the current messages state.Manifest().SetHash(block.Hash()) - messages = state.Manifest().Messages // Reset the manifest XXX We need this? state.Manifest().Reset() // Remove transactions from the pool @@ -235,7 +236,7 @@ func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash()[0:4]) - return td, messages, nil + return td, nil } // Validates the current block. Returns an error if the block was invalid, diff --git a/core/chain_manager.go b/core/chain_manager.go index 9646bfc530..0847980ca8 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -359,7 +359,7 @@ func (bc *ChainManager) Stop() { func (self *ChainManager) InsertChain(chain types.Blocks) error { for _, block := range chain { - td, messages, err := self.processor.Process(block) + td, err := self.processor.Process(block) if err != nil { if IsKnownBlockErr(err) { continue @@ -391,7 +391,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { self.mu.Unlock() self.eventMux.Post(NewBlockEvent{block}) - self.eventMux.Post(messages) } return nil diff --git a/core/filter.go b/core/filter.go index d154e7b7af..a458165f5b 100644 --- a/core/filter.go +++ b/core/filter.go @@ -2,6 +2,7 @@ package core import ( "bytes" + "fmt" "math" "github.com/ethereum/go-ethereum/core/types" @@ -130,6 +131,7 @@ func (self *Filter) Find() state.Logs { func includes(addresses [][]byte, a []byte) (found bool) { for _, addr := range addresses { + fmt.Println("INCLUDES", addr, a) if bytes.Compare(addr, a) == 0 { return true } @@ -139,20 +141,25 @@ func includes(addresses [][]byte, a []byte) (found bool) { } func (self *Filter) FilterLogs(logs state.Logs) state.Logs { + fmt.Println("FILTER LOGS", self.topics) var ret state.Logs // Filter the logs for interesting stuff for _, log := range logs { + fmt.Println(log) + if len(self.address) > 0 && !bytes.Equal(self.address, log.Address()) { continue } for _, topic := range self.topics { + fmt.Println("TOPIC:", topic) if !includes(log.Topics(), topic) { continue } } + fmt.Println("APPENDED") ret = append(ret, log) } diff --git a/core/types/common.go b/core/types/common.go index ba88b77e1a..7953749594 100644 --- a/core/types/common.go +++ b/core/types/common.go @@ -1,11 +1,7 @@ package types -import ( - "math/big" - - "github.com/ethereum/go-ethereum/state" -) +import "math/big" type BlockProcessor interface { - Process(*Block) (*big.Int, state.Messages, error) + Process(*Block) (*big.Int, error) } diff --git a/event/filter/old_filter.go b/event/filter/old_filter.go index c30a7e5841..4c01572db5 100644 --- a/event/filter/old_filter.go +++ b/event/filter/old_filter.go @@ -59,7 +59,7 @@ func (self *FilterManager) GetFilter(id int) *core.Filter { func (self *FilterManager) filterLoop() { // Subscribe to events - events := self.eventMux.Subscribe(core.NewBlockEvent{}, state.Messages(nil)) + events := self.eventMux.Subscribe(core.NewBlockEvent{}, state.Logs(nil)) out: for { diff --git a/miner/worker.go b/miner/worker.go index dbb8a58328..ea8f2e8b5c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -41,6 +41,10 @@ func env(block *types.Block, eth *eth.Ethereum) *environment { return env } +type Agent interface { + Comms() chan<- *types.Block +} + type worker struct { agents []chan<- *types.Block mux *event.TypeMux @@ -68,11 +72,12 @@ out: case event := <-events.Chan(): switch event := event.(type) { case core.NewBlockEvent: - block := event.Block - if self.eth.ChainManager().HasBlock(block.Hash()) { - } else if true { + if self.eth.ChainManager().HasBlock(event.Block.Hash()) { + } + case core.TxPreEvent: + if err := self.commitTransaction(event.Tx); err != nil { + self.commit() } - case core.TxPreEvent, *LocalTx: } case <-self.quit: break out diff --git a/rpc/http/server.go b/rpc/http/server.go index 965727a4ed..bcfd46234b 100644 --- a/rpc/http/server.go +++ b/rpc/http/server.go @@ -87,7 +87,7 @@ func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler { fn := func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") - rpchttplogger.Debugln("Handling request") + rpchttplogger.DebugDetailln("Handling request") reqParsed, reqerr := JSON.ParseRequestBody(req) if reqerr != nil { @@ -103,7 +103,7 @@ func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler { return } - rpchttplogger.Debugf("Generated response: %T %s", response, response) + rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) } diff --git a/rpc/message.go b/rpc/message.go index 26fac9d377..f1e982397d 100644 --- a/rpc/message.go +++ b/rpc/message.go @@ -206,6 +206,7 @@ func (req *RpcRequest) ToFilterArgs() (*FilterOptions, error) { if len(req.Params) < 1 { return nil, NewErrorResponse(ErrorArguments) } + fmt.Println("filter params", req.Params) args := new(FilterOptions) r := bytes.NewReader(req.Params[0]) diff --git a/rpc/util.go b/rpc/util.go index bb4087c51c..509d9a17d3 100644 --- a/rpc/util.go +++ b/rpc/util.go @@ -18,10 +18,11 @@ package rpc import ( "encoding/json" - "github.com/ethereum/go-ethereum/logger" "io" "net/http" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/state" ) @@ -36,7 +37,7 @@ func (self JsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) rpclogger.Fatalln("Error marshalling JSON", err) return 0, err } - rpclogger.Infof("Sending payload: %s", payload) + rpclogger.DebugDetailf("Sending payload: %s", payload) return writer.Write(payload) }