MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / NewP2P

Function NewP2P

IceFireDB-SQLProxy/pkg/p2p/p2p.go:62–97  ·  view source on GitHub ↗

* A constructor function that generates and returns a P2P object. Constructs a libp2p host with TLS encrypted secure transportation that works over a TCP transport connection using a Yamux Stream Multiplexer and uses UPnP for the NAT traversal. A Kademlia DHT is then bootstrapped on this host usin

(serviceName string, nodeHostIP string, nodeHostPort int)

Source from the content-addressed store, hash-verified

60*/
61
62func NewP2P(serviceName string, nodeHostIP string, nodeHostPort int) *P2P {
63 // Setup a background context
64 ctx := context.Background()
65
66 // Setup a P2P Host Node
67 nodehost, kaddht := setupHost(ctx, nodeHostIP, nodeHostPort)
68 // Debug log
69 logrus.Infoln("Setup the p2p host,listen on", nodehost.Addrs())
70 log.Println("MY P2P Node ID", nodehost.ID())
71
72 // Bootstrap the Kad DHT
73 bootstrapDHT(ctx, nodehost, kaddht)
74
75 // Debug log
76 logrus.Debugln("Bootstrapped the Kademlia DHT and Connected to Bootstrap Peers")
77
78 // Create a peer discovery service using the Kad DHT
79 routingdiscovery := discoveryRouting.NewRoutingDiscovery(kaddht)
80 // Debug log
81 logrus.Debugln("Created the Peer Discovery Service.")
82
83 // Create a PubSub handler with the routing discovery PubSu
84 pubsubhandler := setupPubSub(ctx, nodehost, routingdiscovery)
85 // Debug log
86 logrus.Debugln("Created the PubSub Handler.")
87
88 // Return the P2P object
89 return &P2P{
90 Ctx: ctx,
91 Host: nodehost,
92 KadDHT: kaddht,
93 Discovery: routingdiscovery,
94 PubSub: pubsubhandler,
95 service: serviceName,
96 }
97}
98
99// A method of P2P to connect to service peers.
100// This method uses the Advertise() functionality of the Peer Discovery Service

Callers 1

initP2PFunction · 0.92

Calls 3

setupHostFunction · 0.70
bootstrapDHTFunction · 0.70
setupPubSubFunction · 0.70

Tested by

no test coverage detected