From 079d3e6368bc20e4e3b1bcc8ffe4156e94705e1c Mon Sep 17 00:00:00 2001 From: Rahul Garg Date: Wed, 21 Oct 2020 22:39:50 -0700 Subject: [PATCH] Add support for HIP15 --- include/blockchain_vars.hrl | 1 + .../v1/blockchain_txn_rewards_v1.erl | 28 +++++++++++++++++-- .../v1/blockchain_txn_vars_v1.erl | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/blockchain_vars.hrl b/include/blockchain_vars.hrl index 3911cc7418..bb11d182df 100644 --- a/include/blockchain_vars.hrl +++ b/include/blockchain_vars.hrl @@ -179,6 +179,7 @@ -define(poc_witnesses_percent, poc_witnesses_percent). -define(poc_challengers_percent, poc_challengers_percent). -define(dc_percent, dc_percent). +-define(witness_redundancy, witness_redundancy). %%% %%% bundle txn vars diff --git a/src/transactions/v1/blockchain_txn_rewards_v1.erl b/src/transactions/v1/blockchain_txn_rewards_v1.erl index 8ce834dd0c..7734290436 100644 --- a/src/transactions/v1/blockchain_txn_rewards_v1.erl +++ b/src/transactions/v1/blockchain_txn_rewards_v1.erl @@ -291,6 +291,12 @@ get_reward_vars(Start, End, Ledger) -> {ok, R4} -> R4; _ -> 1 end, + + WitnessRedundancy = case blockchain:config(?witness_redundancy, Ledger) of + {ok, WR} -> WR; + _ -> 1 + end, + EpochReward = calculate_epoch_reward(Start, End, Ledger), #{ monthly_reward => MonthlyReward, @@ -300,6 +306,7 @@ get_reward_vars(Start, End, Ledger) -> poc_challengees_percent => PocChallengeesPercent, poc_challengers_percent => PocChallengersPercent, poc_witnesses_percent => PocWitnessesPercent, + witness_redundancy => WitnessRedundancy, consensus_percent => ConsensusPercent, consensus_members => ConsensusMembers, dc_percent => DCPercent, @@ -578,7 +585,7 @@ poc_challengees_rewards_(Version, [Elem|Path], StaticPath, Txn, Chain, Ledger, _ Ledger :: blockchain_ledger_v1:ledger(), WitnessRewards :: map()) -> #{{gateway, libp2p_crypto:pubkey_bin()} => non_neg_integer()}. poc_witnesses_rewards(Transactions, - #{poc_version := POCVersion}, + #{poc_version := POCVersion, witness_redundancy := WitnessRedundancy}, Chain, Ledger, WitnessRewards) -> @@ -604,11 +611,28 @@ poc_witnesses_rewards(Transactions, [] -> Acc1; ValidWitnesses -> + ToAdd = case WitnessRedundancy of + 1 -> + %% continue with the old thing + 1; + N -> + L = length(ValidWitnesses), + case L =< N of + true -> + %% Consider ideal redundant coverage when number of valid_witnesses is less than + %% or equal to configured witness_redundancy + 1; + false -> + %% Proportional reward_unit as there are more valid_witnesses than necessary + blockchain_utils:normalize_float(N/L) + end + end, + lists:foldl( fun(WitnessRecord, Map) -> Witness = blockchain_poc_witness_v1:gateway(WitnessRecord), I = maps:get(Witness, Map, 0), - maps:put(Witness, I+1, Map) + maps:put(Witness, I+ToAdd, Map) end, Acc1, ValidWitnesses diff --git a/src/transactions/v1/blockchain_txn_vars_v1.erl b/src/transactions/v1/blockchain_txn_vars_v1.erl index 702635dc25..2c6d8fec7e 100644 --- a/src/transactions/v1/blockchain_txn_vars_v1.erl +++ b/src/transactions/v1/blockchain_txn_vars_v1.erl @@ -914,6 +914,8 @@ validate_var(?poc_challengers_percent, Value) -> validate_float(Value, "poc_challengers_percent", 0.0, 1.0); validate_var(?dc_percent, Value) -> validate_float(Value, "dc_percent", 0.0, 1.0); +validate_var(?witness_redundancy, Value) -> + validate_int(Value, "witness_redundancy", 2, 100, false); validate_var(?reward_version, Value) -> case Value of N when is_integer(N), N >= 1, N =< 5 ->