Skip to content

Commit

Permalink
Support FloodSub (FullyConnected) Networks
Browse files Browse the repository at this point in the history
Added ability in the code to select if you would like to use a floodsub (fully connected) or a gossip sub network.
  • Loading branch information
Joshua Dahl committed Jan 18, 2024
1 parent 30c8373 commit 5f38483
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/chat.capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int main(int argc, char* argv[]) {
p2p_set_peer_connected_callback(p2p_initial_network(), peerJoined); // NOTE: these callbacks will only be called for peers directly connected... if you need to know about all peers in the network that will need to be done at a higher level!
p2p_set_peer_disconnected_callback(p2p_initial_network(), peerLeft); // NOTE: these callbacks will only be called for peers directly connected... if you need to know about all peers in the network that will need to be done at a higher level!

auto network = p2p_initialize(p2p_initialize_args_from_strings("/ip4/0.0.0.0/udp/0/quic-v1", "simplep2p/examples/chat/capi/v1.0.0", key, 60, false));
auto network = p2p_initialize(p2p_initialize_args_from_strings("/ip4/0.0.0.0/udp/0/quic-v1", "simplep2p/examples/chat/capi/v1.0.0", key, 60, false, false));

p2p_set_message_callback(network, print);
p2p_set_connected_callback(network, connected);
Expand Down
19 changes: 14 additions & 5 deletions src/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func generateKey() []byte {
// initialize starts up a connection to the p2p network and initializes some library states
//
//export initialize
func initialize(listenAddress string, discoveryTopic string, keyString string, connectionTimeout float64, verbose bool) int {
func initialize(listenAddress string, discoveryTopic string, keyString string, connectionTimeout float64, fullyConnected bool, verbose bool) int {
var nid = len(states)
var localState State
localState.verbose = verbose
Expand Down Expand Up @@ -208,11 +208,20 @@ func initialize(listenAddress string, discoveryTopic string, keyString string, c

go discoverPeers(nid, states[nid].ctx, states[nid].host, discoveryTopic)

ps, err := pubsub.NewGossipSub(localState.ctx, localState.host)
if err != nil {
panic(err)
if fullyConnected {
ps, err := pubsub.NewFloodSub(localState.ctx, localState.host)
if err != nil {
panic(err)
}
localState.ps = ps
} else {
ps, err := pubsub.NewGossipSub(localState.ctx, localState.host)
if err != nil {
panic(err)
}
localState.ps = ps
}
localState.ps = ps

localState.topics = make(map[int]Topic)

states[nid] = localState
Expand Down
6 changes: 4 additions & 2 deletions src/simplep2p.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ P2PInitializationArguments p2p_default_initialize_args() {
out.discoveryTopicSize = strlen(out.discoveryTopic);
out.identity = p2p_null_key();
out.connectionTimeout = 60 /*seconds*/;
out.fullyConnected = false;
out.verbose = false;
return out;
}
Expand All @@ -277,14 +278,15 @@ P2PInitializationArguments p2p_default_initialize_args() {
* @param verbose The verbose flag.
* @return The initialization arguments.
*/
P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose) {
P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool fullyConnected, bool verbose) {
P2PInitializationArguments out;
out.listenAddress = listenAddress;
out.listenAddressSize = strlen(out.listenAddress);
out.discoveryTopic = discoveryTopic;
out.discoveryTopicSize = strlen(out.discoveryTopic);
out.identity = identity;
out.connectionTimeout = connectionTimeout;
out.fullyConnected = fullyConnected;
out.verbose = verbose;
return out;
}
Expand All @@ -307,7 +309,7 @@ P2PTopic p2p_initialize(P2PInitializationArguments args) {
GoString key;
key.p = args.identity.data;
key.n = args.identity.size;
return initialize(listenAddress, discoveryTopic, key, args.connectionTimeout, args.verbose);
return initialize(listenAddress, discoveryTopic, key, args.connectionTimeout, args.fullyConnected, args.verbose);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/simplep2p.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ typedef struct {
long long discoveryTopicSize; ///< The size of the discovery topic.
P2PKey identity; ///< The P2P key identity.
double connectionTimeout; ///< The time in seconds to try connecting before giving up
bool fullyConnected; ///< Weather every message should be sent to every peer, or if the network should be more intelligent
bool verbose; ///< The verbose flag.
} P2PInitializationArguments;

Expand All @@ -174,7 +175,7 @@ P2PInitializationArguments p2p_default_initialize_args();
* @param verbose The verbose flag.
* @return The initialization arguments.
*/
P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose);
P2PInitializationArguments p2p_initialize_args_from_strings(const char* listenAddress, const char* discoveryTopic, P2PKey identity, double connectionTimeout, bool verbose, bool fullyConnected);

/**
* @brief Initializes P2P network with the provided arguments.
Expand Down
7 changes: 6 additions & 1 deletion src/simplep2p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ namespace p2p {
* @param identityKey The identity key for network initialization.
* @param do_on_connected Callback function to register in on_connected before initializing the connection
* @param connectionTimeout The time to wait for a connection before giving up.
* @param fullyConnected Weather or not every message should be sent to every peer, or if the network should be more intelligent.
* @param verbose Flag indicating if the GO library should spew some more verbose messages.
*/
Network(
Expand All @@ -247,12 +248,13 @@ namespace p2p {
const Key& identityKey = {},
delegate_function<void(Network&)> do_on_connected = nullptr,
std::chrono::milliseconds connectionTimeout = std::chrono::seconds(60),
bool fullyConnected = false,
bool verbose = false
) {
if(do_on_connected != nullptr)
on_connected = do_on_connected;

initialize(listenAddress, discoveryTopic, identityKey, connectionTimeout, verbose);
initialize(listenAddress, discoveryTopic, identityKey, connectionTimeout, fullyConnected, verbose);
}

/**
Expand Down Expand Up @@ -290,13 +292,15 @@ namespace p2p {
* @param discoveryTopic The discovery topic for network initialization.
* @param identityKey The identity key for network initialization.
* @param connectionTimeout The time to wait for a connection before giving up.
* @param fullyConnected Weather or not every message should be sent to every peer, or if the network should be more intelligent.
* @param verbose Flag indicating if the GO library should spew some more verbose messages.
*/
void initialize(
std::string_view listenAddress = default_listen_address,
std::string_view discoveryTopic = default_discovery_topic,
const Key& identityKey = {},
std::chrono::milliseconds connectionTimeout = std::chrono::seconds(60),
bool fullyConnected = false,
bool verbose = false
) {
// Connect the connect delegate to its callback
Expand All @@ -310,6 +314,7 @@ namespace p2p {
.discoveryTopicSize = (long long)discoveryTopic.size(),
.identity = identityKey,
.connectionTimeout = std::chrono::duration_cast<std::chrono::duration<double>>(connectionTimeout).count(),
.fullyConnected = fullyConnected,
.verbose = verbose
});

Expand Down

0 comments on commit 5f38483

Please sign in to comment.