Commit 7383d3dc authored by Adam Uhlíř's avatar Adam Uhlíř Committed by GitHub

fix: marshal bigint as string (#1735)

parent e1025971
......@@ -34,6 +34,11 @@ components:
pssPublicKey:
$ref: "#/components/schemas/PublicKey"
BigInt:
description: Numeric string that represents integer which might exceeds `Number.MAX_SAFE_INTEGER` limit (2^53-1)
type: string
example: "1000000000000000000"
ReserveState:
type: object
properties:
......@@ -42,9 +47,9 @@ components:
available:
type: integer
outer:
type: integer
$ref: "#/components/schemas/BigInt"
inner:
type: integer
$ref: "#/components/schemas/BigInt"
ChainState:
type: object
......@@ -62,7 +67,7 @@ components:
peer:
$ref: "#/components/schemas/SwarmAddress"
balance:
type: integer
$ref: "#/components/schemas/BigInt"
Balances:
type: object
......@@ -109,7 +114,7 @@ components:
chequebook:
$ref: "#/components/schemas/EthereumAddress"
payout:
type: integer
$ref: "#/components/schemas/BigInt"
ChequeAllPeersResponse:
type: object
......@@ -134,9 +139,9 @@ components:
type: object
properties:
totalBalance:
type: integer
$ref: "#/components/schemas/BigInt"
availableBalance:
type: integer
$ref: "#/components/schemas/BigInt"
ChequebookAddress:
type: object
......@@ -159,7 +164,7 @@ components:
type: string
pattern: "^[A-Fa-f0-9]{40}$"
example: "36b7efd913ca4cf880b8eeac5093fa27b0825906"
FileName:
type: string
......@@ -388,7 +393,7 @@ components:
recipient:
$ref: "#/components/schemas/EthereumAddress"
lastPayout:
type: integer
$ref: "#/components/schemas/BigInt"
bounced:
type: boolean
......@@ -404,7 +409,7 @@ components:
result:
$ref: "#/components/schemas/SwapCashoutResult"
uncashedAmount:
type: integer
$ref: "#/components/schemas/BigInt"
TagName:
type: string
......
// 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 bigint
import (
"encoding/json"
"fmt"
"math/big"
)
type BigInt struct {
*big.Int
}
func (i *BigInt) MarshalJSON() ([]byte, error) {
if i.Int == nil {
return []byte("null"), nil
}
return []byte(fmt.Sprintf(`"%s"`, i.String())), nil
}
func (i *BigInt) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
if err != nil {
return err
}
if i.Int == nil {
i.Int = new(big.Int)
}
i.SetString(val, 10)
return nil
}
//Wrap wraps big.Int pointer into BigInt struct.
func Wrap(i *big.Int) *BigInt {
return &BigInt{Int: i}
}
// 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 bigint_test
import (
"encoding/json"
"github.com/ethersphere/bee/pkg/bigint"
"math"
"math/big"
"reflect"
"testing"
)
func TestMarshaling(t *testing.T) {
mar, err := json.Marshal(struct {
Bg *bigint.BigInt
}{
Bg: bigint.Wrap(new(big.Int).Mul(big.NewInt(math.MaxInt64), big.NewInt(math.MaxInt64))),
})
if err != nil {
t.Errorf("Marshaling failed: %v", err)
}
if !reflect.DeepEqual(mar, []byte("{\"Bg\":\"85070591730234615847396907784232501249\"}")) {
t.Errorf("Wrongly marshaled data")
}
}
......@@ -6,10 +6,10 @@ package debugapi
import (
"errors"
"math/big"
"net/http"
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/gorilla/mux"
......@@ -23,8 +23,8 @@ var (
)
type balanceResponse struct {
Peer string `json:"peer"`
Balance *big.Int `json:"balance"`
Peer string `json:"peer"`
Balance *bigint.BigInt `json:"balance"`
}
type balancesResponse struct {
......@@ -45,7 +45,7 @@ func (s *Service) balancesHandler(w http.ResponseWriter, r *http.Request) {
for k := range balances {
balResponses[i] = balanceResponse{
Peer: k,
Balance: balances[k],
Balance: bigint.Wrap(balances[k]),
}
i++
}
......@@ -77,7 +77,7 @@ func (s *Service) peerBalanceHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.OK(w, balanceResponse{
Peer: peer.String(),
Balance: balance,
Balance: bigint.Wrap(balance),
})
}
......@@ -95,7 +95,7 @@ func (s *Service) compensatedBalancesHandler(w http.ResponseWriter, r *http.Requ
for k := range balances {
balResponses[i] = balanceResponse{
Peer: k,
Balance: balances[k],
Balance: bigint.Wrap(balances[k]),
}
i++
}
......@@ -127,6 +127,6 @@ func (s *Service) compensatedPeerBalanceHandler(w http.ResponseWriter, r *http.R
jsonhttp.OK(w, balanceResponse{
Peer: peer.String(),
Balance: balance,
Balance: bigint.Wrap(balance),
})
}
......@@ -13,6 +13,7 @@ import (
"github.com/ethersphere/bee/pkg/accounting"
"github.com/ethersphere/bee/pkg/accounting/mock"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
......@@ -35,15 +36,15 @@ func TestBalances(t *testing.T) {
[]debugapi.BalanceResponse{
{
Peer: "DEAD",
Balance: big.NewInt(1000000000000000000),
Balance: bigint.Wrap(big.NewInt(1000000000000000000)),
},
{
Peer: "BEEF",
Balance: big.NewInt(-100000000000000000),
Balance: bigint.Wrap(big.NewInt(-100000000000000000)),
},
{
Peer: "PARTY",
Balance: big.NewInt(0),
Balance: bigint.Wrap(big.NewInt(0)),
},
},
}
......@@ -89,7 +90,7 @@ func TestBalancesPeers(t *testing.T) {
jsonhttptest.Request(t, testServer.Client, http.MethodGet, "/balances/"+peer, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.BalanceResponse{
Peer: peer,
Balance: big.NewInt(100000000000000000),
Balance: bigint.Wrap(big.NewInt(100000000000000000)),
}),
)
}
......@@ -188,15 +189,15 @@ func TestConsumedBalances(t *testing.T) {
[]debugapi.BalanceResponse{
{
Peer: "DEAD",
Balance: big.NewInt(1000000000000000000),
Balance: bigint.Wrap(big.NewInt(1000000000000000000)),
},
{
Peer: "BEEF",
Balance: big.NewInt(-100000000000000000),
Balance: bigint.Wrap(big.NewInt(-100000000000000000)),
},
{
Peer: "PARTY",
Balance: big.NewInt(0),
Balance: bigint.Wrap(big.NewInt(0)),
},
},
}
......@@ -242,7 +243,7 @@ func TestConsumedPeers(t *testing.T) {
jsonhttptest.Request(t, testServer.Client, http.MethodGet, "/consumed/"+peer, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.BalanceResponse{
Peer: peer,
Balance: big.NewInt(1000000000000000000),
Balance: bigint.Wrap(big.NewInt(1000000000000000000)),
}),
)
}
......
......@@ -11,6 +11,7 @@ import (
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
......@@ -39,8 +40,8 @@ var (
)
type chequebookBalanceResponse struct {
TotalBalance *big.Int `json:"totalBalance"`
AvailableBalance *big.Int `json:"availableBalance"`
TotalBalance *bigint.BigInt `json:"totalBalance"`
AvailableBalance *bigint.BigInt `json:"availableBalance"`
}
type chequebookAddressResponse struct {
......@@ -48,9 +49,9 @@ type chequebookAddressResponse struct {
}
type chequebookLastChequePeerResponse struct {
Beneficiary string `json:"beneficiary"`
Chequebook string `json:"chequebook"`
Payout *big.Int `json:"payout"`
Beneficiary string `json:"beneficiary"`
Chequebook string `json:"chequebook"`
Payout *bigint.BigInt `json:"payout"`
}
type chequebookLastChequesPeerResponse struct {
......@@ -80,7 +81,7 @@ func (s *Service) chequebookBalanceHandler(w http.ResponseWriter, r *http.Reques
return
}
jsonhttp.OK(w, chequebookBalanceResponse{TotalBalance: balance, AvailableBalance: availableBalance})
jsonhttp.OK(w, chequebookBalanceResponse{TotalBalance: bigint.Wrap(balance), AvailableBalance: bigint.Wrap(availableBalance)})
}
func (s *Service) chequebookAddressHandler(w http.ResponseWriter, r *http.Request) {
......@@ -110,7 +111,7 @@ func (s *Service) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Reque
lastSentResponse = &chequebookLastChequePeerResponse{
Beneficiary: lastSent.Cheque.Beneficiary.String(),
Chequebook: lastSent.Cheque.Chequebook.String(),
Payout: lastSent.Cheque.CumulativePayout,
Payout: bigint.Wrap(lastSent.Cheque.CumulativePayout),
}
}
......@@ -126,7 +127,7 @@ func (s *Service) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Reque
lastReceivedResponse = &chequebookLastChequePeerResponse{
Beneficiary: lastReceived.Cheque.Beneficiary.String(),
Chequebook: lastReceived.Cheque.Chequebook.String(),
Payout: lastReceived.Cheque.CumulativePayout,
Payout: bigint.Wrap(lastReceived.Cheque.CumulativePayout),
}
}
......@@ -160,7 +161,7 @@ func (s *Service) chequebookAllLastHandler(w http.ResponseWriter, r *http.Reques
LastSent: &chequebookLastChequePeerResponse{
Beneficiary: j.Cheque.Beneficiary.String(),
Chequebook: j.Cheque.Chequebook.String(),
Payout: j.Cheque.CumulativePayout,
Payout: bigint.Wrap(j.Cheque.CumulativePayout),
},
LastReceived: nil,
}
......@@ -171,7 +172,7 @@ func (s *Service) chequebookAllLastHandler(w http.ResponseWriter, r *http.Reques
t.LastReceived = &chequebookLastChequePeerResponse{
Beneficiary: j.Cheque.Beneficiary.String(),
Chequebook: j.Cheque.Chequebook.String(),
Payout: j.Cheque.CumulativePayout,
Payout: bigint.Wrap(j.Cheque.CumulativePayout),
}
lcr[i] = t
} else {
......@@ -181,7 +182,7 @@ func (s *Service) chequebookAllLastHandler(w http.ResponseWriter, r *http.Reques
LastReceived: &chequebookLastChequePeerResponse{
Beneficiary: j.Cheque.Beneficiary.String(),
Chequebook: j.Cheque.Chequebook.String(),
Payout: j.Cheque.CumulativePayout,
Payout: bigint.Wrap(j.Cheque.CumulativePayout),
},
}
}
......@@ -246,7 +247,7 @@ func (s *Service) swapCashoutHandler(w http.ResponseWriter, r *http.Request) {
type swapCashoutStatusResult struct {
Recipient common.Address `json:"recipient"`
LastPayout *big.Int `json:"lastPayout"`
LastPayout *bigint.BigInt `json:"lastPayout"`
Bounced bool `json:"bounced"`
}
......@@ -255,7 +256,7 @@ type swapCashoutStatusResponse struct {
Cheque *chequebookLastChequePeerResponse `json:"lastCashedCheque"`
TransactionHash *common.Hash `json:"transactionHash"`
Result *swapCashoutStatusResult `json:"result"`
UncashedAmount *big.Int `json:"uncashedAmount"`
UncashedAmount *bigint.BigInt `json:"uncashedAmount"`
}
func (s *Service) swapCashoutStatusHandler(w http.ResponseWriter, r *http.Request) {
......@@ -295,13 +296,13 @@ func (s *Service) swapCashoutStatusHandler(w http.ResponseWriter, r *http.Reques
if status.Last.Result != nil {
result = &swapCashoutStatusResult{
Recipient: status.Last.Result.Recipient,
LastPayout: status.Last.Result.TotalPayout,
LastPayout: bigint.Wrap(status.Last.Result.TotalPayout),
Bounced: status.Last.Result.Bounced,
}
}
chequeResponse = &chequebookLastChequePeerResponse{
Chequebook: status.Last.Cheque.Chequebook.String(),
Payout: status.Last.Cheque.CumulativePayout,
Payout: bigint.Wrap(status.Last.Cheque.CumulativePayout),
Beneficiary: status.Last.Cheque.Beneficiary.String(),
}
txHash = &status.Last.TxHash
......@@ -312,7 +313,7 @@ func (s *Service) swapCashoutStatusHandler(w http.ResponseWriter, r *http.Reques
TransactionHash: txHash,
Cheque: chequeResponse,
Result: result,
UncashedAmount: status.UncashedAmount,
UncashedAmount: bigint.Wrap(status.UncashedAmount),
})
}
......
......@@ -13,6 +13,7 @@ import (
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
......@@ -44,8 +45,8 @@ func TestChequebookBalance(t *testing.T) {
})
expected := &debugapi.ChequebookBalanceResponse{
TotalBalance: returnedBalance,
AvailableBalance: returnedAvailableBalance,
TotalBalance: bigint.Wrap(returnedBalance),
AvailableBalance: bigint.Wrap(returnedAvailableBalance),
}
var got *debugapi.ChequebookBalanceResponse
......@@ -339,12 +340,12 @@ func TestChequebookLastCheques(t *testing.T) {
LastReceived: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary.String(),
Chequebook: chequebookAddress1.String(),
Payout: cumulativePayout4,
Payout: bigint.Wrap(cumulativePayout4),
},
LastSent: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary1.String(),
Chequebook: chequebookAddress1.String(),
Payout: cumulativePayout1,
Payout: bigint.Wrap(cumulativePayout1),
},
},
{
......@@ -353,7 +354,7 @@ func TestChequebookLastCheques(t *testing.T) {
LastSent: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary2.String(),
Chequebook: chequebookAddress2.String(),
Payout: cumulativePayout2,
Payout: bigint.Wrap(cumulativePayout2),
},
},
{
......@@ -362,7 +363,7 @@ func TestChequebookLastCheques(t *testing.T) {
LastSent: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary3.String(),
Chequebook: chequebookAddress3.String(),
Payout: cumulativePayout3,
Payout: bigint.Wrap(cumulativePayout3),
},
},
{
......@@ -370,7 +371,7 @@ func TestChequebookLastCheques(t *testing.T) {
LastReceived: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary.String(),
Chequebook: chequebookAddress4.String(),
Payout: cumulativePayout5,
Payout: bigint.Wrap(cumulativePayout5),
},
LastSent: nil,
},
......@@ -379,7 +380,7 @@ func TestChequebookLastCheques(t *testing.T) {
LastReceived: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary.String(),
Chequebook: chequebookAddress5.String(),
Payout: cumulativePayout6,
Payout: bigint.Wrap(cumulativePayout6),
},
LastSent: nil,
},
......@@ -450,12 +451,12 @@ func TestChequebookLastChequesPeer(t *testing.T) {
LastReceived: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary0.String(),
Chequebook: chequebookAddress.String(),
Payout: cumulativePayout2,
Payout: bigint.Wrap(cumulativePayout2),
},
LastSent: &debugapi.ChequebookLastChequePeerResponse{
Beneficiary: beneficiary1.String(),
Chequebook: chequebookAddress.String(),
Payout: cumulativePayout1,
Payout: bigint.Wrap(cumulativePayout1),
},
}
......@@ -589,15 +590,15 @@ func TestChequebookCashoutStatus(t *testing.T) {
TransactionHash: &actionTxHash,
Cheque: &debugapi.ChequebookLastChequePeerResponse{
Chequebook: chequebookAddress.String(),
Payout: cumulativePayout,
Payout: bigint.Wrap(cumulativePayout),
Beneficiary: cheque.Beneficiary.String(),
},
Result: &debugapi.SwapCashoutStatusResult{
Recipient: recipientAddress,
LastPayout: totalPayout,
LastPayout: bigint.Wrap(totalPayout),
Bounced: false,
},
UncashedAmount: uncashedAmount,
UncashedAmount: bigint.Wrap(uncashedAmount),
}
var got *debugapi.SwapCashoutStatusResponse
......@@ -633,11 +634,11 @@ func TestChequebookCashoutStatus(t *testing.T) {
TransactionHash: &actionTxHash,
Cheque: &debugapi.ChequebookLastChequePeerResponse{
Chequebook: chequebookAddress.String(),
Payout: cumulativePayout,
Payout: bigint.Wrap(cumulativePayout),
Beneficiary: cheque.Beneficiary.String(),
},
Result: nil,
UncashedAmount: uncashedAmount,
UncashedAmount: bigint.Wrap(uncashedAmount),
}
var got *debugapi.SwapCashoutStatusResponse
......@@ -668,7 +669,7 @@ func TestChequebookCashoutStatus(t *testing.T) {
TransactionHash: nil,
Cheque: nil,
Result: nil,
UncashedAmount: uncashedAmount,
UncashedAmount: bigint.Wrap(uncashedAmount),
}
var got *debugapi.SwapCashoutStatusResponse
......
......@@ -26,6 +26,8 @@ type (
SwapCashoutStatusResponse = swapCashoutStatusResponse
SwapCashoutStatusResult = swapCashoutStatusResult
TagResponse = tagResponse
ReserveStateResponse = reserveStateResponse
ChainStateResponse = chainStateResponse
)
var (
......
......@@ -7,14 +7,41 @@ package debugapi
import (
"net/http"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
)
type reserveStateResponse struct {
Radius uint8 `json:"radius"`
Available int64 `json:"available"`
Outer *bigint.BigInt `json:"outer"` // lower value limit for outer layer = the further half of chunks
Inner *bigint.BigInt `json:"inner"`
}
type chainStateResponse struct {
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.
}
func (s *Service) reserveStateHandler(w http.ResponseWriter, _ *http.Request) {
jsonhttp.OK(w, s.batchStore.GetReserveState())
state := s.batchStore.GetReserveState()
jsonhttp.OK(w, reserveStateResponse{
Radius: state.Radius,
Available: state.Available,
Outer: bigint.Wrap(state.Outer),
Inner: bigint.Wrap(state.Inner),
})
}
// chainStateHandler returns the current chain state.
func (s *Service) chainStateHandler(w http.ResponseWriter, _ *http.Request) {
jsonhttp.OK(w, s.batchStore.GetChainState())
state := s.batchStore.GetChainState()
jsonhttp.OK(w, chainStateResponse{
Block: state.Block,
TotalAmount: bigint.Wrap(state.TotalAmount),
CurrentPrice: bigint.Wrap(state.CurrentPrice),
})
}
......@@ -9,6 +9,8 @@ import (
"net/http"
"testing"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/postage/batchstore/mock"
......@@ -19,11 +21,15 @@ func TestReserveState(t *testing.T) {
ts := newTestServer(t, testServerOptions{
BatchStore: mock.New(mock.WithReserveState(&postage.ReserveState{
Radius: 5,
Outer: big.NewInt(5),
Inner: big.NewInt(5),
})),
})
jsonhttptest.Request(t, ts.Client, http.MethodGet, "/reservestate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&postage.ReserveState{
jsonhttptest.WithExpectedJSONResponse(&debugapi.ReserveStateResponse{
Radius: 5,
Outer: bigint.Wrap(big.NewInt(5)),
Inner: bigint.Wrap(big.NewInt(5)),
}),
)
})
......@@ -32,7 +38,7 @@ func TestReserveState(t *testing.T) {
BatchStore: mock.New(),
})
jsonhttptest.Request(t, ts.Client, http.MethodGet, "/reservestate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&postage.ReserveState{}),
jsonhttptest.WithExpectedJSONResponse(&debugapi.ReserveStateResponse{}),
)
})
}
......@@ -48,7 +54,11 @@ func TestChainState(t *testing.T) {
BatchStore: mock.New(mock.WithChainState(cs)),
})
jsonhttptest.Request(t, ts.Client, http.MethodGet, "/chainstate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(cs),
jsonhttptest.WithExpectedJSONResponse(&debugapi.ChainStateResponse{
Block: 123456,
TotalAmount: bigint.Wrap(big.NewInt(50)),
CurrentPrice: bigint.Wrap(big.NewInt(5)),
}),
)
})
......@@ -57,7 +67,7 @@ func TestChainState(t *testing.T) {
BatchStore: mock.New(),
})
jsonhttptest.Request(t, ts.Client, http.MethodGet, "/chainstate", http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(&postage.ChainState{}),
jsonhttptest.WithExpectedJSONResponse(&debugapi.ChainStateResponse{}),
)
})
}
......@@ -9,6 +9,7 @@ import (
"math/big"
"net/http"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/settlement"
"github.com/ethersphere/bee/pkg/swarm"
......@@ -21,14 +22,14 @@ var (
)
type settlementResponse struct {
Peer string `json:"peer"`
SettlementReceived *big.Int `json:"received"`
SettlementSent *big.Int `json:"sent"`
Peer string `json:"peer"`
SettlementReceived *bigint.BigInt `json:"received"`
SettlementSent *bigint.BigInt `json:"sent"`
}
type settlementsResponse struct {
TotalSettlementReceived *big.Int `json:"totalReceived"`
TotalSettlementSent *big.Int `json:"totalSent"`
TotalSettlementReceived *bigint.BigInt `json:"totalReceived"`
TotalSettlementSent *bigint.BigInt `json:"totalSent"`
Settlements []settlementResponse `json:"settlements"`
}
......@@ -57,8 +58,8 @@ func (s *Service) settlementsHandler(w http.ResponseWriter, r *http.Request) {
for a, b := range settlementsSent {
settlementResponses[a] = settlementResponse{
Peer: a,
SettlementSent: b,
SettlementReceived: big.NewInt(0),
SettlementSent: bigint.Wrap(b),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
}
totalSent.Add(b, totalSent)
}
......@@ -66,13 +67,13 @@ func (s *Service) settlementsHandler(w http.ResponseWriter, r *http.Request) {
for a, b := range settlementsReceived {
if _, ok := settlementResponses[a]; ok {
t := settlementResponses[a]
t.SettlementReceived = b
t.SettlementReceived = bigint.Wrap(b)
settlementResponses[a] = t
} else {
settlementResponses[a] = settlementResponse{
Peer: a,
SettlementSent: big.NewInt(0),
SettlementReceived: b,
SettlementSent: bigint.Wrap(big.NewInt(0)),
SettlementReceived: bigint.Wrap(b),
}
}
totalReceived.Add(b, totalReceived)
......@@ -85,7 +86,7 @@ func (s *Service) settlementsHandler(w http.ResponseWriter, r *http.Request) {
i++
}
jsonhttp.OK(w, settlementsResponse{TotalSettlementReceived: totalReceived, TotalSettlementSent: totalSent, Settlements: settlementResponsesArray})
jsonhttp.OK(w, settlementsResponse{TotalSettlementReceived: bigint.Wrap(totalReceived), TotalSettlementSent: bigint.Wrap(totalSent), Settlements: settlementResponsesArray})
}
func (s *Service) peerSettlementsHandler(w http.ResponseWriter, r *http.Request) {
......@@ -139,8 +140,8 @@ func (s *Service) peerSettlementsHandler(w http.ResponseWriter, r *http.Request)
jsonhttp.OK(w, settlementResponse{
Peer: peer.String(),
SettlementReceived: received,
SettlementSent: sent,
SettlementReceived: bigint.Wrap(received),
SettlementSent: bigint.Wrap(sent),
})
}
......@@ -169,8 +170,8 @@ func (s *Service) settlementsHandlerPseudosettle(w http.ResponseWriter, r *http.
for a, b := range settlementsSent {
settlementResponses[a] = settlementResponse{
Peer: a,
SettlementSent: b,
SettlementReceived: big.NewInt(0),
SettlementSent: bigint.Wrap(b),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
}
totalSent.Add(b, totalSent)
}
......@@ -178,13 +179,13 @@ func (s *Service) settlementsHandlerPseudosettle(w http.ResponseWriter, r *http.
for a, b := range settlementsReceived {
if _, ok := settlementResponses[a]; ok {
t := settlementResponses[a]
t.SettlementReceived = b
t.SettlementReceived = bigint.Wrap(b)
settlementResponses[a] = t
} else {
settlementResponses[a] = settlementResponse{
Peer: a,
SettlementSent: big.NewInt(0),
SettlementReceived: b,
SettlementSent: bigint.Wrap(big.NewInt(0)),
SettlementReceived: bigint.Wrap(b),
}
}
totalReceived.Add(b, totalReceived)
......@@ -197,5 +198,5 @@ func (s *Service) settlementsHandlerPseudosettle(w http.ResponseWriter, r *http.
i++
}
jsonhttp.OK(w, settlementsResponse{TotalSettlementReceived: totalReceived, TotalSettlementSent: totalSent, Settlements: settlementResponsesArray})
jsonhttp.OK(w, settlementsResponse{TotalSettlementReceived: bigint.Wrap(totalReceived), TotalSettlementSent: bigint.Wrap(totalSent), Settlements: settlementResponsesArray})
}
......@@ -11,6 +11,7 @@ import (
"reflect"
"testing"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
......@@ -40,28 +41,28 @@ func TestSettlements(t *testing.T) {
})
expected := &debugapi.SettlementsResponse{
TotalSettlementReceived: big.NewInt(15000),
TotalSettlementSent: big.NewInt(80000),
TotalSettlementReceived: bigint.Wrap(big.NewInt(15000)),
TotalSettlementSent: bigint.Wrap(big.NewInt(80000)),
Settlements: []debugapi.SettlementResponse{
{
Peer: "DEAD",
SettlementReceived: big.NewInt(0),
SettlementSent: big.NewInt(10000),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
SettlementSent: bigint.Wrap(big.NewInt(10000)),
},
{
Peer: "BEEF",
SettlementReceived: big.NewInt(10000),
SettlementSent: big.NewInt(20000),
SettlementReceived: bigint.Wrap(big.NewInt(10000)),
SettlementSent: bigint.Wrap(big.NewInt(20000)),
},
{
Peer: "FFFF",
SettlementReceived: big.NewInt(0),
SettlementSent: big.NewInt(50000),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
SettlementSent: bigint.Wrap(big.NewInt(50000)),
},
{
Peer: "EEEE",
SettlementReceived: big.NewInt(5000),
SettlementSent: big.NewInt(0),
SettlementReceived: bigint.Wrap(big.NewInt(5000)),
SettlementSent: bigint.Wrap(big.NewInt(0)),
},
},
}
......@@ -107,8 +108,8 @@ func TestSettlementsPeers(t *testing.T) {
jsonhttptest.Request(t, testServer.Client, http.MethodGet, "/settlements/"+peer, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.SettlementResponse{
Peer: peer,
SettlementSent: big.NewInt(1000000000000000000),
SettlementReceived: big.NewInt(0),
SettlementSent: bigint.Wrap(big.NewInt(1000000000000000000)),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
}),
)
}
......@@ -133,8 +134,8 @@ func TestSettlementsPeersNoSettlements(t *testing.T) {
jsonhttptest.Request(t, testServer.Client, http.MethodGet, "/settlements/"+peer, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.SettlementResponse{
Peer: peer,
SettlementSent: big.NewInt(0),
SettlementReceived: big.NewInt(1000000000000000000),
SettlementSent: bigint.Wrap(big.NewInt(0)),
SettlementReceived: bigint.Wrap(big.NewInt(1000000000000000000)),
}),
)
})
......@@ -150,8 +151,8 @@ func TestSettlementsPeersNoSettlements(t *testing.T) {
jsonhttptest.Request(t, testServer.Client, http.MethodGet, "/settlements/"+peer, http.StatusOK,
jsonhttptest.WithExpectedJSONResponse(debugapi.SettlementResponse{
Peer: peer,
SettlementSent: big.NewInt(1000000000000000000),
SettlementReceived: big.NewInt(0),
SettlementSent: bigint.Wrap(big.NewInt(1000000000000000000)),
SettlementReceived: bigint.Wrap(big.NewInt(0)),
}),
)
})
......@@ -217,11 +218,11 @@ func equalSettlements(a, b *debugapi.SettlementsResponse) bool {
}
}
if a.TotalSettlementReceived.Cmp(b.TotalSettlementReceived) != 0 {
if a.TotalSettlementReceived.Cmp(b.TotalSettlementReceived.Int) != 0 {
return false
}
if a.TotalSettlementSent.Cmp(b.TotalSettlementSent) != 0 {
if a.TotalSettlementSent.Cmp(b.TotalSettlementSent.Int) != 0 {
return false
}
......
......@@ -8,7 +8,7 @@ import "math/big"
// ChainState contains data the batch service reads from the chain.
type ChainState struct {
Block uint64 `json:"block"` // The block number of the last postage event.
TotalAmount *big.Int `json:"totalAmount"` // Cumulative amount paid per stamp.
CurrentPrice *big.Int `json:"currentPrice"` // Bzz/chunk/block normalised price.
Block uint64 // The block number of the last postage event.
TotalAmount *big.Int // Cumulative amount paid per stamp.
CurrentPrice *big.Int // Bzz/chunk/block normalised price.
}
......@@ -7,8 +7,8 @@ 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"`
Radius uint8
Available int64
Outer *big.Int // lower value limit for outer layer = the further half of chunks
Inner *big.Int
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment