Skip to content

Instantly share code, notes, and snippets.

@xandkar
Last active May 12, 2022 18:35
Show Gist options
  • Save xandkar/4e236879fcf4f69b2e4d0071cc3e6070 to your computer and use it in GitHub Desktop.
Save xandkar/4e236879fcf4f69b2e4d0071cc3e6070 to your computer and use it in GitHub Desktop.
-spec find_connected_node_pair([{N, libp2p_crypto:pubkey_bin()}]) ->
disjoint_network | {N, N} when N :: node().
find_connected_node_pair(NodeAddrs) ->
P2P = fun libp2p_crypto:pubkey_bin_to_p2p/1,
P2PAddrToNode =
lists:foldl(fun ({N, A}, A2N) -> maps:put(P2P(A), N, A2N) end, #{}, NodeAddrs),
Topology = digraph:new(),
lists:foreach(
fun ({NodeA, _}) ->
_ = digraph:add_vertex(Topology, NodeA),
lists:foreach(
fun (P2PAddr) ->
{ok, NodeB} = maps:find(P2PAddr, P2PAddrToNode),
_ = digraph:add_vertex(Topology, NodeB),
_ = digraph:add_edge(Topology, NodeA, NodeB),
_ = digraph:add_edge(Topology, NodeB, NodeA),
ok
end,
ct_rpc:call(NodeA, blockchain_swarm, gossip_peers, [], 500)
)
end,
NodeAddrs
),
ConnectedPair =
case [C || C <- digraph_utils:components(Topology), length(C) > 1] of
[[NodeA, NodeB | _] | _] -> % XXX Can pick any random pair here.
{NodeA, NodeB};
[] ->
disjoint_network
end,
true = digraph:delete(Topology),
ConnectedPair.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment