1

我正在尝试初始化两个libp2p.Host实例之间的连接。为此,我需要获得一个peerstore.PeerInfo. 显而易见的方法是获取由multiaddr.Multiaddr返回的实例Host.Addrs并将其传递给peerstore.InfoFromP2pAddr.

很简单,对吧?

这是一些可以做到这一点的代码:

package main

import (
    "github.com/SentimensRG/ctx"
    "github.com/SentimensRG/ctx/sigctx"

    libp2p "github.com/libp2p/go-libp2p"
    pstore "github.com/libp2p/go-libp2p-peerstore"
)

var c = ctx.AsContext(sigctx.New())

func main() {
    h0, err := libp2p.New(c)
    if err != nil {
        panic(err)
    }

    h1, err := libp2p.New(c)
    if err != nil {
        panic(err)
    }

    addr0 := h0.Addrs()[0]
    peerinfo, _ := pstore.InfoFromP2pAddr(addr0)  # type error here
}

上面的代码不会编译。编译器返回以下错误:

# command-line-arguments
01-basic-conn/main.go:25:39: cannot use addr0 (type "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr".Multiaddr) as type "github.com/multiformats/go-multiaddr".Multiaddr in argument to "github.com/libp2p/go-libp2p-peerstore".InfoFromP2pAddr:
    "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr".Multiaddr does not implement "github.com/multiformats/go-multiaddr".Multiaddr (wrong type for Decapsulate method)
        have Decapsulate("gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr".Multiaddr) "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr".Multiaddr
        want Decapsulate("github.com/multiformats/go-multiaddr".Multiaddr) "github.com/multiformats/go-multiaddr".Multiaddr

这似乎与gx的路径管理有关,但我不确定如何。我唯一的交互gx是在以推荐的方式安装 libp2p 时,即make && make deps从 libp2p 根目录运行,这在语义上等同于:

gx --verbose install --global
gx-go rewrite

是什么赋予了? 为了编译我的程序,我必须做什么?

4

0 回答 0