|
| 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 { |
| 6 | + executeProgram, |
| 7 | + type ExecuteProgramResult, |
| 8 | +} from "#test/evm/behavioral"; |
| 9 | + |
| 10 | +const lengths = [0, 31, 32, 33, 100]; |
| 11 | +const levels = [0, 1, 2, 3] as const; |
| 12 | + |
| 13 | +// 101 distinct bytes, 0x01 to 0x65 |
| 14 | +const source = Array.from({ length: 101 }, (_, i) => |
| 15 | + (i + 1).toString(16).padStart(2, "0"), |
| 16 | +).join(""); |
| 17 | + |
| 18 | +const word = (n: bigint) => hexToBytes(n.toString(16).padStart(64, "0")); |
| 19 | + |
| 20 | +/** Storage `[1] out: mapping<uint256, uint256>` at key `i` */ |
| 21 | +const outSlot = (i: number) => |
| 22 | + BigInt( |
| 23 | + "0x" + |
| 24 | + bytesToHex(keccak256(new Uint8Array([...word(BigInt(i)), ...word(1n)]))), |
| 25 | + ); |
| 26 | + |
| 27 | +/** |
| 28 | + * The program copies a slice's length to slot 0, and its bytes, one by |
| 29 | + * one, to `out` |
| 30 | + */ |
| 31 | +const copyOut = (slice: string) => ` |
| 32 | + let x = ${slice}; |
| 33 | + n = x.length; |
| 34 | + for (let i = 0; i < x.length; i = i + 1) { |
| 35 | + out[i] = x[i]; |
| 36 | + }`; |
| 37 | + |
| 38 | +async function expectBytes(result: ExecuteProgramResult, expected: string) { |
| 39 | + expect(result.callSuccess).toBe(true); |
| 40 | + const bytes = hexToBytes(expected); |
| 41 | + expect(await result.getStorage(0n)).toBe(BigInt(bytes.length)); |
| 42 | + for (let i = 0; i < bytes.length; i++) { |
| 43 | + expect(await result.getStorage(outSlot(i)), `byte ${i}`).toBe( |
| 44 | + BigInt(bytes[i]), |
| 45 | + ); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +describe("a slice copies all its bytes", () => { |
| 50 | + for (const level of levels) { |
| 51 | + for (const length of lengths) { |
| 52 | + it(`of msg.data, ${length} bytes (level ${level})`, async () => { |
| 53 | + const result = await executeProgram( |
| 54 | + `name CalldataSlice; |
| 55 | +storage { [0] n: uint256; [1] out: mapping<uint256, uint256>; } |
| 56 | +code {${copyOut("msg.data[1:msg.data.length]")} |
| 57 | +}`, |
| 58 | + { |
| 59 | + calldata: source.slice(0, (length + 1) * 2), |
| 60 | + optimizationLevel: level, |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + await expectBytes(result, source.slice(2, (length + 1) * 2)); |
| 65 | + }); |
| 66 | + |
| 67 | + it(`of memory bytes, ${length} bytes (level ${level})`, async () => { |
| 68 | + const result = await executeProgram( |
| 69 | + `name MemorySlice; |
| 70 | +storage { [0] n: uint256; [1] out: mapping<uint256, uint256>; } |
| 71 | +code { |
| 72 | + let m: bytes = 0x${source};${copyOut(`m[1:${length + 1}]`)} |
| 73 | +}`, |
| 74 | + { calldata: "", optimizationLevel: level }, |
| 75 | + ); |
| 76 | + |
| 77 | + await expectBytes(result, source.slice(2, (length + 1) * 2)); |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | +}); |
0 commit comments