From f40cfbfefca51089a8cd6c3b3089091b29519fe3 Mon Sep 17 00:00:00 2001 From: obdCoder Date: Wed, 30 Dec 2020 09:43:29 +0800 Subject: [PATCH 1/5] config --- config/cfg.go | 9 +++------ config/conf.ini | 6 +++--- tracker/config/conf.ini | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/config/cfg.go b/config/cfg.go index c38d1ffff..fc0944d3f 100644 --- a/config/cfg.go +++ b/config/cfg.go @@ -24,7 +24,7 @@ var ( ChainNodeType = "test" //P2P P2P_hostIp = "127.0.0.1" - P2P_sourcePort = 4001 + P2P_sourcePort = 60801 BootstrapPeers addrList ) @@ -45,8 +45,8 @@ func Init() { return } ServerPort = section.Key("port").MustInt(60020) - ReadTimeout = time.Duration(section.Key("readTimeout").MustInt(5)) * time.Second - WriteTimeout = time.Duration(section.Key("writeTimeout").MustInt(5)) * time.Second + ReadTimeout = time.Duration(section.Key("readTimeout").MustInt(60)) * time.Second + WriteTimeout = time.Duration(section.Key("writeTimeout").MustInt(60)) * time.Second htlcNode, err := Cfg.GetSection("htlc") if err != nil { @@ -70,8 +70,5 @@ func Init() { log.Println(err) return } - if len(tracker.Key("host").String()) == 0 { - panic("empty tracker host") - } TrackerHost = tracker.Key("host").MustString("localhost:60060") } diff --git a/config/conf.ini b/config/conf.ini index bc90f7408..e79fd3fd4 100644 --- a/config/conf.ini +++ b/config/conf.ini @@ -20,7 +20,7 @@ maxFee = 0.01 [p2p] ;My node ip and port, default is the localhost 127.0.0.1 hostIp = 127.0.0.1 -sourcePort = 60801 +sourcePort = 60081 [tracker] ;Trackers offer such anomymous full node services: monitor node service quality, channel balances if the channel is not private, update routing table for connected nodes, broadcaste transactions, etc. @@ -29,6 +29,6 @@ sourcePort = 60801 ;We suggest you to connect the trackers we deployed for the public: ;https://omnilaboratory.github.io/obd/#/nodes-in-testnet ; -;host = 62.234.216.108:60060 -host = 127.0.0.1:60060 +host = 62.234.216.108:60060 +;host = 127.0.0.1:60060 diff --git a/tracker/config/conf.ini b/tracker/config/conf.ini index ac5297f0f..1a3ce52b7 100644 --- a/tracker/config/conf.ini +++ b/tracker/config/conf.ini @@ -14,5 +14,5 @@ pass = cB3]iL2@eZ1?cB2? [p2p] localHostIp = 127.0.0.1 -sourcePort = 60800 -bootstrapPeers = /ip4/127.0.0.1/tcp/60800/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR \ No newline at end of file +sourcePort = 60080 +;bootstrapPeers = /ip4/127.0.0.1/tcp/60080/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR \ No newline at end of file From 471a9cccba27e54dcfab2f736df3cc5fb5f2c84e Mon Sep 17 00:00:00 2001 From: obdCoder Date: Wed, 30 Dec 2020 10:53:13 +0800 Subject: [PATCH 2/5] relay --- lightclient/p2p_util.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lightclient/p2p_util.go b/lightclient/p2p_util.go index 0da35a708..89c0ccceb 100644 --- a/lightclient/p2p_util.go +++ b/lightclient/p2p_util.go @@ -15,6 +15,7 @@ import ( "github.com/libp2p/go-libp2p-core/peerstore" discovery "github.com/libp2p/go-libp2p-discovery" dht "github.com/libp2p/go-libp2p-kad-dht" + swarm "github.com/libp2p/go-libp2p-swarm" "github.com/multiformats/go-multiaddr" "github.com/omnilaboratory/obd/bean" "github.com/omnilaboratory/obd/config" @@ -97,7 +98,7 @@ func StartP2PNode() (err error) { hostNode.SetStreamHandler(protocolIdForScanObd, handleScanStream) hostNode.SetStreamHandler(protocolIdForBetweenObd, handleStream) - kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAuto)) + kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) if err != nil { log.Println(err) return err @@ -135,6 +136,7 @@ func connP2PNode(dest string) (string, error) { log.Println("wrong dest address") return "", errors.New("wrong dest address") } + ctx := context.Background() destMaddr, err := multiaddr.NewMultiaddr(dest) if err != nil { @@ -157,6 +159,24 @@ 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()) + 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}, + } + + if err := hostNode.Connect(ctx, peerRelayInfo); err != nil { + log.Println(err) + } else { + log.Println("Connection established with RELAY node:", relayaddr) + } + hostNode.Peerstore().AddAddrs(destHostPeerInfo.ID, destHostPeerInfo.Addrs, peerstore.PermanentAddrTTL) stream, err := hostNode.NewStream(context.Background(), destHostPeerInfo.ID, protocolIdForBetweenObd) From 0a34b8a7915112f71df3e068bc165fb5ed19ab15 Mon Sep 17 00:00:00 2001 From: obdCoder Date: Wed, 30 Dec 2020 12:52:38 +0800 Subject: [PATCH 3/5] relay --- go.mod | 2 ++ lightclient/p2p_util.go | 2 ++ tracker/service/p2p_service.go | 5 +++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e9abb602d..d60d2a619 100644 --- a/go.mod +++ b/go.mod @@ -21,9 +21,11 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible github.com/lestrrat-go/strftime v1.0.1 // indirect github.com/libp2p/go-libp2p v0.12.0 + github.com/libp2p/go-libp2p-circuit v0.4.0 github.com/libp2p/go-libp2p-core v0.7.0 github.com/libp2p/go-libp2p-discovery v0.5.0 github.com/libp2p/go-libp2p-kad-dht v0.11.1 + github.com/libp2p/go-libp2p-swarm v0.3.1 github.com/multiformats/go-multiaddr v0.3.1 github.com/satori/go.uuid v1.2.0 github.com/shopspring/decimal v1.2.0 diff --git a/lightclient/p2p_util.go b/lightclient/p2p_util.go index 89c0ccceb..24e728483 100644 --- a/lightclient/p2p_util.go +++ b/lightclient/p2p_util.go @@ -76,6 +76,7 @@ func StartP2PNode() (err error) { hostNode, err = libp2p.New( ctx, libp2p.ListenAddrs(sourceMultiAddr), + libp2p.EnableRelay(), libp2p.Identity(prvKey), ) if err != nil { @@ -173,6 +174,7 @@ func connP2PNode(dest string) (string, error) { if err := hostNode.Connect(ctx, peerRelayInfo); err != nil { log.Println(err) + return "", err } else { log.Println("Connection established with RELAY node:", relayaddr) } diff --git a/tracker/service/p2p_service.go b/tracker/service/p2p_service.go index f96239b88..0775a300f 100644 --- a/tracker/service/p2p_service.go +++ b/tracker/service/p2p_service.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "fmt" "github.com/libp2p/go-libp2p" + circuit "github.com/libp2p/go-libp2p-circuit" "github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" @@ -42,7 +43,7 @@ func StartP2PNode() { ctx, libp2p.ListenAddrs(sourceMultiAddr), libp2p.Identity(prvKey), - libp2p.EnableRelay(), + libp2p.EnableRelay(circuit.OptHop), ) if err != nil { panic(err) @@ -50,7 +51,7 @@ func StartP2PNode() { cfg.P2pLocalAddress = fmt.Sprintf("/ip4/%s/tcp/%v/p2p/%s", cfg.P2P_hostIp, cfg.P2P_sourcePort, hostNode.ID().Pretty()) log.Println("local p2p node address: ", cfg.P2pLocalAddress) - kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) + kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeServer)) if err != nil { panic(err) From 026b2f0fba7f7755560669fe33f359ab5b14ff1f Mon Sep 17 00:00:00 2001 From: obdCoder Date: Wed, 30 Dec 2020 13:00:28 +0800 Subject: [PATCH 4/5] relay --- lightclient/p2p_util.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lightclient/p2p_util.go b/lightclient/p2p_util.go index 24e728483..c329ea706 100644 --- a/lightclient/p2p_util.go +++ b/lightclient/p2p_util.go @@ -99,7 +99,8 @@ func StartP2PNode() (err error) { hostNode.SetStreamHandler(protocolIdForScanObd, handleScanStream) hostNode.SetStreamHandler(protocolIdForBetweenObd, handleStream) - kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) + //kademliaDHT, _ := dht.New(ctx, hostNode, dht.Mode(dht.ModeAutoServer)) + kademliaDHT, _ := dht.New(ctx, hostNode) if err != nil { log.Println(err) return err From 069aafd2eb49f68c9477c744a259dccd2757870d Mon Sep 17 00:00:00 2001 From: obdCoder Date: Wed, 30 Dec 2020 13:52:49 +0800 Subject: [PATCH 5/5] relay --- lightclient/p2p_util.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lightclient/p2p_util.go b/lightclient/p2p_util.go index c329ea706..3dadd54bb 100644 --- a/lightclient/p2p_util.go +++ b/lightclient/p2p_util.go @@ -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 @@ -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() + } } }() } @@ -161,8 +167,7 @@ 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 @@ -170,14 +175,14 @@ func connP2PNode(dest string) (string, error) { 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)