a bunch of [32]byte conversions. still more needed.

This commit is contained in:
fiatjaf
2025-04-14 17:31:23 -03:00
parent 40535e6b19
commit b4268d649c
132 changed files with 857 additions and 879 deletions
+8 -10
View File
@@ -1,14 +1,13 @@
package badgerh
import (
"encoding/hex"
"fmt"
"math"
"slices"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/sdk/hints"
"github.com/dgraph-io/badger/v4"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/sdk/hints"
)
var _ hints.HintsDB = (*BadgerHints)(nil)
@@ -30,7 +29,7 @@ func (bh *BadgerHints) Close() {
bh.db.Close()
}
func (bh *BadgerHints) Save(pubkey string, relay string, hintkey hints.HintKey, ts nostr.Timestamp) {
func (bh *BadgerHints) Save(pubkey nostr.PubKey, relay string, hintkey hints.HintKey, ts nostr.Timestamp) {
if now := nostr.Now(); ts > now {
ts = now
}
@@ -64,7 +63,7 @@ func (bh *BadgerHints) Save(pubkey string, relay string, hintkey hints.HintKey,
}
}
func (bh *BadgerHints) TopN(pubkey string, n int) []string {
func (bh *BadgerHints) TopN(pubkey nostr.PubKey, n int) []string {
type relayScore struct {
relay string
score int64
@@ -73,7 +72,7 @@ func (bh *BadgerHints) TopN(pubkey string, n int) []string {
scores := make([]relayScore, 0, n)
err := bh.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.Prefix, _ = hex.DecodeString(pubkey)
opts.Prefix = pubkey[:]
it := txn.NewIterator(opts)
defer it.Close()
@@ -112,7 +111,7 @@ func (bh *BadgerHints) TopN(pubkey string, n int) []string {
return result
}
func (bh *BadgerHints) GetDetailedScores(pubkey string, n int) []hints.RelayScores {
func (bh *BadgerHints) GetDetailedScores(pubkey nostr.PubKey, n int) []hints.RelayScores {
type relayScore struct {
relay string
tss timestamps
@@ -121,11 +120,10 @@ func (bh *BadgerHints) GetDetailedScores(pubkey string, n int) []hints.RelayScor
scores := make([]relayScore, 0, n)
err := bh.db.View(func(txn *badger.Txn) error {
prefix, _ := hex.DecodeString(pubkey)
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
for it.Seek(pubkey[:]); it.ValidForPrefix(pubkey[:]); it.Next() {
item := it.Item()
k := item.Key()
relay := string(k[32:])
@@ -172,7 +170,7 @@ func (bh *BadgerHints) PrintScores() {
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
var lastPubkey string
var lastPubkey nostr.PubKey
i := 0
for it.Seek(nil); it.Valid(); it.Next() {
+5 -6
View File
@@ -2,20 +2,19 @@ package badgerh
import (
"encoding/binary"
"encoding/hex"
"github.com/nbd-wtf/go-nostr"
"fiatjaf.com/nostr"
)
func encodeKey(pubhintkey, relay string) []byte {
func encodeKey(pubhintkey nostr.PubKey, relay string) []byte {
k := make([]byte, 32+len(relay))
hex.Decode(k[0:32], []byte(pubhintkey))
copy(k[0:32], pubhintkey[:])
copy(k[32:], relay)
return k
}
func parseKey(k []byte) (pubkey string, relay string) {
pubkey = hex.EncodeToString(k[0:32])
func parseKey(k []byte) (pubkey nostr.PubKey, relay string) {
pubkey = [32]byte(k[0:32])
relay = string(k[32:])
return
}
+6 -4
View File
@@ -1,6 +1,8 @@
package hints
import "github.com/nbd-wtf/go-nostr"
import (
"fiatjaf.com/nostr"
)
type RelayScores struct {
Relay string
@@ -9,8 +11,8 @@ type RelayScores struct {
}
type HintsDB interface {
TopN(pubkey string, n int) []string
Save(pubkey string, relay string, key HintKey, score nostr.Timestamp)
TopN(pubkey nostr.PubKey, n int) []string
Save(pubkey nostr.PubKey, relay string, key HintKey, score nostr.Timestamp)
PrintScores()
GetDetailedScores(pubkey string, n int) []RelayScores
GetDetailedScores(pubkey nostr.PubKey, n int) []RelayScores
}
+1 -1
View File
@@ -1,6 +1,6 @@
package hints
import "github.com/nbd-wtf/go-nostr"
import "fiatjaf.com/nostrlib"
const END_OF_WORLD nostr.Timestamp = 2208999600 // 2040-01-01
+2 -2
View File
@@ -6,8 +6,8 @@ import (
"slices"
"sync"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/sdk/hints"
"fiatjaf.com/nostrlib"
"fiatjaf.com/nostrlib/sdk/hints"
)
var _ hints.HintsDB = (*HintDB)(nil)
+2 -2
View File
@@ -7,8 +7,8 @@ import (
"strings"
"github.com/jmoiron/sqlx"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/sdk/hints"
"fiatjaf.com/nostrlib"
"fiatjaf.com/nostrlib/sdk/hints"
)
type SQLHints struct {
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"os"
"testing"
"github.com/nbd-wtf/go-nostr/sdk/hints/sqlh"
"fiatjaf.com/nostrlib/sdk/hints/sqlh"
"github.com/stretchr/testify/require"
_ "github.com/tursodatabase/go-libsql"
)
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/nbd-wtf/go-nostr/sdk/hints/sqlh"
"fiatjaf.com/nostrlib/sdk/hints/sqlh"
"github.com/stretchr/testify/require"
)
+1 -1
View File
@@ -3,7 +3,7 @@ package test
import (
"testing"
"github.com/nbd-wtf/go-nostr/sdk/hints/memoryh"
"fiatjaf.com/nostrlib/sdk/hints/memoryh"
)
func TestMemoryHints(t *testing.T) {
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"os"
"testing"
"github.com/nbd-wtf/go-nostr/sdk/hints/sqlh"
"fiatjaf.com/nostrlib/sdk/hints/sqlh"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
+2 -2
View File
@@ -4,8 +4,8 @@ import (
"testing"
"time"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/sdk/hints"
"fiatjaf.com/nostrlib"
"fiatjaf.com/nostrlib/sdk/hints"
"github.com/stretchr/testify/require"
)
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"os"
"testing"
"github.com/nbd-wtf/go-nostr/sdk/hints/sqlh"
"fiatjaf.com/nostrlib/sdk/hints/sqlh"
_ "github.com/ncruces/go-sqlite3/driver"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/stretchr/testify/require"