Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Use chain vars from header #150

Merged
merged 1 commit into from
Jul 26, 2019
Merged
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
45 changes: 22 additions & 23 deletions src/cli/miner_cli_genesis.erl
Original file line number Diff line number Diff line change
Expand Up @@ -290,36 +290,35 @@ make_vars() ->
{ok, Curve} = application:get_env(miner, curve),
{ok, N} = application:get_env(blockchain, num_consensus_members),

#{block_time => BlockTime,
#{?block_time => BlockTime,
?election_interval => Interval,
?election_restart_interval => 10,
?num_consensus_members => N,
batch_size => BatchSize,
?batch_size => BatchSize,
?vars_commit_delay => 20,
%% poc challenge interval * 20
?var_gw_inactivity_thresh => 600,
block_version => v1,
dkg_curve => Curve,
predicate_callback_mod => miner,
predicate_callback_fun => version,
?block_version => v1,
?dkg_curve => Curve,
?predicate_callback_mod => miner,
?predicate_callback_fun => version,
?predicate_threshold => 0.95,
monthly_reward => 50000 * 1000000,
securities_percent => 0.35,
dc_percent => 0,
poc_challengees_percent => 0.19 + 0.16,
poc_challengers_percent => 0.09 + 0.06,
poc_witnesses_percent => 0.02 + 0.03,
consensus_percent => 0.10,
?monthly_reward => 50000 * 1000000,
?securities_percent => 0.35,
?dc_percent => 0,
?poc_challengees_percent => 0.19 + 0.16,
?poc_challengers_percent => 0.09 + 0.06,
?poc_witnesses_percent => 0.02 + 0.03,
?consensus_percent => 0.10,
?election_selection_pct => 60,
?election_replacement_factor => 4,
?election_replacement_slope => 20,
min_score => 0.2,
alpha_decay => 0.007,
beta_decay => 0.0005,
max_staleness => 100000,
poc_challenge_interval => 30,
min_assert_h3_res => 12,
h3_neighbor_res => 12,
h3_max_grid_distance => 13,
h3_exclusion_ring_dist => 2
?min_score => 0.2,
?alpha_decay => 0.007,
?beta_decay => 0.0005,
?max_staleness => 100000,
?poc_challenge_interval => 30,
?min_assert_h3_res => 12,
?h3_neighbor_res => 12,
?h3_max_grid_distance => 13,
?h3_exclusion_ring_dist => 2
}.
18 changes: 10 additions & 8 deletions src/miner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

-behavior(gen_server).

-include_lib("blockchain/include/blockchain_vars.hrl").

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
Expand Down Expand Up @@ -295,7 +297,7 @@ init(_Args) ->
{ok, Block} = blockchain:get_block(Top, Chain),
{ElectionEpoch, EpochStart} = blockchain_block_v1:election_info(Block),
Ledger = blockchain:ledger(Chain),
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),
self() ! init,

{ok, #state{blockchain = Chain,
Expand Down Expand Up @@ -364,7 +366,7 @@ handle_call({start_chain, ConsensusGroup, Chain}, _From, State) ->
ConsensusGroup]}),

Ledger = blockchain:ledger(Chain),
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

Ref = set_next_block_timer(Chain),
{reply, ok, State#state{consensus_group = ConsensusGroup,
Expand Down Expand Up @@ -503,7 +505,7 @@ handle_info({blockchain_event, {add_block, Hash, Sync, _Ledger}},
%% there's a new group now, and we're still in, so pass over the
%% buffer, shut down the old one and elevate the new one
{true, true, ElectionHeight} ->
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

miner_consensus_mgr:cancel_dkg(),
lager:info("stay in ~p", [Waiting]),
Expand Down Expand Up @@ -534,7 +536,7 @@ handle_info({blockchain_event, {add_block, Hash, Sync, _Ledger}},
consensus_start = Height};
%% we're not a member of the new group, we can shut down
{true, false, _} ->
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

miner_consensus_mgr:cancel_dkg(),
lager:info("leave"),
Expand Down Expand Up @@ -583,7 +585,7 @@ handle_info({blockchain_event, {add_block, Hash, Sync, _Ledger}},
miner_consensus_mgr:maybe_start_election(Hash, Height, NextElection),
{noreply, State#state{current_height = Height}};
{true, true, ElectionHeight} ->
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

lager:info("nc start group"),
miner_consensus_mgr:cancel_dkg(),
Expand Down Expand Up @@ -615,7 +617,7 @@ handle_info({blockchain_event, {add_block, Hash, Sync, _Ledger}},
consensus_start = Height}};
%% we're not a member of the new group, we can stay down
{true, false, _} ->
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

lager:info("nc stay out"),
miner_consensus_mgr:cancel_dkg(),
Expand All @@ -640,7 +642,7 @@ handle_info({blockchain_event, {add_block, _Hash, _Sync, _Ledger}},
State) when State#state.blockchain == undefined ->
Chain = blockchain_worker:blockchain(),
Ledger = blockchain:ledger(Chain),
{ok, Interval} = blockchain:config(election_interval, Ledger),
{ok, Interval} = blockchain:config(?election_interval, Ledger),

{noreply, State#state{blockchain = Chain,
election_interval = Interval}};
Expand Down Expand Up @@ -683,7 +685,7 @@ restore(Chain, Block, Height, Interval) ->
Group.

set_next_block_timer(Chain) ->
{ok, BlockTime} = blockchain:config(block_time, blockchain:ledger(Chain)),
{ok, BlockTime} = blockchain:config(?block_time, blockchain:ledger(Chain)),
{ok, HeadBlock} = blockchain:head_block(Chain),
LastBlockTimestamp = blockchain_block:time(HeadBlock),
NextBlockTime = max(0, (LastBlockTimestamp + (BlockTime div 1000)) - erlang:system_time(seconds)),
Expand Down
27 changes: 14 additions & 13 deletions src/miner_consensus_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-behaviour(gen_server).

-include_lib("blockchain/include/blockchain.hrl").
-include_lib("blockchain/include/blockchain_vars.hrl").

%% API
-export([
Expand Down Expand Up @@ -137,9 +138,9 @@ handle_call({genesis_block_done, BinaryGenesisBlock, Signatures, Members, PrivKe
Chain = blockchain_worker:blockchain(),
Ledger = blockchain:ledger(Chain),

{ok, N} = blockchain:config(num_consensus_members, Ledger),
{ok, N} = blockchain:config(?num_consensus_members, Ledger),
F = ((N - 1) div 3),
{ok, BatchSize} = blockchain:config(batch_size, Ledger),
{ok, BatchSize} = blockchain:config(?batch_size, Ledger),

GroupArg = [miner_hbbft_handler, [Members,
State#state.consensus_pos,
Expand All @@ -164,7 +165,7 @@ handle_call({election_done, _Artifact, Signatures, Members, PrivKey}, _From,
delay = Delay}) ->
lager:info("election done at ~p delay ~p", [Height, Delay]),

{ok, N} = blockchain:config(num_consensus_members, blockchain:ledger(Chain)),
{ok, N} = blockchain:config(?num_consensus_members, blockchain:ledger(Chain)),
F = ((N - 1) div 3),

Proof = term_to_binary(Signatures, [compressed]),
Expand All @@ -184,7 +185,7 @@ handle_call({election_done, _Artifact, Signatures, Members, PrivKey}, _From,
end
end
),
{ok, BatchSize} = blockchain:config(batch_size, blockchain:ledger(Chain)),
{ok, BatchSize} = blockchain:config(?batch_size, blockchain:ledger(Chain)),

GroupArg = [miner_hbbft_handler, [Members,
State#state.consensus_pos,
Expand All @@ -209,7 +210,7 @@ handle_call({maybe_start_consensus_group, StartHeight}, _From,

Chain = blockchain_worker:blockchain(),
Ledger = blockchain:ledger(Chain),
{ok, N} = blockchain:config(num_consensus_members, Ledger),
{ok, N} = blockchain:config(?num_consensus_members, Ledger),
F = ((N - 1) div 3),
case blockchain_ledger_v1:consensus_members(Ledger) of
{error, _} ->
Expand All @@ -220,7 +221,7 @@ handle_call({maybe_start_consensus_group, StartHeight}, _From,
true ->
lager:info("restoring consensus group"),
Pos = miner_util:index_of(blockchain_swarm:pubkey_bin(), ConsensusAddrs),
{ok, BatchSize} = blockchain:config(batch_size, Ledger),
{ok, BatchSize} = blockchain:config(?batch_size, Ledger),
GroupArg = [miner_hbbft_handler, [ConsensusAddrs,
Pos,
N,
Expand Down Expand Up @@ -274,7 +275,7 @@ handle_call({start_election, Hash0, CurrentHeight, StartHeight}, _From,
lager:info("election started at ~p curr ~p", [StartHeight, CurrentHeight]),
Diff = CurrentHeight - StartHeight,
Ledger = blockchain:ledger(Chain),
{ok, Interval} = blockchain:config(election_restart_interval, Ledger),
{ok, Interval} = blockchain:config(?election_restart_interval, Ledger),

Delay = (Diff div Interval) * Interval,
State = State0#state{initial_height = StartHeight,
Expand Down Expand Up @@ -344,7 +345,7 @@ handle_info({blockchain_event, {add_block, Hash, _Sync, _Ledger}},
Height =/= 0 ->

Ledger = blockchain:ledger(State#state.chain),
{ok, Interval} = blockchain:config(election_restart_interval, Ledger),
{ok, Interval} = blockchain:config(?election_restart_interval, Ledger),
case blockchain:get_block(Hash, State#state.chain) of
{ok, Block} ->
NextRestart = Height + Interval + Delay,
Expand Down Expand Up @@ -387,8 +388,8 @@ code_change(_OldVsn, State, _Extra) ->
initiate_election(Hash, Height, #state{delay = Delay} = State) ->
Chain = blockchain_worker:blockchain(),
Ledger = blockchain:ledger(Chain),
{ok, N} = blockchain:config(num_consensus_members, Ledger),
{ok, Curve} = blockchain:config(dkg_curve, Ledger),
{ok, N} = blockchain:config(?num_consensus_members, Ledger),
{ok, Curve} = blockchain:config(?dkg_curve, Ledger),

ConsensusAddrs = blockchain_election:new_group(Ledger, Hash, N, Delay),
Artifact = term_to_binary(ConsensusAddrs),
Expand All @@ -408,12 +409,12 @@ restart_election(#state{delay = Delay0,
chain=Chain} = State, Hash, Height) ->

Ledger = blockchain:ledger(Chain),
{ok, Interval} = blockchain:config(election_restart_interval, Ledger),
{ok, Interval} = blockchain:config(?election_restart_interval, Ledger),

Delay = Delay0 + Interval,
lager:warning("restarting election at ~p delay ~p", [Height, Delay]),
{ok, N} = blockchain:config(num_consensus_members, Ledger),
{ok, Curve} = blockchain:config(dkg_curve, Ledger),
{ok, N} = blockchain:config(?num_consensus_members, Ledger),
{ok, Curve} = blockchain:config(?dkg_curve, Ledger),

ConsensusAddrs = blockchain_election:new_group(Ledger, Hash, N, Delay),
case length(ConsensusAddrs) == N of
Expand Down
60 changes: 30 additions & 30 deletions test/miner_blockchain_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,38 @@ init_per_testcase(TestCase, Config0) ->
#{secret := Priv, public := Pub} =
libp2p_crypto:generate_keys(ecc_compact),

Vars = #{block_time => BlockTime,
election_interval => Interval,
election_restart_interval => 10,
num_consensus_members => NumConsensusMembers,
batch_size => BatchSize,
Vars = #{?block_time => BlockTime,
?election_interval => Interval,
?election_restart_interval => 10,
?num_consensus_members => NumConsensusMembers,
?batch_size => BatchSize,
?vars_commit_delay => 5,
?var_gw_inactivity_thresh => 20,
block_version => v1,
dkg_curve => Curve,
garbage_value => totes_garb,
predicate_callback_mod => miner,
predicate_callback_fun => test_version,
predicate_threshold => 0.85,
monthly_reward => 50000 * 1000000,
securities_percent => 0.35,
dc_percent => 0,
poc_challengees_percent => 0.19 + 0.16,
poc_challengers_percent => 0.09 + 0.06,
poc_witnesses_percent => 0.02 + 0.03,
consensus_percent => 0.10,
election_selection_pct => 60,
election_replacement_factor => 4,
election_replacement_slope => 20,
min_score => 0.2,
alpha_decay => 0.007,
beta_decay => 0.0005,
max_staleness => 100000,
min_assert_h3_res => 12,
h3_neighbor_res => 12,
h3_max_grid_distance => 13,
h3_exclusion_ring_dist => 2,
poc_challenge_interval => 10
?block_version => v1,
?dkg_curve => Curve,
?garbage_value => totes_garb,
?predicate_callback_mod => miner,
?predicate_callback_fun => test_version,
?predicate_threshold => 0.85,
?monthly_reward => 50000 * 1000000,
?securities_percent => 0.35,
?dc_percent => 0,
?poc_challengees_percent => 0.19 + 0.16,
?poc_challengers_percent => 0.09 + 0.06,
?poc_witnesses_percent => 0.02 + 0.03,
?consensus_percent => 0.10,
?election_selection_pct => 60,
?election_replacement_factor => 4,
?election_replacement_slope => 20,
?min_score => 0.2,
?alpha_decay => 0.007,
?beta_decay => 0.0005,
?max_staleness => 100000,
?min_assert_h3_res => 12,
?h3_neighbor_res => 12,
?h3_max_grid_dist => 13,
?h3_exclusion_ring_dist => 2,
?poc_challenge_interval => 10
},

BinPub = libp2p_crypto:pubkey_to_bin(Pub),
Expand Down
58 changes: 29 additions & 29 deletions test/miner_payment_txn_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,37 @@ init_per_testcase(_TestCase, Config0) ->
#{secret := Priv, public := Pub} =
libp2p_crypto:generate_keys(ecc_compact),

Vars = #{block_time => BlockTime,
election_interval => Interval,
election_restart_interval => 10,
num_consensus_members => N,
batch_size => BatchSize,
Vars = #{?block_time => BlockTime,
?election_interval => Interval,
?election_restart_interval => 10,
?num_consensus_members => N,
?batch_size => BatchSize,
?vars_commit_delay => 2,
?var_gw_inactivity_thresh => 20,
block_version => v1,
dkg_curve => Curve,
predicate_callback_mod => miner,
predicate_callback_fun => test_version,
proposal_threshold => 0.85,
monthly_reward => 0,
securities_percent => 0,
dc_percent => 0,
poc_challengees_percent => 0,
poc_challengers_percent => 0,
poc_witnesses_percent => 0,
consensus_percent => 0,
election_selection_pct => 60,
election_replacement_factor => 4,
election_replacement_slope => 20,
min_score => 0.2,
alpha_decay => 0.007,
beta_decay => 0.0005,
max_staleness => 100000,
min_assert_h3_res => 12,
h3_neighbor_res => 12,
h3_max_grid_distance => 13,
h3_exclusion_ring_dist => 2,
poc_challenge_interval => 30
?block_version => v1,
?dkg_curve => Curve,
?predicate_callback_mod => miner,
?predicate_callback_fun => test_version,
?proposal_threshold => 0.85,
?monthly_reward => 0,
?securities_percent => 0,
?dc_percent => 0,
?poc_challengees_percent => 0,
?poc_challengers_percent => 0,
?poc_witnesses_percent => 0,
?consensus_percent => 0,
?election_selection_pct => 60,
?election_replacement_factor => 4,
?election_replacement_slope => 20,
?min_score => 0.2,
?alpha_decay => 0.007,
?beta_decay => 0.0005,
?max_staleness => 100000,
?min_assert_h3_res => 12,
?h3_neighbor_res => 12,
?h3_max_grid_distance => 13,
?h3_exclusion_ring_dist => 2,
?poc_challenge_interval => 30
},

BinPub = libp2p_crypto:pubkey_to_bin(Pub),
Expand Down
Loading