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

try not to re-validate txns in blocks that we've signed #337

Merged
merged 1 commit into from Jan 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/blockchain.erl
Expand Up @@ -580,6 +580,7 @@ add_block_(Block, Blockchain, Syncing) ->
end;
{true, true} ->
lager:info("prev hash matches the gossiped block"),
MyAddress = blockchain_swarm:pubkey_bin(),
case blockchain_ledger_v1:consensus_members(Ledger) of
{error, _Reason}=Error ->
lager:error("could not get consensus_members ~p", [_Reason]),
Expand All @@ -589,9 +590,10 @@ add_block_(Block, Blockchain, Syncing) ->
F = (N-1) div 3,
{ok, MasterKey} = blockchain_ledger_v1:master_key(Ledger),
Txns = blockchain_block:transactions(Block),
Sigs = blockchain_block:signatures(Block),
case blockchain_block:verify_signatures(Block,
ConsensusAddrs,
blockchain_block:signatures(Block),
Sigs,
N - F,
MasterKey)
of
Expand All @@ -607,7 +609,12 @@ add_block_(Block, Blockchain, Syncing) ->
lager:info("adding block ~p", [Height]),
ok = ?save_block(Block, Blockchain)
end,
case blockchain_txn:absorb_and_commit(Block, Blockchain, BeforeCommit, Rescue) of
{Signers, _Signatures} = lists:unzip(Sigs),
Fun = case lists:member(MyAddress, Signers) of
true -> unvalidated_absorb_and_commit;
_ -> absorb_and_commit
end,
case blockchain_txn:Fun(Block, Blockchain, BeforeCommit, Rescue) of
{error, Reason}=Error ->
lager:error("Error absorbing transaction, Ignoring Hash: ~p, Reason: ~p", [blockchain_block:hash_block(Block), Reason]),
Error;
Expand Down Expand Up @@ -1484,6 +1491,11 @@ blocks_test() ->
meck:expect(blockchain_election, has_new_group, fun(_) ->
false
end),
meck:new(blockchain_swarm, [passthrough]),
meck:expect(blockchain_swarm, pubkey_bin, fun() ->
crypto:strong_rand_bytes(33)
end),

{ok, Pid} = blockchain_lock:start_link(),

#{secret := Priv, public := Pub} = libp2p_crypto:generate_keys(ecc_compact),
Expand Down Expand Up @@ -1523,7 +1535,10 @@ blocks_test() ->
?assert(meck:validate(blockchain_worker)),
meck:unload(blockchain_worker),
?assert(meck:validate(blockchain_election)),
meck:unload(blockchain_election).
meck:unload(blockchain_election),
?assert(meck:validate(blockchain_swarm)),
meck:unload(blockchain_swarm).



get_block_test() ->
Expand All @@ -1546,7 +1561,10 @@ get_block_test() ->
meck:expect(blockchain_election, has_new_group, fun(_) ->
false
end),

meck:new(blockchain_swarm, [passthrough]),
meck:expect(blockchain_swarm, pubkey_bin, fun() ->
crypto:strong_rand_bytes(33)
end),

{ok, Pid} = blockchain_lock:start_link(),

Expand Down Expand Up @@ -1583,7 +1601,9 @@ get_block_test() ->
?assert(meck:validate(blockchain_worker)),
meck:unload(blockchain_worker),
?assert(meck:validate(blockchain_election)),
meck:unload(blockchain_election).
meck:unload(blockchain_election),
?assert(meck:validate(blockchain_swarm)),
meck:unload(blockchain_swarm).


-endif.
3 changes: 2 additions & 1 deletion test/blockchain_simple_SUITE.erl
Expand Up @@ -77,7 +77,8 @@ init_per_testcase(TestCase, Config) ->
BaseDir = "data/test_SUITE/" ++ erlang:atom_to_list(TestCase),
Balance = 5000,
{ok, Sup, {PrivKey, PubKey}, Opts} = test_utils:init(BaseDir),
{ok, GenesisMembers, ConsensusMembers, Keys} = test_utils:init_chain(Balance, {PrivKey, PubKey}),
%% two tests rely on the swarm not being in the consensus group, so exclude them here
{ok, GenesisMembers, ConsensusMembers, Keys} = test_utils:init_chain(Balance, {PrivKey, PubKey}, not lists:member(TestCase, [bogus_coinbase_test, bogus_coinbase_with_good_payment_test])),

Chain = blockchain_worker:blockchain(),
Swarm = blockchain_swarm:swarm(),
Expand Down
22 changes: 15 additions & 7 deletions test/test_utils.erl
Expand Up @@ -5,7 +5,7 @@
-include("blockchain_vars.hrl").

-export([
init/1, init_chain/2,
init/1, init_chain/2, init_chain/3,
generate_keys/1, generate_keys/2,
wait_until/1, wait_until/3,
create_block/2,
Expand All @@ -30,13 +30,21 @@ init(BaseDir) ->
?assert(erlang:is_pid(blockchain_swarm:swarm())),
{ok, Sup, {PrivKey, PubKey}, Opts}.

init_chain(Balance, {PrivKey, PubKey}) ->
init_chain(Balance, Keys) ->
init_chain(Balance, Keys, true).

init_chain(Balance, {PrivKey, PubKey}, InConsensus) ->
% Generate fake blockchains (just the keys)
RandomKeys = test_utils:generate_keys(10),
Address = blockchain_swarm:pubkey_bin(),
GenesisMembers = [
{Address, {PubKey, PrivKey, libp2p_crypto:mk_sig_fun(PrivKey)}}
] ++ RandomKeys,
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,

% Create genesis block
{InitialVars, Keys} = blockchain_ct_utils:create_vars(#{}),
Expand Down