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

[DNM] Test bls12_381 keys with peer and peerbook SUITEs #390

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{deps, [
lager,
{ranch, "1.5.0"},
{libp2p_crypto, "1.4.1"},
{libp2p_crypto, ".*", {git, "https://github.com/helium/libp2p-crypto", {branch, "rg/bls12-381"}}},
{nat, ".*", {git, "https://github.com/benoitc/erlang-nat", {branch, "master"}}},
{backoff, "1.1.6"},
{throttle, "0.2.0", {pkg, lambda_throttle}},
Expand Down
11 changes: 8 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{ref,"8fc2a5aa2454bca3b57047c0b3f79f8d9d219483"}},
0},
{<<"erl_base58">>,{pkg,<<"erl_base58">>,<<"0.0.1">>},1},
{<<"erlang_tc">>,
{git,"https://github.com/helium/erlang-tc.git",
{ref,"588ca62467a1cbfe43d20e6f681656613dab0622"}},
1},
{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},1},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
{<<"inert">>,{pkg,<<"inert">>,<<"1.0.2">>},1},
Expand All @@ -22,7 +26,10 @@
0},
{<<"intercept">>,{pkg,<<"intercept">>,<<"1.0.0">>},1},
{<<"lager">>,{pkg,<<"lager">>,<<"3.9.2">>},0},
{<<"libp2p_crypto">>,{pkg,<<"libp2p_crypto">>,<<"1.4.1">>},0},
{<<"libp2p_crypto">>,
{git,"https://github.com/helium/libp2p-crypto",
{ref,"111469308be3060c2c1c24b450f57046bd549a02"}},
0},
{<<"multiaddr">>,{pkg,<<"multiaddr">>,<<"1.1.3">>},1},
{<<"multihash">>,{pkg,<<"multihash">>,<<"2.1.0">>},1},
{<<"nat">>,
Expand Down Expand Up @@ -56,7 +63,6 @@
{<<"inet_cidr">>, <<"4814A5B78B969A5E069B0CECBB102622AB0C459B690053ED94543CD529915A43">>},
{<<"intercept">>, <<"1F6C725E6FC070720643BD4D97EE53B1209365C80E520E1F5A1ACB36712A7EB5">>},
{<<"lager">>, <<"4CAB289120EB24964E3886BD22323CB5FEFE4510C076992A23AD18CF85413D8C">>},
{<<"libp2p_crypto">>, <<"CB93FE834A53059671C547957C1235C9426E5B7EE91D156AD5D90A9710BF4AF0">>},
{<<"multiaddr">>, <<"978E58E28F6FACAF428C87AF933612B1E2F3F2775F1794EDA5E831A4EACD2984">>},
{<<"multihash">>, <<"F084F7C6BEC062F0C0E82AE18CFDC8DAEC8F4FAA4C8E1ACE0B9C676A9323162F">>},
{<<"procket">>, <<"1FC0D557ACC0145DD167C566C68F83B183E923016904DFE980534B3C9150E92B">>},
Expand All @@ -77,7 +83,6 @@
{<<"inet_cidr">>, <<"97046492E5C5BE0D8B92CD275980D667A8D28D9E79B2305828E358CC7D30A935">>},
{<<"intercept">>, <<"FC5F5E26A571637B0B80119FE2F1E83DDDEAC8078BD76279C015474AF432B32E">>},
{<<"lager">>, <<"7F904D9E87A8CB7E66156ED31768D1C8E26EBA1D54F4BC85B1AA4AC1F6340C28">>},
{<<"libp2p_crypto">>, <<"A21C278896DA65E31A2C8FE767784F1493289CD956E172E7BA247EAB75207037">>},
{<<"multiaddr">>, <<"980D3EA5EB0EB2EFC51E3D10953F17447D417C49BD2FCD7FC6A2A42D1F66D5EE">>},
{<<"multihash">>, <<"E73AD5D0099DBFFB4EE429A78436B605AFE2530ED684AB36BB86733AB65707C8">>},
{<<"procket">>, <<"647C2D1F5006370E457522ECC69509F1020D7D558F3D6E196825E1F4F3660218">>},
Expand Down
59 changes: 44 additions & 15 deletions test/peer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,49 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-export([all/0]).
-export([all/0,
init_per_group/2,
end_per_group/2,
groups/0]).

-export([coding_test/1, metadata_test/1, blacklist_test/1, association_test/1, signed_metadata_test/1]).

all() ->
test_cases() ->
[coding_test,
metadata_test,
blacklist_test,
association_test,
signed_metadata_test
].

coding_test(_) ->
#{public := PubKey1, secret := PrivKey1} = libp2p_crypto:generate_keys(ecc_compact),
#{public := PubKey2, secret := PrivKey2} = libp2p_crypto:generate_keys(ecc_compact),
all() ->
[{group, ecc_compact}, {group, ed25519}, {group, bls12_381}].

groups() ->
[
{ecc_compact, [], test_cases()},
{ed25519, [], test_cases()},
{bls12_381, [], test_cases()}
].

init_per_group(ecc_compact, Config) ->
[{key_type, ecc_compact} | Config];
init_per_group(ed25519, Config) ->
[{key_type, ed25519} | Config];
init_per_group(bls12_381, Config) ->
[{key_type, bls12_381} | Config].

end_per_group(_, _Config) ->
ok.

coding_test(Config) ->
KeyType = ?config(key_type, Config),
#{public := PubKey1, secret := PrivKey1} = libp2p_crypto:generate_keys(KeyType),
#{public := PubKey2, secret := PrivKey2} = libp2p_crypto:generate_keys(KeyType),
SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1),
SigFun2 = libp2p_crypto:mk_sig_fun(PrivKey2),

