sdk/hints: use a single "hint" type instead of nprofile/nevent/tag/nip05.

This commit is contained in:
fiatjaf 2025-01-18 18:19:54 -03:00
parent 6cffcc3b47
commit 40538486d5
6 changed files with 24 additions and 50 deletions

View File

@ -10,20 +10,14 @@ const (
LastFetchAttempt HintKey = iota
MostRecentEventFetched
LastInRelayList
LastInTag
LastInNprofile
LastInNevent
LastInNIP05
LastInHint
)
var KeyBasePoints = [7]int64{
var KeyBasePoints = [4]int64{
-500, // attempting has negative power because it may fail
700, // when it succeeds that should cancel the negative effect of trying
350, // a relay list is a very strong indicator
5, // tag hints are often autogenerated so we don't care very much about them (that may change)
22, // it feels like people take nprofiles slightly more seriously so we value these a bit more
8, // these are also not often too bad
7, // nip05 hints should be a strong indicator, although in practice they're kinda bad
20, // hints from various sources (tags, nprofile, nevent, nip05)
}
func (hk HintKey) BasePoints() int64 { return KeyBasePoints[hk] }
@ -36,14 +30,8 @@ func (hk HintKey) String() string {
return "most_recent_event_fetched"
case LastInRelayList:
return "last_in_relay_list"
case LastInTag:
return "last_in_tag"
case LastInNprofile:
return "last_in_nprofile"
case LastInNevent:
return "last_in_nevent"
case LastInNIP05:
return "last_in_nip05"
case LastInHint:
return "last_in_hint"
}
return "<unexpected>"
}

View File

