|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { keccak256 } from "ethereum-cryptography/keccak"; |
| 3 | +import { bytesToHex, hexToBytes } from "ethereum-cryptography/utils"; |
| 4 | + |
| 5 | +import { executeProgram } from "#test/evm/behavioral"; |
| 6 | + |
| 7 | +const levels = [0, 1, 2, 3] as const; |
| 8 | + |
| 9 | +const word = (n: bigint) => n.toString(16).padStart(64, "0"); |
| 10 | + |
| 11 | +/** Calldata for `setMotd(string)`, ABI-encoded, without the 0x */ |
| 12 | +function setMotd(text: string): string { |
| 13 | + const data = Buffer.from(text).toString("hex"); |
| 14 | + const padded = data.padEnd(Math.ceil(data.length / 64) * 64, "0"); |
| 15 | + return "deadbeef" + word(32n) + word(BigInt(text.length)) + padded; |
| 16 | +} |
| 17 | + |
| 18 | +/** The slot of `out[i]`, with `[3] out: mapping<uint256, uint256>` */ |
| 19 | +const outSlot = (i: number) => |
| 20 | + BigInt("0x" + bytesToHex(keccak256(hexToBytes(word(BigInt(i)) + word(3n))))); |
| 21 | + |
| 22 | +/** A string's slot as Solidity stores it, and its data slots */ |
| 23 | +function stored(text: string, slot: bigint): Map<bigint, bigint> { |
| 24 | + const bytes = Buffer.from(text); |
| 25 | + const slots = new Map<bigint, bigint>(); |
| 26 | + if (bytes.length < 32) { |
| 27 | + const data = Buffer.alloc(32); |
| 28 | + bytes.copy(data); |
| 29 | + data[31] = bytes.length * 2; |
| 30 | + slots.set(slot, BigInt("0x" + data.toString("hex"))); |
| 31 | + return slots; |
| 32 | + } |
| 33 | + slots.set(slot, BigInt(bytes.length * 2 + 1)); |
| 34 | + const base = BigInt("0x" + bytesToHex(keccak256(hexToBytes(word(slot))))); |
| 35 | + for (let i = 0; i * 32 < bytes.length; i++) { |
| 36 | + const data = Buffer.alloc(32); |
| 37 | + bytes.copy(data, 0, i * 32, i * 32 + 32); |
| 38 | + slots.set(base + BigInt(i), BigInt("0x" + data.toString("hex"))); |
| 39 | + } |
| 40 | + return slots; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Declare `text`, with an annotation (`: bytes calldata` or `: bytes`) |
| 45 | + * or none. Its bounds come from its ABI offset and length words. |
| 46 | + */ |
| 47 | +const decodeAs = (annotation: string) => ` |
| 48 | + let offset = msg.data[4:36] as bytes32 as uint256; |
| 49 | + let n = msg.data[4 + offset:36 + offset] as bytes32 as uint256; |
| 50 | + let text${annotation} = msg.data[36 + offset:36 + offset + n];`; |
| 51 | + |
| 52 | +const decode = decodeAs(""); |
| 53 | + |
| 54 | +// A calldata reference untyped or typed so, and a copy in memory |
| 55 | +const annotations = ["", ": bytes calldata", ": bytes"]; |
| 56 | + |
| 57 | +const texts = [ |
| 58 | + "", |
| 59 | + "hello", |
| 60 | + "a".repeat(31), |
| 61 | + "b".repeat(32), |
| 62 | + "the quick brown fox jumps over the lazy dog, twice", |
| 63 | +]; |
| 64 | + |
| 65 | +describe.each(levels)("a slice of calldata at O%i", (level) => { |
| 66 | + for (const [text, annotation] of texts.flatMap((text) => |
| 67 | + annotations.map((annotation) => [text, annotation] as const), |
| 68 | + )) { |
| 69 | + const label = `${text.length} bytes, \`let text${annotation}\``; |
| 70 | + const decode = decodeAs(annotation); |
| 71 | + |
| 72 | + it(`reads its length and bytes from calldata, ${label}`, async () => { |
| 73 | + const result = await executeProgram( |
| 74 | + `name Read; |
| 75 | +storage { |
| 76 | + [0] motd: string; [1] len: uint256; [3] out: mapping<uint256, uint256>; |
| 77 | +} |
| 78 | +code {${decode} |
| 79 | + len = text.length; |
| 80 | + for (let i = 0; i < text.length; i = i + 1) { |
| 81 | + out[i] = text[i]; |
| 82 | + } |
| 83 | +}`, |
| 84 | + { calldata: setMotd(text), optimizationLevel: level }, |
| 85 | + ); |
| 86 | + expect(result.callSuccess).toBe(true); |
| 87 | + expect(await result.getStorage(1n)).toBe(BigInt(text.length)); |
| 88 | + for (let i = 0; i < text.length; i++) { |
| 89 | + expect(await result.getStorage(outSlot(i)), `byte ${i}`).toBe( |
| 90 | + BigInt(text.charCodeAt(i)), |
| 91 | + ); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it(`stores its bytes to storage, ${label}`, async () => { |
| 96 | + const result = await executeProgram( |
| 97 | + `name Store; |
| 98 | +storage { [0] motd: string; [1] raw: bytes; } |
| 99 | +code {${decode} |
| 100 | + motd = text as string; |
| 101 | + raw = text; |
| 102 | +}`, |
| 103 | + { calldata: setMotd(text), optimizationLevel: level }, |
| 104 | + ); |
| 105 | + expect(result.callSuccess).toBe(true); |
| 106 | + for (const slot of [0n, 1n]) { |
| 107 | + for (const [at, value] of stored(text, slot)) { |
| 108 | + expect(await result.getStorage(at), `slot ${at}`).toBe(value); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + it("copies to a memory local, a cast and a parameter", async () => { |
| 115 | + const text = "the quick brown fox jumps over the lazy dog, twice"; |
| 116 | + const result = await executeProgram( |
| 117 | + `name Copy; |
| 118 | +define { |
| 119 | + function sum(b: bytes) -> uint256 { |
| 120 | + let total = 0; |
| 121 | + for (let i = 0; i < b.length; i = i + 1) { |
| 122 | + total = total + b[i]; |
| 123 | + } |
| 124 | + return total; |
| 125 | + }; |
| 126 | +} |
| 127 | +storage { |
| 128 | + [0] motd: string; [1] len: uint256; [2] total: uint256; |
| 129 | + [3] out: mapping<uint256, uint256>; |
| 130 | +} |
| 131 | +code {${decode} |
| 132 | + let m: bytes = text; |
| 133 | + m[0] = 88; |
| 134 | + len = m.length; |
| 135 | + let c = text as bytes; |
| 136 | + c[1] = 89; |
| 137 | + out[0] = m[0]; |
| 138 | + out[1] = text[0]; |
| 139 | + out[2] = c[1]; |
| 140 | + out[3] = text[1]; |
| 141 | + total = sum(text); |
| 142 | +}`, |
| 143 | + { calldata: setMotd(text), optimizationLevel: level }, |
| 144 | + ); |
| 145 | + expect(result.callSuccess).toBe(true); |
| 146 | + expect(await result.getStorage(1n)).toBe(BigInt(text.length)); |
| 147 | + expect(await result.getStorage(outSlot(0))).toBe(0x58n); |
| 148 | + expect(await result.getStorage(outSlot(1))).toBe( |
| 149 | + BigInt(text.charCodeAt(0)), |
| 150 | + ); |
| 151 | + expect(await result.getStorage(outSlot(2))).toBe(89n); |
| 152 | + expect(await result.getStorage(outSlot(3))).toBe( |
| 153 | + BigInt(text.charCodeAt(1)), |
| 154 | + ); |
| 155 | + expect(await result.getStorage(2n)).toBe( |
| 156 | + [...Buffer.from(text)].reduce((a, b) => a + BigInt(b), 0n), |
| 157 | + ); |
| 158 | + }); |
| 159 | + |
| 160 | + it("slices a slice, and casts one to bytesN", async () => { |
| 161 | + const result = await executeProgram( |
| 162 | + `name SliceCast; |
| 163 | +storage { |
| 164 | + [0] a: bytes32; [1] b: bytes4; [2] c: uint256; |
| 165 | + [3] out: mapping<uint256, uint256>; |
| 166 | +} |
| 167 | +code { |
| 168 | + let all = msg.data[2:msg.data.length]; |
| 169 | + let part = all[1:4]; |
| 170 | + a = part as bytes32; |
| 171 | + b = msg.data[0:4] as bytes4; |
| 172 | + c = part.length; |
| 173 | + out[0] = part[0]; |
| 174 | + out[1] = part[2]; |
| 175 | +}`, |
| 176 | + { calldata: "0102030405060708", optimizationLevel: level }, |
| 177 | + ); |
| 178 | + expect(result.callSuccess).toBe(true); |
| 179 | + // Bytes past the slice's length are zero |
| 180 | + expect(await result.getStorage(0n)).toBe(0x040506n << 232n); |
| 181 | + expect(await result.getStorage(1n)).toBe(0x01020304n); |
| 182 | + expect(await result.getStorage(2n)).toBe(3n); |
| 183 | + expect(await result.getStorage(outSlot(0))).toBe(4n); |
| 184 | + expect(await result.getStorage(outSlot(1))).toBe(6n); |
| 185 | + }); |
| 186 | + |
| 187 | + it("reverts on an index or slice past the end", async () => { |
| 188 | + for (const access of ["text[5]", "text[2:6].length", "text[3:2].length"]) { |
| 189 | + const result = await executeProgram( |
| 190 | + `name Bounds; |
| 191 | +storage { [1] len: uint256; } |
| 192 | +code {${decode} |
| 193 | + len = ${access}; |
| 194 | +}`, |
| 195 | + { calldata: setMotd("hello"), optimizationLevel: level }, |
| 196 | + ); |
| 197 | + expect(result.callSuccess, access).toBe(false); |
| 198 | + } |
| 199 | + }); |
| 200 | +}); |
0 commit comments