Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 14m10s
97 lines
3.4 KiB
Erlang
97 lines
3.4 KiB
Erlang
-module(bootstrap).
|
|
-export([read_genesis/0, read_genesis/1,
|
|
read_section/2, sections/0, section_subdir/1,
|
|
default_base/0, ends_with_sx/1]).
|
|
|
|
%% Genesis bundle reader per design §12.2.
|
|
%%
|
|
%% read_genesis/0,1 walks the seven canonical section subdirectories
|
|
%% under `next/genesis/`, filters .sx files, reads each file into a
|
|
%% binary, and returns a structured snapshot:
|
|
%%
|
|
%% {ok, [{Section :: atom,
|
|
%% [{FileName :: binary, FileBytes :: binary}, ...]},
|
|
%% ...]}
|
|
%%
|
|
%% Step 4d will compute the bundle CID by hashing the assembled
|
|
%% byte string across all entries; Step 4e will register the parsed
|
|
%% definitions in the kernel registry.
|
|
%%
|
|
%% Port note: this module does NOT parse the .sx contents. The
|
|
%% Erlang-on-SX port has no in-Erlang path from binary bytes to SX
|
|
%% structured terms (same substrate gap that parked Step 3b); the
|
|
%% bundle CID needs only the raw bytes, and registry registration
|
|
%% will happen via an SX-side helper that the kernel hands the
|
|
%% binary contents to. read_genesis/1 ignores its arg in v1 except
|
|
%% to swap the BasePath — `default_base/0` is "next/genesis".
|
|
%%
|
|
%% Port note 2: string-literal binary segments `<<"abc">>` truncate
|
|
%% to one byte in this port, so all path constants are hand-spelled
|
|
%% as integer-segment binaries.
|
|
|
|
%% ── Public API ──────────────────────────────────────────────────
|
|
|
|
%% "next/genesis"
|
|
default_base() ->
|
|
<<110,101,120,116,47,103,101,110,101,115,105,115>>.
|
|
|
|
read_genesis() ->
|
|
read_genesis(default_base()).
|
|
|
|
read_genesis(BasePath) ->
|
|
{ok, lists:map(
|
|
fun (S) -> {S, read_section(BasePath, S)} end,
|
|
sections())}.
|
|
|
|
sections() ->
|
|
[activity_types, object_types, projections,
|
|
validators, codecs, sig_suites, audience].
|
|
|
|
%% "activity-types"
|
|
section_subdir(activity_types) ->
|
|
<<97,99,116,105,118,105,116,121,45,116,121,112,101,115>>;
|
|
%% "object-types"
|
|
section_subdir(object_types) ->
|
|
<<111,98,106,101,99,116,45,116,121,112,101,115>>;
|
|
%% "projections"
|
|
section_subdir(projections) ->
|
|
<<112,114,111,106,101,99,116,105,111,110,115>>;
|
|
%% "validators"
|
|
section_subdir(validators) ->
|
|
<<118,97,108,105,100,97,116,111,114,115>>;
|
|
%% "codecs"
|
|
section_subdir(codecs) ->
|
|
<<99,111,100,101,99,115>>;
|
|
%% "sig-suites"
|
|
section_subdir(sig_suites) ->
|
|
<<115,105,103,45,115,117,105,116,101,115>>;
|
|
%% "audience"
|
|
section_subdir(audience) ->
|
|
<<97,117,100,105,101,110,99,101>>.
|
|
|
|
read_section(BasePath, Section) ->
|
|
SubDir = section_subdir(Section),
|
|
%% 47 = '/'
|
|
Path = <<BasePath/binary, 47, SubDir/binary>>,
|
|
case file:list_dir(Path) of
|
|
{ok, Names} ->
|
|
SxNames = lists:filter(fun (N) -> ends_with_sx(N) end, Names),
|
|
lists:map(fun (Name) -> read_one(Path, Name) end, SxNames);
|
|
{error, _} ->
|
|
[]
|
|
end.
|
|
|
|
%% Suffix check on the .sx extension. 46='.' 115='s' 120='x'.
|
|
ends_with_sx(<<46, 115, 120>>) -> true;
|
|
ends_with_sx(<<>>) -> false;
|
|
ends_with_sx(<<_, Rest/binary>>) -> ends_with_sx(Rest).
|
|
|
|
%% ── Internal ────────────────────────────────────────────────────
|
|
|
|
read_one(DirPath, Name) ->
|
|
Full = <<DirPath/binary, 47, Name/binary>>,
|
|
case file:read_file(Full) of
|
|
{ok, Bytes} -> {Name, Bytes};
|
|
{error, R} -> {Name, {error, R}}
|
|
end.
|