Skip to content

Commit

Permalink
test: ensure we always reset the config after changing it
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPiechota committed Dec 26, 2024
1 parent 0b9b69b commit 54a9bfc
Show file tree
Hide file tree
Showing 11 changed files with 973 additions and 933 deletions.
91 changes: 48 additions & 43 deletions apps/arweave/src/ar_chunk_storage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1537,49 +1537,54 @@ test_replica_2_9() ->
{?PARTITION_SIZE, 0, {replica_2_9, RewardAddr}},
{?PARTITION_SIZE, 1, {replica_2_9, RewardAddr}}
],
ar_test_node:start(#{ reward_addr => RewardAddr, storage_modules => StorageModules }),
StoreID1 = ar_storage_module:id(lists:nth(1, StorageModules)),
StoreID2 = ar_storage_module:id(lists:nth(2, StorageModules)),
C1 = crypto:strong_rand_bytes(?DATA_CHUNK_SIZE),
%% The replica_2_9 storage does not support updates and three chunks are written
%% into the first partition when the test node is launched.
?assertEqual({error, already_stored},
ar_chunk_storage:put(?DATA_CHUNK_SIZE, C1, StoreID1)),
?assertEqual({error, already_stored},
ar_chunk_storage:put(2 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
?assertEqual({error, already_stored},
ar_chunk_storage:put(3 * ?DATA_CHUNK_SIZE, C1, StoreID1)),

%% Store the new chunk.
?assertEqual(ok, ar_chunk_storage:put(4 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
{ok, P1, _Entropy} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 4 * ?DATA_CHUNK_SIZE, C1),
assert_get(P1, 4 * ?DATA_CHUNK_SIZE, StoreID1),

assert_get(not_found, 8 * ?DATA_CHUNK_SIZE, StoreID1),
?assertEqual(ok, ar_chunk_storage:put(8 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
{ok, P2, _} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 8 * ?DATA_CHUNK_SIZE, C1),
assert_get(P2, 8 * ?DATA_CHUNK_SIZE, StoreID1),

%% Store chunks in the second partition.
?assertEqual(ok, ar_chunk_storage:put(12 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P3, Entropy3} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 12 * ?DATA_CHUNK_SIZE, C1),

assert_get(P3, 12 * ?DATA_CHUNK_SIZE, StoreID2),
?assertEqual(ok, ar_chunk_storage:put(15 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P4, Entropy4} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 15 * ?DATA_CHUNK_SIZE, C1),
assert_get(P4, 15 * ?DATA_CHUNK_SIZE, StoreID2),
?assertNotEqual(P3, P4),
?assertNotEqual(Entropy3, Entropy4),

?assertEqual(ok, ar_chunk_storage:put(16 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P5, Entropy5} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 16 * ?DATA_CHUNK_SIZE, C1),
assert_get(P5, 16 * ?DATA_CHUNK_SIZE, StoreID2),
?assertNotEqual(Entropy4, Entropy5).
{ok, Config} = application:get_env(arweave, config),
try
ar_test_node:start(#{ reward_addr => RewardAddr, storage_modules => StorageModules }),
StoreID1 = ar_storage_module:id(lists:nth(1, StorageModules)),
StoreID2 = ar_storage_module:id(lists:nth(2, StorageModules)),
C1 = crypto:strong_rand_bytes(?DATA_CHUNK_SIZE),
%% The replica_2_9 storage does not support updates and three chunks are written
%% into the first partition when the test node is launched.
?assertEqual({error, already_stored},
ar_chunk_storage:put(?DATA_CHUNK_SIZE, C1, StoreID1)),
?assertEqual({error, already_stored},
ar_chunk_storage:put(2 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
?assertEqual({error, already_stored},
ar_chunk_storage:put(3 * ?DATA_CHUNK_SIZE, C1, StoreID1)),

%% Store the new chunk.
?assertEqual(ok, ar_chunk_storage:put(4 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
{ok, P1, _Entropy} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 4 * ?DATA_CHUNK_SIZE, C1),
assert_get(P1, 4 * ?DATA_CHUNK_SIZE, StoreID1),

assert_get(not_found, 8 * ?DATA_CHUNK_SIZE, StoreID1),
?assertEqual(ok, ar_chunk_storage:put(8 * ?DATA_CHUNK_SIZE, C1, StoreID1)),
{ok, P2, _} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 8 * ?DATA_CHUNK_SIZE, C1),
assert_get(P2, 8 * ?DATA_CHUNK_SIZE, StoreID1),

%% Store chunks in the second partition.
?assertEqual(ok, ar_chunk_storage:put(12 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P3, Entropy3} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 12 * ?DATA_CHUNK_SIZE, C1),

assert_get(P3, 12 * ?DATA_CHUNK_SIZE, StoreID2),
?assertEqual(ok, ar_chunk_storage:put(15 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P4, Entropy4} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 15 * ?DATA_CHUNK_SIZE, C1),
assert_get(P4, 15 * ?DATA_CHUNK_SIZE, StoreID2),
?assertNotEqual(P3, P4),
?assertNotEqual(Entropy3, Entropy4),

?assertEqual(ok, ar_chunk_storage:put(16 * ?DATA_CHUNK_SIZE, C1, StoreID2)),
{ok, P5, Entropy5} =
ar_packing_server:pack_replica_2_9_chunk(RewardAddr, 16 * ?DATA_CHUNK_SIZE, C1),
assert_get(P5, 16 * ?DATA_CHUNK_SIZE, StoreID2),
?assertNotEqual(Entropy4, Entropy5)
after
ok = application:set_env(arweave, config, Config)
end.

well_aligned_test_() ->
{timeout, 20, fun test_well_aligned/0}.
Expand Down
1 change: 0 additions & 1 deletion apps/arweave/src/ar_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ get_block_index() ->
%% initialized the state.
get_current_block() ->
[{_, Current}] = ets:lookup(node_state, current),
?LOG_ERROR([{event, get_current_block}, {current, ar_util:encode(Current)}]),
ar_block_cache:get(block_cache, Current).

%% @doc Return the current network difficulty. Assume the node has joined the network and
Expand Down
40 changes: 22 additions & 18 deletions apps/arweave/test/ar_coordinated_mining_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,28 @@ test_bad_secret() ->
[Node, _ExitNode, _ValidatorNode] = ar_test_node:start_coordinated(1),
Peer = ar_test_node:peer_ip(Node),
{ok, Config} = application:get_env(arweave, config),
ok = application:set_env(arweave, config,
Config#config{ cm_api_secret = <<"this_is_not_the_actual_secret">> }),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:get_cm_partition_table(Peer)),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_h1_send(Peer, dummy_candidate())),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_h2_send(Peer, dummy_candidate())),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_publish_send(Peer, dummy_solution())).
try
ok = application:set_env(arweave, config,
Config#config{ cm_api_secret = <<"this_is_not_the_actual_secret">> }),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:get_cm_partition_table(Peer)),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_h1_send(Peer, dummy_candidate())),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_h2_send(Peer, dummy_candidate())),
?assertMatch(
{error, {ok, {{<<"421">>, _}, _,
<<"CM API disabled or invalid CM API secret in request.">>, _, _}}},
ar_http_iface_client:cm_publish_send(Peer, dummy_solution()))
after
ok = application:set_env(arweave, config, Config)
end.

test_partition_table() ->
[B0] = ar_weave:init([], ar_test_node:get_difficulty_for_invalid_hash(), 5 * ?PARTITION_SIZE),
Expand Down
190 changes: 97 additions & 93 deletions apps/arweave/test/ar_http_iface_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -781,99 +781,103 @@ test_post_unsigned_tx({_B0, Wallet1, _Wallet2, _StaticWallet}) ->
path => "/wallet"
}),
{ok, Config} = application:get_env(arweave, config),
application:set_env(arweave, config,
Config#config{ internal_api_secret = <<"correct_secret">> }),
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/wallet",
headers => [{<<"X-Internal-Api-Secret">>, <<"incorrect_secret">>}]
}),
{ok, {{<<"200">>, <<"OK">>}, _, CreateWalletBody, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/wallet",
headers => [{<<"X-Internal-Api-Secret">>, <<"correct_secret">>}]
}),
application:set_env(arweave, config, Config#config{ internal_api_secret = not_set }),
{CreateWalletRes} = ar_serialize:dejsonify(CreateWalletBody),
[WalletAccessCode] = proplists:get_all_values(<<"wallet_access_code">>, CreateWalletRes),
[Address] = proplists:get_all_values(<<"wallet_address">>, CreateWalletRes),
%% Top up the new wallet.
TopUpTX = ar_test_node:sign_tx(Wallet, #{
owner => Pub,
target => ar_util:decode(Address),
quantity => ?AR(100),
reward => ?AR(1)
}),
{ok, {{<<"200">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/tx",
body => ar_serialize:jsonify(ar_serialize:tx_to_json_struct(TopUpTX))
}),
wait_until_receives_txs([TopUpTX]),
ar_test_node:mine(),
wait_until_height(main, LocalHeight + 1),
%% Send an unsigned transaction to be signed with the generated key.
TX = (ar_tx:new())#tx{reward = ?AR(1), last_tx = TopUpTX#tx.id},
UnsignedTXProps = [
{<<"last_tx">>, <<>>},
{<<"target">>, TX#tx.target},
{<<"quantity">>, integer_to_binary(TX#tx.quantity)},
{<<"data">>, TX#tx.data},
{<<"reward">>, integer_to_binary(TX#tx.reward)},
{<<"denomination">>, integer_to_binary(TopUpTX#tx.denomination)},
{<<"wallet_access_code">>, WalletAccessCode}
],
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
body => ar_serialize:jsonify({UnsignedTXProps})
}),
application:set_env(arweave, config,
Config#config{ internal_api_secret = <<"correct_secret">> }),
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
headers => [{<<"X-Internal-Api-Secret">>, <<"incorrect_secret">>}],
body => ar_serialize:jsonify({UnsignedTXProps})
}),
{ok, {{<<"200">>, <<"OK">>}, _, Body, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
headers => [{<<"X-Internal-Api-Secret">>, <<"correct_secret">>}],
body => ar_serialize:jsonify({UnsignedTXProps})
}),
application:set_env(arweave, config, Config#config{ internal_api_secret = not_set }),
{Res} = ar_serialize:dejsonify(Body),
TXID = proplists:get_value(<<"id">>, Res),
timer:sleep(200),
ar_test_node:mine(),
wait_until_height(main, LocalHeight + 2),
timer:sleep(200),
{ok, {{<<"200">>, <<"OK">>}, _, GetTXBody, _, _}} =
ar_http:req(#{
method => get,
peer => ar_test_node:peer_ip(main),
path => "/tx/" ++ binary_to_list(TXID) ++ "/status"
}),
{GetTXRes} = ar_serialize:dejsonify(GetTXBody),
?assertMatch(
#{
<<"number_of_confirmations">> := 1
},
maps:from_list(GetTXRes)
).
try
application:set_env(arweave, config,
Config#config{ internal_api_secret = <<"correct_secret">> }),
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/wallet",
headers => [{<<"X-Internal-Api-Secret">>, <<"incorrect_secret">>}]
}),
{ok, {{<<"200">>, <<"OK">>}, _, CreateWalletBody, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/wallet",
headers => [{<<"X-Internal-Api-Secret">>, <<"correct_secret">>}]
}),
application:set_env(arweave, config, Config#config{ internal_api_secret = not_set }),
{CreateWalletRes} = ar_serialize:dejsonify(CreateWalletBody),
[WalletAccessCode] = proplists:get_all_values(<<"wallet_access_code">>, CreateWalletRes),
[Address] = proplists:get_all_values(<<"wallet_address">>, CreateWalletRes),
%% Top up the new wallet.
TopUpTX = ar_test_node:sign_tx(Wallet, #{
owner => Pub,
target => ar_util:decode(Address),
quantity => ?AR(100),
reward => ?AR(1)
}),
{ok, {{<<"200">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/tx",
body => ar_serialize:jsonify(ar_serialize:tx_to_json_struct(TopUpTX))
}),
wait_until_receives_txs([TopUpTX]),
ar_test_node:mine(),
wait_until_height(main, LocalHeight + 1),
%% Send an unsigned transaction to be signed with the generated key.
TX = (ar_tx:new())#tx{reward = ?AR(1), last_tx = TopUpTX#tx.id},
UnsignedTXProps = [
{<<"last_tx">>, <<>>},
{<<"target">>, TX#tx.target},
{<<"quantity">>, integer_to_binary(TX#tx.quantity)},
{<<"data">>, TX#tx.data},
{<<"reward">>, integer_to_binary(TX#tx.reward)},
{<<"denomination">>, integer_to_binary(TopUpTX#tx.denomination)},
{<<"wallet_access_code">>, WalletAccessCode}
],
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
body => ar_serialize:jsonify({UnsignedTXProps})
}),
application:set_env(arweave, config,
Config#config{ internal_api_secret = <<"correct_secret">> }),
{ok, {{<<"421">>, _}, _, _, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
headers => [{<<"X-Internal-Api-Secret">>, <<"incorrect_secret">>}],
body => ar_serialize:jsonify({UnsignedTXProps})
}),
{ok, {{<<"200">>, <<"OK">>}, _, Body, _, _}} =
ar_http:req(#{
method => post,
peer => ar_test_node:peer_ip(main),
path => "/unsigned_tx",
headers => [{<<"X-Internal-Api-Secret">>, <<"correct_secret">>}],
body => ar_serialize:jsonify({UnsignedTXProps})
}),
application:set_env(arweave, config, Config#config{ internal_api_secret = not_set }),
{Res} = ar_serialize:dejsonify(Body),
TXID = proplists:get_value(<<"id">>, Res),
timer:sleep(200),
ar_test_node:mine(),
wait_until_height(main, LocalHeight + 2),
timer:sleep(200),
{ok, {{<<"200">>, <<"OK">>}, _, GetTXBody, _, _}} =
ar_http:req(#{
method => get,
peer => ar_test_node:peer_ip(main),
path => "/tx/" ++ binary_to_list(TXID) ++ "/status"
}),
{GetTXRes} = ar_serialize:dejsonify(GetTXBody),
?assertMatch(
#{
<<"number_of_confirmations">> := 1
},
maps:from_list(GetTXRes)
)
after
ok = application:set_env(arweave, config, Config)
end.

%% @doc Ensure the HTTP client stops fetching data from an endpoint when its data size
%% limit is exceeded.
Expand Down
7 changes: 4 additions & 3 deletions apps/arweave/test/ar_mining_server_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ setup_all() ->
{?PARTITION_SIZE, 1, {spora_2_6, RewardAddr}},
{?PARTITION_SIZE, 2, {spora_2_6, RewardAddr}}
],
ar_test_node:start(B0, RewardAddr, Config, StorageModules).
ar_test_node:start(B0, RewardAddr, Config, StorageModules),
Config.

cleanup_all(_) ->
ok.
cleanup_all(Config) ->
ok = application:set_env(arweave, config, Config).

%% @doc Setup the environment so we can control VDF step generation.
setup_pool_client() ->
Expand Down
Loading

0 comments on commit 54a9bfc

Please sign in to comment.