passing 18 tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import ethUtil = require('ethereumjs-util');
|
||||
import CommunicationChatBubbleOutline from 'material-ui/SvgIcon';
|
||||
var _ = require('lodash');
|
||||
|
||||
export abstract class CalldataBlock {
|
||||
@@ -157,12 +158,18 @@ class Queue<T> {
|
||||
pop(): T | undefined {
|
||||
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 {
|
||||
private selector: string;
|
||||
private sizeInBytes: number;
|
||||
private root: CalldataBlock | undefined;
|
||||
private root: MemberCalldataBlock | undefined;
|
||||
|
||||
constructor() {
|
||||
this.selector = '0x';
|
||||
@@ -170,12 +177,53 @@ export class Calldata {
|
||||
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 {
|
||||
let selectorBuffer = ethUtil.toBuffer(this.selector);
|
||||
if (this.root === undefined) {
|
||||
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>();
|
||||
blockQueue.push(this.root);
|
||||
|
||||
@@ -211,7 +259,7 @@ export class Calldata {
|
||||
blockQueue.pushFront(member);
|
||||
});
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
const combinedBuffers = Buffer.concat(valueBufs);
|
||||
const hexValue = ethUtil.bufferToHex(combinedBuffers);
|
||||
@@ -230,7 +278,7 @@ export class Calldata {
|
||||
return "";
|
||||
}
|
||||
|
||||
public setRoot(block: CalldataBlock) {
|
||||
public setRoot(block: MemberCalldataBlock) {
|
||||
this.root = block;
|
||||
this.sizeInBytes += block.getSizeInBytes();
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ export abstract class PayloadDataType extends DataType {
|
||||
|
||||
public encode(value: any, calldata: Calldata): void {
|
||||
const block = this.generateCalldataBlock(value);
|
||||
calldata.setRoot(block);
|
||||
// calldata.setRoot(block);
|
||||
}
|
||||
|
||||
public isStatic(): boolean {
|
||||
@@ -78,7 +78,7 @@ export abstract class DependentDataType extends DataType {
|
||||
|
||||
public encode(value: any, calldata: Calldata = new Calldata()): void {
|
||||
const block = this.generateCalldataBlock(value);
|
||||
calldata.setRoot(block);
|
||||
//calldata.setRoot(block);
|
||||
}
|
||||
|
||||
public isStatic(): boolean {
|
||||
@@ -125,15 +125,20 @@ export abstract class MemberDataType extends DataType {
|
||||
type: memberItem.type,
|
||||
name: `${dataItem.name}.${memberItem.name}`,
|
||||
} as DataItem;
|
||||
const components = memberItem.components;
|
||||
if (components !== undefined) {
|
||||
childDataItem.components = components;
|
||||
}
|
||||
const child = DataTypeFactory.create(childDataItem, this);
|
||||
memberMap[memberItem.name] = members.length;
|
||||
members.push(child);
|
||||
memberMap[dataItem.name] = members.length;
|
||||
});
|
||||
|
||||
return [members, memberMap];
|
||||
}
|
||||
|
||||
private createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] {
|
||||
console.log('!'.repeat(30), dataItem);
|
||||
let members: DataType[] = [];
|
||||
let memberMap: MemberMap = {};
|
||||
const range = _.range(length);
|
||||
@@ -147,8 +152,8 @@ export abstract class MemberDataType extends DataType {
|
||||
childDataItem.components = components;
|
||||
}
|
||||
const child = DataTypeFactory.create(childDataItem, this);
|
||||
members.push(child);
|
||||
memberMap[idx.toString(10)] = members.length;
|
||||
members.push(child);
|
||||
});
|
||||
|
||||
return [members, memberMap];
|
||||
|
||||
@@ -315,6 +315,7 @@ export class Tuple extends MemberDataType {
|
||||
private tupleSignature: string;
|
||||
|
||||
constructor(dataItem: DataItem) {
|
||||
console.log(dataItem);
|
||||
super(dataItem);
|
||||
if (!Tuple.matchGrammar(dataItem.type)) {
|
||||
throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`);
|
||||
|
||||
@@ -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);
|
||||
console.log(method);
|
||||
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);
|
||||
});
|
||||
|
||||
it.only('Multidimensional Arrays / Static Members', async () => {
|
||||
it('Multidimensional Arrays / Static Members', async () => {
|
||||
const method = new AbiEncoder.Method(AbiSamples.multiDimensionalArraysStaticTypeAbi);
|
||||
|
||||
// Eight 3-dimensional arrays of uint8[2][2][2]
|
||||
|
||||
Reference in New Issue
Block a user