Skip to content

Commit

Permalink
feat: neighborhood suggester
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill committed Feb 13, 2024
1 parent ad0b209 commit c2407b1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/bee/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
optionNameStorageIncentivesEnable = "storage-incentives-enable"
optionNameStateStoreCacheCapacity = "statestore-cache-capacity"
optionNameTargetNeighborhood = "target-neighborhood"
optionNameNeighborhoodSuggester = "neighborhood-suggester"
)

// nolint:gochecknoinits
Expand Down Expand Up @@ -302,6 +303,7 @@ func (c *command) setAllFlags(cmd *cobra.Command) {
cmd.Flags().Bool(optionNameStorageIncentivesEnable, true, "enable storage incentives feature")
cmd.Flags().Uint64(optionNameStateStoreCacheCapacity, 100_000, "lru memory caching capacity in number of statestore entries")
cmd.Flags().String(optionNameTargetNeighborhood, "", "neighborhood to target in binary format (ex: 111111001) for mining the initial overlay")
cmd.Flags().String(optionNameNeighborhoodSuggester, "https://api.swarmscan.io/v1/network/neighborhoods/suggestion", "suggester for target neighborhood")
}

func newLogger(cmd *cobra.Command, verbosity string) (log.Logger, error) {
Expand Down
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func buildBeeNode(ctx context.Context, c *command, cmd *cobra.Command, logger lo
EnableStorageIncentives: c.config.GetBool(optionNameStorageIncentivesEnable),
StatestoreCacheCapacity: c.config.GetUint64(optionNameStateStoreCacheCapacity),
TargetNeighborhood: c.config.GetString(optionNameTargetNeighborhood),
NeighborhoodSuggester: c.config.GetString(optionNameNeighborhoodSuggester),
})

return b, err
Expand Down
40 changes: 37 additions & 3 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ package node
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io"
stdlog "log"
"math/big"
"net"
"net/http"
"net/url"
"path/filepath"
"runtime"
"sync"
Expand Down Expand Up @@ -171,6 +173,7 @@ type Options struct {
EnableStorageIncentives bool
StatestoreCacheCapacity uint64
TargetNeighborhood string
NeighborhoodSuggester string
}

const (
Expand Down Expand Up @@ -282,9 +285,40 @@ func NewBee(

if !nonceExists {
// mine the overlay
if o.TargetNeighborhood != "" {
logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", o.TargetNeighborhood)
swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, o.TargetNeighborhood)
targetNeighborhood := o.TargetNeighborhood
if o.TargetNeighborhood == "" && o.NeighborhoodSuggester != "" {
_, err = url.Parse(o.NeighborhoodSuggester)
if err != nil {
return nil, fmt.Errorf("invalid neighborhood suggester: %w", err)
}
logger.Info("fetching target neighborhood from suggester", "url", o.NeighborhoodSuggester)
type suggestionRes struct {
Neighborhood string `json:"neighborhood"`
}
res, err := http.Get(o.NeighborhoodSuggester)

Check failure on line 298 in pkg/node/node.go

View workflow job for this annotation

GitHub Actions / Lint

net/http.Get must not be called (noctx)
if err != nil {
return nil, fmt.Errorf("get neighborhood suggestion: %w", err)
}
defer res.Body.Close()
var suggestion suggestionRes
d, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("read neighborhood suggestion: %w", err)
}
err = json.Unmarshal(d, &suggestion)
if err != nil {
return nil, fmt.Errorf("unmarshal neighborhood suggestion: %w", err)
}
_, err = swarm.ParseBitStrAddress(suggestion.Neighborhood)
if err != nil {
return nil, fmt.Errorf("invalid neighborhood suggestion. %s", suggestion.Neighborhood)
}
targetNeighborhood = suggestion.Neighborhood
}

if targetNeighborhood != "" {
logger.Info("mining an overlay address for the fresh node to target the selected neighborhood", "target", targetNeighborhood)
swarmAddress, nonce, err = nbhdutil.MineOverlay(ctx, *pubKey, networkID, targetNeighborhood)
if err != nil {
return nil, fmt.Errorf("mine overlay address: %w", err)
}
Expand Down

0 comments on commit c2407b1

Please sign in to comment.