Skip to content

Commit

Permalink
relay
Browse files Browse the repository at this point in the history
  • Loading branch information
obdCoder committed Dec 30, 2020
1 parent 026b2f0 commit 069aafd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lightclient/p2p_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const protocolIdForBetweenObd = "obd/betweenObd/1.0.1"
const protocolIdForScanObd = "obd/forScanObd/1.0.1"

var hostNode host.Host
var relayNode string

var localServerDest string
var p2PLocalPeerId string
Expand Down Expand Up @@ -115,13 +116,18 @@ func StartP2PNode() (err error) {
for _, peerAddr := range config.BootstrapPeers {
peerInfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
wg.Add(1)

go func() {
defer wg.Done()
err = hostNode.Connect(ctx, *peerInfo)

if err != nil {
log.Println(err, peerInfo)
} else {
log.Println("connected to bootstrap node ", *peerInfo)
if len(relayNode) == 0 {
relayNode = peerInfo.ID.Pretty()
}
}
}()
}
Expand Down Expand Up @@ -161,23 +167,22 @@ func connP2PNode(dest string) (string, error) {
return " Remote peer has been connected", nil
}

relayNode := "QmWp5amgedYDc1Feay1sn2Q1dWvePtKAzwCnzCattH2xLR"
relayaddr, err := multiaddr.NewMultiaddr("/p2p/" + relayNode + "/p2p-circuit/p2p/" + destHostPeerInfo.ID.Pretty())
relayAddr, err := multiaddr.NewMultiaddr("/p2p/" + relayNode + "/p2p-circuit/p2p/" + destHostPeerInfo.ID.Pretty())
if err != nil {
log.Println(err)
return "", err
}
hostNode.Network().(*swarm.Swarm).Backoff().Clear(destHostPeerInfo.ID)
peerRelayInfo := peer.AddrInfo{
ID: destHostPeerInfo.ID,
Addrs: []multiaddr.Multiaddr{relayaddr},
Addrs: []multiaddr.Multiaddr{relayAddr},
}

if err := hostNode.Connect(ctx, peerRelayInfo); err != nil {
log.Println(err)
return "", err
} else {
log.Println("Connection established with RELAY node:", relayaddr)
log.Println("Connection established with RELAY node:", relayAddr)
}

hostNode.Peerstore().AddAddrs(destHostPeerInfo.ID, destHostPeerInfo.Addrs, peerstore.PermanentAddrTTL)
Expand Down

0 comments on commit 069aafd

Please sign in to comment.