Skip to content

Commit

Permalink
feat: add last synced block to status endpoint result (#4710)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic authored Jul 1, 2024
1 parent 4b04c08 commit 58b62d6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 32 deletions.
4 changes: 3 additions & 1 deletion openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ components:
reserveSize:
type: integer
reserveSizeWithinRadius:
type: interger
type: integer
pullsyncRate:
type: number
storageRadius:
Expand All @@ -865,6 +865,8 @@ components:
type: integer
isReachable:
type: boolean
lastSyncedBlock:
type: integer

StatusResponse:
type: object
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type statusSnapshotResponse struct {
RequestFailed bool `json:"requestFailed,omitempty"`
BatchCommitment uint64 `json:"batchCommitment"`
IsReachable bool `json:"isReachable"`
LastSyncedBlock uint64 `json:"lastSyncedBlock"`
}

type statusResponse struct {
Expand Down Expand Up @@ -82,6 +83,7 @@ func (s *Service) statusGetHandler(w http.ResponseWriter, _ *http.Request) {
NeighborhoodSize: ss.NeighborhoodSize,
BatchCommitment: ss.BatchCommitment,
IsReachable: ss.IsReachable,
LastSyncedBlock: ss.LastSyncedBlock,
})
}

Expand Down Expand Up @@ -128,6 +130,7 @@ func (s *Service) statusGetPeersHandler(w http.ResponseWriter, r *http.Request)
snapshot.NeighborhoodSize = ss.NeighborhoodSize
snapshot.BatchCommitment = ss.BatchCommitment
snapshot.IsReachable = ss.IsReachable
snapshot.LastSyncedBlock = ss.LastSyncedBlock
}

mu.Lock()
Expand Down
13 changes: 9 additions & 4 deletions pkg/api/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethersphere/bee/v2/pkg/jsonhttp"
"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/postage"
"github.com/ethersphere/bee/v2/pkg/status"
"github.com/ethersphere/bee/v2/pkg/topology"
)
Expand All @@ -36,6 +37,7 @@ func TestGetStatus(t *testing.T) {
NeighborhoodSize: 1,
BatchCommitment: 1,
IsReachable: true,
LastSyncedBlock: 6092500,
}

ssMock := &statusSnapshotMock{
Expand All @@ -44,6 +46,7 @@ func TestGetStatus(t *testing.T) {
reserveSizeWithinRadius: ssr.ReserveSizeWithinRadius,
storageRadius: ssr.StorageRadius,
commitment: ssr.BatchCommitment,
chainState: &postage.ChainState{Block: ssr.LastSyncedBlock},
}

statusSvc := status.NewService(
Expand Down Expand Up @@ -115,12 +118,14 @@ type statusSnapshotMock struct {
reserveSizeWithinRadius uint64
storageRadius uint8
commitment uint64
chainState *postage.ChainState
}

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) 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) GetChainState() *postage.ChainState { return m.chainState }
func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 {
return m.reserveSizeWithinRadius
}
2 changes: 1 addition & 1 deletion pkg/postage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ type ChainSnapshot struct {
// on the current (highest available) block.
type Storer interface {
ChainStateGetter
CommitmentGetter

Radius() uint8

Expand Down Expand Up @@ -80,6 +79,7 @@ type CommitmentGetter interface {
}

type ChainStateGetter interface {
CommitmentGetter
// GetChainState returns the stored chain state from the store.
GetChainState() *ChainState
}
Expand Down
76 changes: 56 additions & 20 deletions pkg/status/internal/pb/status.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/status/internal/pb/status.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ message Snapshot {
uint64 BatchCommitment = 7;
bool IsReachable = 8;
uint64 ReserveSizeWithinRadius = 9;
uint64 LastSyncedBlock = 10;
}
12 changes: 6 additions & 6 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const loggerName = "status"

const (
protocolName = "status"
protocolVersion = "1.1.0"
protocolVersion = "1.1.1"
streamName = "status"
)

Expand Down Expand Up @@ -55,7 +55,7 @@ type Service struct {
beeMode string
reserve Reserve
sync SyncReporter
commitment postage.CommitmentGetter
chainState postage.ChainStateGetter
}

// NewService creates a new status service.
Expand All @@ -64,15 +64,15 @@ func NewService(
streamer p2p.Streamer,
topology topologyDriver,
beeMode string,
commitment postage.CommitmentGetter,
chainState postage.ChainStateGetter,
reserve Reserve,
) *Service {
return &Service{
logger: logger.WithName(loggerName).Register(),
streamer: streamer,
topologyDriver: topology,
beeMode: beeMode,
commitment: commitment,
chainState: chainState,
reserve: reserve,
}
}
Expand All @@ -98,7 +98,7 @@ func (s *Service) LocalSnapshot() (*Snapshot, error) {
syncRate = s.sync.SyncRate()
}

commitment, err := s.commitment.Commitment()
commitment, err := s.chainState.Commitment()
if err != nil {
return nil, fmt.Errorf("batchstore commitment: %w", err)
}
Expand Down Expand Up @@ -127,6 +127,7 @@ func (s *Service) LocalSnapshot() (*Snapshot, error) {
NeighborhoodSize: neighborhoodSize + 1, // include self
BatchCommitment: commitment,
IsReachable: s.topologyDriver.IsReachable(),
LastSyncedBlock: s.chainState.GetChainState().Block,
}, nil
}

Expand All @@ -150,7 +151,6 @@ func (s *Service) PeerSnapshot(ctx context.Context, peer swarm.Address) (*Snapsh
if err := r.ReadMsgWithContext(ctx, ss); err != nil {
return nil, fmt.Errorf("read message failed: %w", err)
}

return (*Snapshot)(ss), nil
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/p2p/protobuf"
"github.com/ethersphere/bee/v2/pkg/p2p/streamtest"
"github.com/ethersphere/bee/v2/pkg/postage"
"github.com/ethersphere/bee/v2/pkg/status"
"github.com/ethersphere/bee/v2/pkg/status/internal/pb"
"github.com/ethersphere/bee/v2/pkg/swarm"
Expand All @@ -31,6 +32,7 @@ func TestStatus(t *testing.T) {
BatchCommitment: 1024,
NeighborhoodSize: 1,
IsReachable: true,
LastSyncedBlock: 6092500,
}

sssMock := &statusSnapshotMock{want}
Expand Down Expand Up @@ -104,13 +106,15 @@ func TestStatusLightNode(t *testing.T) {
BatchCommitment: 1024,
IsReachable: true,
NeighborhoodSize: 1,
LastSyncedBlock: 6092500,
}

sssMock := &statusSnapshotMock{&pb.Snapshot{
ReserveSize: 100, // should be ignored
PullsyncRate: 100, // should be ignored
StorageRadius: 100, // should be ignored
BatchCommitment: 1024,
LastSyncedBlock: 6092500,
}}

peersIterMock := new(topologyPeersIterNoopMock)
Expand Down Expand Up @@ -193,6 +197,9 @@ 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) GetChainState() *postage.ChainState {
return &postage.ChainState{Block: m.Snapshot.LastSyncedBlock}
}
func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 {
return m.Snapshot.ReserveSizeWithinRadius
}

0 comments on commit 58b62d6

Please sign in to comment.