Skip to content

Commit

Permalink
feat: Better self-service commands for DHT providing (#10677)
Browse files Browse the repository at this point in the history
* Add lastRun, NextRun, move reprovide cmd to routing.
* acceleratedDHT logic
* changelog
* depend on latest boxo

---------

Co-authored-by: guillaumemichel <[email protected]>
Co-authored-by: Guillaume Michel <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2025
1 parent e08c7cb commit b355559
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 53 deletions.
33 changes: 3 additions & 30 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ var BitswapCmd = &cmds.Command{
},

Subcommands: map[string]*cmds.Command{
"stat": bitswapStatCmd,
"wantlist": showWantlistCmd,
"ledger": ledgerCmd,
"reprovide": reprovideCmd,
"stat": bitswapStatCmd,
"wantlist": showWantlistCmd,
"ledger": ledgerCmd,
},
}

Expand Down Expand Up @@ -200,29 +199,3 @@ prints the ledger associated with a given peer.
}),
},
}

var reprovideCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Trigger reprovider.",
ShortDescription: `
Trigger reprovider to announce our data to network.
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
nd, err := cmdenv.GetNode(env)
if err != nil {
return err
}

if !nd.IsOnline {
return ErrNotOnline
}

err = nd.Provider.Reprovide(req.Context)
if err != nil {
return err
}

return nil
},
}
2 changes: 1 addition & 1 deletion core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestCommands(t *testing.T) {
"/add",
"/bitswap",
"/bitswap/ledger",
"/bitswap/reprovide",
"/bitswap/stat",
"/bitswap/wantlist",
"/block",
Expand Down Expand Up @@ -72,6 +71,7 @@ func TestCommands(t *testing.T) {
"/routing/findpeer",
"/routing/findprovs",
"/routing/provide",
"/routing/reprovide",
"/diag",
"/diag/cmds",
"/diag/cmds/clear",
Expand Down
28 changes: 28 additions & 0 deletions core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var RoutingCmd = &cmds.Command{
"get": getValueRoutingCmd,
"put": putValueRoutingCmd,
"provide": provideRefRoutingCmd,
"reprovide": reprovideRoutingCmd,
},
}

Expand Down Expand Up @@ -235,6 +236,33 @@ var provideRefRoutingCmd = &cmds.Command{
Type: routing.QueryEvent{},
}

var reprovideRoutingCmd = &cmds.Command{
Status: cmds.Experimental,
Helptext: cmds.HelpText{
Tagline: "Trigger reprovider.",
ShortDescription: `
Trigger reprovider to announce our data to network.
`,
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
nd, err := cmdenv.GetNode(env)
if err != nil {
return err
}

if !nd.IsOnline {
return ErrNotOnline
}

err = nd.Provider.Reprovide(req.Context)
if err != nil {
return err
}

return nil
},
}

func provideKeys(ctx context.Context, r routing.Routing, cids []cid.Cid) error {
for _, c := range cids {
err := r.Provide(ctx, c, true)
Expand Down
24 changes: 20 additions & 4 deletions core/commands/stat_provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import (
"github.com/ipfs/boxo/provider"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/ipfs/kubo/core/commands/cmdenv"
"github.com/libp2p/go-libp2p-kad-dht/fullrt"
"golang.org/x/exp/constraints"
)

type reprovideStats struct {
provider.ReproviderStats
fullRT bool
}

var statProvideCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Returns statistics about the node's (re)provider system.",
Expand All @@ -38,32 +44,42 @@ This interface is not stable and may change from release to release.
if err != nil {
return err
}
_, fullRT := nd.DHTClient.(*fullrt.FullRT)

if err := res.Emit(stats); err != nil {
if err := res.Emit(reprovideStats{stats, fullRT}); err != nil {
return err
}

return nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s *provider.ReproviderStats) error {
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, s reprovideStats) error {
wtr := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0)
defer wtr.Flush()

fmt.Fprintf(wtr, "TotalProvides:\t%s\n", humanNumber(s.TotalProvides))
fmt.Fprintf(wtr, "AvgProvideDuration:\t%s\n", humanDuration(s.AvgProvideDuration))
fmt.Fprintf(wtr, "LastReprovideDuration:\t%s\n", humanDuration(s.LastReprovideDuration))
fmt.Fprintf(wtr, "LastReprovideBatchSize:\t%s\n", humanNumber(s.LastReprovideBatchSize))
if !s.LastRun.IsZero() {
fmt.Fprintf(wtr, "LastRun:\t%s\n", humanTime(s.LastRun))
if s.fullRT {
fmt.Fprintf(wtr, "NextRun:\t%s\n", humanTime(s.LastRun.Add(s.ReprovideInterval)))
}
}
return nil
}),
},
Type: provider.ReproviderStats{},
Type: reprovideStats{},
}

func humanDuration(val time.Duration) string {
return val.Truncate(time.Microsecond).String()
}

func humanTime(val time.Time) string {
return val.Format("2006-01-02 15:04:05")
}

func humanNumber[T constraints.Float | constraints.Integer](n T) string {
nf := float64(n)
str := humanSI(nf, 0)
Expand Down
10 changes: 10 additions & 0 deletions docs/changelogs/v0.34.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@

- [Overview](#overview)
- [🔦 Highlights](#-highlights)
- [Reprovide command moved to routing](#reprovide-command-moved-to-routing)
- [Additional stats for Accelerated DHT Reprovides](#additional-stats-for-accelerated-dht-reprovides)
- [📝 Changelog](#-changelog)
- [👨‍👩‍👧‍👦 Contributors](#-contributors)

### Overview

### 🔦 Highlights

#### Reprovide command moved to routing

Moved the `bitswap reprovide` command to `routing reprovide`. ([#10677](https://github.com/ipfs/kubo/pull/10677))

#### Additional stats for Accelerated DHT Reprovides

The `stats reprovide` command now shows additional stats for the DHT Accelerated Client, indicating the last and next `reprovide` times. ([#10677](https://github.com/ipfs/kubo/pull/10677))

### 📝 Changelog

### 👨‍👩‍👧‍👦 Contributors
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.23
replace github.com/ipfs/kubo => ./../../..

require (
github.com/ipfs/boxo v0.27.2
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
github.com/libp2p/go-libp2p v0.38.2
github.com/multiformats/go-multiaddr v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.25.0 h1:OqNqsGZPX8zh3eFMO8Lf8EHRRnSGBMqcd
github.com/ipfs-shipyard/nopfs/ipfs v0.25.0/go.mod h1:BxhUdtBgOXg1B+gAPEplkg/GpyTZY+kCMSfsJvvydqU=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.27.2 h1:sGo4KdwBaMjdBjH08lqPJyt27Z4CO6sugne3ryX513s=
github.com/ipfs/boxo v0.27.2/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b h1:aHVcNBIAW/eKjnFpwABp+CgbhnYGBnEDdPLN1yYHQX0=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/hashicorp/go-version v1.7.0
github.com/ipfs-shipyard/nopfs v0.0.14
github.com/ipfs-shipyard/nopfs/ipfs v0.25.0
github.com/ipfs/boxo v0.27.2
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b
github.com/ipfs/go-block-format v0.2.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-cidutil v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.25.0 h1:OqNqsGZPX8zh3eFMO8Lf8EHRRnSGBMqcd
github.com/ipfs-shipyard/nopfs/ipfs v0.25.0/go.mod h1:BxhUdtBgOXg1B+gAPEplkg/GpyTZY+kCMSfsJvvydqU=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.27.2 h1:sGo4KdwBaMjdBjH08lqPJyt27Z4CO6sugne3ryX513s=
github.com/ipfs/boxo v0.27.2/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b h1:aHVcNBIAW/eKjnFpwABp+CgbhnYGBnEDdPLN1yYHQX0=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
Expand Down
2 changes: 1 addition & 1 deletion test/cli/delegated_routing_v1_http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestRoutingV1Proxy(t *testing.T) {

cidStr := nodes[0].IPFSAddStr(testutils.RandomStr(1000))
// Reprovide as initialProviderDelay still ongoing
res := nodes[0].IPFS("bitswap", "reprovide")
res := nodes[0].IPFS("routing", "reprovide")
require.NoError(t, res.Err)
res = nodes[1].IPFS("routing", "findprovs", cidStr)
assert.Equal(t, nodes[0].PeerID().String(), res.Stdout.Trimmed())
Expand Down
2 changes: 1 addition & 1 deletion test/cli/delegated_routing_v1_http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestRoutingV1Server(t *testing.T) {
cidStr := nodes[2].IPFSAddStr(text)
_ = nodes[3].IPFSAddStr(text)
// Reprovide as initialProviderDelay still ongoing
res := nodes[3].IPFS("bitswap", "reprovide")
res := nodes[3].IPFS("routing", "reprovide")
require.NoError(t, res.Err)

cid, err := cid.Decode(cidStr)
Expand Down
12 changes: 6 additions & 6 deletions test/cli/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestProvider(t *testing.T) {

cid := nodes[0].IPFSAddStr(time.Now().String())
// Reprovide as initialProviderDelay still ongoing
res := nodes[0].IPFS("bitswap", "reprovide")
res := nodes[0].IPFS("routing", "reprovide")
require.NoError(t, res.Err)
expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...)
})
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestProvider(t *testing.T) {

expectNoProviders(t, cid, nodes[1:]...)

nodes[0].IPFS("bitswap", "reprovide")
nodes[0].IPFS("routing", "reprovide")

expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...)
})
Expand All @@ -89,7 +89,7 @@ func TestProvider(t *testing.T) {

expectNoProviders(t, cid, nodes[1:]...)

nodes[0].IPFS("bitswap", "reprovide")
nodes[0].IPFS("routing", "reprovide")

expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...)
})
Expand All @@ -113,7 +113,7 @@ func TestProvider(t *testing.T) {
expectNoProviders(t, cidBar, nodes[1:]...)
expectNoProviders(t, cidBarDir, nodes[1:]...)

nodes[0].IPFS("bitswap", "reprovide")
nodes[0].IPFS("routing", "reprovide")

expectNoProviders(t, cidFoo, nodes[1:]...)
expectProviders(t, cidBar, nodes[0].PeerID().String(), nodes[1:]...)
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestProvider(t *testing.T) {
expectNoProviders(t, cidBar, nodes[1:]...)
expectNoProviders(t, cidBarDir, nodes[1:]...)

nodes[0].IPFS("bitswap", "reprovide")
nodes[0].IPFS("routing", "reprovide")

expectNoProviders(t, cidFoo, nodes[1:]...)
expectNoProviders(t, cidBar, nodes[1:]...)
Expand All @@ -161,7 +161,7 @@ func TestProvider(t *testing.T) {

expectNoProviders(t, cid, nodes[1:]...)

nodes[0].IPFS("bitswap", "reprovide")
nodes[0].IPFS("routing", "reprovide")

expectProviders(t, cid, nodes[0].PeerID().String(), nodes[1:]...)
})
Expand Down
2 changes: 1 addition & 1 deletion test/cli/routing_dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func testRoutingDHT(t *testing.T, enablePubsub bool) {
t.Parallel()
hash := nodes[3].IPFSAddStr("some stuff")
// Reprovide as initialProviderDelay still ongoing
res := nodes[3].IPFS("bitswap", "reprovide")
res := nodes[3].IPFS("routing", "reprovide")
require.NoError(t, res.Err)
res = nodes[4].IPFS("routing", "findprovs", hash)
assert.Equal(t, nodes[3].PeerID().String(), res.Stdout.Trimmed())
Expand Down
2 changes: 1 addition & 1 deletion test/dependencies/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ require (
github.com/huin/goupnp v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/boxo v0.27.2 // indirect
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions test/dependencies/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.27.2 h1:sGo4KdwBaMjdBjH08lqPJyt27Z4CO6sugne3ryX513s=
github.com/ipfs/boxo v0.27.2/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b h1:aHVcNBIAW/eKjnFpwABp+CgbhnYGBnEDdPLN1yYHQX0=
github.com/ipfs/boxo v0.27.3-0.20250131141414-5c158ecc3b0b/go.mod h1:qEIRrGNr0bitDedTCzyzBHxzNWqYmyuHgK8LG9Q83EM=
github.com/ipfs/go-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs=
github.com/ipfs/go-block-format v0.2.0/go.mod h1:+jpL11nFx5A/SPpsoBn6Bzkra/zaArfSmsknbPMYgzM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down

0 comments on commit b355559

Please sign in to comment.