Skip to content

Commit

Permalink
feat: merge bee and debug APis (part 1) (#4651)
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill authored Apr 30, 2024
1 parent 7bc132d commit 52be59c
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 106 deletions.
1 change: 1 addition & 0 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (c *command) initStartCmd() (err error) {

fmt.Print(beeWelcomeMessage)
fmt.Printf("\n\nversion: %v - planned to be supported until %v, please follow https://ethswarm.org/\n\n", bee.Version, endSupportDate())
fmt.Printf("DEPRECATION NOTICE:\nThe Debug API is deprecated and will be removed in the next release, version [2.2.0].\nPlease update your integrations to use the main Bee API to avoid service disruptions.\n\n")
logger.Info("bee version", "version", bee.Version)

go startTimeBomb(logger)
Expand Down
313 changes: 274 additions & 39 deletions openapi/Swarm.yaml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,11 @@ components:
signer:
type: string

WalletTxResponse:
type: object
properties:
transactionHash:
$ref: "#/components/schemas/TransactionHash"
headers:
SwarmTag:
description: "Tag UID"
Expand Down
16 changes: 4 additions & 12 deletions openapi/SwarmDebug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -699,23 +699,23 @@ paths:
name: amount
required: true
schema:
$ref: "#/components/schemas/BigInt"
$ref: "SwarmCommon.yaml#/components/schemas/BigInt"
- in: query
name: address
required: true
schema:
$ref: "#/components/schemas/EthereumAddress"
$ref: "SwarmCommon.yaml#/components/schemas/EthereumAddress"
- in: path
name: coin
required: true
schema:
$ref: "#/components/schemas/SwarmAddress"
$ref: "SwarmCommon.yaml#/components/schemas/SwarmAddress"
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/WalletTxResponse'
$ref: 'SwarmCommon.yaml#/components/schemas/WalletTxResponse'
description: OK
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
Expand Down Expand Up @@ -1168,11 +1168,3 @@ paths:
$ref: "SwarmCommon.yaml#/components/responses/400"
default:
description: Default response.

components:
schemas:
WalletTxResponse:
type: object
properties:
transactionHash:
$ref: "#/components/schemas/TransactionHash"
3 changes: 1 addition & 2 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ func newTestServer(t *testing.T, o testServerOptions) (*http.Client, *websocket.
WsPingPeriod: o.WsPingPeriod,
Restricted: o.Restricted,
}, extraOpts, 1, erc20)

if o.DebugAPI {
s.MountTechnicalDebug()
s.MountDebug(false)
s.MountDebug()
} else {
s.MountAPI()
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (s *Service) MountTechnicalDebug() {
)
}

func (s *Service) MountDebug(restricted bool) {
s.mountBusinessDebug(restricted)
func (s *Service) MountDebug() {
s.mountBusinessDebug()

s.Handler = web.ChainHandlers(
httpaccess.NewHTTPAccessLogHandler(s.logger, s.tracer, "debug api access"),
Expand Down Expand Up @@ -141,8 +141,12 @@ func (s *Service) mountTechnicalDebug() {
s.router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
s.router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
s.router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
s.router.PathPrefix("/debug/pprof/").Handler(http.HandlerFunc(pprof.Index))

pprofRootHandlerF := pprof.Index
if s.Restricted {
pprofRootHandlerF = web.ChainHandlers(auth.PermissionCheckHandler(s.auth), web.FinalHandler(http.HandlerFunc(pprof.Index))).ServeHTTP
}
s.router.PathPrefix("/debug/pprof/").Handler(http.HandlerFunc(pprofRootHandlerF))
s.router.Handle("/debug/vars", expvar.Handler())

s.router.Handle("/loggers", jsonhttp.MethodHandler{
Expand Down Expand Up @@ -364,9 +368,10 @@ func (s *Service) mountAPI() {
}
}

func (s *Service) mountBusinessDebug(restricted bool) {
func (s *Service) mountBusinessDebug() {
handle := func(path string, handler http.Handler) {
if restricted {
s.logger.Warning("DEPRECATION NOTICE: This endpoint is now part of the main Bee API. The Debug API will be removed in the next release, version [2.2.0]. Update your integrations to use the main Bee API to avoid service disruptions.")
if s.Restricted {
handler = web.ChainHandlers(auth.PermissionCheckHandler(s.auth), web.FinalHandler(handler))
}
s.router.Handle(path, handler)
Expand Down
10 changes: 10 additions & 0 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ func applyPolicies(e *casbin.Enforcer) error {
{"maintainer", "/redistributionstate", "GET"},
{"maintainer", "/debugstore", "GET"},
{"consumer", "/rchash", "GET"},
{"maintainer", "/debug/*", "GET"},
{"maintainer", "/metrics", "GET"},
{"maintainer", "/node", "GET"},
{"maintainer", "/loggers", "GET"},
{"maintainer", "/loggers/*", "(GET)|(PUT)"},
{"maintainer", "/status", "GET"},
{"maintainer", "/status/peers", "GET"},
{"maintainer", "/rcash/*", "GET"},
{"maintainer", "/batches", "GET"},
{"maintainer", "/timesettlements", "GET"},
})

if err != nil {
Expand Down
11 changes: 5 additions & 6 deletions pkg/node/devnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,19 +405,18 @@ func NewDevBee(logger log.Logger, o *DevOptions) (b *DevBee, err error) {
WsPingPeriod: 60 * time.Second,
Restricted: o.Restricted,
}, debugOpts, 1, erc20)
apiService.MountTechnicalDebug()
apiService.MountAPI()
apiService.SetProbe(probe)

if o.Restricted {
apiService.SetP2P(p2ps)
apiService.SetSwarmAddress(&swarmAddress)
apiService.MountDebug(true)
}
apiService.SetP2P(p2ps)
apiService.SetSwarmAddress(&swarmAddress)
apiService.MountDebug()

if o.DebugAPIAddr != "" {
debugApiService.SetP2P(p2ps)
debugApiService.SetSwarmAddress(&swarmAddress)
debugApiService.MountDebug(false)
debugApiService.MountDebug()

debugApiService.Configure(signer, authenticator, tracer, api.Options{
CORSAllowedOrigins: o.CORSAllowedOrigins,
Expand Down
108 changes: 66 additions & 42 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import (
"github.com/ethersphere/bee/v2/pkg/storageincentives"
"github.com/ethersphere/bee/v2/pkg/storageincentives/redistribution"
"github.com/ethersphere/bee/v2/pkg/storageincentives/staking"
storer "github.com/ethersphere/bee/v2/pkg/storer"
"github.com/ethersphere/bee/v2/pkg/storer"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/topology"
"github.com/ethersphere/bee/v2/pkg/topology/kademlia"
Expand Down Expand Up @@ -402,6 +402,7 @@ func NewBee(
}
b.stamperStoreCloser = stamperStore

var apiService *api.Service
var debugService *api.Service

if o.DebugAPIAddr != "" {
Expand Down Expand Up @@ -433,6 +434,7 @@ func NewBee(
o.CORSAllowedOrigins,
stamperStore,
)
debugService.Restricted = o.Restricted
debugService.MountTechnicalDebug()
debugService.SetProbe(probe)

Expand All @@ -455,9 +457,19 @@ func NewBee(
b.debugAPIServer = debugAPIServer
}

var apiService *api.Service
if o.APIAddr != "" {
if o.MutexProfile {
_ = runtime.SetMutexProfileFraction(1)
}
if o.BlockProfile {
runtime.SetBlockProfileRate(1)
}

apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
}

if o.Restricted {
apiService = api.New(
*publicKey,
pssPrivateKey.PublicKey,
Expand All @@ -473,6 +485,7 @@ func NewBee(
o.CORSAllowedOrigins,
stamperStore,
)
apiService.Restricted = o.Restricted
apiService.MountTechnicalDebug()
apiService.SetProbe(probe)

Expand All @@ -483,11 +496,6 @@ func NewBee(
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
}

go func() {
logger.Info("starting debug & api server", "address", apiListener.Addr())

Expand Down Expand Up @@ -558,8 +566,6 @@ func NewBee(
)
}

apiService.SetSwarmAddress(&swarmAddress)

lightNodes := lightnode.NewContainer(swarmAddress)

bootnodes := make([]ma.Multiaddr, 0, len(o.Bootnodes))
Expand Down Expand Up @@ -635,6 +641,8 @@ func NewBee(

if debugService != nil {
registry = debugService.MetricsRegistry()
} else if apiService != nil {
registry = apiService.MetricsRegistry()
}

p2ps, err := libp2p.New(ctx, signer, networkID, swarmAddress, addr, addressbook, stateStore, lightNodes, logger, tracer, libp2p.Options{
Expand Down Expand Up @@ -1096,10 +1104,49 @@ func NewBee(
}

if o.APIAddr != "" {
if apiService == nil {
apiService = api.New(*publicKey, pssPrivateKey.PublicKey, overlayEthAddress, o.WhitelistedWithdrawalAddress, logger, transactionService, batchStore, beeNodeMode, o.ChequebookEnable, o.SwapEnable, chainBackend, o.CORSAllowedOrigins, stamperStore)
apiService.SetProbe(probe)
apiService.SetRedistributionAgent(agent)
// register metrics from components
apiService.MustRegisterMetrics(p2ps.Metrics()...)
apiService.MustRegisterMetrics(pingPong.Metrics()...)
apiService.MustRegisterMetrics(acc.Metrics()...)
apiService.MustRegisterMetrics(localStore.Metrics()...)
apiService.MustRegisterMetrics(kad.Metrics()...)
apiService.MustRegisterMetrics(saludService.Metrics()...)
apiService.MustRegisterMetrics(stateStoreMetrics.Metrics()...)

if pullerService != nil {
apiService.MustRegisterMetrics(pullerService.Metrics()...)
}

if agent != nil {
apiService.MustRegisterMetrics(agent.Metrics()...)
}

apiService.MustRegisterMetrics(pushSyncProtocol.Metrics()...)
apiService.MustRegisterMetrics(pusherService.Metrics()...)
apiService.MustRegisterMetrics(pullSyncProtocol.Metrics()...)
apiService.MustRegisterMetrics(retrieval.Metrics()...)
apiService.MustRegisterMetrics(lightNodes.Metrics()...)
apiService.MustRegisterMetrics(hive.Metrics()...)

if bs, ok := batchStore.(metrics.Collector); ok {
apiService.MustRegisterMetrics(bs.Metrics()...)
}
if ls, ok := eventListener.(metrics.Collector); ok {
apiService.MustRegisterMetrics(ls.Metrics()...)
}
if pssServiceMetrics, ok := pssService.(metrics.Collector); ok {
apiService.MustRegisterMetrics(pssServiceMetrics.Metrics()...)
}
if swapBackendMetrics, ok := chainBackend.(metrics.Collector); ok {
apiService.MustRegisterMetrics(swapBackendMetrics.Metrics()...)
}

if l, ok := logger.(metrics.Collector); ok {
apiService.MustRegisterMetrics(l.Metrics()...)
}
apiService.MustRegisterMetrics(pseudosettleService.Metrics()...)
if swapService != nil {
apiService.MustRegisterMetrics(swapService.Metrics()...)
}

apiService.Configure(signer, authenticator, tracer, api.Options{
Expand All @@ -1109,34 +1156,11 @@ func NewBee(
}, extraOpts, chainID, erc20Service)

apiService.MountAPI()
apiService.MountDebug()

if !o.Restricted {
apiServer := &http.Server{
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
Handler: apiService,
ErrorLog: stdlog.New(b.errorLogWriter, "", 0),
}

apiListener, err := net.Listen("tcp", o.APIAddr)
if err != nil {
return nil, fmt.Errorf("api listener: %w", err)
}

go func() {
logger.Info("starting api server", "address", apiListener.Addr())
if err := apiServer.Serve(apiListener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Debug("api server failed to start", "error", err)
logger.Error(nil, "api server failed to start")
}
}()

b.apiServer = apiServer
b.apiCloser = apiService
} else {
// in Restricted mode we mount debug endpoints
apiService.MountDebug(o.Restricted)
}
debugService.SetP2P(p2ps)
debugService.SetSwarmAddress(&swarmAddress)
debugService.SetRedistributionAgent(agent)
}

if o.DebugAPIAddr != "" {
Expand Down Expand Up @@ -1195,7 +1219,7 @@ func NewBee(

debugService.SetP2P(p2ps)
debugService.SetSwarmAddress(&swarmAddress)
debugService.MountDebug(false)
debugService.MountDebug()
debugService.SetRedistributionAgent(agent)
}

Expand Down

0 comments on commit 52be59c

Please sign in to comment.