Skip to content

Commit

Permalink
add ProvideRecords and ProvidePeerRecords to allow disseminating alre…
Browse files Browse the repository at this point in the history
…ady-signed records
  • Loading branch information
hacdias committed Feb 15, 2024
1 parent b930d1f commit e576b55
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion routing/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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 an [types.AnnouncementRecord] to the server, signed by you.
// This [Client] must be configured with [WithIdentity] and [WithProviderInfo]
// in order to be able to provide.
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 +285,27 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
req := jsontypes.AnnounceProvidersRequest{
Providers: records,
}
return c.provide(ctx, url, req)
}

// ProvideRecords publishes [types.AnnouncementRecord] to the server with already
// signed records. This can be useful if you've gotten an announcement from somewhere
// else and you wish to spread it through the network.
//
// If the records aren't valid, an error will be returned.
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 302 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L296-L302

Added lines #L296 - L302 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 309 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L305-L309

Added lines #L305 - L309 were not covered by tests
}

Expand Down Expand Up @@ -429,7 +452,9 @@ 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] to the server with information
// regarding your own peer. This [Client] must be configured with [WithIdentity]
// and [WithProviderInfo] in order to be able to provide.
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 Down Expand Up @@ -472,6 +497,27 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
return c.provide(ctx, url, req)
}

// ProvidePeerRecords publishes [types.AnnouncementRecord] to the server with already
// signed records. This can be useful if you've gotten an announcement from somewhere
// else and you wish to spread it through the network.
//
// If the records aren't valid, an error will be returned.
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 511 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L505 - L511 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 518 in routing/http/client/client.go

View check run for this annotation

Codecov / codecov/patch

routing/http/client/client.go#L514-L518

Added lines #L514 - L518 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

0 comments on commit e576b55

Please sign in to comment.