aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/test/abi/calldata.ts
blob: c1630c34a353c75116c3ba60dfc7b99c13a56a55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import ethUtil = require('ethereumjs-util');
var _ = require('lodash');

export abstract class CalldataBlock {
    private name: string;
    private signature: string;
    private offsetInBytes: number;
    private headerSizeInBytes: number;
    private bodySizeInBytes: number;
    private relocatable: boolean;

    constructor(name: string, signature: string, /*offsetInBytes: number,*/ headerSizeInBytes: number, bodySizeInBytes: number, relocatable: boolean) {
        this.name = name;
        this.signature = signature;
        this.offsetInBytes = 0;
        this.headerSizeInBytes = headerSizeInBytes;
        this.bodySizeInBytes = bodySizeInBytes;
        this.relocatable = relocatable;
    }

    protected setHeaderSize(headerSizeInBytes: number) {
        this.headerSizeInBytes = headerSizeInBytes;
    }

    protected setBodySize(bodySizeInBytes: number) {
        this.bodySizeInBytes = bodySizeInBytes;
    }

    public getName(): string {
        return this.name;
    }

    public getSignature(): string {
        return this.signature;
    }

    public isRelocatable(): boolean {
        return this.relocatable;
    }

    public getHeaderSizeInBytes(): number {
        return this.headerSizeInBytes;
    }

    public getBodySizeInBytes(): number {
        return this.bodySizeInBytes;
    }

    public getSizeInBytes(): number {
        return this.headerSizeInBytes + this.bodySizeInBytes;
    }

    public getOffsetInBytes(): number {
        return this.offsetInBytes;
    }

    public setOffset(offsetInBytes: number) {
        this.offsetInBytes = offsetInBytes;
    }

    public abstract toBuffer(): Buffer;
}

export class PayloadCalldataBlock extends CalldataBlock {
    private payload: Buffer;

    constructor(name: string, signature: string, /*offsetInBytes: number,*/ relocatable: boolean, payload: Buffer) {
        const headerSizeInBytes = 0;
        const bodySizeInBytes = payload.byteLength;
        super(name, signature, /*offsetInBytes,*/ headerSizeInBytes, bodySizeInBytes, relocatable);
        this.payload = payload;
    }

    public toBuffer(): Buffer {
        return this.payload;
    }
}

export class DependentCalldataBlock extends CalldataBlock {
    public static DEPENDENT_PAYLOAD_SIZE_IN_BYTES = 32;
    private parent: CalldataBlock;
    private dependency: CalldataBlock;

    constructor(name: string, signature: string, /*offsetInBytes: number,*/ relocatable: boolean, dependency: CalldataBlock, parent: CalldataBlock) {
        const headerSizeInBytes = 0;
        const bodySizeInBytes = DependentCalldataBlock.DEPENDENT_PAYLOAD_SIZE_IN_BYTES;
        super(name, signature, /*offsetInBytes,*/ headerSizeInBytes, bodySizeInBytes, relocatable);
        this.parent = parent;
        this.dependency = dependency;
    }

    public toBuffer(): Buffer {
        const dependencyOffset = this.dependency.getOffsetInBytes();
        console.log("Dependency Offset - ", dependencyOffset);
        const parentOffset = this.parent.getOffsetInBytes();
        console.log("Parent Offset - ", parentOffset);
        const parentHeaderSize = this.parent.getHeaderSizeInBytes();
        console.log("Parent Header size - ", parentHeaderSize);
        const pointer: number = dependencyOffset - (parentOffset + parentHeaderSize);
        console.log("DAT PTR = ", pointer);
        const pointerBuf = ethUtil.toBuffer(`0x${pointer.toString(16)}`);
        console.log("Chye - ", pointerBuf);
        const evmWordWidthInBytes = 32;
        const pointerBufPadded = ethUtil.setLengthLeft(pointerBuf, evmWordWidthInBytes);
        return pointerBufPadded;
    }

