Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ components:
$ref: "#/components/schemas/BigInt"
currentPrice:
$ref: "#/components/schemas/BigInt"
minimumValidityBlocks:
type: integer

PeerAccountingData:
type: object
Expand Down
25 changes: 17 additions & 8 deletions pkg/api/postage.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,11 @@ type reserveStateResponse struct {
}

type chainStateResponse struct {
ChainTip uint64 `json:"chainTip"` // ChainTip (block height).
Block uint64 `json:"block"` // The block number of the last postage event.
TotalAmount *bigint.BigInt `json:"totalAmount"` // Cumulative amount paid per stamp.
CurrentPrice *bigint.BigInt `json:"currentPrice"` // Bzz/chunk/block normalised price.
ChainTip uint64 `json:"chainTip"` // ChainTip (block height).
Block uint64 `json:"block"` // The block number of the last postage event.
TotalAmount *bigint.BigInt `json:"totalAmount"` // Cumulative amount paid per stamp.
CurrentPrice *bigint.BigInt `json:"currentPrice"` // Bzz/chunk/block normalised price.
MinimumValidityBlocks uint64 `json:"minimumValidityBlocks,omitempty"` // Minimum number of blocks a new postage batch must remain valid.
}

func (s *Service) reserveStateHandler(w http.ResponseWriter, _ *http.Request) {
Expand Down Expand Up @@ -454,11 +455,19 @@ func (s *Service) chainStateHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.InternalServerError(w, "block number unavailable")
return
}
minimumValidityBlocks, err := s.postageContract.MinimumValidityBlocks(r.Context())
if err != nil && !errors.Is(err, postagecontract.ErrChainDisabled) {
logger.Debug("get minimum validity blocks failed", "error", err)
logger.Error(nil, "get minimum validity blocks failed")
jsonhttp.InternalServerError(w, "minimum validity blocks unavailable")
return
}
jsonhttp.OK(w, chainStateResponse{
ChainTip: chainTip,
Block: state.Block,
TotalAmount: bigint.Wrap(state.TotalAmount),
CurrentPrice: bigint.Wrap(state.CurrentPrice),
ChainTip: chainTip,
Block: state.Block,
TotalAmount: bigint.Wrap(state.TotalAmount),
CurrentPrice: bigint.Wrap(state.CurrentPrice),
MinimumValidityBlocks: minimumValidityBlocks,
})
}

Expand Down
15 changes: 11 additions & 4 deletions pkg/api/postage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,25 @@ func TestChainState(t *testing.T) {
TotalAmount: big.NewInt(50),
CurrentPrice: big.NewInt(5),
}
contract := contractMock.New(
contractMock.WithMinimumValidityBlocksFunc(func(ctx context.Context) (uint64, error) {
return 17280, nil
}),
)
ts, _, _, _ := newTestServer(t, testServerOptions{
BatchStore: mock.New(mock.WithChainState(cs)),
BackendOpts: []backendmock.Option{backendmock.WithBlockNumberFunc(func(ctx context.Context) (uint64, error) {
return 1, nil
})},
PostageContract: contract,
})
jsonhttptest.Request(t, ts, http.MethodGet, "/chainstate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&api.ChainStateResponse{
ChainTip: 1,
Block: 123456,
TotalAmount: bigint.Wrap(big.NewInt(50)),
CurrentPrice: bigint.Wrap(big.NewInt(5)),
ChainTip: 1,
Block: 123456,
TotalAmount: bigint.Wrap(big.NewInt(50)),
CurrentPrice: bigint.Wrap(big.NewInt(5)),
MinimumValidityBlocks: 17280,
}),
)
})
Expand Down
13 changes: 13 additions & 0 deletions pkg/postage/postagecontract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Interface interface {
TopUpBatch(ctx context.Context, batchID []byte, topupBalance *big.Int) (common.Hash, error)
DiluteBatch(ctx context.Context, batchID []byte, newDepth uint8) (common.Hash, error)
Paused(ctx context.Context) (bool, error)
MinimumValidityBlocks(ctx context.Context) (uint64, error)
PostageBatchExpirer
}

Expand Down Expand Up @@ -508,6 +509,14 @@ func (c *postageContract) Paused(ctx context.Context) (bool, error) {
return results[0].(bool), nil
}

func (c *postageContract) MinimumValidityBlocks(ctx context.Context) (uint64, error) {
var minimumValidityBlocks uint64
if err := c.getProperty(ctx, "minimumValidityBlocks", &minimumValidityBlocks); err != nil {
return 0, err
}
return minimumValidityBlocks, nil
}

type batchCreatedEvent struct {
BatchId [32]byte
TotalAmount *big.Int
Expand Down Expand Up @@ -536,6 +545,10 @@ func (m *noOpPostageContract) Paused(context.Context) (bool, error) {
return false, nil
}

func (m *noOpPostageContract) MinimumValidityBlocks(context.Context) (uint64, error) {
return 0, ErrChainDisabled
}

func (m *noOpPostageContract) ExpireBatches(context.Context) error {
return ErrChainDisabled
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/postage/postagecontract/mock/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
)

type contractMock struct {
createBatch func(ctx context.Context, initialBalance *big.Int, depth uint8, immutable bool, label string) (common.Hash, []byte, error)
topupBatch func(ctx context.Context, id []byte, amount *big.Int) (common.Hash, error)
diluteBatch func(ctx context.Context, id []byte, newDepth uint8) (common.Hash, error)
expireBatches func(ctx context.Context) error
paused func(ctx context.Context) (bool, error)
createBatch func(ctx context.Context, initialBalance *big.Int, depth uint8, immutable bool, label string) (common.Hash, []byte, error)
topupBatch func(ctx context.Context, id []byte, amount *big.Int) (common.Hash, error)
diluteBatch func(ctx context.Context, id []byte, newDepth uint8) (common.Hash, error)
expireBatches func(ctx context.Context) error
paused func(ctx context.Context) (bool, error)
minimumValidityBlocks func(ctx context.Context) (uint64, error)
}

func (c *contractMock) CreateBatch(ctx context.Context, initialBalance *big.Int, depth uint8, immutable bool, label string) (common.Hash, []byte, error) {
Expand All @@ -40,6 +41,10 @@ func (s *contractMock) Paused(ctx context.Context) (bool, error) {
return s.paused(ctx)
}

func (c *contractMock) MinimumValidityBlocks(ctx context.Context) (uint64, error) {
return c.minimumValidityBlocks(ctx)
}

// Option is an option passed to New
type Option func(*contractMock)

Expand Down Expand Up @@ -83,3 +88,9 @@ func WithPaused(f func(ctx context.Context) (bool, error)) Option {
mock.paused = f
}
}

func WithMinimumValidityBlocksFunc(f func(ctx context.Context) (uint64, error)) Option {
return func(mock *contractMock) {
mock.minimumValidityBlocks = f
}
}
Loading