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

Commit 0c87807

Browse files
committed
More cross verification
1 parent 797e5cd commit 0c87807

3 files changed

Lines changed: 99 additions & 38 deletions

File tree

src/blockchain_hex.erl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-module(blockchain_hex).
22

3-
-export([var_map/1, scale/4, destroy_memoization/0]).
3+
-export([var_map/1,
4+
scale/2, scale/4,
5+
destroy_memoization/0]).
46

57
-ifdef(TEST).
68
-export([densities/3]).
@@ -31,6 +33,21 @@ destroy_memoization() ->
3133
try ets:delete(?SCALE_MEMO_TBL) catch _:_ -> true end,
3234
try ets:delete(?DENSITY_MEMO_TBL) catch _:_ -> true end.
3335

36+
%% @doc This call is for blockchain_etl to use directly
37+
-spec scale(Location :: h3:h3_index(),
38+
Ledger :: blockchain_ledger_v1:ledger()) -> {error, any()} | {ok, float()}.
39+
scale(Location, Ledger) ->
40+
case var_map(Ledger) of
41+
{error, _}=E -> E;
42+
{ok, VarMap} ->
43+
case get_target_res(Ledger) of
44+
{error, _}=E -> E;
45+
{ok, TargetRes} ->
46+
S = scale(Location, VarMap, TargetRes, Ledger),
47+
{ok, S}
48+
end
49+
end.
50+
3451
-spec scale(
3552
Location :: h3:h3_index(),
3653
VarMap :: var_map(),
@@ -347,3 +364,11 @@ get_density_var(Var, Ledger) ->
347364
],
348365
{ok, [N, Tgt, Max]}
349366
end.
367+
368+
-spec get_target_res(Ledger :: blockchain_ledger_v1:ledger()) -> {error, any()} | {ok, non_neg_integer()}.
369+
get_target_res(Ledger) ->
370+
case blockchain:config(?density_tgt_res, Ledger) of
371+
{error, _}=E -> E;
372+
{ok, V} -> V
373+
end.
374+

