Skip to content

Commit

Permalink
routing/http: add ProvideRecords, ProvidePeerRecords for direct publish
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 15, 2024
1 parent e1aa41c commit 813b59f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
57 changes: 48 additions & 9 deletions routing/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ type httpClient interface {

type Option func(*Client)

func WithIdentity(identity crypto.PrivKey) Option {
return func(c *Client) {
c.identity = identity
}
}

func WithHTTPClient(h httpClient) Option {
return func(c *Client) {
c.httpClient = h
Expand All @@ -101,8 +95,15 @@ func WithUserAgent(ua string) Option {
}
}

func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
// WithProviderInfo configures the [Client] with the given provider information.
// This is used by the methods [Client.Provide] and [Client.ProvidePeer] in order
// to create and sign announcement records.
//
// You can still use [Client.ProvideRecords] and [Client.ProvidePeerRecords]
// without this configuration. Then, you must provide already signed-records.
func WithProviderInfo(identity crypto.PrivKey, peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
return func(c *Client) {
c.identity = identity
c.peerID = peerID
c.protocols = protocols
for _, a := range addrs {
Expand Down Expand Up @@ -236,6 +237,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
return &measuringIter[iter.Result[types.Record]]{Iter: it, ctx: ctx, m: m}, nil
}

// Provide publishes [types.AnnouncementRecord]s based on the given [types.AnnouncementRequests].
// This records will be signed by your provided. Therefore, the [Client] must have been configured
// with [WithProviderInfo].
func (c *Client) Provide(ctx context.Context, announcements ...types.AnnouncementRequest) (iter.ResultIter[*types.AnnouncementRecord], error) {
if err := c.canProvide(); err != nil {
return nil, err
Expand Down Expand Up @@ -282,7 +286,24 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
req := jsontypes.AnnounceProvidersRequest{
Providers: records,
}
return c.provide(ctx, url, req)
}

// ProvideRecords publishes the given [types.AnnouncementRecord]. An error will
// be returned if the records aren't signed or valid.
func (c *Client) ProvideRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
providerRecords := make([]types.Record, len(records))
for i, record := range records {
if err := record.Verify(); err != nil {
return nil, err
}
providerRecords[i] = records[i]

Check warning on line 300 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L294-L300

Added lines #L294 - L300 were not covered by tests
}

url := c.baseURL + "/routing/v1/providers"
req := jsontypes.AnnounceProvidersRequest{
Providers: providerRecords,
}
return c.provide(ctx, url, req)

Check warning on line 307 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L303-L307

Added lines #L303 - L307 were not covered by tests
}

Expand Down Expand Up @@ -429,7 +450,8 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
return &measuringIter[iter.Result[*types.PeerRecord]]{Iter: it, ctx: ctx, m: m}, nil
}

// ProvidePeer provides information regarding your own peer, setup with [WithProviderInfo].
// ProvidePeer publishes an [types.AnnouncementRecord] with the provider
// information from your peer, configured with [WithProviderInfo].
func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []byte) (iter.ResultIter[*types.AnnouncementRecord], error) {
if err := c.canProvide(); err != nil {
return nil, err
Expand All @@ -438,7 +460,6 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
record := &types.AnnouncementRecord{
Schema: types.SchemaAnnouncement,
Payload: types.AnnouncementPayload{
// TODO: CID, Scope not present for /routing/v1/peers, right?
Timestamp: time.Now(),
TTL: ttl,
ID: &c.peerID,
Expand Down Expand Up @@ -472,6 +493,24 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
return c.provide(ctx, url, req)
}

// ProvidePeerRecords publishes the given [types.AnnouncementRecord]. An error will
// be returned if the records aren't signed or valid.
func (c *Client) ProvidePeerRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
providerRecords := make([]types.Record, len(records))
for i, record := range records {
if err := record.Verify(); err != nil {
return nil, err
}
providerRecords[i] = records[i]

Check warning on line 504 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L498-L504

Added lines #L498 - L504 were not covered by tests
}

url := c.baseURL + "/routing/v1/peers"
req := jsontypes.AnnouncePeersRequest{
Peers: providerRecords,
}
return c.provide(ctx, url, req)

Check warning on line 511 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L507-L511

Added lines #L507 - L511 were not covered by tests
}

// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
// validated against the given name. If validation fails, an error is returned, but no
// record.
Expand Down
3 changes: 1 addition & 2 deletions routing/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ func makeTestDeps(t *testing.T, clientsOpts []Option, serverOpts []server.Option
serverAddr := "http://" + server.Listener.Addr().String()
recordingHTTPClient := &recordingHTTPClient{httpClient: defaultHTTPClient}
defaultClientOpts := []Option{
WithProviderInfo(peerID, addrs, nil),
WithIdentity(identity),
WithProviderInfo(identity, peerID, addrs, nil),
WithUserAgent(testUserAgent),
WithHTTPClient(recordingHTTPClient),
}
Expand Down
2 changes: 1 addition & 1 deletion routing/http/types/record_announcement.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (ar AnnouncementRecord) IsSigned() bool {

func (ar *AnnouncementRecord) Sign(peerID peer.ID, key crypto.PrivKey) error {
if ar.IsSigned() {
return errors.New("already signed")
return ar.Verify()
}

Check warning on line 214 in routing/http/types/record_announcement.go

View check run for this annotation

Codecov / codecov/patch

routing/http/types/record_announcement.go#L213-L214

Added lines #L213 - L214 were not covered by tests

if key == nil {
Expand Down

0 comments on commit 813b59f

Please sign in to comment.