Skip to content

Commit

Permalink
refactor: transports protocol can have multiple multiaddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Aug 23, 2022
1 parent 08d341e commit 0c4a1cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
13 changes: 6 additions & 7 deletions node/modules/retrieval.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ func NewTransportsListener(cfg *config.Boost) func(h host.Host) (*lp2pimpl.Trans
return func(h host.Host) (*lp2pimpl.TransportsListener, error) {
protos := []types.Protocol{}

// Get the libp2p address from the Host
// Get the libp2p addresses from the Host
if len(h.Addrs()) > 0 {
// TODO: should this be a list of addresses instead?
protos = append(protos, types.Protocol{
Name: "libp2p",
Endpoint: h.Addrs()[0],
Name: "libp2p",
Addresses: h.Addrs(),
})
}

Expand All @@ -36,16 +35,16 @@ func NewTransportsListener(cfg *config.Boost) func(h host.Host) (*lp2pimpl.Trans
}

protos = append(protos, types.Protocol{
Name: "http",
Endpoint: maddr,
Name: "http",
Addresses: []multiaddr.Multiaddr{maddr},
})
}

return lp2pimpl.NewTransportsListener(h, protos), nil
}
}

func HandleRetrievalTransports(lc fx.Lifecycle, l lp2pimpl.TransportsListener) {
func HandleRetrievalTransports(lc fx.Lifecycle, l *lp2pimpl.TransportsListener) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
log.Debug("starting retrieval transports listener")
Expand Down
30 changes: 24 additions & 6 deletions retrievalmarket/types/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
_ "embed"
"fmt"

"github.com/filecoin-project/go-address"
"github.com/ipld/go-ipld-prime/node/bindnode"
bindnoderegistry "github.com/ipld/go-ipld-prime/node/bindnode/registry"
"github.com/multiformats/go-multiaddr"
Expand All @@ -14,7 +13,7 @@ type Protocol struct {
// The name of the transport protocol eg "libp2p" or "http"
Name string
// The address of the endpoint in multiaddr format
Endpoint multiaddr.Multiaddr
Addresses []multiaddr.Multiaddr
}

type QueryResponse struct {
Expand All @@ -24,10 +23,6 @@ type QueryResponse struct {
//go:embed transports.ipldsch
var embedSchema []byte

// MultiAddrBindnodeOption converts a filecoin Address type to and from a Bytes
// field in a schema
var MultiAddrBindnodeOption = bindnode.TypedBytesConverter(&address.Address{}, multiAddrFromBytes, multiAddrToBytes)

func multiAddrFromBytes(b []byte) (interface{}, error) {
return multiaddr.NewMultiaddrBytes(b)
}
Expand All @@ -42,4 +37,27 @@ func multiAddrToBytes(iface interface{}) ([]byte, error) {
return ma.Bytes(), nil
}

// MultiAddrBindnodeOption converts a filecoin Multiaddr type to and from a Bytes
// field in a schema
var dummyMa multiaddr.Multiaddr
var MultiAddrBindnodeOption = bindnode.TypedBytesConverter(&dummyMa, multiAddrFromBytes, multiAddrToBytes)

var bindnodeOptions = []bindnode.Option{
MultiAddrBindnodeOption,
}

var BindnodeRegistry = bindnoderegistry.NewRegistry()

func init() {
for _, r := range []struct {
typ interface{}
typName string
}{
{(*QueryResponse)(nil), "QueryResponse"},
{(*Protocol)(nil), "Protocol"},
} {
if err := BindnodeRegistry.RegisterType(r.typ, string(embedSchema), r.typName, bindnodeOptions...); err != nil {
panic(err.Error())
}
}
}
4 changes: 2 additions & 2 deletions retrievalmarket/types/transports.ipldsch
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
type Protocol struct {
# The name of the transport protocol eg "libp2p" or "http"
Name string
# The address of the endpoint in multiaddr format
Endpoint Multiaddr
# The addresses of the endpoint in multiaddr format
Addresses [Multiaddr]
}

type QueryResponse struct {
Expand Down

0 comments on commit 0c4a1cd

Please sign in to comment.