Skip to content

Commit

Permalink
Refactor contract event filter
Browse files Browse the repository at this point in the history
The event filter does not works for string indexed filter, this
change uses sha3 value of the string value to filter the event
logs, which were stored on the block.
  • Loading branch information
ruopuzi committed Apr 10, 2019
1 parent f409e18 commit 7581bb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
16 changes: 10 additions & 6 deletions lib/types/coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var TypeString = require('./string');
var TypeReal = require('./real');
var TypeUReal = require('./ureal');
var TypeBytes = require('./bytes');
var sha3 = require('../utils/sha3')

var isDynamic = function (Type, type) {
return Type.isDynamicType(type) ||
Expand Down Expand Up @@ -72,8 +73,11 @@ Coder.prototype._requireType = function (type) {
* @param {Object} plain param
* @return {String} encoded plain param
*/
Coder.prototype.encodeParam = function (type, param) {
return this.encodeParams([type], [param]);
Coder.prototype.encodeParam = function (type, param, indexed=false) {
if(type === "string" && indexed) {
return sha3(param)
}
return this.encodeParams([type], [param], indexed);
};

/**
Expand All @@ -84,7 +88,7 @@ Coder.prototype.encodeParam = function (type, param) {
* @param {Array} params
* @return {String} encoded list of params
*/
Coder.prototype.encodeParams = function (types, params) {
Coder.prototype.encodeParams = function (types, params, indexed=false) {
var Types = this.getTypes(types);

var encodeds = Types.map(function (Type, index) {
Expand Down Expand Up @@ -187,8 +191,8 @@ Coder.prototype.encodeWithOffset = function (type, Type, encoded, offset) {
* @param {String} bytes
* @return {Object} plain param
*/
Coder.prototype.decodeParam = function (type, bytes) {
return this.decodeParams([type], bytes)[0];
Coder.prototype.decodeParam = function (type, bytes, indexed=false) {
return this.decodeParams([type], bytes, indexed)[0];
};

/**
Expand All @@ -204,7 +208,7 @@ Coder.prototype.decodeParams = function (types, bytes, indexed=false) {
var offsets = this.getOffsets(types, Types);

return Types.map(function (Type, index) {
return Type.decode(bytes, offsets[index], types[index], indexed);
return Type.decode(bytes, offsets[index], types[index], indexed);
});
};

Expand Down
3 changes: 2 additions & 1 deletion lib/types/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ var formatOutputDynamicBytes = function (param) {
*/
var formatOutputString = function (param, name, indexed=false) {
if(indexed) {
return utils.toUtf8(param.value)
// return utils.toUtf8(param.value)
return ""
}
var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2;
return utils.toUtf8(param.dynamicPart().substr(64, length));
Expand Down
20 changes: 16 additions & 4 deletions lib/vnt/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ Event.prototype.encode = function (indexed, options) {

if (utils.isArray(value)) {
return value.map(function (v) {
return '0x' + coder.encodeParam(i.type, v);
return '0x' + coder.encodeParam(i.type, v, true);
});
}
return '0x' + coder.encodeParam(i.type, value);
return '0x' + coder.encodeParam(i.type, value, true);
});

result.topics = result.topics.concat(indexedTopics);
Expand Down Expand Up @@ -177,9 +177,21 @@ Event.prototype.execute = function (indexed, options, callback) {

if (utils.isFunction(arguments[arguments.length - 1])) {
callback = arguments[arguments.length - 1];
if(arguments.length === 2)
if(arguments.length === 2){
options = arguments[0];
indxed = {}
} else if(arguments.length === 1) {
options = null;
if(arguments.length === 1) {
indexed = {};
}
} else {
if(arguments.length === 2){
options = arguments[1];
indxed = arguments[0];
} else if(arguments.length === 1) {
options = arguments[0];
indexed = {};
} else {
options = null;
indexed = {};
}
Expand Down

0 comments on commit 7581bb9

Please sign in to comment.