Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 9 additions & 26 deletions src/dev_json_iface.erl
Original file line number Diff line number Diff line change
Expand Up @@ -139,40 +139,23 @@ message_to_json_struct(RawMsg, Features, Opts) ->
{CommitmentOwner, CommitmentSignature, CommitmentKeyId}
end
end,
Last =
hb_ao:get(
<<"anchor">>,
{as, <<"message@1.0">>, MsgWithoutCommitments},
<<>>,
Opts
),
DataBytes =
hb_ao:get(
<<"data">>,
{Last, DataBytes, Target, From} =
hb_ao:get_many(
[
<<"anchor">>,
<<"data">>,
<<"target">>,
<<"from-process">>
],
{as, <<"message@1.0">>, MsgWithoutCommitments},
<<>>,
[<<>>, <<>>, <<>>, hb_util:encode(Owner)],
Opts
),
Data =
case hb_util:is_printable_string(DataBytes) of
true -> DataBytes;
false -> null
end,
Target =
hb_ao:get(
<<"target">>,
{as, <<"message@1.0">>, MsgWithoutCommitments},
<<>>,
Opts
),
% Set "From" if From-Process is Tag or set with "Owner" address
From =
hb_ao:get(
<<"from-process">>,
{as, <<"message@1.0">>, MsgWithoutCommitments},
hb_util:encode(Owner),
Opts
),
#{
<<"Id">> => safe_to_id(ID),
% NOTE: In Arweave TXs, these are called "last_tx"
Expand Down
2 changes: 1 addition & 1 deletion src/dev_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ set(Base, NewValuesMsg, Opts) ->
_ ->
% We did overwrite some keys, but do their values match the original?
% If not, we must remove the commitments.
case hb_message:match(Merged, Base, Opts) of
case hb_message:match(Merged, Base, strict, Opts) of
true ->
?event(message_set, {set_keys_matched, {merged, Merged}}),
{ok, Merged};
Expand Down
46 changes: 27 additions & 19 deletions src/dev_push.erl
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ full_push_test_() ->
Opts = #{
process_async_cache => false,
priv_wallet => hb:wallet(),
cache_control => <<"always">>
cache_control => <<"always">>,
store => [hb_test_utils:test_store()]
},
Base = dev_process:test_aos_process(Opts),
hb_cache:write(Base, Opts),
Expand Down Expand Up @@ -700,7 +701,8 @@ push_as_identity_test_() ->
ComputeID => #{
priv_wallet => ComputeWallet
}
}
},
store => [hb_test_utils:test_store()]
},
% Create a new test AOS process, which will use the given identities as
% its authority and scheduler.
Expand Down Expand Up @@ -766,7 +768,8 @@ multi_process_push_test_() ->
dev_process:init(),
Opts = #{
priv_wallet => hb:wallet(),
cache_control => <<"always">>
cache_control => <<"always">>,
store => [hb_test_utils:test_store()]
},
Proc1 = dev_process:test_aos_process(Opts),
hb_cache:write(Proc1, Opts),
Expand Down Expand Up @@ -822,15 +825,9 @@ multi_process_push_test_() ->
push_with_redirect_hint_test_disabled() ->
{timeout, 30, fun() ->
dev_process:init(),
Stores =
[
#{
<<"store-module">> => hb_store_fs,
<<"name">> => <<"cache-TEST">>
}
],
ExtOpts = #{ priv_wallet => ar_wallet:new(), store => Stores },
LocalOpts = #{ priv_wallet => hb:wallet(), store => Stores },
StoreOpts = [hb_test_utils:test_store(hb_store_fs)],
ExtOpts = #{ priv_wallet => ar_wallet:new(), store => StoreOpts },
LocalOpts = #{ priv_wallet => hb:wallet(), store => StoreOpts },
ExtScheduler = hb_http_server:start_node(ExtOpts),
?event(push, {external_scheduler, {location, ExtScheduler}}),
% Create the Pong server and client
Expand Down Expand Up @@ -944,21 +941,22 @@ push_prompts_encoding_change() ->
oracle_push_test_() -> {timeout, 30, fun oracle_push/0}.
oracle_push() ->
dev_process:init(),
Client = dev_process:test_aos_process(),
{ok, _} = hb_cache:write(Client, #{}),
{ok, _} = dev_process:schedule_aos_call(Client, oracle_script()),
Opts = #{ priv_wallet => hb:wallet(), store => [hb_test_utils:test_store()] },
Client = dev_process:test_aos_process(Opts),
{ok, _} = hb_cache:write(Client, Opts),
{ok, _} = dev_process:schedule_aos_call(Client, oracle_script(), Opts),
Res =
#{
<<"path">> => <<"push">>,
<<"slot">> => 0
},
{ok, PushResult} = hb_ao:resolve(Client, Res, #{ priv_wallet => hb:wallet() }),
{ok, PushResult} = hb_ao:resolve(Client, Res, Opts),
?event({result, PushResult}),
ComputeRes =
hb_ao:resolve(
Client,
<<"now/results/data">>,
#{ priv_wallet => hb:wallet() }
Opts
),
?event({compute_res, ComputeRes}),
?assertMatch({ok, _}, ComputeRes).
Expand All @@ -974,7 +972,17 @@ nested_push_prompts_encoding_change() ->
Opts = #{
priv_wallet => hb:wallet(),
cache_control => <<"always">>,
store => hb_opts:get(store)
store => [
#{ <<"store-module">> => hb_store_lmdb, <<"name">> => <<"cache-TEST/lmdb">> },
% Include a gateway store so that we can get the legacynet
% process when needed.
#{ <<"store-module">> => hb_store_gateway,
<<"store">> => #{
<<"store-module">> => hb_store_lmdb,
<<"name">> => <<"cache-TEST/lmdb">>
}
}
]
},
?event(push_debug, {opts, Opts}),
Base = dev_process:test_aos_process(Opts),
Expand All @@ -990,7 +998,7 @@ nested_push_prompts_encoding_change() ->
?event({test_setup, {base, Base}, {sched_init, SchedInit}}),
Script = message_to_legacynet_scheduler_script(),
?event({script, Script}),
{ok, Req} = dev_process:schedule_aos_call(Base, Script),
{ok, Req} = dev_process:schedule_aos_call(Base, Script, Opts),
?event(push, {msg_sched_result, Req}),
{ok, StartingMsgSlot} =
hb_ao:resolve(Req, #{ <<"path">> => <<"slot">> }, Opts),
Expand Down
78 changes: 30 additions & 48 deletions src/dev_stack.erl
Original file line number Diff line number Diff line change
Expand Up @@ -211,45 +211,29 @@ transform(Base, Key, Opts) ->
% - The prefixes for the device.
% - The prior prefixes for later restoration.
?event({activating_device, DevMsg}),
{IPrefix, OPrefix, PDevice, PIPrefix, POPrefix} =
hb_ao:get_many(
[
[<<"input-prefixes">>, Key],
[<<"output-prefixes">>, Key],
<<"device">>,
<<"previous-input-prefix">>,
<<"previous-output-prefix">>
],
{as, dev_message, Base},
[undefined, undefined, undefined, undefined, undefined],
Opts
),
dev_message:set(
Base,
#{
<<"device">> => DevMsg,
<<"device-key">> => Key,
<<"input-prefix">> =>
hb_ao:get(
[<<"input-prefixes">>, Key],
{as, dev_message, Base},
undefined,
Opts
),
<<"output-prefix">> =>
hb_ao:get(
[<<"output-prefixes">>, Key],
{as, dev_message, Base},
undefined,
Opts
),
<<"previous-device">> =>
hb_ao:get(
<<"device">>,
{as, dev_message, Base},
Opts
),
<<"previous-input-prefix">> =>
hb_ao:get(
<<"input-prefix">>,
{as, dev_message, Base},
undefined,
Opts
),
<<"previous-output-prefix">> =>
hb_ao:get(
<<"output-prefix">>,
{as, dev_message, Base},
undefined,
Opts
)
<<"input-prefix">> => IPrefix,
<<"output-prefix">> => OPrefix,
<<"previous-device">> => PDevice,
<<"previous-input-prefix">> => PIPrefix,
<<"previous-output-prefix">> => POPrefix
},
Opts
);
Expand All @@ -270,24 +254,22 @@ resolve_fold(Base, Request, Opts) ->
{ok, Raw} when not is_map(Raw) ->
{ok, Raw};
{ok, Result} ->
{InputPrefix, OutputPrefix} =
hb_ao:get_many(
[
<<"previous-input-prefix">>,
<<"previous-output-prefix">>
],
{as, dev_message, Result},
[undefined, undefined],
Opts
),
dev_message:set(
Result,
#{
<<"device">> => InitDevMsg,
<<"input-prefix">> =>
hb_ao:get(
<<"previous-input-prefix">>,
{as, dev_message, Result},
undefined,
Opts
),
<<"output-prefix">> =>
hb_ao:get(
<<"previous-output-prefix">>,
{as, dev_message, Result},
undefined,
Opts
),
<<"input-prefix">> => InputPrefix,
<<"output-prefix">> => OutputPrefix,
<<"device-key">> => unset,
<<"device-stack-previous">> => unset,
<<"pass">> => StartingPassValue
Expand Down
24 changes: 23 additions & 1 deletion src/hb_ao.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
-export([force_message/2]).
%%% Shortcuts and tools:
-export([keys/1, keys/2, keys/3]).
-export([get/2, get/3, get/4, get_first/2, get_first/3]).
-export([get/2, get/3, get/4, get_first/2, get_first/3, get_many/4]).
-export([set/3, set/4, remove/2, remove/3]).
%%% Exports for tests in hb_ao_test_vectors.erl:
-export([deep_set/4]).
Expand Down Expand Up @@ -936,6 +936,28 @@ get_first([{Base, Path}|Msgs], Default, Opts) ->
Value -> Value
end.

%% @doc Get the values of multiple paths from a message, only calling set once.
%% Takes a list of paths, a message, a list of defaults, and options.
%% Returns a tuple of values in the same order.
%% This is more efficient than calling get/4 multiple times when working with
%% {as, Device, Msg} tuples because set is only called once.
get_many(Paths, {as, Device, Msg}, Defaults, Opts) ->
ModifiedMsg = set(
Msg,
#{ <<"device">> => Device },
internal_opts(Opts)
),
get_many(Paths, ModifiedMsg, Defaults, Opts);
get_many(Paths, Msg, Defaults, Opts) ->
list_to_tuple(
lists:zipwith(
fun(Path, Default) ->
get(Path, Msg, Default, Opts)
end,
Paths,
Defaults
)
).
%% @doc Shortcut to get the list of keys from a message.
keys(Msg) -> keys(Msg, #{}).
keys(Msg, Opts) -> keys(Msg, Opts, keep).
Expand Down