Skip to content

Commit

Permalink
reserve state debug api (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
istae authored and acud committed Apr 26, 2021
1 parent a671a09 commit 470ea71
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pkg/debugapi/debugapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/settlement"
"github.com/ethersphere/bee/pkg/settlement/swap"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
Expand Down Expand Up @@ -46,6 +47,7 @@ type Service struct {
chequebookEnabled bool
chequebook chequebook.Service
swap swap.ApiInterface
batchStore postage.Storer
corsAllowedOrigins []string
metricsRegistry *prometheus.Registry
// handler is changed in the Configure method
Expand Down Expand Up @@ -76,7 +78,7 @@ func New(overlay swarm.Address, publicKey, pssPublicKey ecdsa.PublicKey, ethereu
// Configure injects required dependencies and configuration parameters and
// constructs HTTP routes that depend on them. It is intended and safe to call
// this method only once.
func (s *Service) Configure(p2p p2p.DebugService, pingpong pingpong.Interface, topologyDriver topology.Driver, storer storage.Storer, tags *tags.Tags, accounting accounting.Interface, settlement settlement.Interface, chequebookEnabled bool, swap swap.ApiInterface, chequebook chequebook.Service) {
func (s *Service) Configure(p2p p2p.DebugService, pingpong pingpong.Interface, topologyDriver topology.Driver, storer storage.Storer, tags *tags.Tags, accounting accounting.Interface, settlement settlement.Interface, chequebookEnabled bool, swap swap.ApiInterface, chequebook chequebook.Service, batchStore postage.Storer) {
s.p2p = p2p
s.pingpong = pingpong
s.topologyDriver = topologyDriver
Expand All @@ -87,6 +89,7 @@ func (s *Service) Configure(p2p p2p.DebugService, pingpong pingpong.Interface, t
s.chequebookEnabled = chequebookEnabled
s.chequebook = chequebook
s.swap = swap
s.batchStore = batchStore

s.setRouter(s.newRouter())
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/debugapi/debugapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethersphere/bee/pkg/p2p/mock"
p2pmock "github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/pingpong"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/resolver"
chequebookmock "github.com/ethersphere/bee/pkg/settlement/swap/chequebook/mock"
swapmock "github.com/ethersphere/bee/pkg/settlement/swap/mock"
Expand Down Expand Up @@ -51,6 +52,7 @@ type testServerOptions struct {
SettlementOpts []swapmock.Option
ChequebookOpts []chequebookmock.Option
SwapOpts []swapmock.Option
BatchStore postage.Storer
}

type testServer struct {
Expand All @@ -65,7 +67,7 @@ func newTestServer(t *testing.T, o testServerOptions) *testServer {
chequebook := chequebookmock.NewChequebook(o.ChequebookOpts...)
swapserv := swapmock.NewApiInterface(o.SwapOpts...)
s := debugapi.New(o.Overlay, o.PublicKey, o.PSSPublicKey, o.EthereumAddress, logging.New(ioutil.Discard, 0), nil, o.CORSAllowedOrigins)
s.Configure(o.P2P, o.Pingpong, topologyDriver, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook)
s.Configure(o.P2P, o.Pingpong, topologyDriver, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, o.BatchStore)
ts := httptest.NewServer(s)
t.Cleanup(ts.Close)

Expand Down Expand Up @@ -162,7 +164,7 @@ func TestServer_Configure(t *testing.T) {
}),
)

s.Configure(o.P2P, o.Pingpong, topologyDriver, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook)
s.Configure(o.P2P, o.Pingpong, topologyDriver, o.Storer, o.Tags, acc, settlement, true, swapserv, chequebook, nil)

testBasicRouter(t, client)
jsonhttptest.Request(t, client, http.MethodGet, "/readiness", http.StatusOK,
Expand Down
15 changes: 15 additions & 0 deletions pkg/debugapi/postage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package debugapi

import (
"net/http"

"github.com/ethersphere/bee/pkg/jsonhttp"
)

func (s *Service) reserveStateHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.OK(w, s.batchStore.GetReserveState())
}
31 changes: 31 additions & 0 deletions pkg/debugapi/postage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package debugapi_test

import (
"net/http"
"testing"

"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/postage/batchstore/mock"
)

func TestReservestate(t *testing.T) {

ts := newTestServer(t, testServerOptions{
BatchStore: mock.New(mock.WithReserveState(&postage.Reservestate{
Radius: 5,
})),
})

t.Run("ok", func(t *testing.T) {
jsonhttptest.Request(t, ts.Client, http.MethodGet, "/reservestate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&postage.Reservestate{
Radius: 5,
}),
)
})
}
4 changes: 4 additions & 0 deletions pkg/debugapi/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (s *Service) newRouter() *mux.Router {
"POST": http.HandlerFunc(s.pingpongHandler),
})

router.Handle("/reservestate", jsonhttp.MethodHandler{
"GET": http.HandlerFunc(s.reserveStateHandler),
})

router.Handle("/connect/{multi-address:.+}", jsonhttp.MethodHandler{
"POST": http.HandlerFunc(s.peerConnectHandler),
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
}

// inject dependencies and configure full debug api http path routes
debugAPIService.Configure(p2ps, pingPong, kad, storer, tagService, acc, settlement, o.SwapEnable, swapService, chequebookService)
debugAPIService.Configure(p2ps, pingPong, kad, storer, tagService, acc, settlement, o.SwapEnable, swapService, chequebookService, batchStore)
}

if err := kad.Start(p2pCtx); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/postage/batchstore/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var _ postage.Storer = (*BatchStore)(nil)

// BatchStore is a mock BatchStorer
type BatchStore struct {
rs *postage.Reservestate
cs *postage.ChainState
id []byte
batch *postage.Batch
Expand All @@ -39,6 +40,13 @@ func New(opts ...Option) *BatchStore {
return bs
}

// WithChainState will set the initial chainstate in the ChainStore mock.
func WithReserveState(rs *postage.Reservestate) Option {
return func(bs *BatchStore) {
bs.rs = rs
}
}

// WithChainState will set the initial chainstate in the ChainStore mock.
func WithChainState(cs *postage.ChainState) Option {
return func(bs *BatchStore) {
Expand Down Expand Up @@ -111,3 +119,7 @@ func (bs *BatchStore) PutChainState(cs *postage.ChainState) error {
bs.cs = cs
return nil
}

func (bs *BatchStore) GetReserveState() *postage.Reservestate {
return bs.rs
}
9 changes: 9 additions & 0 deletions pkg/postage/batchstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func New(st storage.StateStorer, unreserveFunc unreserveFn) (postage.Storer, err
return &store{st, cs, rs, unreserveFunc, newMetrics()}, nil
}

func (s *store) GetReserveState() *postage.Reservestate {
return &postage.Reservestate{
Radius: s.rs.Radius,
Available: s.rs.Available,
Outer: new(big.Int).Set(s.rs.Outer),
Inner: new(big.Int).Set(s.rs.Inner),
}
}

// Get returns a batch from the batchstore with the given ID.
func (s *store) Get(id []byte) (*postage.Batch, error) {
b := &postage.Batch{}
Expand Down
1 change: 1 addition & 0 deletions pkg/postage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Storer interface {
Put(*Batch, *big.Int, uint8) error
PutChainState(*ChainState) error
GetChainState() *ChainState
GetReserveState() *Reservestate
}

// Listener provides a blockchain event iterator.
Expand Down
14 changes: 14 additions & 0 deletions pkg/postage/reservestate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package postage

import "math/big"

type Reservestate struct {
Radius uint8 `json:"radius"`
Available int64 `json:"available"`
Outer *big.Int `json:"outer"` // lower value limit for outer layer = the further half of chunks
Inner *big.Int `json:"inner"`
}

0 comments on commit 470ea71

Please sign in to comment.