Why am I getting a type-error in this libp2p code?

247 Views Asked by At

I'm trying to initialize a connection between two libp2p.Host instances. For this I need to get a peerstore.PeerInfo. The obvious way to do this is to take a multiaddr.Multiaddr instance returned by Host.Addrs and pass it to peerstore.InfoFromP2pAddr.

Simple, right?

Here's some code that does just that:

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
}

The above code will not compile. The compiler returns the following error:

# 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

This appears related to gx's path management, but I'm not exactly sure how. My only interaction with gx was when installing libp2p in the recommended way, namely running make && make deps from the libp2p root, which is semantically equivalent to:

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

What gives? What do I have to do in order to compile my program?

0

There are 0 best solutions below