@ -114,7 +114,7 @@ type RelaysForPubKey struct {
type RelayEntry struct {
Relay int
Timestamps [7]nostr.Timestamp
Timestamps [4]nostr.Timestamp
}
func (re RelayEntry) Sum() int64 {

View File

@ -23,21 +23,21 @@ func runTestWith(t *testing.T, hdb hints.HintsDB) {
// key1: finding out
// add some random parameters things and see what we get
hdb.Save(key1, relayA, hints.LastInTag, nostr.Now()-60*hour)
hdb.Save(key1, relayA, hints.LastInHint, nostr.Now()-60*hour)
hdb.Save(key1, relayB, hints.LastInRelayList, nostr.Now()-day*10)
hdb.Save(key1, relayB, hints.LastInNevent, nostr.Now()-day*30)
hdb.Save(key1, relayA, hints.LastInNprofile, nostr.Now()-hour*10)
hdb.Save(key1, relayB, hints.LastInHint, nostr.Now()-day*30)
hdb.Save(key1, relayA, hints.LastInHint, nostr.Now()-hour*6)
hdb.PrintScores()
require.Equal(t, []string{relayB, relayA}, hdb.TopN(key1, 3))
hdb.Save(key1, relayA, hints.LastFetchAttempt, nostr.Now()-5*hour)
hdb.Save(key1, relayC, hints.LastInNIP05, nostr.Now()-5*hour)
hdb.Save(key1, relayC, hints.LastInHint, nostr.Now()-5*hour)
hdb.PrintScores()
require.Equal(t, []string{relayB, relayC, relayA}, hdb.TopN(key1, 3))
hdb.Save(key1, relayC, hints.LastInTag, nostr.Now()-5*hour)
hdb.Save(key1, relayA, hints.LastInHint, nostr.Now()-1*hour)
hdb.Save(key1, relayC, hints.LastFetchAttempt, nostr.Now()-5*hour)
hdb.PrintScores()
@ -54,10 +54,7 @@ func runTestWith(t *testing.T, hdb hints.HintsDB) {
hdb.Save(key2, relayB, hints.LastInRelayList, nostr.Now()-day*25)
// but it's old, recently we only see hints for relay C
hdb.Save(key2, relayC, hints.LastInTag, nostr.Now()-5*hour)
hdb.Save(key2, relayC, hints.LastInNIP05, nostr.Now()-5*hour)
hdb.Save(key2, relayC, hints.LastInNevent, nostr.Now()-5*hour)
hdb.Save(key2, relayC, hints.LastInNprofile, nostr.Now()-5*hour)
hdb.Save(key2, relayC, hints.LastInHint, nostr.Now()-4*hour)
// at this point we just barely see C coming first
hdb.PrintScores()
@ -66,9 +63,9 @@ func runTestWith(t *testing.T, hdb hints.HintsDB) {
// yet a different thing for key3
// it doesn't have relay lists published because it's banned everywhere
// all it has are references to its posts from others
hdb.Save(key3, relayA, hints.LastInTag, nostr.Now()-day*2)
hdb.Save(key3, relayB, hints.LastInNevent, nostr.Now()-day)
hdb.Save(key3, relayB, hints.LastInTag, nostr.Now()-day)
hdb.Save(key3, relayA, hints.LastInHint, nostr.Now()-day*2)
hdb.Save(key3, relayB, hints.LastInHint, nostr.Now()-day)
hdb.Save(key3, relayB, hints.LastInHint, nostr.Now()-day)
hdb.PrintScores()
require.Equal(t, []string{relayB, relayA}, hdb.TopN(key3, 3))
@ -88,22 +85,16 @@ func runTestWith(t *testing.T, hdb hints.HintsDB) {
hdb.Save(key4, relayA, hints.LastInRelayList, banDate)
hdb.Save(key4, relayA, hints.LastFetchAttempt, banDate)
hdb.Save(key4, relayA, hints.MostRecentEventFetched, banDate)
hdb.Save(key4, relayA, hints.LastInNprofile, banDate+8*day)
hdb.Save(key4, relayA, hints.LastInNIP05, banDate+5*day)
hdb.Save(key4, relayA, hints.LastInHint, banDate+12*day)
hdb.Save(key4, relayB, hints.LastInRelayList, banDate)
hdb.Save(key4, relayB, hints.LastFetchAttempt, banDate)
hdb.Save(key4, relayB, hints.MostRecentEventFetched, banDate)
hdb.Save(key4, relayB, hints.LastInNevent, banDate+5*day)
hdb.Save(key4, relayB, hints.LastInNIP05, banDate+8*day)
hdb.Save(key4, relayB, hints.LastInNprofile, banDate+5*day)
hdb.Save(key4, relayB, hints.LastInHint, banDate+2*day)
hdb.PrintScores()
require.Equal(t, []string{relayA, relayB}, hdb.TopN(key4, 3))
// information about the new relay starts to spread through relay hints in tags only
hdb.Save(key4, relayC, hints.LastInTag, nostr.Now()-5*day)
hdb.Save(key4, relayC, hints.LastInTag, nostr.Now()-5*day)
hdb.Save(key4, relayC, hints.LastInNevent, nostr.Now()-5*day)
hdb.Save(key4, relayC, hints.LastInNIP05, nostr.Now()-5*day)
hdb.Save(key4, relayC, hints.LastInHint, nostr.Now()-3*day)
// as long as we see one tag hint the new relay will already be in our map
hdb.PrintScores()

View File

@ -3,7 +3,6 @@ package sdk
import (
"context"
"fmt"
"strings"
"sync"
"time"
@ -83,13 +82,9 @@ func (sys System) FetchProfileFromInput(ctx context.Context, nip19OrNip05Code st
return ProfileMetadata{}, fmt.Errorf("couldn't decode profile reference")
}
hintType := hints.LastInNIP05
if strings.HasPrefix(nip19OrNip05Code, "nprofile") {
hintType = hints.LastInNprofile
}
for _, r := range p.Relays {
if !IsVirtualRelay(r) {
sys.Hints.Save(p.PublicKey, nostr.NormalizeURL(r), hintType, nostr.Now())
sys.Hints.Save(p.PublicKey, nostr.NormalizeURL(r), hints.LastInHint, nostr.Now())
}
}

View File

@ -91,7 +91,7 @@ func (sys *System) FetchSpecificEvent(
// (we do this after fetching author outbox relays because we are already going to prioritize these hints)
now := nostr.Now()
for _, relay := range priorityRelays {
sys.Hints.Save(author, nostr.NormalizeURL(relay), hints.LastInNevent, now)
sys.Hints.Save(author, nostr.NormalizeURL(relay), hints.LastInHint, now)
}
// arrange these

View File

@ -82,7 +82,7 @@ func (sys *System) trackEventHints(ie nostr.RelayEvent) {
continue
}
if tag[0] == "p" && nostr.IsValidPublicKey(tag[1]) {
sys.Hints.Save(tag[1], nostr.NormalizeURL(tag[2]), hints.LastInTag, ie.CreatedAt)
sys.Hints.Save(tag[1], nostr.NormalizeURL(tag[2]), hints.LastInHint, ie.CreatedAt)
}
}
default:
@ -100,7 +100,7 @@ func (sys *System) trackEventHints(ie nostr.RelayEvent) {
continue
}
if tag[0] == "p" && nostr.IsValidPublicKey(tag[1]) {
sys.Hints.Save(tag[1], nostr.NormalizeURL(tag[2]), hints.LastInTag, ie.CreatedAt)
sys.Hints.Save(tag[1], nostr.NormalizeURL(tag[2]), hints.LastInHint, ie.CreatedAt)
}
}
@ -114,7 +114,7 @@ func (sys *System) trackEventHints(ie nostr.RelayEvent) {
continue
}
if nostr.IsValidPublicKey(ref.Profile.PublicKey) {
sys.Hints.Save(ref.Profile.PublicKey, nostr.NormalizeURL(relay), hints.LastInNprofile, ie.CreatedAt)
sys.Hints.Save(ref.Profile.PublicKey, nostr.NormalizeURL(relay), hints.LastInHint, ie.CreatedAt)
}
}
} else if ref.Event != nil && nostr.IsValidPublicKey(ref.Event.Author) {
@ -125,7 +125,7 @@ func (sys *System) trackEventHints(ie nostr.RelayEvent) {
if p, err := url.Parse(relay); err != nil || (p.Scheme != "wss" && p.Scheme != "ws") {
continue
}
sys.Hints.Save(ref.Event.Author, nostr.NormalizeURL(relay), hints.LastInNevent, ie.CreatedAt)
sys.Hints.Save(ref.Event.Author, nostr.NormalizeURL(relay), hints.LastInHint, ie.CreatedAt)
}
}
}