Skip to content

Commit

Permalink
refactor: simplify DHT reusing rainbow's
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Nov 27, 2023
1 parent 7b73b1a commit abb0aad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE

var dhtRouting routing.Routing
if runAcceleratedDHTClient {
wrappedDHT, err := newWrappedStandardAndAcceleratedDHTClient(ctx, h)
wrappedDHT, err := newBundledDHT(ctx, h)
if err != nil {
return err
}
Expand Down
67 changes: 28 additions & 39 deletions server_dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
"github.com/libp2p/go-libp2p/core/routing"
)

type wrappedStandardAndAcceleratedDHTClient struct {
standard *dht.IpfsDHT
accelerated *fullrt.FullRT
type bundledDHT struct {
standard *dht.IpfsDHT
fullRT *fullrt.FullRT
}

func newWrappedStandardAndAcceleratedDHTClient(ctx context.Context, h host.Host) (routing.Routing, error) {
func newBundledDHT(ctx context.Context, h host.Host) (routing.Routing, error) {
standardDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeClient), dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
if err != nil {
return nil, err
}

acceleratedDHT, err := fullrt.NewFullRT(h, "/ipfs",
fullRT, err := fullrt.NewFullRT(h, "/ipfs",
fullrt.DHTOption(
dht.BucketSize(20),
dht.Validator(record.NamespacedValidator{
Expand All @@ -38,54 +38,43 @@ func newWrappedStandardAndAcceleratedDHTClient(ctx context.Context, h host.Host)
return nil, err
}

return &wrappedStandardAndAcceleratedDHTClient{
standard: standardDHT,
accelerated: acceleratedDHT,
return &bundledDHT{
standard: standardDHT,
fullRT: fullRT,
}, nil
}

func (w *wrappedStandardAndAcceleratedDHTClient) Provide(ctx context.Context, c cid.Cid, b bool) error {
if w.accelerated.Ready() {
return w.accelerated.Provide(ctx, c, b)
func (b *bundledDHT) getDHT() routing.Routing {
if b.fullRT.Ready() {
return b.fullRT
}
return w.standard.Provide(ctx, c, b)
return b.standard
}

func (w *wrappedStandardAndAcceleratedDHTClient) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.AddrInfo {
if w.accelerated.Ready() {
return w.accelerated.FindProvidersAsync(ctx, c, i)
}
return w.standard.FindProvidersAsync(ctx, c, i)
func (b *bundledDHT) Provide(ctx context.Context, c cid.Cid, brdcst bool) error {
return b.getDHT().Provide(ctx, c, brdcst)
}

func (w *wrappedStandardAndAcceleratedDHTClient) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
if w.accelerated.Ready() {
return w.accelerated.FindPeer(ctx, p)
}
return w.standard.FindPeer(ctx, p)
func (b *bundledDHT) FindProvidersAsync(ctx context.Context, c cid.Cid, i int) <-chan peer.AddrInfo {
return b.getDHT().FindProvidersAsync(ctx, c, i)
}

func (w *wrappedStandardAndAcceleratedDHTClient) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
if w.accelerated.Ready() {
return w.accelerated.PutValue(ctx, key, value, opts...)
}
return w.standard.PutValue(ctx, key, value, opts...)
func (b *bundledDHT) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error) {
return b.getDHT().FindPeer(ctx, id)
}

func (w *wrappedStandardAndAcceleratedDHTClient) GetValue(ctx context.Context, s string, opts ...routing.Option) ([]byte, error) {
if w.accelerated.Ready() {
return w.accelerated.GetValue(ctx, s, opts...)
}
return w.standard.GetValue(ctx, s, opts...)
func (b *bundledDHT) PutValue(ctx context.Context, k string, v []byte, option ...routing.Option) error {
return b.getDHT().PutValue(ctx, k, v, option...)
}

func (w *wrappedStandardAndAcceleratedDHTClient) SearchValue(ctx context.Context, s string, opts ...routing.Option) (<-chan []byte, error) {
if w.accelerated.Ready() {
return w.accelerated.SearchValue(ctx, s, opts...)
}
return w.standard.SearchValue(ctx, s, opts...)
func (b *bundledDHT) GetValue(ctx context.Context, s string, option ...routing.Option) ([]byte, error) {
return b.getDHT().GetValue(ctx, s, option...)
}

func (b *bundledDHT) SearchValue(ctx context.Context, s string, option ...routing.Option) (<-chan []byte, error) {
return b.getDHT().SearchValue(ctx, s, option...)
}

func (w *wrappedStandardAndAcceleratedDHTClient) Bootstrap(ctx context.Context) error {
return w.standard.Bootstrap(ctx)
func (b *bundledDHT) Bootstrap(ctx context.Context) error {
return b.standard.Bootstrap(ctx)
}

0 comments on commit abb0aad

Please sign in to comment.