Skip to content

Commit

Permalink
fix storeiface, publisher multiaddrs
Browse files Browse the repository at this point in the history
  • Loading branch information
LexLuthr committed Aug 30, 2024
1 parent 563f95c commit 3d82c0c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 19 deletions.
6 changes: 3 additions & 3 deletions deps/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func DefaultCurioConfig() *CurioConfig {
},
Market: MarketConfig{
HTTP: HTTPConfig{
ListenAddress: "0.0.0.0:8888",
ListenAddress: "0.0.0.0:12400",
AnnounceAddresses: []string{},
},
StorageMarketConfig: StorageMarketConfig{
Expand Down Expand Up @@ -666,11 +666,11 @@ type IPNIConfig struct {
}

type HTTPConfig struct {
// ListenAddress is where HTTP server will be listening on
// ListenAddress is where HTTP server will be listening on. Default is "0.0.0.0:12400"
ListenAddress string

// AnnounceAddresses is a list of addresses clients can use to reach to the HTTP market node.
// Curio allows running more than one node for HTTP server and thus all addressed can be announced
// simultaneously to the client
// simultaneously to the client. Example: ["https://mycurio.com", "http://myNewCurio:433/XYZ", "http://1.2.3.4:433"]
AnnounceAddresses []string
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ require (
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/time v0.5.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
Expand Down
35 changes: 31 additions & 4 deletions lib/ipni/ipni-provider/ipni-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"github.com/filecoin-project/curio/lib/storiface"
"github.com/filecoin-project/curio/lib/urltomultiaddr"
"github.com/gorilla/mux"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -40,9 +42,9 @@ import (
"github.com/filecoin-project/curio/lib/pieceprovider"

"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/storage/sealer/storiface"
)

const IPNIRoutePath = "/ipni-provider"
const IPNIPath = "/ipni/v1/ad/"
const ProviderPath = "/ipni-provider"

Expand Down Expand Up @@ -141,7 +143,11 @@ func NewProvider(api ipniAPI, deps *deps.Deps) (*Provider, error) {
var httpServerAddresses []multiaddr.Multiaddr

for _, a := range deps.Cfg.Market.HTTP.AnnounceAddresses {
addr, err := multiaddr.NewMultiaddr(a)
addr, err := urltomultiaddr.UrlToMultiaddr(a)
if err != nil {
return nil, err
}
addr, err = multiaddr.NewMultiaddr(addr.String() + IPNIRoutePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -393,6 +399,9 @@ func (p *Provider) GetEntry(block cid.Cid, provider string) ([]byte, error) {

mis := make(index.MultihashIndexSorted)
err = mis.Load(recs)
if err != nil {
return nil, xerrors.Errorf("failed to load indexed in multihash sorter: %w", err)
}

// To avoid - Cannot assert pinter to interface
idxF := func(sorted *index.MultihashIndexSorted) index.Index {
Expand Down Expand Up @@ -516,7 +525,7 @@ func contentRouter(r *mux.Router, p *Provider) {
}

func Routes(r *mux.Router, p *Provider) {
contentRouter(r.PathPrefix("/ipni-provider").Subrouter(), p)
contentRouter(r.PathPrefix(IPNIRoutePath).Subrouter(), p)
}

func (p *Provider) StartPublishing(ctx context.Context) {
Expand Down Expand Up @@ -572,6 +581,24 @@ func (p *Provider) publishhttp(ctx context.Context, adCid cid.Cid, peer string)
return fmt.Errorf("cannot create http announce sender: %w", err)
}

addrs, err := p.getHTTPAddressForPeer(peer)
if err != nil {
return fmt.Errorf("cannot create provider http addresses: %w", err)
}

log.Infow("Announcing advertisements over HTTP", "urls", p.announceURLs)
return announce.Send(ctx, adCid, p.httpServerAddresses, httpSender)
return announce.Send(ctx, adCid, addrs, httpSender)
}

func (p *Provider) getHTTPAddressForPeer(peer string) ([]multiaddr.Multiaddr, error) {
var ret []multiaddr.Multiaddr
for _, addr := range p.httpServerAddresses {
a, err := multiaddr.NewMultiaddr(addr.String() + "/" + peer)
if err != nil {
return nil, err
}
ret = append(ret, a)
}

return ret, nil
}
13 changes: 3 additions & 10 deletions lib/ipni/ipniculib/ipniculib.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package ipniculib

import (
"crypto/sha256"

"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/datamodel"
"golang.org/x/xerrors"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

func NodeToLink(node datamodel.Node, lp datamodel.LinkPrototype) (datamodel.Link, error) {
hasher := sha256.New()
err := dagjson.Encode(node, hasher)
if err != nil {
return nil, xerrors.Errorf("failed to encode: %w", err)
}
return lp.BuildLink(hasher.Sum(nil)), nil
linkSystem := cidlink.DefaultLinkSystem()
return linkSystem.ComputeLink(lp, node)
}
53 changes: 53 additions & 0 deletions lib/urltomultiaddr/urltomultiaddr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package urltomultiaddr

import (
"errors"
"fmt"
"net"
"net/url"

"github.com/multiformats/go-multiaddr"
)

const protocol = "tcp"

func UrlToMultiaddr(urlStr string) (multiaddr.Multiaddr, error) {
// Parse the URL
parsedURL, err := url.Parse(urlStr)
if err != nil {
return nil, err
}

// Extract the host and port
host := parsedURL.Hostname()
port := parsedURL.Port()
if port == "" {
// Default port based on the scheme
switch parsedURL.Scheme {
case "https":
port = "443"
case "http":
port = "80"
default:
return nil, errors.New("invalid URL scheme")
}
}

// Determine if the host is an IP address or a DNS name
var addrStr string
if ip := net.ParseIP(host); ip != nil {
// It's an IP address, check if it's IPv4 or IPv6
if ip.To4() != nil {
// IPv4
addrStr = fmt.Sprintf("/ip4/%s/%s/%s/%s", ip.String(), protocol, port, parsedURL.Scheme)
} else {
// IPv6
addrStr = fmt.Sprintf("/ip6/%s/%s/%s/%s", ip.String(), protocol, port, parsedURL.Scheme)
}
} else {
// It's a DNS name
addrStr = fmt.Sprintf("/dns/%s/%s/%s/%s", host, protocol, port, parsedURL.Scheme)
}

return multiaddr.NewMultiaddr(addrStr)
}
6 changes: 4 additions & 2 deletions tasks/indexing/task_ipni.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/filecoin-project/curio/lib/storiface"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
carv2 "github.com/ipld/go-car/v2"
Expand All @@ -32,8 +33,6 @@ import (
"github.com/filecoin-project/curio/lib/ipni/ipniculib"
"github.com/filecoin-project/curio/lib/passcall"
"github.com/filecoin-project/curio/lib/pieceprovider"

"github.com/filecoin-project/lotus/storage/sealer/storiface"
)

var ilog = logging.Logger("ipni")
Expand Down Expand Up @@ -145,6 +144,9 @@ func (I *IPNITask) Do(taskID harmonytask.TaskID, stillOwned func() bool) (done b

mis := make(index.MultihashIndexSorted)
err = mis.Load(recs)
if err != nil {
return false, xerrors.Errorf("failed to load indexed in multihash sorter: %w", err)
}

// To avoid - Cannot assert pinter to interface
idxF := func(sorted *index.MultihashIndexSorted) index.Index {
Expand Down

0 comments on commit 3d82c0c

Please sign in to comment.