UNPKG

4.86 kBTypeScriptView Raw
1import { EncodingRules } from '../utils/rules';
2import { CalldataBlock } from './calldata_block';
3export declare class Calldata {
4 private readonly _rules;
5 private _selector;
6 private _root;
7 constructor(rules: EncodingRules);
8 /**
9 * Sets the root calldata block. This block usually corresponds to a Method.
10 */
11 setRoot(block: CalldataBlock): void;
12 /**
13 * Sets the selector to be prepended onto the calldata.
14 * If the root block was created by a Method then a selector will likely be set.
15 */
16 setSelector(selector: string): void;
17 /**
18 * Iterates through the calldata blocks, starting from the root block, to construct calldata as a hex string.
19 * If the `optimize` flag is set then this calldata will be condensed, to save gas.
20 * If the `annotate` flag is set then this will return human-readable calldata.
21 * If the `annotate` flag is *not* set then this will return EVM-compatible calldata.
22 */
23 toString(): string;
24 /**
25 * There are three types of calldata blocks: Blob, Set and Pointer.
26 * Scenarios arise where distinct pointers resolve to identical values.
27 * We optimize by keeping only one such instance of the identical value, and redirecting all pointers here.
28 * We keep the last such duplicate value because pointers can only be positive (they cannot point backwards).
29 *
30 * Example #1:
31 * function f(string[], string[])
32 * f(["foo", "bar", "blitz"], ["foo", "bar", "blitz"])
33 * The array ["foo", "bar", "blitz"] will only be included in the calldata once.
34 *
35 * Example #2:
36 * function f(string[], string)
37 * f(["foo", "bar", "blitz"], "foo")
38 * The string "foo" will only be included in the calldata once.
39 *
40 * Example #3:
41 * function f((string, uint, bytes), string, uint, bytes)
42 * f(("foo", 5, "0x05"), "foo", 5, "0x05")
43 * The string "foo" and bytes "0x05" will only be included in the calldata once.
44 * The duplicate `uint 5` values cannot be optimized out because they are static values (no pointer points to them).
45 *
46 * @TODO #1:
47 * This optimization strategy handles blocks that are exact duplicates of one another.
48 * But what if some block is a combination of two other blocks? Or a subset of another block?
49 * This optimization problem is not much different from the current implemetation.
50 * Instead of tracking "observed" hashes, at each node we would simply do pattern-matching on the calldata.
51 * This strategy would be applied after assigning offsets to the tree, rather than before (as in this strategy).
52 * Note that one consequence of this strategy is pointers may resolve to offsets that are not word-aligned.
53 * This shouldn't be a problem but further investigation should be done.
54 *
55 * @TODO #2:
56 * To be done as a follow-up to @TODO #1.
57 * Since we optimize from the bottom-up, we could be affecting the outcome of a later potential optimization.
58 * For example, what if by removing one duplicate value we miss out on optimizing another block higher in the tree.
59 * To handle this case, at each node we can store a candidate optimization in a priority queue (sorted by calldata size).
60 * At the end of traversing the tree, the candidate at the front of the queue will be the most optimal output.
61 *
62 */
63 private _optimize;
64 private _toEvmCompatibeCallDataHex;
65 /**
66 * Returns human-readable calldata.
67 *
68 * Example:
69 * simpleFunction(string[], string[])
70 * strings = ["Hello", "World"]
71 * simpleFunction(strings, strings)
72 *
73 * Output:
74 * 0xbb4f12e3
75 * ### simpleFunction
76 * 0x0 0000000000000000000000000000000000000000000000000000000000000040 ptr<array1> (alias for array2)
77 * 0x20 0000000000000000000000000000000000000000000000000000000000000040 ptr<array2>
78 *
79 * 0x40 0000000000000000000000000000000000000000000000000000000000000002 ### array2
80 * 0x60 0000000000000000000000000000000000000000000000000000000000000040 ptr<array2[0]>
81 * 0x80 0000000000000000000000000000000000000000000000000000000000000080 ptr<array2[1]>
82 * 0xa0 0000000000000000000000000000000000000000000000000000000000000005 array2[0]
83 * 0xc0 48656c6c6f000000000000000000000000000000000000000000000000000000
84 * 0xe0 0000000000000000000000000000000000000000000000000000000000000005 array2[1]
85 * 0x100 576f726c64000000000000000000000000000000000000000000000000000000
86 */
87 private _toHumanReadableCallData;
88}
89//# sourceMappingURL=calldata.d.ts.map
\No newline at end of file