github.com/ethersphere/bee/v2@v2.2.0/pkg/api/redistribution.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"net/http"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/bigint"
    11  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    12  	"github.com/ethersphere/bee/v2/pkg/tracing"
    13  )
    14  
    15  type redistributionStatusResponse struct {
    16  	MinimumGasFunds           *bigint.BigInt `json:"minimumGasFunds"`
    17  	HasSufficientFunds        bool           `json:"hasSufficientFunds"`
    18  	IsFrozen                  bool           `json:"isFrozen"`
    19  	IsFullySynced             bool           `json:"isFullySynced"`
    20  	Phase                     string         `json:"phase"`
    21  	Round                     uint64         `json:"round"`
    22  	LastWonRound              uint64         `json:"lastWonRound"`
    23  	LastPlayedRound           uint64         `json:"lastPlayedRound"`
    24  	LastFrozenRound           uint64         `json:"lastFrozenRound"`
    25  	LastSelectedRound         uint64         `json:"lastSelectedRound"`
    26  	LastSampleDurationSeconds float64        `json:"lastSampleDurationSeconds"`
    27  	Block                     uint64         `json:"block"`
    28  	Reward                    *bigint.BigInt `json:"reward"`
    29  	Fees                      *bigint.BigInt `json:"fees"`
    30  	IsHealthy                 bool           `json:"isHealthy"`
    31  }
    32  
    33  func (s *Service) redistributionStatusHandler(w http.ResponseWriter, r *http.Request) {
    34  	logger := tracing.NewLoggerWithTraceID(r.Context(), s.logger.WithName("get_redistributionstate").Build())
    35  
    36  	if s.beeMode != FullMode {
    37  		jsonhttp.BadRequest(w, errOperationSupportedOnlyInFullMode)
    38  		return
    39  	}
    40  
    41  	status, err := s.redistributionAgent.Status()
    42  	if err != nil {
    43  		logger.Debug("get redistribution status", "overlay_address", s.overlay.String(), "error", err)
    44  		logger.Error(nil, "get redistribution status")
    45  		jsonhttp.InternalServerError(w, "failed to get redistribution status")
    46  		return
    47  	}
    48  
    49  	minGasFunds, hasSufficientFunds, err := s.redistributionAgent.HasEnoughFundsToPlay(r.Context())
    50  	if err != nil {
    51  		logger.Debug("has enough funds to play", "overlay_address", s.overlay.String(), "error", err)
    52  		logger.Error(nil, "has enough funds to play")
    53  		jsonhttp.InternalServerError(w, "failed to calculate if node has enough funds to play")
    54  		return
    55  	}
    56  
    57  	jsonhttp.OK(w, redistributionStatusResponse{
    58  		MinimumGasFunds:           bigint.Wrap(minGasFunds),
    59  		HasSufficientFunds:        hasSufficientFunds,
    60  		IsFrozen:                  status.IsFrozen,
    61  		IsFullySynced:             status.IsFullySynced,
    62  		Phase:                     status.Phase.String(),
    63  		LastWonRound:              status.LastWonRound,
    64  		LastPlayedRound:           status.LastPlayedRound,
    65  		LastFrozenRound:           status.LastFrozenRound,
    66  		LastSelectedRound:         status.LastSelectedRound,
    67  		LastSampleDurationSeconds: status.SampleDuration.Seconds(),
    68  		Round:                     status.Round,
    69  		Block:                     status.Block,
    70  		Reward:                    bigint.Wrap(status.Reward),
    71  		Fees:                      bigint.Wrap(status.Fees),
    72  		IsHealthy:                 status.IsHealthy,
    73  	})
    74  }