src/transactions/v1/blockchain_txn_rewards_v1.erl

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,27 @@ get_rewards_for_epoch(Current, End, Chain, Vars, Ledger, ChallengerRewards, Chal
269269
dc_rewards(Transactions, End, Vars,
270270
Ledger, DCRewards));
271271
{ok, VarMap} ->
272+
WR = poc_witnesses_rewards(Transactions, Vars,
273+
Chain, Ledger,
274+
WitnessRewards, VarMap),
275+
276+
C0R = poc_challengers_rewards(Transactions, Vars,
277+
ChallengerRewards),
278+
279+
DCR = dc_rewards(Transactions, End, Vars,
280+
Ledger, DCRewards),
281+
282+
CR = poc_challengees_rewards(Transactions, Vars,
283+
Chain, Ledger,
284+
ChallengeeRewards,
285+
VarMap),
286+
272287
%% do the new thing
273288
get_rewards_for_epoch(Current+1, End, Chain, Vars, Ledger,
274-
poc_challengers_rewards(Transactions, Vars,
275-
ChallengerRewards),
276-
poc_challengees_rewards(Transactions, Vars,
277-
Chain, Ledger,
278-
ChallengeeRewards,
279-
VarMap),
280-
poc_witnesses_rewards(Transactions, Vars,
281-
Chain, Ledger,
282-
WitnessRewards, VarMap),
283-
dc_rewards(Transactions, End, Vars,
284-
Ledger, DCRewards))
289+
C0R,
290+
CR,
291+
WR,
292+
DCR)
285293
end
286294
end
287295
end.
@@ -619,7 +627,9 @@ poc_challengees_rewards_(#{poc_version := Version}=Vars,
619627
ChallengeeLoc,
620628
VarMap,
621629
Ledger),
622-
maps:put(Challengee, I+(ToAdd * TxScale), Acc0)
630+
X = maps:put(Challengee, I+(ToAdd * TxScale), Acc0),
631+
lager:info("MARKER1 +++++++~p", [print_intermediate_reward_map(X)]),
632+
X
623633
end;
624634
false when is_integer(Version), Version > 4 ->
625635
%% this challengee rx'd and sent a receipt
@@ -633,7 +643,9 @@ poc_challengees_rewards_(#{poc_version := Version}=Vars,
633643
ChallengeeLoc,
634644
VarMap,
635645
Ledger),
636-
maps:put(Challengee, I+(ToAdd * TxScale), Acc0)
646+
X = maps:put(Challengee, I+(ToAdd * TxScale), Acc0),
647+
lager:info("MARKER2 +++++++~p", [print_intermediate_reward_map(X)]),
648+
X
637649
end;
638650
_ ->
639651
case poc_challengee_reward_unit(WitnessRedundancy, DecayRate, Witnesses) of
@@ -646,7 +658,9 @@ poc_challengees_rewards_(#{poc_version := Version}=Vars,
646658
ChallengeeLoc,
647659
VarMap,
648660
Ledger),
649-
maps:put(Challengee, I+(ToAdd * TxScale), Acc0)
661+
X = maps:put(Challengee, I+(ToAdd * TxScale), Acc0),
662+
lager:info("MARKER3 +++++++~p", [print_intermediate_reward_map(X)]),
663+
X
650664
end
651665
end,
652666
poc_challengees_rewards_(Vars, Path, StaticPath, Txn, Chain, Ledger, false, VarMap, Acc1);
@@ -672,7 +686,9 @@ poc_challengees_rewards_(#{poc_version := Version}=Vars,
672686
ChallengeeLoc,
673687
VarMap,
674688
Ledger),
675-
maps:put(Challengee, I+(ToAdd * TxScale), Acc0)
689+
X = maps:put(Challengee, I+(ToAdd * TxScale), Acc0),
690+
lager:info("MARKER4 +++++++~p", [print_intermediate_reward_map(X)]),
691+
X
676692
end;
677693
true ->
678694
case poc_challengee_reward_unit(WitnessRedundancy, DecayRate, Witnesses) of
@@ -685,7 +701,9 @@ poc_challengees_rewards_(#{poc_version := Version}=Vars,
685701
ChallengeeLoc,
686702
VarMap,
687703
Ledger),
688-
maps:put(Challengee, I+(ToAdd * TxScale), Acc0)
704+
X = maps:put(Challengee, I+(ToAdd * TxScale), Acc0),
705+
lager:info("MARKER5 +++++++~p", [print_intermediate_reward_map(X)]),
706+
X
689707
end
690708
end,
691709
poc_challengees_rewards_(Vars, Path, StaticPath, Txn, Chain, Ledger, false, VarMap, Acc1)
@@ -750,7 +768,7 @@ poc_witnesses_rewards(Transactions,
750768
Path = blockchain_txn_poc_receipts_v1:path(Txn),
751769

752770
%% Do the new thing for witness filtering
753-
lists:foldl(
771+
Res = lists:foldl(
754772
fun(Elem, Acc1) ->
755773
ElemPos = blockchain_utils:index_of(Elem, Path),
756774
WitnessChannel = lists:nth(ElemPos, Channels),
@@ -777,37 +795,38 @@ poc_witnesses_rewards(Transactions,
777795
undefined ->
778796
%% old (HIP15)
779797
lists:foldl(
780-
fun(WitnessRecord, Map) ->
798+
fun(WitnessRecord, Acc2) ->
781799
Witness = blockchain_poc_witness_v1:gateway(WitnessRecord),
782-
I = maps:get(Witness, Map, 0),
783-
maps:put(Witness, I+ToAdd, Map)
800+
I = maps:get(Witness, Acc2, 0),
801+
maps:put(Witness, I+ToAdd, Acc2)
784802
end,
785803
Acc1,
786804
ValidWitnesses
787805
);
788806
D ->
789807
%% new (HIP17)
790808
lists:foldl(
791-
fun(WitnessRecord, Map) ->
809+
fun(WitnessRecord, Acc2) ->
792810
Challengee = blockchain_poc_path_element_v1:challengee(Elem),
793811
case blockchain_ledger_v1:find_gateway_info(Challengee, Ledger) of
794812
{ok, ChallengeeGw} ->
795813
case blockchain_ledger_gateway_v2:location(ChallengeeGw) of
796814
undefined ->
797-
Map;
815+
Acc2;
798816
ChallengeeLoc ->
799817
Witness = blockchain_poc_witness_v1:gateway(WitnessRecord),
800818
%% The witnesses get scaled by the value of their transmitters
801819
RxScale = blockchain_hex:scale(ChallengeeLoc,
802820
VarMap,
803821
D,
804822
Ledger),
805-
lager:info("WitnessGw: ~p, RxScale: ~p", [blockchain_utils:addr2name(Witness), RxScale]),
806-
I = maps:get(Witness, Map, 0),
807-
maps:put(Witness, I+(ToAdd*RxScale), Map)
823+
lager:info("WitnessGw: ~p, RxScale: ~p",
824+
[blockchain_utils:addr2name(Witness), RxScale]),
825+
I = maps:get(Witness, Acc2, 0),
826+
maps:put(Witness, I+(ToAdd*RxScale), Acc2)
808827
end;
809828
_ ->
810-
Map
829+
Acc2
811830
end
812831
end,
813832
Acc1,
@@ -818,7 +837,9 @@ poc_witnesses_rewards(Transactions,
818837
end,
819838
Acc0,
820839
Path
821-
)
840+
),
841+
lager:info("Reward Result: ~p", [print_intermediate_reward_map(Res)]),
842+
Res
822843
catch What:Why:ST ->
823844
lager:error("failed to calculate poc_witnesses_rewards, error ~p:~p:~p", [What, Why, ST]),
824845
Acc0
@@ -1104,6 +1125,9 @@ share_of_dc_rewards(_Key, #{dc_remainder := 0}) ->
11041125
share_of_dc_rewards(Key, Vars=#{dc_remainder := DCRemainder}) ->
11051126
erlang:round(DCRemainder * ((maps:get(Key, Vars) / (maps:get(poc_challengers_percent, Vars) + maps:get(poc_challengees_percent, Vars) + maps:get(poc_witnesses_percent, Vars))))).
11061127

1128+
print_intermediate_reward_map(Map) ->
1129+
maps:fold(fun(K, Val, Acc) -> maps:put(blockchain_utils:addr2name(K), Val, Acc) end, #{}, Map).
1130+
11071131
%% ------------------------------------------------------------------
11081132
%% EUNIT Tests
11091133
%% ------------------------------------------------------------------

test/blockchain_reward_hip17_SUITE.erl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ run_test(Witnesses, Config) ->
452452
hip15_vars() ->
453453
#{
454454
%% configured on chain
455-
?poc_version => 9,
455+
?poc_version => 10,
456456
?reward_version => 5,
457457
%% new hip15 vars for testing
458458
?poc_reward_decay_rate => 0.8,
@@ -522,14 +522,26 @@ known_locations() ->
522522
%% 631786581944091647],
523523

524524

525+
%% [631211351866199551,
526+
%% 631211351866199551,
527+
%% 631211351866084351,
528+
%% 631211351866223615,
529+
%% 631211351866300415,
530+
%% 631211351866239999,
531+
%% 631211351866165759,
532+
%% 631211351866165247,
533+
%% 631211351866289663,
534+
%% 631211351865407487,
535+
%% 631211351865991679],
536+
525537
[631211351866199551,
526538
631211351866199551,
527-
631211351866084351,
528-
631211351866223615,
529-
631211351866300415,
530-
631211351866239999,
531-
631211351866165759,
532-
631211351866165247,
533-
631211351866289663,
534-
631211351865407487,
535-
631211351865991679].
539+
631211351866199551,
540+
631211351866199551,
541+
631211351866199551,
542+
631211351866199551,
543+
631211351866199551,
544+
631211351866199551,
545+
631211351866199551,
546+
631211351866199551,
547+
631211351866199551].

0 commit comments

Comments
 (0)