Skip to content

Commit edd9ffc

Browse files
authored
Merge pull request #271 from permaweb/fix/register_attestation_opts
fix: trusted -> snp_trusted | update registration in lua script | snp_trusted is map
2 parents 58e3404 + 185b2df commit edd9ffc

File tree

4 files changed

+187
-85
lines changed

4 files changed

+187
-85
lines changed

scripts/dynamic-router.lua

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,31 +200,20 @@ function recalculate(state, _, opts)
200200
return "ok", state
201201
end
202202

203-
local function findId(commitments, alg)
204-
local id = ""
205-
for k,v in pairs(commitments) do
206-
if v["alg"] == alg then
207-
id = k
208-
end
209-
end
210-
return id
211-
end
212-
213203
-- Register a new host to a route.
214204
function register(state, assignment, opts)
215205
state = ensure_defaults(state)
216206
local req = assignment.body
217207
local status, is_admissible = "ok", "not_found"
218-
if state["is-admissible"] and state["is-admissible"].device == "snp@1.0" then
208+
if state["is-admissible"] then
219209
req.path = "verify"
220-
-- remove commitments of the register/verify message
221-
req.commitments = nil
222-
-- add a target node of the id of the register message
223-
req.target = findId(req.body.commitments, "rsa-pss-sha512")
224-
-- this only works when there is a real message for state['is_admissible']
210+
-- Remove commitments of the register/verify message
211+
-- Add a target node of the id of the register message
212+
req.target = "body"
213+
-- This only works when there is a real message for state['is_admissible']
225214
status, is_admissible = ao.resolve(state["is-admissible"], req)
226215
end
227-
ao.event("is_admissible", { status, is_admissible })
216+
ao.event("debug_dynrouter", { "is_admissible", { status, is_admissible } })
228217
if status == "ok" and is_admissible ~= false then
229218
state = add_node(state, req)
230219
return recalculate(state, assignment, opts)
@@ -234,7 +223,6 @@ function register(state, assignment, opts)
234223
ao.event("error", { "untrusted peer requested", req})
235224
return "ok", state
236225
end
237-
return "ok", state
238226
end
239227

240228
-- Update the performance of a host by its reference.

src/dev_router.erl

