From 3381ad1e5cdbe61862ced6b1e79f649e813b89b0 Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Mon, 19 Feb 2024 15:06:13 +0100 Subject: [PATCH 1/2] feat: add reserveSizeWithinRadius to status protocol --- openapi/SwarmCommon.yaml | 2 + pkg/api/status.go | 43 +++++++------- pkg/api/status_test.go | 38 +++++++----- pkg/status/internal/pb/status.pb.go | 90 ++++++++++++++++++++--------- pkg/status/internal/pb/status.proto | 1 + pkg/status/status.go | 34 ++++++----- pkg/status/status_test.go | 3 + pkg/storer/reserve.go | 12 ++++ 8 files changed, 147 insertions(+), 76 deletions(-) diff --git a/openapi/SwarmCommon.yaml b/openapi/SwarmCommon.yaml index 07119cc6894..721516aa9bb 100644 --- a/openapi/SwarmCommon.yaml +++ b/openapi/SwarmCommon.yaml @@ -839,6 +839,8 @@ components: type: integer reserveSize: type: integer + reserveSizeWithinRadius: + type: interger pullsyncRate: type: number storageRadius: diff --git a/pkg/api/status.go b/pkg/api/status.go index af0b5b99a27..4c89e63b0b2 100644 --- a/pkg/api/status.go +++ b/pkg/api/status.go @@ -17,17 +17,18 @@ import ( ) type statusSnapshotResponse struct { - Peer string `json:"peer"` - Proximity uint8 `json:"proximity"` - BeeMode string `json:"beeMode"` - ReserveSize uint64 `json:"reserveSize"` - PullsyncRate float64 `json:"pullsyncRate"` - StorageRadius uint8 `json:"storageRadius"` - ConnectedPeers uint64 `json:"connectedPeers"` - NeighborhoodSize uint64 `json:"neighborhoodSize"` - RequestFailed bool `json:"requestFailed,omitempty"` - BatchCommitment uint64 `json:"batchCommitment"` - IsReachable bool `json:"isReachable"` + Peer string `json:"peer"` + Proximity uint8 `json:"proximity"` + BeeMode string `json:"beeMode"` + ReserveSize uint64 `json:"reserveSize"` + ReserveSizeWithinRadius uint64 `json:"reserveSizeWithinRadius"` + PullsyncRate float64 `json:"pullsyncRate"` + StorageRadius uint8 `json:"storageRadius"` + ConnectedPeers uint64 `json:"connectedPeers"` + NeighborhoodSize uint64 `json:"neighborhoodSize"` + RequestFailed bool `json:"requestFailed,omitempty"` + BatchCommitment uint64 `json:"batchCommitment"` + IsReachable bool `json:"isReachable"` } type statusResponse struct { @@ -70,15 +71,16 @@ func (s *Service) statusGetHandler(w http.ResponseWriter, _ *http.Request) { } jsonhttp.OK(w, statusSnapshotResponse{ - Peer: s.overlay.String(), - BeeMode: ss.BeeMode, - ReserveSize: ss.ReserveSize, - PullsyncRate: ss.PullsyncRate, - StorageRadius: uint8(ss.StorageRadius), - ConnectedPeers: ss.ConnectedPeers, - NeighborhoodSize: ss.NeighborhoodSize, - BatchCommitment: ss.BatchCommitment, - IsReachable: ss.IsReachable, + Peer: s.overlay.String(), + BeeMode: ss.BeeMode, + ReserveSize: ss.ReserveSize, + ReserveSizeWithinRadius: ss.ReserveSizeWithinRadius, + PullsyncRate: ss.PullsyncRate, + StorageRadius: uint8(ss.StorageRadius), + ConnectedPeers: ss.ConnectedPeers, + NeighborhoodSize: ss.NeighborhoodSize, + BatchCommitment: ss.BatchCommitment, + IsReachable: ss.IsReachable, }) } @@ -118,6 +120,7 @@ func (s *Service) statusGetPeersHandler(w http.ResponseWriter, r *http.Request) } else { snapshot.BeeMode = ss.BeeMode snapshot.ReserveSize = ss.ReserveSize + snapshot.ReserveSizeWithinRadius = ss.ReserveSizeWithinRadius snapshot.PullsyncRate = ss.PullsyncRate snapshot.StorageRadius = uint8(ss.StorageRadius) snapshot.ConnectedPeers = ss.ConnectedPeers diff --git a/pkg/api/status_test.go b/pkg/api/status_test.go index b7a9b92dc57..d9d25e73046 100644 --- a/pkg/api/status_test.go +++ b/pkg/api/status_test.go @@ -26,21 +26,23 @@ func TestGetStatus(t *testing.T) { mode := api.FullMode ssr := api.StatusSnapshotResponse{ - BeeMode: mode.String(), - ReserveSize: 128, - PullsyncRate: 64, - StorageRadius: 8, - ConnectedPeers: 0, - NeighborhoodSize: 0, - BatchCommitment: 1, - IsReachable: true, + BeeMode: mode.String(), + ReserveSize: 128, + ReserveSizeWithinRadius: 64, + PullsyncRate: 64, + StorageRadius: 8, + ConnectedPeers: 0, + NeighborhoodSize: 0, + BatchCommitment: 1, + IsReachable: true, } ssMock := &statusSnapshotMock{ - syncRate: ssr.PullsyncRate, - reserveSize: int(ssr.ReserveSize), - storageRadius: ssr.StorageRadius, - commitment: ssr.BatchCommitment, + syncRate: ssr.PullsyncRate, + reserveSize: int(ssr.ReserveSize), + reserveSizeWithinRadius: ssr.ReserveSizeWithinRadius, + storageRadius: ssr.StorageRadius, + commitment: ssr.BatchCommitment, } statusSvc := status.NewService( @@ -109,13 +111,17 @@ func (m *topologyPeersIterNoopMock) IsReachable() bool { // - status.SyncReporter // - postage.CommitmentGetter type statusSnapshotMock struct { - syncRate float64 - reserveSize int - storageRadius uint8 - commitment uint64 + syncRate float64 + reserveSize int + reserveSizeWithinRadius uint64 + storageRadius uint8 + commitment uint64 } func (m *statusSnapshotMock) SyncRate() float64 { return m.syncRate } func (m *statusSnapshotMock) ReserveSize() int { return m.reserveSize } func (m *statusSnapshotMock) StorageRadius() uint8 { return m.storageRadius } func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.commitment, nil } +func (m *statusSnapshotMock) ReserveSizeWithinRadius(uint8) (uint64, error) { + return m.reserveSizeWithinRadius, nil +} diff --git a/pkg/status/internal/pb/status.pb.go b/pkg/status/internal/pb/status.pb.go index a4f6ecf2ba5..868cc0f11e0 100644 --- a/pkg/status/internal/pb/status.pb.go +++ b/pkg/status/internal/pb/status.pb.go @@ -64,14 +64,15 @@ var xxx_messageInfo_Get proto.InternalMessageInfo // the appropriate values that are a snapshot of the current state // of the running node. type Snapshot struct { - ReserveSize uint64 `protobuf:"varint,1,opt,name=ReserveSize,proto3" json:"ReserveSize,omitempty"` - PullsyncRate float64 `protobuf:"fixed64,2,opt,name=PullsyncRate,proto3" json:"PullsyncRate,omitempty"` - StorageRadius uint32 `protobuf:"varint,3,opt,name=StorageRadius,proto3" json:"StorageRadius,omitempty"` - ConnectedPeers uint64 `protobuf:"varint,4,opt,name=ConnectedPeers,proto3" json:"ConnectedPeers,omitempty"` - NeighborhoodSize uint64 `protobuf:"varint,5,opt,name=NeighborhoodSize,proto3" json:"NeighborhoodSize,omitempty"` - BeeMode string `protobuf:"bytes,6,opt,name=BeeMode,proto3" json:"BeeMode,omitempty"` - BatchCommitment uint64 `protobuf:"varint,7,opt,name=BatchCommitment,proto3" json:"BatchCommitment,omitempty"` - IsReachable bool `protobuf:"varint,8,opt,name=IsReachable,proto3" json:"IsReachable,omitempty"` + ReserveSize uint64 `protobuf:"varint,1,opt,name=ReserveSize,proto3" json:"ReserveSize,omitempty"` + PullsyncRate float64 `protobuf:"fixed64,2,opt,name=PullsyncRate,proto3" json:"PullsyncRate,omitempty"` + StorageRadius uint32 `protobuf:"varint,3,opt,name=StorageRadius,proto3" json:"StorageRadius,omitempty"` + ConnectedPeers uint64 `protobuf:"varint,4,opt,name=ConnectedPeers,proto3" json:"ConnectedPeers,omitempty"` + NeighborhoodSize uint64 `protobuf:"varint,5,opt,name=NeighborhoodSize,proto3" json:"NeighborhoodSize,omitempty"` + BeeMode string `protobuf:"bytes,6,opt,name=BeeMode,proto3" json:"BeeMode,omitempty"` + BatchCommitment uint64 `protobuf:"varint,7,opt,name=BatchCommitment,proto3" json:"BatchCommitment,omitempty"` + IsReachable bool `protobuf:"varint,8,opt,name=IsReachable,proto3" json:"IsReachable,omitempty"` + ReserveSizeWithinRadius uint64 `protobuf:"varint,9,opt,name=ReserveSizeWithinRadius,proto3" json:"ReserveSizeWithinRadius,omitempty"` } func (m *Snapshot) Reset() { *m = Snapshot{} } @@ -163,6 +164,13 @@ func (m *Snapshot) GetIsReachable() bool { return false } +func (m *Snapshot) GetReserveSizeWithinRadius() uint64 { + if m != nil { + return m.ReserveSizeWithinRadius + } + return 0 +} + func init() { proto.RegisterType((*Get)(nil), "status.Get") proto.RegisterType((*Snapshot)(nil), "status.Snapshot") @@ -171,25 +179,26 @@ func init() { func init() { proto.RegisterFile("status.proto", fileDescriptor_dfe4fce6682daf5b) } var fileDescriptor_dfe4fce6682daf5b = []byte{ - // 279 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0xd0, 0xc1, 0x4a, 0xf3, 0x40, - 0x10, 0x07, 0xf0, 0x6e, 0xda, 0xa6, 0xf9, 0xf6, 0x6b, 0x55, 0xf6, 0xb4, 0x07, 0x59, 0x96, 0x20, - 0xb2, 0x78, 0xf0, 0xe2, 0x1b, 0xa4, 0x07, 0xf1, 0xa0, 0x94, 0xcd, 0xcd, 0xdb, 0x26, 0x19, 0x9a, - 0x40, 0x92, 0x0d, 0xd9, 0x89, 0xa0, 0x4f, 0xe1, 0xa3, 0xf8, 0x18, 0x1e, 0x7b, 0xf4, 0x28, 0xc9, - 0x8b, 0x88, 0x11, 0xa1, 0xad, 0xc7, 0xff, 0x8f, 0x61, 0x66, 0xf8, 0xd3, 0xa5, 0x43, 0x83, 0x9d, - 0xbb, 0x6e, 0x5a, 0x8b, 0x96, 0xf9, 0x3f, 0x29, 0x9c, 0xd3, 0xe9, 0x2d, 0x60, 0xf8, 0xe6, 0xd1, - 0x20, 0xae, 0x4d, 0xe3, 0x72, 0x8b, 0x4c, 0xd2, 0xff, 0x1a, 0x1c, 0xb4, 0x4f, 0x10, 0x17, 0x2f, - 0xc0, 0x89, 0x24, 0x6a, 0xa6, 0xf7, 0x89, 0x85, 0x74, 0xb9, 0xe9, 0xca, 0xd2, 0x3d, 0xd7, 0xa9, - 0x36, 0x08, 0xdc, 0x93, 0x44, 0x11, 0x7d, 0x60, 0xec, 0x82, 0xae, 0x62, 0xb4, 0xad, 0xd9, 0x82, - 0x36, 0x59, 0xd1, 0x39, 0x3e, 0x95, 0x44, 0xad, 0xf4, 0x21, 0xb2, 0x4b, 0x7a, 0xb2, 0xb6, 0x75, - 0x0d, 0x29, 0x42, 0xb6, 0x01, 0x68, 0x1d, 0x9f, 0x8d, 0xe7, 0x8e, 0x94, 0x5d, 0xd1, 0xb3, 0x07, - 0x28, 0xb6, 0x79, 0x62, 0xdb, 0xdc, 0xda, 0x6c, 0x7c, 0x6c, 0x3e, 0x4e, 0xfe, 0x71, 0xc6, 0xe9, - 0x22, 0x02, 0xb8, 0xb7, 0x19, 0x70, 0x5f, 0x12, 0xf5, 0x4f, 0xff, 0x46, 0xa6, 0xe8, 0x69, 0x64, - 0x30, 0xcd, 0xd7, 0xb6, 0xaa, 0x0a, 0xac, 0xa0, 0x46, 0xbe, 0x18, 0x97, 0x1c, 0xf3, 0x77, 0x07, - 0x77, 0x4e, 0x83, 0x49, 0x73, 0x93, 0x94, 0xc0, 0x03, 0x49, 0x54, 0xa0, 0xf7, 0x29, 0x3a, 0x7f, - 0xef, 0x05, 0xd9, 0xf5, 0x82, 0x7c, 0xf6, 0x82, 0xbc, 0x0e, 0x62, 0xb2, 0x1b, 0xc4, 0xe4, 0x63, - 0x10, 0x93, 0x47, 0xaf, 0x49, 0x12, 0x7f, 0xac, 0xf9, 0xe6, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x64, - 0x9a, 0xeb, 0xde, 0x76, 0x01, 0x00, 0x00, + // 298 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0x3f, 0x4b, 0x73, 0x31, + 0x14, 0xc6, 0x9b, 0xfe, 0x6f, 0xde, 0xf6, 0x55, 0xb2, 0x98, 0x41, 0x42, 0x28, 0x22, 0xc1, 0xc1, + 0xc5, 0xc5, 0xb9, 0x1d, 0xc4, 0x41, 0x29, 0xe9, 0x20, 0xb8, 0xe5, 0xde, 0x7b, 0x68, 0x02, 0x6d, + 0x52, 0x6e, 0x4e, 0x05, 0xfd, 0x14, 0x7e, 0x2c, 0xc7, 0x8e, 0x8e, 0xd2, 0x6e, 0x7e, 0x0a, 0x31, + 0x2a, 0xdc, 0x56, 0x1c, 0x9f, 0xdf, 0x39, 0x3c, 0xe7, 0x9c, 0xe7, 0xd0, 0x7e, 0x44, 0x83, 0xab, + 0x78, 0xbe, 0x2c, 0x03, 0x06, 0xd6, 0xfe, 0x52, 0xc3, 0x16, 0x6d, 0x5c, 0x01, 0x0e, 0xdf, 0xeb, + 0xb4, 0x3b, 0xf5, 0x66, 0x19, 0x6d, 0x40, 0x26, 0xe9, 0x3f, 0x0d, 0x11, 0xca, 0x07, 0x98, 0xba, + 0x27, 0xe0, 0x44, 0x12, 0xd5, 0xd4, 0x55, 0xc4, 0x86, 0xb4, 0x3f, 0x59, 0xcd, 0xe7, 0xf1, 0xd1, + 0xe7, 0xda, 0x20, 0xf0, 0xba, 0x24, 0x8a, 0xe8, 0x1d, 0xc6, 0x4e, 0xe8, 0x60, 0x8a, 0xa1, 0x34, + 0x33, 0xd0, 0xa6, 0x70, 0xab, 0xc8, 0x1b, 0x92, 0xa8, 0x81, 0xde, 0x85, 0xec, 0x94, 0xfe, 0x1f, + 0x07, 0xef, 0x21, 0x47, 0x28, 0x26, 0x00, 0x65, 0xe4, 0xcd, 0x34, 0x6e, 0x8f, 0xb2, 0x33, 0x7a, + 0x78, 0x0b, 0x6e, 0x66, 0xb3, 0x50, 0xda, 0x10, 0x8a, 0xb4, 0x58, 0x2b, 0x75, 0xfe, 0xe2, 0x8c, + 0xd3, 0xce, 0x08, 0xe0, 0x26, 0x14, 0xc0, 0xdb, 0x92, 0xa8, 0x9e, 0xfe, 0x91, 0x4c, 0xd1, 0x83, + 0x91, 0xc1, 0xdc, 0x8e, 0xc3, 0x62, 0xe1, 0x70, 0x01, 0x1e, 0x79, 0x27, 0x99, 0xec, 0xe3, 0xcf, + 0x0c, 0xae, 0xa3, 0x06, 0x93, 0x5b, 0x93, 0xcd, 0x81, 0x77, 0x25, 0x51, 0x5d, 0x5d, 0x45, 0xec, + 0x92, 0x1e, 0x55, 0x22, 0xb9, 0x73, 0x68, 0x9d, 0xff, 0xbe, 0xb4, 0x97, 0x3c, 0xff, 0x2a, 0x8f, + 0x8e, 0x5f, 0x36, 0x82, 0xac, 0x37, 0x82, 0xbc, 0x6d, 0x04, 0x79, 0xde, 0x8a, 0xda, 0x7a, 0x2b, + 0x6a, 0xaf, 0x5b, 0x51, 0xbb, 0xaf, 0x2f, 0xb3, 0xac, 0x9d, 0x1e, 0x74, 0xf1, 0x11, 0x00, 0x00, + 0xff, 0xff, 0xb8, 0xde, 0xcb, 0x07, 0xb0, 0x01, 0x00, 0x00, } func (m *Get) Marshal() (dAtA []byte, err error) { @@ -235,6 +244,11 @@ func (m *Snapshot) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.ReserveSizeWithinRadius != 0 { + i = encodeVarintStatus(dAtA, i, uint64(m.ReserveSizeWithinRadius)) + i-- + dAtA[i] = 0x48 + } if m.IsReachable { i-- if m.IsReachable { @@ -337,6 +351,9 @@ func (m *Snapshot) Size() (n int) { if m.IsReachable { n += 2 } + if m.ReserveSizeWithinRadius != 0 { + n += 1 + sovStatus(uint64(m.ReserveSizeWithinRadius)) + } return n } @@ -586,6 +603,25 @@ func (m *Snapshot) Unmarshal(dAtA []byte) error { } } m.IsReachable = bool(v != 0) + case 9: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ReserveSizeWithinRadius", wireType) + } + m.ReserveSizeWithinRadius = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowStatus + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.ReserveSizeWithinRadius |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipStatus(dAtA[iNdEx:]) diff --git a/pkg/status/internal/pb/status.proto b/pkg/status/internal/pb/status.proto index f4cecd47088..a62eb327433 100644 --- a/pkg/status/internal/pb/status.proto +++ b/pkg/status/internal/pb/status.proto @@ -23,4 +23,5 @@ message Snapshot { string BeeMode = 6; uint64 BatchCommitment = 7; bool IsReachable = 8; + uint64 ReserveSizeWithinRadius = 9; } diff --git a/pkg/status/status.go b/pkg/status/status.go index 93a498bb20a..fc5520fa597 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -37,6 +37,7 @@ type SyncReporter interface { // Reserve defines the reserve storage related information required. type Reserve interface { ReserveSize() int + ReserveSizeWithinRadius(uint8) (uint64, error) StorageRadius() uint8 } @@ -79,16 +80,22 @@ func NewService( // LocalSnapshot returns the current status snapshot of this node. func (s *Service) LocalSnapshot() (*Snapshot, error) { var ( - storageRadius uint8 - syncRate float64 - reserveSize uint64 - connectedPeers uint64 - neighborhoodSize uint64 + storageRadius uint8 + syncRate float64 + reserveSize uint64 + reserveSizeWithinRadius uint64 + connectedPeers uint64 + neighborhoodSize uint64 ) if s.reserve != nil { storageRadius = s.reserve.StorageRadius() reserveSize = uint64(s.reserve.ReserveSize()) + sizeWithinRadius, err := s.reserve.ReserveSizeWithinRadius(storageRadius) + if err != nil { + return nil, fmt.Errorf("reserve size within radius: %w", err) + } + reserveSizeWithinRadius = sizeWithinRadius } if s.sync != nil { @@ -115,14 +122,15 @@ func (s *Service) LocalSnapshot() (*Snapshot, error) { } return &Snapshot{ - BeeMode: s.beeMode, - ReserveSize: reserveSize, - PullsyncRate: syncRate, - StorageRadius: uint32(storageRadius), - ConnectedPeers: connectedPeers, - NeighborhoodSize: neighborhoodSize, - BatchCommitment: commitment, - IsReachable: s.topologyDriver.IsReachable(), + BeeMode: s.beeMode, + ReserveSize: reserveSize, + ReserveSizeWithinRadius: reserveSizeWithinRadius, + PullsyncRate: syncRate, + StorageRadius: uint32(storageRadius), + ConnectedPeers: connectedPeers, + NeighborhoodSize: neighborhoodSize, + BatchCommitment: commitment, + IsReachable: s.topologyDriver.IsReachable(), }, nil } diff --git a/pkg/status/status_test.go b/pkg/status/status_test.go index 0bca393ee7d..999fe8f1061 100644 --- a/pkg/status/status_test.go +++ b/pkg/status/status_test.go @@ -191,3 +191,6 @@ func (m *statusSnapshotMock) SyncRate() float64 { return m.Snapshot.Pu func (m *statusSnapshotMock) ReserveSize() int { return int(m.Snapshot.ReserveSize) } func (m *statusSnapshotMock) StorageRadius() uint8 { return uint8(m.Snapshot.StorageRadius) } func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.Snapshot.BatchCommitment, nil } +func (m *statusSnapshotMock) ReserveSizeWithinRadius(uint8) (uint64, error) { + return m.Snapshot.ReserveSizeWithinRadius, nil +} diff --git a/pkg/storer/reserve.go b/pkg/storer/reserve.go index 66a8fd0c464..e6b24da9b53 100644 --- a/pkg/storer/reserve.go +++ b/pkg/storer/reserve.go @@ -474,6 +474,18 @@ func (db *DB) ReserveSize() int { return db.reserve.Size() } +func (db *DB) ReserveSizeWithinRadius(radius uint8) (uint64, error) { + var count uint64 + err := db.reserve.IterateChunksItems(db.repo, radius, func(_ reserve.ChunkItem) (bool, error) { + count++ + return false, nil + }) + if err != nil { + return 0, err + } + return count, nil +} + func (db *DB) IsWithinStorageRadius(addr swarm.Address) bool { if db.reserve == nil { return false From 85397c27103b3b465699607dd0011c777063616a Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Tue, 20 Feb 2024 12:34:34 +0100 Subject: [PATCH 2/2] fix: precomputed radius --- pkg/api/status_test.go | 4 ++-- pkg/status/status.go | 8 ++------ pkg/status/status_test.go | 4 ++-- pkg/storer/reserve.go | 14 ++++---------- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/pkg/api/status_test.go b/pkg/api/status_test.go index d9d25e73046..5526d3734aa 100644 --- a/pkg/api/status_test.go +++ b/pkg/api/status_test.go @@ -122,6 +122,6 @@ func (m *statusSnapshotMock) SyncRate() float64 { return m.syncRate } func (m *statusSnapshotMock) ReserveSize() int { return m.reserveSize } func (m *statusSnapshotMock) StorageRadius() uint8 { return m.storageRadius } func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.commitment, nil } -func (m *statusSnapshotMock) ReserveSizeWithinRadius(uint8) (uint64, error) { - return m.reserveSizeWithinRadius, nil +func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 { + return m.reserveSizeWithinRadius } diff --git a/pkg/status/status.go b/pkg/status/status.go index fc5520fa597..9b0b6fcb8d8 100644 --- a/pkg/status/status.go +++ b/pkg/status/status.go @@ -37,7 +37,7 @@ type SyncReporter interface { // Reserve defines the reserve storage related information required. type Reserve interface { ReserveSize() int - ReserveSizeWithinRadius(uint8) (uint64, error) + ReserveSizeWithinRadius() uint64 StorageRadius() uint8 } @@ -91,11 +91,7 @@ func (s *Service) LocalSnapshot() (*Snapshot, error) { if s.reserve != nil { storageRadius = s.reserve.StorageRadius() reserveSize = uint64(s.reserve.ReserveSize()) - sizeWithinRadius, err := s.reserve.ReserveSizeWithinRadius(storageRadius) - if err != nil { - return nil, fmt.Errorf("reserve size within radius: %w", err) - } - reserveSizeWithinRadius = sizeWithinRadius + reserveSizeWithinRadius = s.reserve.ReserveSizeWithinRadius() } if s.sync != nil { diff --git a/pkg/status/status_test.go b/pkg/status/status_test.go index 999fe8f1061..887b6175770 100644 --- a/pkg/status/status_test.go +++ b/pkg/status/status_test.go @@ -191,6 +191,6 @@ func (m *statusSnapshotMock) SyncRate() float64 { return m.Snapshot.Pu func (m *statusSnapshotMock) ReserveSize() int { return int(m.Snapshot.ReserveSize) } func (m *statusSnapshotMock) StorageRadius() uint8 { return uint8(m.Snapshot.StorageRadius) } func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.Snapshot.BatchCommitment, nil } -func (m *statusSnapshotMock) ReserveSizeWithinRadius(uint8) (uint64, error) { - return m.Snapshot.ReserveSizeWithinRadius, nil +func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 { + return m.Snapshot.ReserveSizeWithinRadius } diff --git a/pkg/storer/reserve.go b/pkg/storer/reserve.go index e6b24da9b53..56bd12bf80f 100644 --- a/pkg/storer/reserve.go +++ b/pkg/storer/reserve.go @@ -35,6 +35,7 @@ func reserveUpdateBatchLockKey(batchID []byte) string { } var errMaxRadius = errors.New("max radius reached") +var reserveSizeWithinRadius atomic.Uint64 type Syncer interface { // Number of active historical syncing jobs. @@ -137,6 +138,7 @@ func (db *DB) reserveSizeWithinRadiusWorker(ctx context.Context) { if err != nil { db.logger.Error(err, "reserve count within radius") } + reserveSizeWithinRadius.Store(uint64(count)) for batch := range evictBatches { db.logger.Debug("reserve size worker, invalid batch id", "batch_id", hex.EncodeToString([]byte(batch))) @@ -474,16 +476,8 @@ func (db *DB) ReserveSize() int { return db.reserve.Size() } -func (db *DB) ReserveSizeWithinRadius(radius uint8) (uint64, error) { - var count uint64 - err := db.reserve.IterateChunksItems(db.repo, radius, func(_ reserve.ChunkItem) (bool, error) { - count++ - return false, nil - }) - if err != nil { - return 0, err - } - return count, nil +func (db *DB) ReserveSizeWithinRadius() uint64 { + return reserveSizeWithinRadius.Load() } func (db *DB) IsWithinStorageRadius(addr swarm.Address) bool {