Skip to content

Commit b42f041

Browse files
chore: remove boxo/util deps
1 parent dbb8d9e commit b42f041

File tree

8 files changed

+78
-43
lines changed

8 files changed

+78
-43
lines changed

dht_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515
"time"
1616

17+
"github.com/libp2p/go-libp2p-kad-dht/internal"
1718
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
1819
"github.com/libp2p/go-libp2p-kad-dht/providers"
1920
"github.com/libp2p/go-libp2p/core/crypto"
@@ -35,7 +36,6 @@ import (
3536
test "github.com/libp2p/go-libp2p-kad-dht/internal/testing"
3637
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
3738

38-
u "github.com/ipfs/boxo/util"
3939
"github.com/ipfs/go-cid"
4040
detectrace "github.com/ipfs/go-detect-race"
4141
kb "github.com/libp2p/go-libp2p-kbucket"
@@ -53,10 +53,10 @@ func init() {
5353
var newCid cid.Cid
5454
switch i % 3 {
5555
case 0:
56-
mhv := u.Hash([]byte(v))
56+
mhv := internal.Hash([]byte(v))
5757
newCid = cid.NewCidV0(mhv)
5858
case 1:
59-
mhv := u.Hash([]byte(v))
59+
mhv := internal.Hash([]byte(v))
6060
newCid = cid.NewCidV1(cid.DagCBOR, mhv)
6161
case 2:
6262
rawMh := make([]byte, 12)
@@ -857,7 +857,7 @@ func TestRefresh(t *testing.T) {
857857
time.Sleep(time.Microsecond * 50)
858858
}
859859

860-
if u.Debug {
860+
if testing.Verbose() {
861861
// the routing tables should be full now. let's inspect them.
862862
printRoutingTables(dhts)
863863
}
@@ -1002,7 +1002,7 @@ func TestPeriodicRefresh(t *testing.T) {
10021002
}
10031003
}
10041004

1005-
if u.Debug {
1005+
if testing.Verbose() {
10061006
printRoutingTables(dhts)
10071007
}
10081008

@@ -1021,7 +1021,7 @@ func TestPeriodicRefresh(t *testing.T) {
10211021
// until the routing tables look better, or some long timeout for the failure case.
10221022
waitForWellFormedTables(t, dhts, 7, 10, 20*time.Second)
10231023

1024-
if u.Debug {
1024+
if testing.Verbose() {
10251025
printRoutingTables(dhts)
10261026
}
10271027
}
@@ -1056,7 +1056,7 @@ func TestProvidesMany(t *testing.T) {
10561056
defer cancel()
10571057
bootstrap(t, ctxT, dhts)
10581058

1059-
if u.Debug {
1059+
if testing.Verbose() {
10601060
// the routing tables should be full now. let's inspect them.
10611061
t.Logf("checking routing table of %d", nDHTs)
10621062
for _, dht := range dhts {

dual/dual_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"testing"
66
"time"
77

8-
u "github.com/ipfs/boxo/util"
98
"github.com/ipfs/go-cid"
109
dht "github.com/libp2p/go-libp2p-kad-dht"
10+
"github.com/libp2p/go-libp2p-kad-dht/internal"
1111
test "github.com/libp2p/go-libp2p-kad-dht/internal/testing"
1212
record "github.com/libp2p/go-libp2p-record"
1313
"github.com/libp2p/go-libp2p/core/host"
@@ -22,8 +22,8 @@ import (
2222
var wancid, lancid cid.Cid
2323

2424
func init() {
25-
wancid = cid.NewCidV1(cid.DagCBOR, u.Hash([]byte("wan cid -- value")))
26-
lancid = cid.NewCidV1(cid.DagCBOR, u.Hash([]byte("lan cid -- value")))
25+
wancid = cid.NewCidV1(cid.DagCBOR, internal.Hash([]byte("wan cid -- value")))
26+
lancid = cid.NewCidV1(cid.DagCBOR, internal.Hash([]byte("lan cid -- value")))
2727
}
2828

2929
type blankValidator struct{}

fullrt/dht.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
swarm "github.com/libp2p/go-libp2p/p2p/net/swarm"
2727

2828
"github.com/gogo/protobuf/proto"
29-
u "github.com/ipfs/boxo/util"
3029
"github.com/ipfs/go-cid"
3130
ds "github.com/ipfs/go-datastore"
3231
dssync "github.com/ipfs/go-datastore/sync"
@@ -53,8 +52,10 @@ import (
5352

5453
var logger = logging.Logger("fullrtdht")
5554

56-
const tracer = tracing.Tracer("go-libp2p-kad-dht/fullrt")
57-
const dhtName = "FullRT"
55+
const (
56+
tracer = tracing.Tracer("go-libp2p-kad-dht/fullrt")
57+
dhtName = "FullRT"
58+
)
5859

5960
const rtRefreshLimitsMsg = `Accelerated DHT client was unable to fully refresh its routing table due to Resource Manager limits, which may degrade content routing. Consider increasing resource limits. See debug logs for the "dht-crawler" subsystem for details.`
6061

@@ -530,7 +531,7 @@ func (dht *FullRT) PutValue(ctx context.Context, key string, value []byte, opts
530531
}
531532

532533
rec := record.MakePutRecord(key, value)
533-
rec.TimeReceived = u.FormatRFC3339(time.Now())
534+
rec.TimeReceived = internal.FormatRFC3339(time.Now())
534535
err = dht.putLocal(ctx, key, rec)
535536
if err != nil {
536537
return err
@@ -656,7 +657,8 @@ func (dht *FullRT) SearchValue(ctx context.Context, key string, opts ...routing.
656657
}
657658

658659
func (dht *FullRT) searchValueQuorum(ctx context.Context, key string, valCh <-chan RecvdVal, stopCh chan struct{},
659-
out chan<- []byte, nvals int) ([]byte, map[peer.ID]struct{}, bool) {
660+
out chan<- []byte, nvals int,
661+
) ([]byte, map[peer.ID]struct{}, bool) {
660662
numResponses := 0
661663
return dht.processValues(ctx, key, valCh,
662664
func(ctx context.Context, v RecvdVal, better bool) bool {
@@ -678,7 +680,8 @@ func (dht *FullRT) searchValueQuorum(ctx context.Context, key string, valCh <-ch
678680
}
679681

680682
func (dht *FullRT) processValues(ctx context.Context, key string, vals <-chan RecvdVal,
681-
newVal func(ctx context.Context, v RecvdVal, better bool) bool) (best []byte, peersWithBest map[peer.ID]struct{}, aborted bool) {
683+
newVal func(ctx context.Context, v RecvdVal, better bool) bool,
684+
) (best []byte, peersWithBest map[peer.ID]struct{}, aborted bool) {
682685
loop:
683686
for {
684687
if aborted {

handlers.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
pstore "github.com/libp2p/go-libp2p/p2p/host/peerstore"
1212

1313
"github.com/gogo/protobuf/proto"
14-
u "github.com/ipfs/boxo/util"
1514
ds "github.com/ipfs/go-datastore"
1615
"github.com/libp2p/go-libp2p-kad-dht/internal"
1716
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
@@ -115,7 +114,7 @@ func (dht *IpfsDHT) checkLocalDatastore(ctx context.Context, k []byte) (*recpb.R
115114
}
116115

117116
var recordIsBad bool
118-
recvtime, err := u.ParseRFC3339(rec.GetTimeReceived())
117+
recvtime, err := internal.ParseRFC3339(rec.GetTimeReceived())
119118
if err != nil {
120119
logger.Info("either no receive time set on record, or it was invalid: ", err)
121120
recordIsBad = true
@@ -206,7 +205,7 @@ func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Mess
206205
}
207206

208207
// record the time we receive every record
209-
rec.TimeReceived = u.FormatRFC3339(time.Now())
208+
rec.TimeReceived = internal.FormatRFC3339(time.Now())
210209

211210
data, err := proto.Marshal(rec)
212211
if err != nil {

internal/util.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package internal
2+
3+
import (
4+
"time"
5+
6+
mh "github.com/multiformats/go-multihash"
7+
)
8+
9+
// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
10+
func Hash(data []byte) mh.Multihash {
11+
h, err := mh.Sum(data, mh.SHA2_256, -1)
12+
if err != nil {
13+
// this error can be safely ignored (panic) because multihash only fails
14+
// from the selection of hash function. If the fn + length are valid, it
15+
// won't error.
16+
panic("multihash failed to hash using SHA2_256.")
17+
}
18+
return h
19+
}
20+
21+
// ParseRFC3339 parses an RFC3339Nano-formatted time stamp and
22+
// returns the UTC time.
23+
func ParseRFC3339(s string) (time.Time, error) {
24+
t, err := time.Parse(time.RFC3339Nano, s)
25+
if err != nil {
26+
return time.Time{}, err
27+
}
28+
return t.UTC(), nil
29+
}
30+
31+
// FormatRFC3339 returns the string representation of the
32+
// UTC value of the given time in RFC3339Nano format.
33+
func FormatRFC3339(t time.Time) string {
34+
return t.UTC().Format(time.RFC3339Nano)
35+
}

providers/providers_manager_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/libp2p/go-libp2p-kad-dht/internal"
1112
"github.com/libp2p/go-libp2p/core/peer"
1213
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
1314

1415
mh "github.com/multiformats/go-multihash"
1516

16-
u "github.com/ipfs/boxo/util"
1717
ds "github.com/ipfs/go-datastore"
1818
dsq "github.com/ipfs/go-datastore/query"
1919
dssync "github.com/ipfs/go-datastore/sync"
@@ -35,7 +35,7 @@ func TestProviderManager(t *testing.T) {
3535
if err != nil {
3636
t.Fatal(err)
3737
}
38-
a := u.Hash([]byte("test"))
38+
a := internal.Hash([]byte("test"))
3939
p.AddProvider(ctx, a, peer.AddrInfo{ID: peer.ID("testingprovider")})
4040

4141
// Not cached
@@ -86,7 +86,7 @@ func TestProvidersDatastore(t *testing.T) {
8686
friend := peer.ID("friend")
8787
var mhs []mh.Multihash
8888
for i := 0; i < 100; i++ {
89-
h := u.Hash([]byte(fmt.Sprint(i)))
89+
h := internal.Hash([]byte(fmt.Sprint(i)))
9090
mhs = append(mhs, h)
9191
p.AddProvider(ctx, h, peer.AddrInfo{ID: friend})
9292
}
@@ -105,7 +105,7 @@ func TestProvidersDatastore(t *testing.T) {
105105
func TestProvidersSerialization(t *testing.T) {
106106
dstore := dssync.MutexWrap(ds.NewMapDatastore())
107107

108-
k := u.Hash(([]byte("my key!")))
108+
k := internal.Hash(([]byte("my key!")))
109109
p1 := peer.ID("peer one")
110110
p2 := peer.ID("peer two")
111111
pt1 := time.Now()
@@ -174,7 +174,7 @@ func TestProvidesExpire(t *testing.T) {
174174
peers := []peer.ID{"a", "b"}
175175
var mhs []mh.Multihash
176176
for i := 0; i < 10; i++ {
177-
h := u.Hash([]byte(fmt.Sprint(i)))
177+
h := internal.Hash([]byte(fmt.Sprint(i)))
178178
mhs = append(mhs, h)
179179
}
180180

@@ -235,8 +235,10 @@ func TestProvidesExpire(t *testing.T) {
235235
}
236236
}
237237

238-
var _ = io.NopCloser
239-
var _ = os.DevNull
238+
var (
239+
_ = io.NopCloser
240+
_ = os.DevNull
241+
)
240242

241243
// TestLargeProvidersSet can be used for profiling.
242244
// The datastore can be switched to levelDB by uncommenting the section below and the import above
@@ -286,7 +288,7 @@ func TestLargeProvidersSet(t *testing.T) {
286288

287289
var mhs []mh.Multihash
288290
for i := 0; i < 1000; i++ {
289-
h := u.Hash([]byte(fmt.Sprint(i)))
291+
h := internal.Hash([]byte(fmt.Sprint(i)))
290292
mhs = append(mhs, h)
291293
for _, pid := range peers {
292294
p.AddProvider(ctx, h, peer.AddrInfo{ID: pid})
@@ -311,8 +313,8 @@ func TestUponCacheMissProvidersAreReadFromDatastore(t *testing.T) {
311313
defer cancel()
312314

313315
p1, p2 := peer.ID("a"), peer.ID("b")
314-
h1 := u.Hash([]byte("1"))
315-
h2 := u.Hash([]byte("2"))
316+
h1 := internal.Hash([]byte("1"))
317+
h2 := internal.Hash([]byte("2"))
316318
ps, err := pstoremem.NewPeerstore()
317319
if err != nil {
318320
t.Fatal(err)
@@ -341,7 +343,7 @@ func TestWriteUpdatesCache(t *testing.T) {
341343
defer cancel()
342344

343345
p1, p2 := peer.ID("a"), peer.ID("b")
344-
h1 := u.Hash([]byte("1"))
346+
h1 := internal.Hash([]byte("1"))
345347
ps, err := pstoremem.NewPeerstore()
346348
if err != nil {
347349
t.Fatal(err)

records_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/libp2p/go-libp2p-kad-dht/internal"
1011
"github.com/libp2p/go-libp2p/core/test"
1112

12-
u "github.com/ipfs/boxo/util"
1313
"github.com/ipfs/go-test/random"
1414
record "github.com/libp2p/go-libp2p-record"
1515
tnet "github.com/libp2p/go-libp2p-testing/net"
@@ -205,7 +205,7 @@ func TestPubkeyBadKeyFromDHT(t *testing.T) {
205205

206206
// Store incorrect public key on node B
207207
rec := record.MakePutRecord(pkkey, wrongbytes)
208-
rec.TimeReceived = u.FormatRFC3339(time.Now())
208+
rec.TimeReceived = internal.FormatRFC3339(time.Now())
209209
err = dhtB.putLocal(ctx, pkkey, rec)
210210
if err != nil {
211211
t.Fatal(err)
@@ -244,7 +244,7 @@ func TestPubkeyBadKeyFromDHTGoodKeyDirect(t *testing.T) {
244244

245245
// Store incorrect public key on node B
246246
rec := record.MakePutRecord(pkkey, wrongbytes)
247-
rec.TimeReceived = u.FormatRFC3339(time.Now())
247+
rec.TimeReceived = internal.FormatRFC3339(time.Now())
248248
err = dhtB.putLocal(ctx, pkkey, rec)
249249
if err != nil {
250250
t.Fatal(err)
@@ -317,9 +317,7 @@ func TestValuesDisabled(t *testing.T) {
317317
ctx, cancel := context.WithCancel(context.Background())
318318
defer cancel()
319319

320-
var (
321-
optsA, optsB []Option
322-
)
320+
var optsA, optsB []Option
323321
optsA = append(optsA, ProtocolPrefix("/valuesMaybeDisabled"))
324322
optsB = append(optsB, ProtocolPrefix("/valuesMaybeDisabled"))
325323

routing.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"go.opentelemetry.io/otel/attribute"
1515
"go.opentelemetry.io/otel/trace"
1616

17-
u "github.com/ipfs/boxo/util"
1817
"github.com/ipfs/go-cid"
1918
"github.com/libp2p/go-libp2p-kad-dht/internal"
2019
internalConfig "github.com/libp2p/go-libp2p-kad-dht/internal/config"
@@ -65,7 +64,7 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key string, value []byte, opts
6564
}
6665

6766
rec := record.MakePutRecord(key, value)
68-
rec.TimeReceived = u.FormatRFC3339(time.Now())
67+
rec.TimeReceived = internal.FormatRFC3339(time.Now())
6968
err = dht.putLocal(ctx, key, rec)
7069
if err != nil {
7170
return err
@@ -195,7 +194,8 @@ func (dht *IpfsDHT) SearchValue(ctx context.Context, key string, opts ...routing
195194
}
196195

197196
func (dht *IpfsDHT) searchValueQuorum(ctx context.Context, key string, valCh <-chan recvdVal, stopCh chan struct{},
198-
out chan<- []byte, nvals int) ([]byte, map[peer.ID]struct{}, bool) {
197+
out chan<- []byte, nvals int,
198+
) ([]byte, map[peer.ID]struct{}, bool) {
199199
numResponses := 0
200200
return dht.processValues(ctx, key, valCh,
201201
func(ctx context.Context, v recvdVal, better bool) bool {
@@ -217,7 +217,8 @@ func (dht *IpfsDHT) searchValueQuorum(ctx context.Context, key string, valCh <-c
217217
}
218218

219219
func (dht *IpfsDHT) processValues(ctx context.Context, key string, vals <-chan recvdVal,
220-
newVal func(ctx context.Context, v recvdVal, better bool) bool) (best []byte, peersWithBest map[peer.ID]struct{}, aborted bool) {
220+
newVal func(ctx context.Context, v recvdVal, better bool) bool,
221+
) (best []byte, peersWithBest map[peer.ID]struct{}, aborted bool) {
221222
loop:
222223
for {
223224
if aborted {
@@ -357,7 +358,6 @@ func (dht *IpfsDHT) getValues(ctx context.Context, key string, stopQuery chan st
357358
}
358359
},
359360
)
360-
361361
if err != nil {
362362
return
363363
}
@@ -568,7 +568,6 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key multihash
568568

569569
lookupRes, err := dht.runLookupWithFollowup(ctx, string(key),
570570
func(ctx context.Context, p peer.ID) ([]*peer.AddrInfo, error) {
571-
572571
// For DHT query command
573572
routing.PublishQueryEvent(ctx, &routing.QueryEvent{
574573
Type: routing.SendingQuery,
@@ -670,7 +669,6 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pi peer.AddrInfo,
670669
return hasValidConnectedness(dht.host, id)
671670
},
672671
)
673-
674672
if err != nil {
675673
return peer.AddrInfo{}, err
676674
}

0 commit comments

Comments
 (0)