|
| 1 | +import fd58 |
| 2 | +import hashlib |
| 3 | +import test_suite.elf_pb2 as elf_pb |
| 4 | +from dataclasses import dataclass |
| 5 | +import binascii |
| 6 | +import requests |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | +OUTPUT_DIR = "./test-vectors/elf/tests" |
| 11 | + |
| 12 | + |
| 13 | +# manual code cov |
| 14 | +test_vectors = [ |
| 15 | + { |
| 16 | + "elf_path": "../firedancer/src/ballet/sbpf/fixtures/hello_solana_program.so", |
| 17 | + "features": [], |
| 18 | + "deploy_checks": False, |
| 19 | + } |
| 20 | +] |
| 21 | +# fmt: on |
| 22 | + |
| 23 | + |
| 24 | +def list_dir_recursive_os_walk(start_path="."): |
| 25 | + """ |
| 26 | + Recursively lists all files and directories starting from start_path using os.walk(). |
| 27 | + """ |
| 28 | + for root, dirs, files in os.walk(start_path): |
| 29 | + # print(f"Directory: {root}") |
| 30 | + # for d in dirs: |
| 31 | + # print(f" Subdirectory: {os.path.join(root, d)}") |
| 32 | + for f in files: |
| 33 | + # print(f" File: {os.path.join(root, f)}") |
| 34 | + if f.endswith(".elf"): |
| 35 | + # print(f" File: {os.path.join(root, f)}") |
| 36 | + yield (root, f) |
| 37 | + |
| 38 | + |
| 39 | +def _into_key_data(key_prefix, test_vectors): |
| 40 | + return [(key_prefix + str(j), data) for j, data in enumerate(test_vectors)] |
| 41 | + |
| 42 | + |
| 43 | +print("Generating ELF tests...") |
| 44 | + |
| 45 | +test_vectors = _into_key_data("e", test_vectors) |
| 46 | + |
| 47 | +# for key, test in test_vectors: |
| 48 | +# elf_ctx = elf_pb.ELFLoaderCtx() |
| 49 | + |
| 50 | +# with open(test.get("elf_path"), "rb") as f: |
| 51 | +# elf_ctx.elf.data = f.read() |
| 52 | +# elf_ctx.features.features.extend(test.get("features", [])) |
| 53 | +# elf_ctx.deploy_checks = test.get("deploy_checks", False) |
| 54 | + |
| 55 | +# serialized_elf = elf_ctx.SerializeToString(deterministic=True) |
| 56 | +# filename = str(key) + "_" + hashlib.sha3_256(serialized_elf).hexdigest()[:16] |
| 57 | +# with open(f"{OUTPUT_DIR}/{filename}.bin", "wb") as f: |
| 58 | +# f.write(serialized_elf) |
| 59 | + |
| 60 | +for root, file in list_dir_recursive_os_walk("../solana-programs/programs/mainnet"): |
| 61 | + elf_ctx = elf_pb.ELFLoaderCtx() |
| 62 | + |
| 63 | + with open(os.path.join(root, file), "rb") as f: |
| 64 | + elf_ctx.elf.data = f.read() |
| 65 | + elf_ctx.features.features.extend([]) |
| 66 | + elf_ctx.deploy_checks = False |
| 67 | + |
| 68 | + filename = file.replace(".elf", "") |
| 69 | + |
| 70 | + serialized_elf = elf_ctx.SerializeToString(deterministic=True) |
| 71 | + with open(f"{OUTPUT_DIR}/d0_{filename}.bin", "wb") as f: |
| 72 | + f.write(serialized_elf) |
| 73 | + |
| 74 | + elf_ctx.deploy_checks = True |
| 75 | + serialized_elf = elf_ctx.SerializeToString(deterministic=True) |
| 76 | + with open(f"{OUTPUT_DIR}/d1_{filename}.bin", "wb") as f: |
| 77 | + f.write(serialized_elf) |
| 78 | + |
| 79 | +print("done!") |
0 commit comments