#{public := AssocPubKey1, secret := AssocPrivKey1} = libp2p_crypto:generate_keys(ecc_compact),
#{public := AssocPubKey1, secret := AssocPrivKey1} = libp2p_crypto:generate_keys(KeyType),
AssocSigFun1 = libp2p_crypto:mk_sig_fun(AssocPrivKey1),
Associations = [libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey1),
libp2p_crypto:pubkey_to_bin(PubKey1),
Expand Down Expand Up @@ -62,8 +87,9 @@ coding_test(_) ->

ok.

metadata_test(_) ->
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact),
metadata_test(Config) ->
KeyType = ?config(key_type, Config),
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType),
SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1),

PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1),
Expand Down Expand Up @@ -91,8 +117,9 @@ metadata_test(_) ->

ok.

blacklist_test(_) ->
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact),
blacklist_test(Config) ->
KeyType = ?config(key_type, Config),
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType),
SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1),

BadListenAddr = "/ip4/8.8.8.8/tcp/1234",
Expand All @@ -111,16 +138,17 @@ blacklist_test(_) ->

ok.

association_test(_) ->
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact),
association_test(Config) ->
KeyType = ?config(key_type, Config),
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType),
SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1),

PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1),
listen_addrs => ["/ip4/8.8.8.8/tcp/1234"],
connected => [],
nat_type => static},

#{secret := AssocPrivKey1, public := AssocPubKey1} = libp2p_crypto:generate_keys(ecc_compact),
#{secret := AssocPrivKey1, public := AssocPubKey1} = libp2p_crypto:generate_keys(KeyType),
ValidAssoc = libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey1),
libp2p_crypto:pubkey_to_bin(PubKey1),
libp2p_crypto:mk_sig_fun(AssocPrivKey1)),
Expand Down Expand Up @@ -158,8 +186,9 @@ association_test(_) ->

ok.

signed_metadata_test(_) ->
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact),
signed_metadata_test(Config) ->
KeyType = ?config(key_type, Config),
#{secret := PrivKey1, public := PubKey1} = libp2p_crypto:generate_keys(KeyType),
SigFun1 = libp2p_crypto:mk_sig_fun(PrivKey1),

PeerMap = #{pubkey => libp2p_crypto:pubkey_to_bin(PubKey1),
Expand Down
57 changes: 42 additions & 15 deletions test/peerbook_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").

-export([all/0, init_per_testcase/2, end_per_testcase/2]).
-export([all/0,
init_per_testcase/2,
end_per_testcase/2,
init_per_group/2,
end_per_group/2,
groups/0
]).
-export([accessor_test/1, bad_peer_test/1, put_test/1, blacklist_test/1,
association_test/1, gossip_test/1, network_id_gossip_test/1, stale_test/1]).

all() ->
[{group, ecc_compact}, {group, ed25519}, {group, bls12_381}].

test_cases() ->
[
accessor_test,
bad_peer_test,
Expand All @@ -19,10 +28,24 @@ all() ->
association_test
].

groups() ->
[
{ecc_compact, [], test_cases()},
{ed25519, [], test_cases()},
{bls12_381, [], test_cases()}
].

init_per_group(ecc_compact, Config) ->
[{key_type, ecc_compact} | Config];
init_per_group(ed25519, Config) ->
[{key_type, ed25519} | Config];
init_per_group(bls12_381, Config) ->
[{key_type, bls12_381} | Config].

end_per_group(_, _Config) ->
ok.

init_per_testcase(accessor_test = TestCase, Config) ->

Config0 = test_util:init_base_dir_config(?MODULE, TestCase, Config),
setup_peerbook(Config0, []);
init_per_testcase(bad_peer_test = TestCase, Config) ->
Expand Down Expand Up @@ -98,8 +121,8 @@ accessor_test(Config) ->
{_PeerBook, Address} = ?config(peerbook, Config),
PeerBook = libp2p_swarm:peerbook(?config(tid, Config)),

