passing 18 tests

This commit is contained in:
Greg Hysen
2018-11-09 23:00:20 -08:00
parent 59206c387e
commit bb7dd23738
4 changed files with 63 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
import ethUtil = require('ethereumjs-util'); import ethUtil = require('ethereumjs-util');
import CommunicationChatBubbleOutline from 'material-ui/SvgIcon';
var _ = require('lodash'); var _ = require('lodash');
export abstract class CalldataBlock { export abstract class CalldataBlock {
@@ -157,12 +158,18 @@ class Queue<T> {
pop(): T | undefined { pop(): T | undefined {
return this.store.shift(); return this.store.shift();
} }
merge(q: Queue<T>) {
this.store = this.store.concat(q.store);
}
mergeFront(q: Queue<T>) {
this.store = q.store.concat(this.store);
}
} }
export class Calldata { export class Calldata {
private selector: string; private selector: string;
private sizeInBytes: number; private sizeInBytes: number;
private root: CalldataBlock | undefined; private root: MemberCalldataBlock | undefined;
constructor() { constructor() {
this.selector = '0x'; this.selector = '0x';
@@ -170,12 +177,53 @@ export class Calldata {
this.root = undefined; this.root = undefined;
} }
private createQueue(memberBlock: MemberCalldataBlock): Queue<CalldataBlock> {
const blockQueue = new Queue<CalldataBlock>();
_.eachRight(memberBlock.getMembers(), (member: CalldataBlock) => {
if (member instanceof MemberCalldataBlock) {
blockQueue.mergeFront(this.createQueue(member));
} else {
blockQueue.pushFront(member);
}
});
// Children
_.each(memberBlock.getMembers(), (member: CalldataBlock) => {
if (member instanceof DependentCalldataBlock) {
const dependency = member.getDependency();
if (dependency instanceof MemberCalldataBlock) {
blockQueue.merge(this.createQueue(dependency));
} else {
blockQueue.push(dependency);
}
}
});
blockQueue.pushFront(memberBlock);
return blockQueue;
}
public toHexString(): string { public toHexString(): string {
let selectorBuffer = ethUtil.toBuffer(this.selector); let selectorBuffer = ethUtil.toBuffer(this.selector);
if (this.root === undefined) { if (this.root === undefined) {
throw new Error('expected root'); throw new Error('expected root');
} }
const offsetQueue = this.createQueue(this.root);
let block: CalldataBlock | undefined;
let offset = 0;
while ((block = offsetQueue.pop()) !== undefined) {
block.setOffset(offset);
offset += block.getSizeInBytes();
}
const valueQueue = this.createQueue(this.root);
const valueBufs: Buffer[] = [selectorBuffer];
while ((block = valueQueue.pop()) !== undefined) {
valueBufs.push(block.toBuffer());
}
/*
const blockQueue = new Queue<CalldataBlock>(); const blockQueue = new Queue<CalldataBlock>();
blockQueue.push(this.root); blockQueue.push(this.root);
@@ -211,7 +259,7 @@ export class Calldata {
blockQueue.pushFront(member); blockQueue.pushFront(member);
}); });
} }
} }*/
const combinedBuffers = Buffer.concat(valueBufs); const combinedBuffers = Buffer.concat(valueBufs);
const hexValue = ethUtil.bufferToHex(combinedBuffers); const hexValue = ethUtil.bufferToHex(combinedBuffers);
@@ -230,7 +278,7 @@ export class Calldata {
return ""; return "";
} }
public setRoot(block: CalldataBlock) { public setRoot(block: MemberCalldataBlock) {
this.root = block; this.root = block;
this.sizeInBytes += block.getSizeInBytes(); this.sizeInBytes += block.getSizeInBytes();
} }

View File

@@ -43,7 +43,7 @@ export abstract class PayloadDataType extends DataType {
public encode(value: any, calldata: Calldata): void { public encode(value: any, calldata: Calldata): void {
const block = this.generateCalldataBlock(value); const block = this.generateCalldataBlock(value);
calldata.setRoot(block); // calldata.setRoot(block);
} }
public isStatic(): boolean { public isStatic(): boolean {
@@ -78,7 +78,7 @@ export abstract class DependentDataType extends DataType {
public encode(value: any, calldata: Calldata = new Calldata()): void { public encode(value: any, calldata: Calldata = new Calldata()): void {
const block = this.generateCalldataBlock(value); const block = this.generateCalldataBlock(value);
calldata.setRoot(block); //calldata.setRoot(block);
} }
public isStatic(): boolean { public isStatic(): boolean {
@@ -125,15 +125,20 @@ export abstract class MemberDataType extends DataType {
type: memberItem.type, type: memberItem.type,
name: `${dataItem.name}.${memberItem.name}`, name: `${dataItem.name}.${memberItem.name}`,
} as DataItem; } as DataItem;
const components = memberItem.components;
if (components !== undefined) {
childDataItem.components = components;
}
const child = DataTypeFactory.create(childDataItem, this); const child = DataTypeFactory.create(childDataItem, this);
memberMap[memberItem.name] = members.length;
members.push(child); members.push(child);
memberMap[dataItem.name] = members.length;
}); });
return [members, memberMap]; return [members, memberMap];
} }
private createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] { private createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] {
console.log('!'.repeat(30), dataItem);
let members: DataType[] = []; let members: DataType[] = [];
let memberMap: MemberMap = {}; let memberMap: MemberMap = {};
const range = _.range(length); const range = _.range(length);
@@ -147,8 +152,8 @@ export abstract class MemberDataType extends DataType {
childDataItem.components = components; childDataItem.components = components;
} }
const child = DataTypeFactory.create(childDataItem, this); const child = DataTypeFactory.create(childDataItem, this);
members.push(child);
memberMap[idx.toString(10)] = members.length; memberMap[idx.toString(10)] = members.length;
members.push(child);
}); });
return [members, memberMap]; return [members, memberMap];

View File

@@ -315,6 +315,7 @@ export class Tuple extends MemberDataType {
private tupleSignature: string; private tupleSignature: string;
constructor(dataItem: DataItem) { constructor(dataItem: DataItem) {
console.log(dataItem);
super(dataItem); super(dataItem);
if (!Tuple.matchGrammar(dataItem.type)) { if (!Tuple.matchGrammar(dataItem.type)) {
throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`);

View File

@@ -116,7 +116,7 @@ describe.only('ABI Encoder', () => {
}); });
it.only('Types with default widths', async () => { it('Types with default widths', async () => {
const method = new AbiEncoder.Method(AbiSamples.typesWithDefaultWidthsAbi); const method = new AbiEncoder.Method(AbiSamples.typesWithDefaultWidthsAbi);
console.log(method); console.log(method);
const args = [new BigNumber(1), new BigNumber(-1), '0x56', [new BigNumber(1)], [new BigNumber(-1)], ['0x56']]; const args = [new BigNumber(1), new BigNumber(-1), '0x56', [new BigNumber(1)], [new BigNumber(-1)], ['0x56']];
@@ -201,7 +201,7 @@ describe.only('ABI Encoder', () => {
expect(calldata).to.be.equal(expectedCalldata); expect(calldata).to.be.equal(expectedCalldata);
}); });
it.only('Multidimensional Arrays / Static Members', async () => { it('Multidimensional Arrays / Static Members', async () => {
const method = new AbiEncoder.Method(AbiSamples.multiDimensionalArraysStaticTypeAbi); const method = new AbiEncoder.Method(AbiSamples.multiDimensionalArraysStaticTypeAbi);
// Eight 3-dimensional arrays of uint8[2][2][2] // Eight 3-dimensional arrays of uint8[2][2][2]