Skip to content

Commit

Permalink
fix: don't use heyfil for peer IDs
Browse files Browse the repository at this point in the history
disabled in code, but can still be done in the heyfil package
  • Loading branch information
rvagg committed Oct 2, 2023
1 parent 747a7c9 commit ab11c2b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 80 deletions.
14 changes: 7 additions & 7 deletions cmd/lassie/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ var FlagAllowProviders = &cli.StringFlag{
DefaultText: "Providers will be discovered automatically",
Usage: "Comma-separated addresses of providers, to use instead of " +
"automatic discovery. Accepts full multiaddrs including peer ID, " +
"multiaddrs without peer ID and url-style addresses for HTTP, Filecoin " +
"SP f0 actor addresses, and Filecoin peer IDs. Lassie will attempt to " +
"connect to the peer(s). " +
"Example: " +
"multiaddrs without peer ID and url-style addresses for HTTP and " +
"Filecoin SP f0 actor addresses. Lassie will attempt to connect to the " +
"peer(s). Example: " +
"/ip4/1.2.3.4/tcp/1234/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4,http://ipfs.io,f01234",
EnvVars: []string{"LASSIE_ALLOW_PROVIDERS"},
Action: func(cctx *cli.Context, v string) error {
Expand All @@ -137,9 +136,10 @@ var FlagAllowProviders = &cli.StringFlag{
return nil
}

// in case we have been given peer IDs or filecoin actor addresses we can
// look-up with heyfil, do it, otherwise this is a pass-through
trans, err := heyfil.TranslateAll(strings.Split(v, ","))
// in case we have been given filecoin actor addresses we can look them up
// with heyfil and translate to full multiaddrs, otherwise this is a
// pass-through
trans, err := heyfil.Heyfil{TranslateFaddr: true}.TranslateAll(strings.Split(v, ","))
if err != nil {
return err
}
Expand Down
35 changes: 14 additions & 21 deletions pkg/heyfil/heyfil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func isFilecoinFaddr(s string) bool {
return false
}

func CanHeyfil(s string) bool {
return !strings.Contains(s, "/") && (isPeerId(s) || isFilecoinFaddr(s))
type Heyfil struct {
Endpoint string
TranslatePeerId bool
TranslateFaddr bool
}

type Heyfil struct {
Endpoint string
func (h Heyfil) CanTranslate(s string) bool {
return !strings.Contains(s, "/") && ((h.TranslatePeerId && isPeerId(s)) || (h.TranslateFaddr && isFilecoinFaddr(s)))
}

func (h Heyfil) endpoint() string {
Expand All @@ -49,20 +51,6 @@ func (h Heyfil) endpoint() string {
return h.Endpoint
}

// TranslateAll performs a Translate on all strings in the input slice. If none
// of the strings can be translated, the input slice is returned as-is.
func TranslateAll(ss []string) ([]string, error) {
return Heyfil{}.TranslateAll(ss)
}

// Translate will translate an input string to a full multiaddr if the string
// appears to be a Filecoin SP actor address or a peer id using the Heyfil
// service. If the input string is not a Filecoin SP actor address or a peer id,
// it will be returned as-is.
func Translate(s string) (string, error) {
return Heyfil{}.Translate(s)
}

// TranslateAll performs a Translate on all strings in the input slice. If none
// of the strings can be translated, the input slice is returned as-is.
func (h Heyfil) TranslateAll(ss []string) ([]string, error) {
Expand All @@ -71,7 +59,7 @@ func (h Heyfil) TranslateAll(ss []string) ([]string, error) {
var translatedLk sync.Mutex
var wg sync.WaitGroup
for ii, s := range ss {
if !CanHeyfil(s) {
if !h.CanTranslate(s) {
translated[ii] = s
continue
}
Expand Down Expand Up @@ -102,15 +90,15 @@ func (h Heyfil) TranslateAll(ss []string) ([]string, error) {
// service. If the input string is not a Filecoin SP actor address or a peer id,
// it will be returned as-is.
func (h Heyfil) Translate(s string) (string, error) {
if isPeerId(s) {
if h.TranslatePeerId && isPeerId(s) {
res, err := h.translatePeerId(s)
if err != nil {
logger.Debugw("failed to translate peer id", "input", s, "err", err)
return "", err
}
return res, nil
}
if isFilecoinFaddr(s) {
if h.TranslateFaddr && isFilecoinFaddr(s) {
res, err := h.translateFaddr(s)
if err != nil {
logger.Debugw("failed to translate faddr", "input", s, "err", err)
Expand Down Expand Up @@ -155,6 +143,11 @@ func (h Heyfil) translateFaddr(s string) (string, error) {
if len(addrs) == 0 {
return "", fmt.Errorf("expected addr_info.Addrs to be non-empty")
}
// TODO: only using the first multiaddr for now, this could be expanded to use
// all of them, either as separate addrs in the translated string, or, we could
// allow a translate that spits out a full AddrInfo that maps directly to the
// value here. TranslateAll([]string) ([]string, error) could become
// TranslateAll([]string) ([]AddrInfo, []string, error)?
addr, ok := addrs[0].(string)
if !ok {
return "", fmt.Errorf("expected addr_info.Addrs[0] to be string, got %T", addrs[0])
Expand Down
119 changes: 70 additions & 49 deletions pkg/heyfil/heyfil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,30 @@ import (

func TestCanHeyfil(t *testing.T) {
testCase := []struct {
name string
input string
can bool
name string
input string
canWithPeerID bool
canWithFaddr bool
}{
{"empty", "", false},
{"faddr", "f01234", true},
{"faddr non miner", "f1234", false},
{"bad faddr", "f00cafebeef", false},
{"p2p", "12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", true},
{"cid p2p", "QmUA9D3H7HeCYsirB3KmPSvZh3dNXMZas6Lwgr4fv1HTTp", true},
{"cidv1 p2p", "bafzbeicwot2npbkuyjppqaoibbohqemn5dnbidt66mjdfso725vm4lmmlm", true},
{"cidv1 not p2p", "bafybeicwot2npbkuyjppqaoibbohqemn5dnbidt66mjdfso725vm4lmmlm", false},
{"multiaddr", "/dns4/dag.w3s.link/tcp/443/https", false},
{"http addr", "http://dag.w3s.link:443", false},
{"multiaddr long", "/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", false},
{"multiaddr ip4", "/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA", false},
{"empty", "", false, false},
{"faddr", "f01234", false, true},
{"faddr non miner", "f1234", false, false},
{"bad faddr", "f00cafebeef", false, false},
{"p2p", "12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", true, false},
{"cid p2p", "QmUA9D3H7HeCYsirB3KmPSvZh3dNXMZas6Lwgr4fv1HTTp", true, false},
{"cidv1 p2p", "bafzbeicwot2npbkuyjppqaoibbohqemn5dnbidt66mjdfso725vm4lmmlm", true, false},
{"cidv1 not p2p", "bafybeicwot2npbkuyjppqaoibbohqemn5dnbidt66mjdfso725vm4lmmlm", false, false},
{"multiaddr", "/dns4/dag.w3s.link/tcp/443/https", false, false},
{"http addr", "http://dag.w3s.link:443", false, false},
{"multiaddr long", "/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", false, false},
{"multiaddr ip4", "/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA", false, false},
}

for _, tc := range testCase {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, heyfil.CanHeyfil(tc.input), tc.can)
require.False(t, heyfil.Heyfil{}.CanTranslate(tc.input))
require.Equal(t, heyfil.Heyfil{TranslatePeerId: true}.CanTranslate(tc.input), tc.canWithPeerID)
require.Equal(t, heyfil.Heyfil{TranslateFaddr: true}.CanTranslate(tc.input), tc.canWithFaddr)
})
}
}
Expand All @@ -42,10 +45,16 @@ func TestHeyfil(t *testing.T) {

trans, err := heyfil.Heyfil{Endpoint: ts.URL}.Translate("12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ")
require.NoError(t, err)
require.Equal(t, "12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", trans)
trans, err = heyfil.Heyfil{Endpoint: ts.URL, TranslatePeerId: true}.Translate("12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ")
require.NoError(t, err)
require.Equal(t, "/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", trans)

trans, err = heyfil.Heyfil{Endpoint: ts.URL}.Translate("f0127896")
require.NoError(t, err)
require.Equal(t, "f0127896", trans)
trans, err = heyfil.Heyfil{Endpoint: ts.URL, TranslateFaddr: true}.Translate("f0127896")
require.NoError(t, err)
require.Equal(t, "/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", trans)

// no translation, pass-through
Expand All @@ -56,7 +65,7 @@ func TestHeyfil(t *testing.T) {
"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA",
"WOT?", // this is an error for another layer ...
} {
trans, err := heyfil.Heyfil{Endpoint: ts.URL}.Translate(inp)
trans, err := heyfil.Heyfil{Endpoint: ts.URL, TranslatePeerId: true, TranslateFaddr: true}.Translate(inp)
require.NoError(t, err)
require.Equal(t, trans, inp)
}
Expand All @@ -66,48 +75,60 @@ func TestHeyfilTranslateAll(t *testing.T) {
ts := newHeyfilServer()
defer ts.Close()

input := []string{
"/dns4/dag.w3s.link/tcp/443/https",
"http://dag.w3s.link:443",
"12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ",
"/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA",
"f0127896",
"WOT?", // this is an error for another layer ...
testData := []struct {
addr string
want string
ispeerid bool
isfaddr bool
}{
{"/dns4/dag.w3s.link/tcp/443/https", "/dns4/dag.w3s.link/tcp/443/https", false, false},
{"http://dag.w3s.link:443", "http://dag.w3s.link:443", false, false},
{"12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", "/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", true, false},
{"/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", "/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4", false, false},
{"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA", "/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA", false, false},
{"f0127896", "/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ", false, true},
{"WOT?", "WOT?", false, false}, // this is an error for another layer ..."WOT?"
}
expected := []string{
"/dns4/dag.w3s.link/tcp/443/https",
"http://dag.w3s.link:443",
"/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ",
"/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA",
"/ip4/85.11.148.122/tcp/24001/p2p/12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ",
"WOT?",
input := make([]string, len(testData))
for ii, td := range testData {
input[ii] = td.addr
}

trans, err := heyfil.Heyfil{Endpoint: ts.URL}.TranslateAll(input)
require.NoError(t, err)
require.Equal(t, trans, expected)
for ii, td := range testData {
want := td.want
if td.isfaddr || td.ispeerid {
want = td.addr
}
require.Equal(t, trans[ii], want)
}

// same but nothing to translate, make sure we can pass through without even trying
input = []string{
"/dns4/dag.w3s.link/tcp/443/https",
"http://dag.w3s.link:443",
"/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA",
"WOT?", // this is an error for another layer ...
trans, err = heyfil.Heyfil{Endpoint: ts.URL, TranslatePeerId: true}.TranslateAll(input)
require.NoError(t, err)
for ii, td := range testData {
want := td.want
if td.isfaddr {
want = td.addr
}
require.Equal(t, trans[ii], want)
}
expected = []string{
"/dns4/dag.w3s.link/tcp/443/https",
"http://dag.w3s.link:443",
"/dns4/dag.w3s.link/tcp/443/https/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
"/ip4/127.0.0.1/tcp/5000/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA",
"WOT?",

trans, err = heyfil.Heyfil{Endpoint: ts.URL, TranslateFaddr: true}.TranslateAll(input)
require.NoError(t, err)
for ii, td := range testData {
want := td.want
if td.ispeerid {
want = td.addr
}
require.Equal(t, trans[ii], want)
}

trans, err = heyfil.Heyfil{Endpoint: ts.URL}.TranslateAll(input)
trans, err = heyfil.Heyfil{Endpoint: ts.URL, TranslateFaddr: true, TranslatePeerId: true}.TranslateAll(input)
require.NoError(t, err)
require.Equal(t, trans, expected)
for ii, td := range testData {
require.Equal(t, trans[ii], td.want)
}
}

func newHeyfilServer() *httptest.Server {
Expand Down
7 changes: 4 additions & 3 deletions pkg/server/http/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ func parseProtocols(req *http.Request) ([]multicodec.Code, error) {

func parseProviders(req *http.Request) ([]peer.AddrInfo, error) {
if req.URL.Query().Has("providers") {
// in case we have been given peer IDs or filecoin actor addresses we can
// look-up with heyfil, do it, otherwise this is a pass-through
trans, err := heyfil.TranslateAll(strings.Split(req.URL.Query().Get("providers"), ","))
// in case we have been given filecoin actor addresses we can look them up
// with heyfil and translate to full multiaddrs, otherwise this is a
// pass-through
trans, err := heyfil.Heyfil{TranslateFaddr: true}.TranslateAll(strings.Split(req.URL.Query().Get("providers"), ","))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ab11c2b

Please sign in to comment.