Peer1 = mk_peer(),
Peer2 = mk_peer(),
Peer1 = mk_peer(Config),
Peer2 = mk_peer(Config),

libp2p_peerbook:put(PeerBook, [Peer1]),
libp2p_peerbook:put(PeerBook, [Peer2]),
Expand All @@ -125,10 +148,11 @@ accessor_test(Config) ->
ok.

bad_peer_test(Config) ->
KeyType = ?config(key_type, Config),
PeerBook = libp2p_swarm:peerbook(?config(tid, Config)),

#{public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact),
#{secret := PrivKey2, public := PubKey2} = libp2p_crypto:generate_keys(ecc_compact),
#{public := PubKey1} = libp2p_crypto:generate_keys(KeyType),
#{secret := PrivKey2, public := PubKey2} = libp2p_crypto:generate_keys(KeyType),

SigFun2 = libp2p_crypto:mk_sig_fun(PrivKey2),

Expand All @@ -149,7 +173,7 @@ blacklist_test(Config) ->
{_PeerBook, _Address} = ?config(peerbook, Config),
PeerBook = libp2p_swarm:peerbook(?config(tid, Config)),

Peer1 = mk_peer(),
Peer1 = mk_peer(Config),

libp2p_peerbook:put(PeerBook, [Peer1]),

Expand All @@ -165,10 +189,11 @@ blacklist_test(Config) ->


association_test(Config) ->
KeyType = ?config(key_type, Config),
{_PeerBook, Address} = ?config(peerbook, Config),
PeerBook = libp2p_swarm:peerbook(?config(tid, Config)),

#{secret := AssocPrivKey, public := AssocPubKey} = libp2p_crypto:generate_keys(ecc_compact),
#{secret := AssocPrivKey, public := AssocPubKey} = libp2p_crypto:generate_keys(KeyType),
AssocSigFun = libp2p_crypto:mk_sig_fun(AssocPrivKey),
Assoc = libp2p_peer:mk_association(libp2p_crypto:pubkey_to_bin(AssocPubKey), Address, AssocSigFun),

Expand All @@ -190,9 +215,9 @@ put_test(Config) ->
{_PeerBook, Address} = ?config(peerbook, Config),
PeerBook = libp2p_swarm:peerbook(?config(tid, Config)),

PeerList1 = [mk_peer() || _ <- lists:seq(1, 5)],
PeerList1 = [mk_peer(Config) || _ <- lists:seq(1, 5)],

ExtraPeers = [mk_peer() || _ <- lists:seq(1, 3)],
ExtraPeers = [mk_peer(Config) || _ <- lists:seq(1, 3)],
PeerList2 = lists:sublist(PeerList1, 1, 3) ++ ExtraPeers,

ok = libp2p_peerbook:put(PeerBook, PeerList1),
Expand Down Expand Up @@ -351,7 +376,7 @@ stale_test(Config) ->
S1Addr = libp2p_swarm:pubkey_bin(S1),
{ok, S1First} = libp2p_peerbook:get(PeerBook, S1Addr),

Peer1 = mk_peer(),
Peer1 = mk_peer(Config),
libp2p_peerbook:put(PeerBook, [Peer1]),

%% This peer should remew itself after stale_time
Expand Down Expand Up @@ -400,9 +425,10 @@ stale_test(Config) ->
peer_keys(PeerList) ->
[libp2p_crypto:bin_to_b58(libp2p_peer:pubkey_bin(P)) || P <- PeerList].

mk_peer() ->
#{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(ecc_compact),
#{ public := PubKey2} = libp2p_crypto:generate_keys(ecc_compact),
mk_peer(Config) ->
KeyType = ?config(key_type, Config),
#{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(KeyType),
#{ public := PubKey2} = libp2p_crypto:generate_keys(KeyType),
{ok, Peer} = libp2p_peer:from_map(#{pubkey => libp2p_crypto:pubkey_to_bin(PubKey),
listen_addrs => ["/ip4/8.8.8.8/tcp/1234"],
connected => [libp2p_crypto:pubkey_to_bin(PubKey2)],
Expand All @@ -413,10 +439,11 @@ mk_peer() ->

setup_peerbook(Config, Opts) ->
test_util:setup(),
KeyType = ?config(key_type, Config),
Name = list_to_atom("swarm" ++ integer_to_list(erlang:monotonic_time())),
TID = ets:new(Name, [public, ordered_set, {read_concurrency, true}]),
ets:insert(TID, {swarm_name, Name}),
#{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(ecc_compact),
#{secret := PrivKey, public := PubKey} = libp2p_crypto:generate_keys(KeyType),
CompactKey = libp2p_crypto:pubkey_to_bin(PubKey),
ets:insert(TID, {swarm_address, CompactKey}),
PrivDir = ?config(priv_dir, Config),
Expand Down