    public getDependency(): CalldataBlock {
        return this.dependency;
    }
}

export class MemberCalldataBlock extends CalldataBlock {
    private static DEPENDENT_PAYLOAD_SIZE_IN_BYTES = 32;
    private header: Buffer | undefined;
    private members: CalldataBlock[];

    constructor(name: string, signature: string, /*offsetInBytes: number,*/ relocatable: boolean) {
        super(name, signature, /*offsetInBytes,*/ 0, 0, relocatable);
        this.members = [];
        this.header = undefined;
    }

    public setMembers(members: CalldataBlock[]) {
        let bodySizeInBytes = 0;
        _.each(members, (member: CalldataBlock) => {
            bodySizeInBytes += member.getSizeInBytes();
        });
        this.members = members;
        this.setBodySize(0);
    }

    public setHeader(header: Buffer) {
        this.setHeaderSize(header.byteLength);
        this.header = header;
    }

    public toBuffer(): Buffer {
        if (this.header !== undefined) return this.header;
        return new Buffer('');
    }

    public getMembers(): CalldataBlock[] {
        return this.members;
    }
}

class Queue<T> {
    private store: T[] = [];
    push(val: T) {
        this.store.push(val);
    }
    pop(): T | undefined {
        return this.store.shift();
    }
}

export class Calldata {
    private selector: string;
    private sizeInBytes: number;
    private root: CalldataBlock | undefined;

    constructor() {
        this.selector = '0x';
        this.sizeInBytes = 0;
        this.root = undefined;
    }

    public toHexString(): string {
        let selectorBuffer = ethUtil.toBuffer(this.selector);
        if (this.root === undefined) {
            throw new Error('expected root');
        }

        const blockQueue = new Queue<CalldataBlock>();
        blockQueue.push(this.root);

        // Assign locations in breadth-first manner
        let block: CalldataBlock | undefined;
        let offset = 0;
        while ((block = blockQueue.pop()) !== undefined) {
            console.log(block.getName());
            block.setOffset(offset);
            offset += block.getSizeInBytes();
            if (block instanceof DependentCalldataBlock) {
                console.log(block.getDependency());
                blockQueue.push(block.getDependency());
            } else if (block instanceof MemberCalldataBlock) {
                _.each(block.getMembers(), (member: CalldataBlock) => {
                    blockQueue.push(member);
                });
            }
        }

        console.log(this.root);

        // Fetch values using same technique
        const valueBufs: Buffer[] = [selectorBuffer];
        blockQueue.push(this.root);
        while ((block = blockQueue.pop()) !== undefined) {
            valueBufs.push(block.toBuffer());

            if (block instanceof DependentCalldataBlock) {
                blockQueue.push(block.getDependency());
            } else if (block instanceof MemberCalldataBlock) {
                _.each(block.getMembers(), (member: CalldataBlock) => {
                    blockQueue.push(member);
                });
            }
        }

        const combinedBuffers = Buffer.concat(valueBufs);
        const hexValue = ethUtil.bufferToHex(combinedBuffers);
        return hexValue;
    }

    public getSelectorHex(): string {
        return this.selector;
    }

    public getSizeInBytes(): number {
        return this.sizeInBytes;
    }

    public toAnnotatedString(): string {
        return "";
    }

    public setRoot(block: CalldataBlock) {
        this.root = block;
        this.sizeInBytes += block.getSizeInBytes();
    }

    public setSelector(selector: string) {
        // Ensure we have a 0x prefix
        if (selector.startsWith('0x')) {
            this.selector = selector;
        } else {
            this.selector = `$0x${selector}`;
        }

        // The selector must be 10 characters: '0x' followed by 4 bytes (two hex chars per byte)
        if (this.selector.length !== 10) {
            throw new Error(`Invalid selector '${this.selector}'`);
        }
        this.sizeInBytes += 8;
    }
}