// Package discovery turns a relay address into a gateway connection. // // A user should not have to know a gateway's public key or its relay list. Given one relay and a // chain name, this asks the relay for gateway announcements (kind 30333), picks the gateway that // serves the chain, and returns a connection URI whose relay list is the gateway's own advertised // relays merged with the seed. A client that dials with that URI is connected to every relay the // gateway announced, so one relay going down is not a single point of failure. // // It is deliberately read-only and needs no identity: an announcement is public, and a relay that // requires NIP-42 for writes still answers a REQ. package discovery import ( "context" "fmt" "strings" "time" "encoding/hex" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/registry" "git.smesh.lol/nostrino/pkg/relay" "git.smesh.lol/nostrino/pkg/tunnel" ) // DefaultCollect is how long to wait for announcements after EOSE. A busy relay answers within a // second; the grace period is what lets several relays be merged. const DefaultCollect = 1500 * time.Millisecond // Lookup finds a gateway for chain among seed relays and returns its connection URI. // // The returned URI carries every relay the announcement names plus the seeds, so the caller gets // the gateway's own redundancy without knowing about it. When several gateways serve the chain, // the one with the most chains (then the newest announcement) wins; callers that want the whole // set can use Announcements and choose for themselves. func Lookup(ctx context.Context, factory func(string) (relay.Transport, error), seeds []string, chainName string, collect time.Duration) (*tunnel.URI, error) { if len(seeds) == 0 { return nil, fmt.Errorf("discovery: no relay to ask") } infos, err := registry.Discover(ctx, factory, seeds, chainName, collect) if err != nil { return nil, fmt.Errorf("discovery: %w", err) } if len(infos) == 0 { return nil, fmt.Errorf("discovery: no gateway for chain %q on %s", chainName, strings.Join(seeds, ", ")) } best := infos[0] for _, info := range infos[1:] { if len(info.Chains) > len(best.Chains) || (len(info.Chains) == len(best.Chains) && info.UpdatedAt > best.UpdatedAt) { best = info } } rawGateway, err := hex.DecodeString(strings.TrimSpace(best.Gateway)) if err != nil || len(rawGateway) != 32 { return nil, fmt.Errorf("discovery: gateway %q is not a 32-byte public key", best.Gateway) } var gateway [32]byte copy(gateway[:], rawGateway) uri := &tunnel.URI{ Gateway: gateway, Relays: mergeRelays(seeds, best.Relays), Chains: best.Chains, Version: 1, } // A gateway that was announced with a websocket bridge keeps it: the client tries that first // and falls back to the relays, which is the same preference the URI itself expresses. if parsed, err := tunnel.ParseURI(best.URI); err == nil { uri.Bridges = parsed.Bridges if len(uri.Chains) == 0 { uri.Chains = parsed.Chains } } // A gateway may serve the chain under another of its names; xbt and blake are one chain, and // refusing the listing because it spells it differently would hide a working gateway. if !containsAny(uri.Chains, chain.Aliases(chainName)) && len(uri.Chains) > 0 { return nil, fmt.Errorf("discovery: %s announces %v, not %q", best.Gateway, uri.Chains, chainName) } return uri, nil } // Announcements returns every announcement found among the seeds, for a caller that wants to // aggregate all the back ends rather than pick one. func Announcements(ctx context.Context, factory func(string) (relay.Transport, error), seeds []string, chainName string, collect time.Duration) ([]registry.Info, error) { return registry.Discover(ctx, factory, seeds, chainName, collect) } // mergeRelays returns the seeds followed by the advertised relays, each normalised and unique. // The seed first is deliberate: it is the relay the caller knows works. func mergeRelays(seeds, advertised []string) []string { out := make([]string, 0, len(seeds)+len(advertised)) seen := make(map[string]struct{}, len(seeds)+len(advertised)) for _, list := range [][]string{seeds, advertised} { for _, raw := range list { relayURL := strings.TrimSpace(raw) if relayURL == "" { continue } relayURL = strings.TrimRight(relayURL, "/") if _, ok := seen[relayURL]; ok { continue } seen[relayURL] = struct{}{} out = append(out, relayURL) } } return out } // contains reports whether values contains want. func contains(values []string, want string) bool { for _, value := range values { if value == want { return true } } return false } func containsAny(have, want []string) bool { for _, name := range want { if contains(have, name) { return true } } return false }