Lines changed: 123 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ register(_M1, _M2, Opts) ->
6464
#{},
6565
#{
6666
priv_wallet => hb:wallet(),
67-
trusted => hb_opts:get(trusted, [], Opts)
67+
snp_trusted => hb_opts:get(snp_trusted, #{}, Opts)
6868
}
6969
),
7070
?event(debug_register, {attestion, Attestion}),
@@ -703,16 +703,123 @@ dynamic_router_test() ->
703703
{ok, Script} = file:read_file("scripts/dynamic-router.lua"),
704704
Run = hb_util:bin(rand:uniform(1337)),
705705
Node = hb_http_server:start_node(Opts = #{
706-
trusted => [#{
707-
vcpus => 1,
708-
vcpu_type => 5,
709-
vmm_type => 1,
710-
guest_features => 1,
711-
firmware => <<"b8c5d4082d5738db6b0fb0294174992738645df70c44cdecf7fad3a62244b788e7e408c582ee48a74b289f3acec78510">>,
712-
kernel => <<"69d0cd7d13858e4fcef6bc7797aebd258730f215bc5642c4ad8e4b893cc67576">>,
713-
initrd => <<"da6dffff50373e1d393bf92cb9b552198b1930068176a046dda4e23bb725b3bb">>,
714-
append => <<"aaf13c9ed2e821ea8c82fcc7981c73a14dc2d01c855f09262d42090fa0424422">>
715-
}],
706+
snp_trusted => #{
707+
<<"1">> =>
708+
#{
709+
<<"vcpus">> => 1,
710+
<<"vcpu_type">> => 5,
711+
<<"vmm_type">> => 1,
712+
<<"guest_features">> => 1,
713+
<<"firmware">> => <<"b8c5d4082d5738db6b0fb0294174992738645df70c44cdecf7fad3a62244b788e7e408c582ee48a74b289f3acec78510">>,
714+
<<"kernel">> => <<"69d0cd7d13858e4fcef6bc7797aebd258730f215bc5642c4ad8e4b893cc67576">>,
715+
<<"initrd">> => <<"da6dffff50373e1d393bf92cb9b552198b1930068176a046dda4e23bb725b3bb">>,
716+
<<"append">> => <<"aaf13c9ed2e821ea8c82fcc7981c73a14dc2d01c855f09262d42090fa0424422">>
717+
}
718+
},
719+
store => [
720+
#{
721+
<<"store-module">> => hb_store_fs,
722+
<<"prefix">> => <<"cache-TEST/dynrouter-", Run/binary>>
723+
}
724+
],
725+
priv_wallet => ar_wallet:new(),
726+
preprocessor => #{
727+
<<"device">> => <<"router@1.0">>
728+
},
729+
route_provider => #{
730+
<<"path">> =>
731+
<<"/router~node-process@1.0/compute/routes~message@1.0">>
732+
},
733+
node_processes => #{
734+
<<"router">> => #{
735+
<<"type">> => <<"Process">>,
736+
<<"device">> => <<"process@1.0">>,
737+
<<"execution-device">> => <<"lua@5.3a">>,
738+
<<"scheduler-device">> => <<"scheduler@1.0">>,
739+
<<"script">> => #{
740+
<<"content-type">> => <<"application/lua">>,
741+
<<"module">> => <<"dynamic-router">>,
742+
<<"body">> => Script
743+
},
744+
% Set script-specific factors for the test
745+
<<"pricing-weight">> => 9,
746+
<<"performance-weight">> => 1,
747+
<<"score-preference">> => 4,
748+
<<"is-admissible">> => #{
749+
<<"device">> => <<"snp@1.0">>
750+
}
751+
}
752+
}
753+
}),
754+
% mergeRight this takes our defined Opts and merges them into the
755+
% node opts configs.
756+
Store = hb_opts:get(store, no_store, Opts),
757+
?event(debug_dynrouter, {store, Store}),
758+
% Register workers with the dynamic router with varied prices.
759+
{ok, [Req]} = file:consult(<<"test/admissible-report.eterm">>),
760+
lists:foreach(fun(X) ->
761+
case hb_http:post(
762+
Node,
763+
#{
764+
<<"path">> => <<"/router~node-process@1.0/schedule">>,
765+
<<"method">> => <<"POST">>,
766+
<<"body">> =>
767+
hb_message:commit(
768+
#{
769+
<<"path">> => <<"register">>,
770+
<<"route">> =>
771+
#{
772+
<<"prefix">> =>
773+
<<
774+
"http://149.28.249.192:10000"
775+
>>,
776+
<<"template">> => <<"/.*~process@1.0/.*">>,
777+
<<"price">> => X * 250
778+
},
779+
<<"body">> => Req
780+
},
781+
Opts
782+
)
783+
},
784+
Opts
785+
) of
786+
{ok, Res} ->
787+
?event(debug_dynrouter, { res, Res});
788+
{failure, Res} ->
789+
?event(debug_router_register, { failure, Res})
790+
end
791+
end, lists:seq(1, 1)),
792+
% Force computation of the current state. This should be done with a
793+
% background worker (ex: a `~cron@1.0/every' task).
794+
{Status, NodeRoutes} = hb_http:get(Node, <<"/router~node-process@1.0/now">>, #{}),
795+
?event(debug_dynrouter, {got_node_routes, NodeRoutes}),
796+
% Meta info is a part of the exempt routes. Make sure this returns our address
797+
{ok, _} = hb_http:get(Node, <<"/~meta@1.0/info/address">>, Opts),
798+
% ?assertEqual(hb_util:human_id(ar_wallet:to_address(hb_opts:get(priv_wallet, not_found, Opts))), Res),
799+
% {Status, _} = hb_http:get(Node, <<"/RhguwWmQJ-wWCXhRH_NtTDHRRgfCqNDZckXtJK52zKs~process@1.0/compute&slot=1">>, Opts),
800+
?assertEqual(ok, Status).
801+
802+
%% @doc Example of a Lua script being used as the `route_provider' for a
803+
%% HyperBEAM node. This test specifically sends an invalid attestation report
804+
%% (with mismatched firmware hash) which should be rejected by the SNP
805+
%% validation system. It tests the error handling for invalid attestations.
806+
dynamic_router_invalid_report_test() ->
807+
{ok, Script} = file:read_file("scripts/dynamic-router.lua"),
808+
Run = hb_util:bin(rand:uniform(1337)),
809+
Node = hb_http_server:start_node(Opts = #{
810+
snp_trusted => #{
811+
<<"1">> =>
812+
#{
813+
<<"vcpus">> => 1,
814+
<<"vcpu_type">> => 5,
815+
<<"vmm_type">> => 1,
816+
<<"guest_features">> => 1,
817+
<<"firmware">> => <<"b8c5d4082d5738db6b0fb0294174992734645df70c44cdecf7fad3a62244b788e7e408c582ee48a74b289f3acec78510">>,
818+
<<"kernel">> => <<"69d0cd7d13858e4fcef6bc7797aebd258730f215bc5642c4ad8e4b893cc67576">>,
819+
<<"initrd">> => <<"da6dffff50373e1d393bf92cb9b552198b1930068176a046dda4e23bb725b3bb">>,
820+
<<"append">> => <<"aaf13c9ed2e821ea8c82fcc7981c73a14dc2d01c855f09262d42090fa0424422">>
821+
}
822+
},
716823
store => [
717824
#{
718825
<<"store-module">> => hb_store_fs,
@@ -790,12 +897,17 @@ dynamic_router_test() ->
790897
% background worker (ex: a `~cron@1.0/every' task).
791898
{Status, NodeRoutes} = hb_http:get(Node, <<"/router~node-process@1.0/now">>, #{}),
792899
?event(debug_dynrouter, {got_node_routes, NodeRoutes}),
900+
Routes = hb_ao:get(<<"routes">>, NodeRoutes, Opts),
901+
% This should be 0, as the invalid report should have been rejected.
902+
?event(debug_dynrouter, {routes_length, length(Routes)}),
903+
?assertEqual(0, length(Routes)),
793904
% Meta info is a part of the exempt routes. Make sure this returns our address
794905
{ok, _} = hb_http:get(Node, <<"/~meta@1.0/info/address">>, Opts),
795906
% ?assertEqual(hb_util:human_id(ar_wallet:to_address(hb_opts:get(priv_wallet, not_found, Opts))), Res),
796907
% {Status, _} = hb_http:get(Node, <<"/RhguwWmQJ-wWCXhRH_NtTDHRRgfCqNDZckXtJK52zKs~process@1.0/compute&slot=1">>, Opts),
797908
?assertEqual(ok, Status).
798909

910+
799911
%% @doc Demonstrates routing tables being dynamically created and adjusted
800912
%% according to the real-time performance of nodes. This test utilizes the
801913
%% `dynamic-router' script to manage routes and recalculate weights based on the

0 commit comments

Comments
 (0)