Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2108 from ethcore/jg-abi-indexd-string-fix
Browse files Browse the repository at this point in the history
contract events, indexed string fix
  • Loading branch information
jacogr authored Sep 19, 2016
2 parents 4396952 + c966cbd commit 241ea7c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 17 deletions.
6 changes: 6 additions & 0 deletions js/src/abi/decoder/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export default class Decoder {
return new DecodeResult(new Token(param.type, taken.bytes), offset + 1);

case 'string':
if (param.indexed) {
taken = Decoder.takeBytes(slices, offset, 32);

return new DecodeResult(new Token('fixedBytes', taken.bytes), offset + 1);
}

lengthOffset = asU32(Decoder.peek(slices, offset)).div(32).toNumber();
length = asU32(Decoder.peek(slices, lengthOffset)).toNumber();
taken = Decoder.takeBytes(slices, lengthOffset + 1, length);
Expand Down
6 changes: 6 additions & 0 deletions js/src/abi/decoder/decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ describe('abi/decoder/Decoder', () => {
Decoder.decodeParam(new ParamType('string'), [padU32(0x20), padU32(9), string1], 0).token
).to.deep.equal(tokenString1);
});

it('decodes string (indexed)', () => {
expect(
Decoder.decodeParam(new ParamType('string', null, 0, true), [bytes1], 0)
).to.deep.equal(Decoder.decodeParam(new ParamType('fixedBytes', null, 32, true), [bytes1], 0));
});
});

describe('decode', () => {
Expand Down
6 changes: 3 additions & 3 deletions js/src/abi/spec/event/event.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('abi/spec/event/Event', () => {

describe('inputParamTypes', () => {
it('returns all the types', () => {
expect(event.inputParamTypes()).to.deep.equal([new ParamType('bool'), new ParamType('uint', null, 256)]);
expect(event.inputParamTypes()).to.deep.equal([new ParamType('bool'), new ParamType('uint', null, 256, true)]);
});
});

Expand Down Expand Up @@ -86,9 +86,9 @@ describe('abi/spec/event/Event', () => {
expect(decoded.address).to.equal('0x4444444444444444444444444444444444444444');
expect(decoded.params).to.deep.equal([
new DecodedLogParam('a', new ParamType('int', null, 256), new Token('int', new BigNumber(3))),
new DecodedLogParam('b', new ParamType('int', null, 256), new Token('int', new BigNumber(2))),
new DecodedLogParam('b', new ParamType('int', null, 256, true), new Token('int', new BigNumber(2))),
new DecodedLogParam('c', new ParamType('address'), new Token('address', '0x2222222222222222222222222222222222222222')),
new DecodedLogParam('d', new ParamType('address'), new Token('address', '0x1111111111111111111111111111111111111111'))
new DecodedLogParam('d', new ParamType('address', null, 0, true), new Token('address', '0x1111111111111111111111111111111111111111'))
]);
});

Expand Down
6 changes: 3 additions & 3 deletions js/src/abi/spec/event/eventParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import { toParamType } from '../paramType/format';

export default class EventParam {
constructor (name, type, indexed) {
constructor (name, type, indexed = false) {
this._name = name;
this._kind = toParamType(type);
this._indexed = !!indexed;
this._indexed = indexed;
this._kind = toParamType(type, indexed);
}

get name () {
Expand Down
7 changes: 5 additions & 2 deletions js/src/abi/spec/event/eventParam.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import EventParam from './eventParam';

describe('abi/spec/event/EventParam', () => {
describe('constructor', () => {
const param = new EventParam('foo', 'uint', true);

it('sets the properties', () => {
const param = new EventParam('foo', 'uint', true);
expect(param.name).to.equal('foo');
expect(param.kind.type).to.equal('uint');
expect(param.indexed).to.be.true;
});

it('uses defaults for indexed', () => {
expect(new EventParam('foo', 'uint').indexed).to.be.false;
});
});

describe('toEventParams', () => {
Expand Down
16 changes: 8 additions & 8 deletions js/src/abi/spec/paramType/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@

import ParamType from './paramType';

export function toParamType (type) {
export function toParamType (type, indexed) {
if (type[type.length - 1] === ']') {
const last = type.lastIndexOf('[');
const length = type.substr(last + 1, type.length - last - 2);
const subtype = toParamType(type.substr(0, last));

if (length.length === 0) {
return new ParamType('array', subtype);
return new ParamType('array', subtype, 0, indexed);
}

return new ParamType('fixedArray', subtype, parseInt(length, 10));
return new ParamType('fixedArray', subtype, parseInt(length, 10), indexed);
}

switch (type) {
case 'address':
case 'bool':
case 'bytes':
case 'string':
return new ParamType(type);
return new ParamType(type, null, 0, indexed);

case 'int':
case 'uint':
return new ParamType(type, null, 256);
return new ParamType(type, null, 256, indexed);

default:
if (type.indexOf('uint') === 0) {
return new ParamType('uint', null, parseInt(type.substr(4), 10));
return new ParamType('uint', null, parseInt(type.substr(4), 10), indexed);
} else if (type.indexOf('int') === 0) {
return new ParamType('int', null, parseInt(type.substr(3), 10));
return new ParamType('int', null, parseInt(type.substr(3), 10), indexed);
} else if (type.indexOf('bytes') === 0) {
return new ParamType('fixedBytes', null, parseInt(type.substr(5), 10));
return new ParamType('fixedBytes', null, parseInt(type.substr(5), 10), indexed);
}

throw new Error(`Cannot convert ${type} to valid ParamType`);
Expand Down
7 changes: 6 additions & 1 deletion js/src/abi/spec/paramType/paramType.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import TYPES from './types';

export default class ParamType {
constructor (type, subtype, length) {
constructor (type, subtype = null, length = 0, indexed = false) {
ParamType.validateType(type);

this._type = type;
this._subtype = subtype;
this._length = length;
this._indexed = indexed;
}

get type () {
Expand All @@ -37,6 +38,10 @@ export default class ParamType {
return this._length;
}

get indexed () {
return this._indexed;
}

static validateType (type) {
if (TYPES.filter((_type) => type === _type).length) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions js/src/abi/spec/paramType/paramType.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,13 @@ describe('abi/spec/paramType/ParamType', () => {
it('sets the length of the object', () => {
expect((new ParamType('array', 'bool', 1)).length).to.equal(1);
});

it('sets the index of the object', () => {
expect((new ParamType('array', 'bool', 1, true)).indexed).to.be.true;
});

it('sets default values where none supplied', () => {
expect(Object.values(new ParamType('string'))).to.deep.equal(['string', null, 0, false]);
});
});
});

0 comments on commit 241ea7c

Please sign in to comment.