From 03ddb52b3ce5a1f31bdda56d14e67b5b8bb3293c Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Tue, 20 Mar 2018 18:23:09 +0100 Subject: [PATCH 1/7] [examples] added bootstrap nodes example --- examples/bootstrapping-railing/Readme.md | 9 ++++ examples/bootstrapping-railing/railing.go | 61 +++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 examples/bootstrapping-railing/Readme.md create mode 100644 examples/bootstrapping-railing/railing.go diff --git a/examples/bootstrapping-railing/Readme.md b/examples/bootstrapping-railing/Readme.md new file mode 100644 index 0000000000..3da0e5823f --- /dev/null +++ b/examples/bootstrapping-railing/Readme.md @@ -0,0 +1,9 @@ +# Bootstrapping example with bootstrapping nodes + +This is an example that shows how to connect to a list of bootstrapping nodes. + +## Run the example + +1. Run `make` and `make deps` in the main directory +2. Run `go build` in this directory to build the executable +3. Run `./bootstrapping-railing` in this directory in order to start the programm \ No newline at end of file diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go new file mode 100644 index 0000000000..c62e43b88d --- /dev/null +++ b/examples/bootstrapping-railing/railing.go @@ -0,0 +1,61 @@ +package main + +import ( + "context" + "fmt" + "github.com/libp2p/go-libp2p" + "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore" + "gx/ipfs/QmQViVWBHbU6HmYjXcdNq7tVASCNgdg64ZGcauuDkLCivW/go-ipfs-addr" +) + +var bootstrapPeers = []string{ + "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", + "/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", + "/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", + "/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM", + "/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm", + "/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu", + "/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd", + "/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", + "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", +} + + +//This example show's you how you can connect to a list of bootstrapping nodes. +func main() { + + ctx := context.Background() + + //Create the host + host, err := libp2p.New(ctx, libp2p.Defaults) + if err != nil { + panic(err) + } + + //Loop through the bootstrapping peer list and connect to them + for _, addr := range bootstrapPeers { + + //Parse the string to and address + iAddr, err := ipfsaddr.ParseString(addr) + if err != nil { + panic(err) + } + + //Get peer info from multiaddress + pInfo, err := peerstore.InfoFromP2pAddr(iAddr.Multiaddr()) + if err != nil { + panic(err) + } + + //Connect to the peer by it's peer info + if err := host.Connect(ctx, *pInfo); err != nil { + fmt.Println("failed to connect to peer: ", err) + } + + fmt.Println("connected to peer: ", pInfo.ID.String()) + + } + + //You are now connected to all bootstrapping peer's + fmt.Println("Congratulation's, you are connected to all bootstrapping nodes") +} From 6485cc19bde3806aa6ef89b3657ab0e60923e954 Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Tue, 20 Mar 2018 18:27:41 +0100 Subject: [PATCH 2/7] [bootstrapping-railing] removed gx import path --- examples/bootstrapping-railing/railing.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go index c62e43b88d..ccfa16eedd 100644 --- a/examples/bootstrapping-railing/railing.go +++ b/examples/bootstrapping-railing/railing.go @@ -4,8 +4,8 @@ import ( "context" "fmt" "github.com/libp2p/go-libp2p" - "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore" - "gx/ipfs/QmQViVWBHbU6HmYjXcdNq7tVASCNgdg64ZGcauuDkLCivW/go-ipfs-addr" + "github.com/libp2p/go-libp2p-peerstore" + "github.com/ipfs/go-ipfs-addr" ) var bootstrapPeers = []string{ From c609239233da171f5c6b15ee56df9b100b4de54c Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Wed, 21 Mar 2018 10:47:48 +0100 Subject: [PATCH 3/7] [railing] fixed import paths --- examples/bootstrapping-railing/railing.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go index ccfa16eedd..cf3f38b126 100644 --- a/examples/bootstrapping-railing/railing.go +++ b/examples/bootstrapping-railing/railing.go @@ -3,9 +3,10 @@ package main import ( "context" "fmt" - "github.com/libp2p/go-libp2p" - "github.com/libp2p/go-libp2p-peerstore" - "github.com/ipfs/go-ipfs-addr" + + libp2p "github.com/libp2p/go-libp2p" + peerstore "github.com/libp2p/go-libp2p-peerstore" + ipfsaddr "github.com/ipfs/go-ipfs-addr" ) var bootstrapPeers = []string{ From 7e0d9bb6869f030c13d1339f17f953d9c1a97613 Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Wed, 21 Mar 2018 10:56:52 +0100 Subject: [PATCH 4/7] [railing] added comment about bootstrapping nodes --- examples/bootstrapping-railing/railing.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go index cf3f38b126..916f713a0a 100644 --- a/examples/bootstrapping-railing/railing.go +++ b/examples/bootstrapping-railing/railing.go @@ -9,6 +9,9 @@ import ( ipfsaddr "github.com/ipfs/go-ipfs-addr" ) +//The bootstrapping nodes are just long running nodes with a static IP address. +//That means you can easily have your own bootstrapping nodes. Everything you need is +//a server with a static IP address. var bootstrapPeers = []string{ "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", "/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", @@ -21,7 +24,6 @@ var bootstrapPeers = []string{ "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", } - //This example show's you how you can connect to a list of bootstrapping nodes. func main() { From f2e5930373e33a77b66208e64cff02e32e945223 Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Wed, 21 Mar 2018 11:24:12 +0100 Subject: [PATCH 5/7] [deps] added go-ipfs-addr to dependencies --- package.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package.json b/package.json index d2d817b90b..ce8fa9af15 100644 --- a/package.json +++ b/package.json @@ -293,6 +293,12 @@ "hash": "QmcBWojPoNh4qm7zvv4qiepvCnnc7ALS9qcp7TNwwxT1gT", "name": "go.uuid", "version": "1.1.0" + }, + { + "author": "why", + "hash": "QmQViVWBHbU6HmYjXcdNq7tVASCNgdg64ZGcauuDkLCivW", + "name": "go-ipfs-addr", + "version": "0.1.7" } ], "gxVersion": "0.4.0", From 195f6ab1d2d29cbf03724017e3ef568788f5b5c5 Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Wed, 21 Mar 2018 12:08:57 +0100 Subject: [PATCH 6/7] [railing] changed Connect to be async --- examples/bootstrapping-railing/railing.go | 31 +++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go index 916f713a0a..c77365c1e5 100644 --- a/examples/bootstrapping-railing/railing.go +++ b/examples/bootstrapping-railing/railing.go @@ -35,30 +35,39 @@ func main() { panic(err) } + c := make(chan struct{}) + //Loop through the bootstrapping peer list and connect to them for _, addr := range bootstrapPeers { - - //Parse the string to and address + + //Parse the string to an address iAddr, err := ipfsaddr.ParseString(addr) if err != nil { panic(err) } - + //Get peer info from multiaddress pInfo, err := peerstore.InfoFromP2pAddr(iAddr.Multiaddr()) if err != nil { panic(err) } - - //Connect to the peer by it's peer info - if err := host.Connect(ctx, *pInfo); err != nil { - fmt.Println("failed to connect to peer: ", err) - } - fmt.Println("connected to peer: ", pInfo.ID.String()) + go func() { + //Connect to the peer by it's peer info + if err := host.Connect(ctx, *pInfo); err != nil { + fmt.Println("failed to connect to peer: ", err) + return + } + + fmt.Println("connected to peer: ", pInfo.ID.String()) + + c <- struct{}{} + }() + + } + for i := 0; i < 3; i++ { + <-c } - //You are now connected to all bootstrapping peer's - fmt.Println("Congratulation's, you are connected to all bootstrapping nodes") } From 737e3b5f3624e6ada52e4290bc9e90987356dc70 Mon Sep 17 00:00:00 2001 From: Florian Lenz Date: Wed, 21 Mar 2018 12:25:52 +0100 Subject: [PATCH 7/7] [cs] added white spaces in comments --- examples/bootstrapping-railing/railing.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/bootstrapping-railing/railing.go b/examples/bootstrapping-railing/railing.go index c77365c1e5..28573de7c8 100644 --- a/examples/bootstrapping-railing/railing.go +++ b/examples/bootstrapping-railing/railing.go @@ -3,15 +3,15 @@ package main import ( "context" "fmt" - + libp2p "github.com/libp2p/go-libp2p" - peerstore "github.com/libp2p/go-libp2p-peerstore" ipfsaddr "github.com/ipfs/go-ipfs-addr" + peerstore "github.com/libp2p/go-libp2p-peerstore" ) -//The bootstrapping nodes are just long running nodes with a static IP address. -//That means you can easily have your own bootstrapping nodes. Everything you need is -//a server with a static IP address. +// The bootstrapping nodes are just long running nodes with a static IP address. +// That means you can easily have your own bootstrapping nodes. Everything you need is +// a server with a static IP address. var bootstrapPeers = []string{ "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", "/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", @@ -24,12 +24,12 @@ var bootstrapPeers = []string{ "/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", } -//This example show's you how you can connect to a list of bootstrapping nodes. +// This example show's you how you can connect to a list of bootstrapping nodes. func main() { ctx := context.Background() - //Create the host + // Create the host host, err := libp2p.New(ctx, libp2p.Defaults) if err != nil { panic(err) @@ -37,23 +37,23 @@ func main() { c := make(chan struct{}) - //Loop through the bootstrapping peer list and connect to them + // Loop through the bootstrapping peer list and connect to them for _, addr := range bootstrapPeers { - //Parse the string to an address + // Parse the string to an address iAddr, err := ipfsaddr.ParseString(addr) if err != nil { panic(err) } - //Get peer info from multiaddress + // Get peer info from multiaddress pInfo, err := peerstore.InfoFromP2pAddr(iAddr.Multiaddr()) if err != nil { panic(err) } go func() { - //Connect to the peer by it's peer info + // Connect to the peer by it's peer info if err := host.Connect(ctx, *pInfo); err != nil { fmt.Println("failed to connect to peer: ", err) return