mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
fix example test
This commit is contained in:
parent
6d8fdef627
commit
33284724f5
@ -5,17 +5,16 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ipfs/boxo/bootstrap"
|
||||
"github.com/ipfs/boxo/files"
|
||||
"github.com/ipfs/boxo/path"
|
||||
icore "github.com/ipfs/kubo/core/coreiface"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
|
||||
"github.com/ipfs/kubo/config"
|
||||
"github.com/ipfs/kubo/core"
|
||||
@ -59,6 +58,10 @@ func createTempRepo(swarmPort int) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Disable default bootstrap config for this example, since necessary peers are manually connected.
|
||||
// In production, you'd typically keep the default bootstrap peers to join the network.
|
||||
cfg.Bootstrap = []string{}
|
||||
|
||||
// Configure custom ports to avoid conflicts with other IPFS instances.
|
||||
// This demonstrates how to customize the node's network addresses.
|
||||
cfg.Addresses.Swarm = []string{
|
||||
@ -144,40 +147,6 @@ func spawnEphemeral(ctx context.Context, swarmPort int) (icore.CoreAPI, *core.Ip
|
||||
return api, node, err
|
||||
}
|
||||
|
||||
func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) error {
|
||||
var wg sync.WaitGroup
|
||||
peerInfos := make(map[peer.ID]*peer.AddrInfo, len(peers))
|
||||
for _, addrStr := range peers {
|
||||
addr, err := ma.NewMultiaddr(addrStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pii, err := peer.AddrInfoFromP2pAddr(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pi, ok := peerInfos[pii.ID]
|
||||
if !ok {
|
||||
pi = &peer.AddrInfo{ID: pii.ID}
|
||||
peerInfos[pi.ID] = pi
|
||||
}
|
||||
pi.Addrs = append(pi.Addrs, pii.Addrs...)
|
||||
}
|
||||
|
||||
wg.Add(len(peerInfos))
|
||||
for _, peerInfo := range peerInfos {
|
||||
go func(peerInfo *peer.AddrInfo) {
|
||||
defer wg.Done()
|
||||
err := ipfs.Swarm().Connect(ctx, *peerInfo)
|
||||
if err != nil {
|
||||
log.Printf("failed to connect to %s: %s", peerInfo.ID, err)
|
||||
}
|
||||
}(peerInfo)
|
||||
}
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUnixfsNode(path string) (files.Node, error) {
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
@ -224,7 +193,7 @@ func main() {
|
||||
// Spawn a node using a temporary path, creating a temporary repo for the run
|
||||
// Using port 4011 (different from nodeA's port 4010)
|
||||
fmt.Println("Spawning Kubo node on a temporary repo")
|
||||
ipfsB, _, err := spawnEphemeral(ctx, 4011)
|
||||
ipfsB, nodeB, err := spawnEphemeral(ctx, 4011)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to spawn ephemeral node: %s", err))
|
||||
}
|
||||
@ -299,36 +268,12 @@ func main() {
|
||||
|
||||
/// --- Part IV: Getting a file from the IPFS Network
|
||||
|
||||
fmt.Println("\n-- Going to connect to a few nodes in the Network as bootstrappers --")
|
||||
bootstrapPeers := []peer.AddrInfo{nodeA.Peerstore.PeerInfo(nodeA.Identity)}
|
||||
|
||||
// Get nodeA's address so we can fetch the file we added to it
|
||||
peerAddrs, err := ipfsA.Swarm().LocalAddrs(ctx)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not get peer addresses: %s", err))
|
||||
fmt.Println("Bootstrapping to peers")
|
||||
if err := nodeB.Bootstrap(bootstrap.BootstrapConfigWithPeers(bootstrapPeers)); err != nil {
|
||||
panic(fmt.Errorf("failed to bootstrap nodeB: %s", err))
|
||||
}
|
||||
peerMa := peerAddrs[0].String() + "/p2p/" + nodeA.Identity.String()
|
||||
|
||||
bootstrapNodes := []string{
|
||||
// In production, use autoconf.FallbackBootstrapPeers from boxo/autoconf
|
||||
// which includes well-known IPFS bootstrap peers like:
|
||||
// "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
||||
// "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
||||
// "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||
|
||||
// You can add custom peers here. For example, another IPFS node:
|
||||
// "/ip4/192.0.2.1/tcp/4001/p2p/QmYourPeerID...",
|
||||
// "/ip4/192.0.2.1/udp/4001/quic-v1/p2p/QmYourPeerID...",
|
||||
|
||||
// nodeA's address (the peer we created above that has our test file)
|
||||
peerMa,
|
||||
}
|
||||
|
||||
fmt.Println("Connecting to peers...")
|
||||
err = connectToPeers(ctx, ipfsB, bootstrapNodes)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to connect to peers: %s", err))
|
||||
}
|
||||
fmt.Println("Connected to peers")
|
||||
|
||||
exampleCIDStr := peerCidFile.RootCid().String()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user