Skip to content

Commit

Permalink
Merge branch 'master' into travis-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
johng committed Dec 30, 2020
2 parents 30e3ede + 069aafd commit f6244c5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
6 changes: 3 additions & 3 deletions config/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
ChainNodeType = "test"
//P2P
P2P_hostIp = "127.0.0.1"
P2P_sourcePort = 4001
P2P_sourcePort = 60801
BootstrapPeers addrList
)

Expand Down Expand Up @@ -56,8 +56,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 {
Expand Down
6 changes: 3 additions & 3 deletions config/conf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pass = omnipass123

[p2p]
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.
Expand All @@ -21,6 +21,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

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion lightclient/p2p_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,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 @@ -75,6 +77,7 @@ func StartP2PNode() (err error) {
hostNode, err = libp2p.New(
ctx,
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.EnableRelay(),
libp2p.Identity(prvKey),
)
if err != nil {
Expand All @@ -97,7 +100,8 @@ 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))
kademliaDHT, _ := dht.New(ctx, hostNode)
if err != nil {
log.Println(err)
return err
Expand All @@ -112,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 All @@ -135,6 +144,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 {
Expand All @@ -157,6 +167,24 @@ func connP2PNode(dest string) (string, error) {
return " Remote peer has been connected", nil
}

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)
return "", 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)
Expand Down
4 changes: 2 additions & 2 deletions tracker/config/conf.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pass = omnipass123

[p2p]
localHostIp = 127.0.0.1
sourcePort = 60800
bootstrapPeers = /ip4/127.0.0.1/tcp/60800/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR
sourcePort = 60080
;bootstrapPeers = /ip4/127.0.0.1/tcp/60080/p2p/QmaBNPR88FMbdjm6UScNRMLUiiu7i6sdbz48jRhC6UmRzR
5 changes: 3 additions & 2 deletions tracker/service/p2p_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -42,15 +43,15 @@ func StartP2PNode() {
ctx,
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
libp2p.EnableRelay(),
libp2p.EnableRelay(circuit.OptHop),
)
if err != nil {
panic(err)
}
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)
Expand Down

0 comments on commit f6244c5

Please sign in to comment.