Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Add more cross checking for rewards
Browse files Browse the repository at this point in the history
- Fix log msg
- Add another crosscheck
  • Loading branch information
vihu committed Dec 7, 2020
1 parent 46d7b90 commit 797e5cd
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/transactions/v1/blockchain_txn_rewards_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1078,8 +1078,8 @@ maybe_calc_tx_scale(Challengee,
{_, undefined} -> 1.0;
{D, Loc} ->
TxScale = blockchain_hex:scale(Loc, VarMap, D, Ledger),
lager:info("Challengee: ~p, RxScale: ~p", [blockchain_utils:addr2name(Challengee),
TxScale]),
lager:info("Challengee: ~p, TxScale: ~p",
[blockchain_utils:addr2name(Challengee), TxScale]),
TxScale
end.

Expand Down
72 changes: 71 additions & 1 deletion test/blockchain_reward_hip17_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ init_per_testcase(TestCase, Config) ->
#{}
end,


{ok, GenesisMembers, _GenesisBlock, ConsensusMembers, Keys} =
test_utils:init_chain(Balance, {PrivKey, PubKey}, true, ExtraVars),
test_utils:init_chain_with_fixed_locations(Balance, {PrivKey, PubKey}, true, known_locations(), ExtraVars),

Chain = blockchain_worker:blockchain(),
Swarm = blockchain_swarm:swarm(),
Expand Down Expand Up @@ -264,6 +265,17 @@ run_test(Witnesses, Config) ->
lists:zip(AllGws, GatewayAddrs)
),

%% For crosscheck
GatewayLetterLocMap = lists:foldl(
fun({Letter, A}, Acc) ->
{ok, Gw} = blockchain_ledger_v1:find_gateway_info(A, Ledger),
GwLoc = blockchain_ledger_gateway_v2:location(Gw),
maps:put(Letter, GwLoc, Acc)
end,
#{},
lists:zip(AllGws, GatewayAddrs)
),

Challenger = maps:get(k, GatewayLetterToAddrMap),

GwA = maps:get(a, GatewayLetterToAddrMap),
Expand Down Expand Up @@ -422,6 +434,7 @@ run_test(Witnesses, Config) ->
%% Theoretically, gateways J, K should have higher witness rewards than B, C, E, F, G, I
ct:pal("GatewayNameMap: ~p", [GatewayNameMap]),
ct:pal("GatewayLocMap: ~p", [GatewayLocMap]),
ct:pal("GatewayLetterLocMap: ~p", [GatewayLetterLocMap]),
ct:pal("ChallengeesRewardsMap: ~p", [ChallengeesRewardsMap]),
ct:pal("WitnessRewardsMap: ~p", [WitnessRewardsMap]),

Expand Down Expand Up @@ -463,3 +476,60 @@ hip17_vars() ->
?hip17_res_12 => <<"2,100000,100000">>,
?density_tgt_res => 8
}.

known_locations() ->

%% NameToPubkeyBin =
%% #{
%% "rare-amethyst-reindeer" => libp2p_crypto:b58_to_bin("112VfPXs1WTRjp24WkbbjQmbFNhb5ptot7gpGwJULm4SRiacjuvW"),
%% "cold-canvas-duck" => libp2p_crypto:b58_to_bin("112ciDxDUBwJZs5YjjPWJWKGwGtUtdJdxSgDAYJPhu9fHw4sgeQy"),
%% "melted-tangelo-aphid" => libp2p_crypto:b58_to_bin("112eNuzPYSeeo3tqNDidr2gPysz7QtLkePkY5Yn1V7ddNPUDN6p5"),
%% "early-lime-rat" => libp2p_crypto:b58_to_bin("112Xr4ZtiNbeh8wfiWTYfeo7KwBbXwvx5F2LPdNTC8wp8q4EQCAm"),
%% "flat-lilac-shrimp" => libp2p_crypto:b58_to_bin("112euXBKmLzUAfyi7FaYRxRpcH5RmfPKprV3qEyHCTt8nqwyVFYo"),
%% "harsh-sandstone-stork" => libp2p_crypto:b58_to_bin("11UFysjjP9W8S7ZV54iK7L6HpkxkHrSPRm4rKkWq22cStYYhDhM"),
%% "pet-pewter-lobster" => libp2p_crypto:b58_to_bin("112LYrRkJX32jVNsuAzt9kDqrddXqWrwpG8N5QX2hELvzf8JJZbw"),
%% "amateur-tan-monkey" => libp2p_crypto:b58_to_bin("112bQKSN3TiaYMrsjNKGZotd14QPi7DB37FeV88rmVMgP4MgTK9q"),
%% "clean-wooden-zebra" => libp2p_crypto:b58_to_bin("112AT5baYcYG6yHchYa9xnkqNJ4cXbgxCh8i8nvnfZeewAHJ8zKc"),
%% "odd-champagne-nuthatch" => libp2p_crypto:b58_to_bin("112fDV4b5FqSgcnu3F592RauuNo5HkuPzfETM7WJ9AfCdaCo9sLk"),
%% "abundant-grape-butterfly" => libp2p_crypto:b58_to_bin("112pdh3waHFbu3XqtCWwbw9xEtYtUEvbqzgSVbEoENBRQznj9Tuy")
%% },

%% Locs =
%% #{
%% "rare-amethyst-reindeer" => 631786582666296319,
%% "cold-canvas-duck" => 631786582410363903,
%% "melted-tangelo-aphid" => 631786582655116287,
%% "early-lime-rat" => 631786582659491327,
%% "flat-lilac-shrimp" => 631786581941828607,
%% "harsh-sandstone-stork" => 631786581946850303,
%% "pet-pewter-lobster" => 631786581906280959,
%% "amateur-tan-monkey" => 631786581937244159,
%% "clean-wooden-zebra" => 631786581846989823,
%% "odd-champagne-nuthatch" => 631786581944091647,
%% "abundant-grape-butterfly" => 631786582694056959
%% },

