为什么我在此libp2p代码中遇到类型错误?

时间:2018-10-20 21:03:50

标签: go libp2p go-libp2p

我正在尝试初始化两个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的唯一互动是在recommended way中安装libp2p时,即从libp2p根目录运行make && make deps,这在语义上等效于:

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

有什么作用? 我必须做什么才能编译程序?

0 个答案:

没有答案