Skip to content

Commit

Permalink
pass throu
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 15, 2024
1 parent c645374 commit 5719b31
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/CAFxX/httpcompression v0.0.9
github.com/felixge/httpsnoop v1.0.4
github.com/ipfs/boxo v0.17.1-0.20240206103407-342b01d27828
github.com/ipfs/boxo v0.17.1-0.20240215095212-e576b559674a
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-log/v2 v2.5.1
github.com/libp2p/go-libp2p v0.32.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ipfs/boxo v0.17.1-0.20240206103407-342b01d27828 h1:WbZEHuIOqAMb2I67+j6S7uKnPPWBsts09uyec6S2gzY=
github.com/ipfs/boxo v0.17.1-0.20240206103407-342b01d27828/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/boxo v0.17.1-0.20240215095212-e576b559674a h1:44Bfcj6uzx6FOkpJX4qAmSwQy2JaMqpzPNCO7GsuzHM=
github.com/ipfs/boxo v0.17.1-0.20240215095212-e576b559674a/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
Expand Down
52 changes: 38 additions & 14 deletions server_routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type router interface {

type providersRouter interface {
FindProviders(ctx context.Context, cid cid.Cid, limit int) (iter.ResultIter[types.Record], error)
Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error)
Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error)
}

type peersRouter interface {
FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error)
ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error)
ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error)
}

type ipnsRouter interface {
Expand All @@ -53,9 +53,9 @@ func (r composableRouter) FindProviders(ctx context.Context, key cid.Cid, limit
return r.providers.FindProviders(ctx, key, limit)
}

func (r composableRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
func (r composableRouter) Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
if r.providers == nil {
return req.TTL, nil
return 0, nil
}
return r.providers.Provide(ctx, req)

Check warning on line 60 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L56-L60

Added lines #L56 - L60 were not covered by tests
}
Expand All @@ -67,9 +67,9 @@ func (r composableRouter) FindPeers(ctx context.Context, pid peer.ID, limit int)
return r.peers.FindPeers(ctx, pid, limit)
}

func (r composableRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
func (r composableRouter) ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
if r.peers == nil {
return req.TTL, nil
return 0, nil
}
return r.peers.ProvidePeer(ctx, req)

Check warning on line 74 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L70-L74

Added lines #L70 - L74 were not covered by tests
}
Expand Down Expand Up @@ -218,13 +218,13 @@ func (mi *manyIter[T]) Close() error {
return err
}

func (r parallelRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
func (r parallelRouter) Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
return provide(ctx, r.routers, func(ctx context.Context, r router) (time.Duration, error) {
return r.Provide(ctx, req)
})

Check warning on line 224 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L221-L224

Added lines #L221 - L224 were not covered by tests
}

func (r parallelRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
func (r parallelRouter) ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
return provide(ctx, r.routers, func(ctx context.Context, r router) (time.Duration, error) {
return r.ProvidePeer(ctx, req)
})

Check warning on line 230 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L227-L230

Added lines #L227 - L230 were not covered by tests
Expand Down Expand Up @@ -374,7 +374,9 @@ func (d libp2pRouter) FindProviders(ctx context.Context, key cid.Cid, limit int)
}), nil
}

func (d libp2pRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
func (d libp2pRouter) Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
// NOTE: this router cannot provide further to the DHT, since we can only
// announce CIDs that our own node has, which is not the case.
return 0, routing.ErrNotSupported

Check warning on line 380 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L377-L380

Added lines #L377 - L380 were not covered by tests
}

Expand All @@ -399,7 +401,7 @@ func (d libp2pRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (it
return iter.ToResultIter[*types.PeerRecord](iter.FromSlice[*types.PeerRecord]([]*types.PeerRecord{rec})), nil
}

func (r libp2pRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
func (r libp2pRouter) ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
return 0, routing.ErrNotSupported

Check warning on line 405 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L404-L405

Added lines #L404 - L405 were not covered by tests
}

Expand Down Expand Up @@ -475,14 +477,36 @@ func (d clientRouter) FindProviders(ctx context.Context, cid cid.Cid, limit int)
return d.Client.FindProviders(ctx, cid)
}

func (d clientRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
func (d clientRouter) Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
return d.provide(func() (iter.ResultIter[*types.AnnouncementRecord], error) {
return d.Client.ProvideRecords(ctx, req)
})

Check warning on line 483 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L480-L483

Added lines #L480 - L483 were not covered by tests
}

func (d clientRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
return d.Client.FindPeers(ctx, pid)
}

func (d clientRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
return 0, routing.ErrNotSupported
func (d clientRouter) ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
return d.provide(func() (iter.ResultIter[*types.AnnouncementRecord], error) {
return d.Client.ProvidePeerRecords(ctx, req)
})

Check warning on line 493 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L490-L493

Added lines #L490 - L493 were not covered by tests
}

func (d clientRouter) provide(do func() (iter.ResultIter[*types.AnnouncementRecord], error)) (time.Duration, error) {
resIter, err := do()
if err != nil {
return 0, err
}

Check warning on line 500 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L496-L500

Added lines #L496 - L500 were not covered by tests

records, err := iter.ReadAllResults(resIter)
if err != nil {
return 0, err
}

Check warning on line 505 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L502-L505

Added lines #L502 - L505 were not covered by tests

if len(records) != 1 {
return 0, errors.New("invalid number of records returned")
}

Check warning on line 509 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L507-L509

Added lines #L507 - L509 were not covered by tests

return records[0].Payload.TTL, nil

Check warning on line 511 in server_routers.go

View check run for this annotation

Codecov / codecov/patch

server_routers.go#L511

Added line #L511 was not covered by tests
}
5 changes: 2 additions & 3 deletions server_routers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ipfs/boxo/ipns"
"github.com/ipfs/boxo/path"
"github.com/ipfs/boxo/routing/http/server"
"github.com/ipfs/boxo/routing/http/types"
"github.com/ipfs/boxo/routing/http/types/iter"
"github.com/ipfs/go-cid"
Expand All @@ -33,7 +32,7 @@ func (m *mockRouter) FindProviders(ctx context.Context, key cid.Cid, limit int)
return args.Get(0).(iter.ResultIter[types.Record]), args.Error(1)
}

func (m *mockRouter) Provide(ctx context.Context, req *server.ProvideRequest) (time.Duration, error) {
func (m *mockRouter) Provide(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
args := m.Called(ctx, req)
return args.Get(0).(time.Duration), args.Error(1)
}
Expand All @@ -46,7 +45,7 @@ func (m *mockRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (ite
return args.Get(0).(iter.ResultIter[*types.PeerRecord]), args.Error(1)
}

func (m *mockRouter) ProvidePeer(ctx context.Context, req *server.ProvidePeerRequest) (time.Duration, error) {
func (m *mockRouter) ProvidePeer(ctx context.Context, req *types.AnnouncementRecord) (time.Duration, error) {
args := m.Called(ctx, req)
return args.Get(0).(time.Duration), args.Error(1)
}
Expand Down

0 comments on commit 5719b31

Please sign in to comment.