%% [631786581906280959,
%% 631786582666296319,
%% 631786582410363903,
%% 631786582694056959,
%% 631786582655116287,
%% 631786582659491327,
%% 631786581941828607,
%% 631786581937244159,
%% 631786581946850303,
%% 631786581846989823,
%% 631786581944091647],


[631211351866199551,
631211351866199551,
631211351866084351,
631211351866223615,
631211351866300415,
631211351866239999,
631211351866165759,
631211351866165247,
631211351866289663,
631211351865407487,
631211351865991679].
70 changes: 58 additions & 12 deletions test/test_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-export([
init/1, init/2,
init_chain/2, init_chain/3, init_chain/4,
init_chain_with_fixed_locations/5,
generate_keys/1, generate_keys/2,
wait_until/1, wait_until/3,
create_block/2, create_block/3, create_block/4,
Expand Down Expand Up @@ -41,20 +42,23 @@ init_chain(Balance, Keys) ->
init_chain(Balance, Keys, true, #{}).


init_chain(Balance, {PrivKey, PubKey}, InConsensus, ExtraVars) ->
% Generate fake blockchains (just the keys)
GenesisMembers = case InConsensus of
true ->
RandomKeys = test_utils:generate_keys(10),
Address = blockchain_swarm:pubkey_bin(),
[
{Address, {PubKey, PrivKey, libp2p_crypto:mk_sig_fun(PrivKey)}}
] ++ RandomKeys;
false ->
test_utils:generate_keys(11)
end,
init_chain(Balance, {_PrivKey, _PubKey}=Keys, InConsensus, ExtraVars) ->
GenesisMembers = init_genesis_members(Keys, InConsensus),
init_chain(Balance, GenesisMembers, ExtraVars).

init_genesis_members({PrivKey, PubKey}, InConsensus) ->
% Generate fake blockchains (just the keys)
case InConsensus of
true ->
RandomKeys = test_utils:generate_keys(10),
Address = blockchain_swarm:pubkey_bin(),
[
{Address, {PubKey, PrivKey, libp2p_crypto:mk_sig_fun(PrivKey)}}
] ++ RandomKeys;
false ->
test_utils:generate_keys(11)
end.

init_chain(Balance, Keys, InConsensus) when is_tuple(Keys), is_boolean(InConsensus) ->
init_chain(Balance, Keys, InConsensus, #{});
init_chain(Balance, GenesisMembers, ExtraVars) when is_list(GenesisMembers), is_map(ExtraVars) ->
Expand Down Expand Up @@ -103,6 +107,48 @@ init_chain(Balance, GenesisMembers, ExtraVars) when is_list(GenesisMembers), is_
?assertEqual({ok, 1}, blockchain:height(Chain)),
{ok, GenesisMembers, GenesisBlock, ConsensusMembers, Keys}.

init_chain_with_fixed_locations(Balance, {PrivKey, PubKey}, InConsensus, Locations, ExtraVars) when is_list(Locations), is_map(ExtraVars) ->

GenesisMembers = init_genesis_members({PrivKey, PubKey}, InConsensus),

% Create genesis block
{InitialVars, Keys} = blockchain_ct_utils:create_vars(ExtraVars),

GenPaymentTxs = [blockchain_txn_coinbase_v1:new(Addr, Balance)
|| {Addr, _} <- GenesisMembers],

GenSecPaymentTxs = [blockchain_txn_security_coinbase_v1:new(Addr, Balance)
|| {Addr, _} <- GenesisMembers],

Addresses = [Addr || {Addr, _} <- GenesisMembers],

InitialGatewayTxn = [blockchain_txn_gen_gateway_v1:new(Addr, Addr, Loc, 0)
|| {Addr, Loc} <- lists:zip(Addresses, Locations)],

ConsensusMembers = lists:sublist(GenesisMembers, 7),
GenConsensusGroupTx = blockchain_txn_consensus_group_v1:new(
[Addr || {Addr, _} <- ConsensusMembers], <<"proof">>, 1, 0),
Txs = InitialVars ++
GenPaymentTxs ++
GenSecPaymentTxs ++
InitialGatewayTxn ++
[GenConsensusGroupTx],
lager:info("initial transactions: ~p", [Txs]),

GenesisBlock = blockchain_block:new_genesis_block(Txs),
ok = blockchain_worker:integrate_genesis_block(GenesisBlock),

Chain = blockchain_worker:blockchain(),
{ok, HeadBlock} = blockchain:head_block(Chain),
ok = test_utils:wait_until(fun() ->{ok, 1} =:= blockchain:height(Chain) end),

?assertEqual(blockchain_block:hash_block(GenesisBlock), blockchain_block:hash_block(HeadBlock)),
?assertEqual({ok, GenesisBlock}, blockchain:head_block(Chain)),
?assertEqual({ok, blockchain_block:hash_block(GenesisBlock)}, blockchain:genesis_hash(Chain)),
?assertEqual({ok, GenesisBlock}, blockchain:genesis_block(Chain)),
?assertEqual({ok, 1}, blockchain:height(Chain)),
{ok, GenesisMembers, GenesisBlock, ConsensusMembers, Keys}.

generate_keys(N) ->
generate_keys(N, ecc_compact).

Expand Down

0 comments on commit 797e5cd

Please sign in to comment.