Skip to content

Commit

Permalink
fix: reserve returns proximity for neightborhood stats (#4881)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored Oct 31, 2024
1 parent 243bd3c commit 78bea5c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (s *Service) statusGetNeighborhoods(w http.ResponseWriter, r *http.Request)
neighborhoods = append(neighborhoods, statusNeighborhoodResponse{
Neighborhood: n.Neighborhood.String(),
ReserveSizeWithinRadius: n.ReserveSizeWithinRadius,
Proximity: swarm.Proximity(s.overlay.Bytes(), n.Neighborhood.Bytes()),
Proximity: n.Proximity,
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/postage/postagecontract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *postageContract) sendApproveTransaction(ctx context.Context, amount *bi
To: &c.bzzTokenAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 65000,
GasLimit: max(sctx.GetGasLimit(ctx), c.gasLimit),
Value: big.NewInt(0),
Description: approveDescription,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/storageincentives/staking/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (c *contract) sendApproveTransaction(ctx context.Context, amount *big.Int)
To: &c.bzzTokenAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 65000,
GasLimit: max(sctx.GetGasLimit(ctx), c.gasLimit),
Value: big.NewInt(0),
Description: approveDescription,
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/storer/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,23 +500,27 @@ func (db *DB) SubscribeBin(ctx context.Context, bin uint8, start uint64) (<-chan
type NeighborhoodStat struct {
Neighborhood swarm.Neighborhood
ReserveSizeWithinRadius int
Proximity uint8
}

func (db *DB) NeighborhoodsStat(ctx context.Context) ([]*NeighborhoodStat, error) {

radius := db.StorageRadius()

networkRadius := radius + uint8(db.reserveOptions.capacityDoubling)
responsibilityRadius := radius + uint8(db.reserveOptions.capacityDoubling)

prefixes := neighborhoodPrefixes(db.baseAddr, int(radius), db.reserveOptions.capacityDoubling)
neighs := make([]*NeighborhoodStat, len(prefixes))
for i, n := range prefixes {
neighs[i] = &NeighborhoodStat{swarm.NewNeighborhood(n, networkRadius), 0}
neighs[i] = &NeighborhoodStat{
Neighborhood: swarm.NewNeighborhood(n, responsibilityRadius),
ReserveSizeWithinRadius: 0,
Proximity: min(responsibilityRadius, swarm.Proximity(n.Bytes(), db.baseAddr.Bytes()))}
}

err := db.reserve.IterateChunksItems(0, func(ch *reserve.ChunkBinItem) (bool, error) {
for _, n := range neighs {
if swarm.Proximity(ch.Address.Bytes(), n.Neighborhood.Bytes()) >= networkRadius {
if swarm.Proximity(ch.Address.Bytes(), n.Neighborhood.Bytes()) >= responsibilityRadius {
n.ReserveSizeWithinRadius++
break
}
Expand Down
41 changes: 24 additions & 17 deletions pkg/storer/reserve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,11 @@ func TestNeighborhoodStats(t *testing.T) {
t.Parallel()

const (
chunkCountPerPO = 16
maxPO = 5
networkRadius uint8 = 4
doublingFactor uint8 = 2
localRadius uint8 = networkRadius - doublingFactor
chunkCountPerPO = 16
maxPO = 5
committedDepth uint8 = 4
doublingFactor uint8 = 2
responsibiliyDepth uint8 = committedDepth - doublingFactor
)

mustParse := func(s string) swarm.Address {
Expand Down Expand Up @@ -706,10 +706,10 @@ func TestNeighborhoodStats(t *testing.T) {
testF := func(t *testing.T, st *storer.DB) {
t.Helper()

putChunks(baseAddr, int(networkRadius), st)
putChunks(sister1, int(networkRadius), st)
putChunks(sister2, int(networkRadius), st)
putChunks(sister3, int(networkRadius), st)
putChunks(baseAddr, int(committedDepth), st)
putChunks(sister1, int(committedDepth), st)
putChunks(sister2, int(committedDepth), st)
putChunks(sister3, int(committedDepth), st)

neighs, err := st.NeighborhoodsStat(context.Background())
if err != nil {
Expand All @@ -726,12 +726,19 @@ func TestNeighborhoodStats(t *testing.T) {
}
}

if !neighs[0].Neighborhood.Equal(swarm.NewNeighborhood(baseAddr, networkRadius)) ||
!neighs[1].Neighborhood.Equal(swarm.NewNeighborhood(sister1, networkRadius)) ||
!neighs[2].Neighborhood.Equal(swarm.NewNeighborhood(sister2, networkRadius)) ||
!neighs[3].Neighborhood.Equal(swarm.NewNeighborhood(sister3, networkRadius)) {
if !neighs[0].Neighborhood.Equal(swarm.NewNeighborhood(baseAddr, committedDepth)) ||
!neighs[1].Neighborhood.Equal(swarm.NewNeighborhood(sister1, committedDepth)) ||
!neighs[2].Neighborhood.Equal(swarm.NewNeighborhood(sister2, committedDepth)) ||
!neighs[3].Neighborhood.Equal(swarm.NewNeighborhood(sister3, committedDepth)) {
t.Fatal("chunk addresses do not match")
}

if neighs[0].Proximity != committedDepth ||
neighs[1].Proximity != 3 ||
neighs[2].Proximity != 2 ||
neighs[3].Proximity != 2 {
t.Fatalf("wrong proximity")
}
}

t.Run("disk", func(t *testing.T) {
Expand All @@ -742,8 +749,8 @@ func TestNeighborhoodStats(t *testing.T) {
if err != nil {
t.Fatal(err)
}
storer.StartReserveWorker(context.Background(), pullerMock.NewMockRateReporter(0), networkRadiusFunc(localRadius))
err = spinlock.Wait(time.Minute, func() bool { return storer.StorageRadius() == localRadius })
storer.StartReserveWorker(context.Background(), pullerMock.NewMockRateReporter(0), networkRadiusFunc(responsibiliyDepth))
err = spinlock.Wait(time.Minute, func() bool { return storer.StorageRadius() == responsibiliyDepth })
if err != nil {
t.Fatal(err)
}
Expand All @@ -757,8 +764,8 @@ func TestNeighborhoodStats(t *testing.T) {
if err != nil {
t.Fatal(err)
}
storer.StartReserveWorker(context.Background(), pullerMock.NewMockRateReporter(0), networkRadiusFunc(localRadius))
err = spinlock.Wait(time.Minute, func() bool { return storer.StorageRadius() == localRadius })
storer.StartReserveWorker(context.Background(), pullerMock.NewMockRateReporter(0), networkRadiusFunc(responsibiliyDepth))
err = spinlock.Wait(time.Minute, func() bool { return storer.StorageRadius() == responsibiliyDepth })
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 78bea5c

Please sign in to comment.