Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug api as a concrete service #1370

Merged
merged 1 commit into from
Mar 4, 2021
Merged
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
40 changes: 20 additions & 20 deletions pkg/debugapi/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type balancesResponse struct {
Balances []balanceResponse `json:"balances"`
}

func (s *server) balancesHandler(w http.ResponseWriter, r *http.Request) {
balances, err := s.Accounting.Balances()
func (s *Service) balancesHandler(w http.ResponseWriter, r *http.Request) {
balances, err := s.accounting.Balances()
if err != nil {
jsonhttp.InternalServerError(w, errCantBalances)
s.Logger.Debugf("debug api: balances: %v", err)
s.Logger.Error("debug api: can not get balances")
s.logger.Debugf("debug api: balances: %v", err)
s.logger.Error("debug api: can not get balances")
return
}

Expand All @@ -53,24 +53,24 @@ func (s *server) balancesHandler(w http.ResponseWriter, r *http.Request) {
jsonhttp.OK(w, balancesResponse{Balances: balResponses})
}

func (s *server) peerBalanceHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) peerBalanceHandler(w http.ResponseWriter, r *http.Request) {
addr := mux.Vars(r)["peer"]
peer, err := swarm.ParseHexAddress(addr)
if err != nil {
s.Logger.Debugf("debug api: balances peer: invalid peer address %s: %v", addr, err)
s.Logger.Errorf("debug api: balances peer: invalid peer address %s", addr)
s.logger.Debugf("debug api: balances peer: invalid peer address %s: %v", addr, err)
s.logger.Errorf("debug api: balances peer: invalid peer address %s", addr)
jsonhttp.NotFound(w, errInvaliAddress)
return
}

balance, err := s.Accounting.Balance(peer)
balance, err := s.accounting.Balance(peer)
if err != nil {
if errors.Is(err, accounting.ErrPeerNoBalance) {
jsonhttp.NotFound(w, errNoBalance)
return
}
s.Logger.Debugf("debug api: balances peer: get peer %s balance: %v", peer.String(), err)
s.Logger.Errorf("debug api: balances peer: can't get peer %s balance", peer.String())
s.logger.Debugf("debug api: balances peer: get peer %s balance: %v", peer.String(), err)
s.logger.Errorf("debug api: balances peer: can't get peer %s balance", peer.String())
jsonhttp.InternalServerError(w, errCantBalance)
return
}
Expand All @@ -81,12 +81,12 @@ func (s *server) peerBalanceHandler(w http.ResponseWriter, r *http.Request) {
})
}

func (s *server) compensatedBalancesHandler(w http.ResponseWriter, r *http.Request) {
balances, err := s.Accounting.CompensatedBalances()
func (s *Service) compensatedBalancesHandler(w http.ResponseWriter, r *http.Request) {
balances, err := s.accounting.CompensatedBalances()
if err != nil {
jsonhttp.InternalServerError(w, errCantBalances)
s.Logger.Debugf("debug api: compensated balances: %v", err)
s.Logger.Error("debug api: can not get compensated balances")
s.logger.Debugf("debug api: compensated balances: %v", err)
s.logger.Error("debug api: can not get compensated balances")
return
}

Expand All @@ -103,24 +103,24 @@ func (s *server) compensatedBalancesHandler(w http.ResponseWriter, r *http.Reque
jsonhttp.OK(w, balancesResponse{Balances: balResponses})
}

func (s *server) compensatedPeerBalanceHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) compensatedPeerBalanceHandler(w http.ResponseWriter, r *http.Request) {
addr := mux.Vars(r)["peer"]
peer, err := swarm.ParseHexAddress(addr)
if err != nil {
s.Logger.Debugf("debug api: compensated balances peer: invalid peer address %s: %v", addr, err)
s.Logger.Errorf("debug api: compensated balances peer: invalid peer address %s", addr)
s.logger.Debugf("debug api: compensated balances peer: invalid peer address %s: %v", addr, err)
s.logger.Errorf("debug api: compensated balances peer: invalid peer address %s", addr)
jsonhttp.NotFound(w, errInvaliAddress)
return
}

balance, err := s.Accounting.CompensatedBalance(peer)
balance, err := s.accounting.CompensatedBalance(peer)
if err != nil {
if errors.Is(err, accounting.ErrPeerNoBalance) {
jsonhttp.NotFound(w, errNoBalance)
return
}
s.Logger.Debugf("debug api: compensated balances peer: get peer %s balance: %v", peer.String(), err)
s.Logger.Errorf("debug api: compensated balances peer: can't get peer %s balance", peer.String())
s.logger.Debugf("debug api: compensated balances peer: get peer %s balance: %v", peer.String(), err)
s.logger.Errorf("debug api: compensated balances peer: can't get peer %s balance", peer.String())
jsonhttp.InternalServerError(w, errCantBalance)
return
}
Expand Down
114 changes: 57 additions & 57 deletions pkg/debugapi/chequebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,46 @@ type chequebookLastChequesResponse struct {
LastCheques []chequebookLastChequesPeerResponse `json:"lastcheques"`
}

func (s *server) chequebookBalanceHandler(w http.ResponseWriter, r *http.Request) {
balance, err := s.Chequebook.Balance(r.Context())
func (s *Service) chequebookBalanceHandler(w http.ResponseWriter, r *http.Request) {
balance, err := s.chequebook.Balance(r.Context())
if err != nil {
jsonhttp.InternalServerError(w, errChequebookBalance)
s.Logger.Debugf("debug api: chequebook balance: %v", err)
s.Logger.Error("debug api: cannot get chequebook balance")
s.logger.Debugf("debug api: chequebook balance: %v", err)
s.logger.Error("debug api: cannot get chequebook balance")
return
}

availableBalance, err := s.Chequebook.AvailableBalance(r.Context())
availableBalance, err := s.chequebook.AvailableBalance(r.Context())
if err != nil {
jsonhttp.InternalServerError(w, errChequebookBalance)
s.Logger.Debugf("debug api: chequebook availableBalance: %v", err)
s.Logger.Error("debug api: cannot get chequebook availableBalance")
s.logger.Debugf("debug api: chequebook availableBalance: %v", err)
s.logger.Error("debug api: cannot get chequebook availableBalance")
return
}

jsonhttp.OK(w, chequebookBalanceResponse{TotalBalance: balance, AvailableBalance: availableBalance})
}

func (s *server) chequebookAddressHandler(w http.ResponseWriter, r *http.Request) {
address := s.Chequebook.Address()
func (s *Service) chequebookAddressHandler(w http.ResponseWriter, r *http.Request) {
address := s.chequebook.Address()
jsonhttp.OK(w, chequebookAddressResponse{Address: address.String()})
}

func (s *server) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Request) {
addr := mux.Vars(r)["peer"]
peer, err := swarm.ParseHexAddress(addr)
if err != nil {
s.Logger.Debugf("debug api: chequebook cheque peer: invalid peer address %s: %v", addr, err)
s.Logger.Errorf("debug api: chequebook cheque peer: invalid peer address %s", addr)
s.logger.Debugf("debug api: chequebook cheque peer: invalid peer address %s: %v", addr, err)
s.logger.Errorf("debug api: chequebook cheque peer: invalid peer address %s", addr)
jsonhttp.NotFound(w, errInvaliAddress)
return
}

var lastSentResponse *chequebookLastChequePeerResponse
lastSent, err := s.Swap.LastSentCheque(peer)
lastSent, err := s.swap.LastSentCheque(peer)
if err != nil && err != chequebook.ErrNoCheque {
s.Logger.Debugf("debug api: chequebook cheque peer: get peer %s last cheque: %v", peer.String(), err)
s.Logger.Errorf("debug api: chequebook cheque peer: can't get peer %s last cheque", peer.String())
s.logger.Debugf("debug api: chequebook cheque peer: get peer %s last cheque: %v", peer.String(), err)
s.logger.Errorf("debug api: chequebook cheque peer: can't get peer %s last cheque", peer.String())
jsonhttp.InternalServerError(w, errCantLastChequePeer)
return
}
Expand All @@ -108,10 +108,10 @@ func (s *server) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Reques
}

var lastReceivedResponse *chequebookLastChequePeerResponse
lastReceived, err := s.Swap.LastReceivedCheque(peer)
lastReceived, err := s.swap.LastReceivedCheque(peer)
if err != nil && err != chequebook.ErrNoCheque {
s.Logger.Debugf("debug api: chequebook cheque peer: get peer %s last cheque: %v", peer.String(), err)
s.Logger.Errorf("debug api: chequebook cheque peer: can't get peer %s last cheque", peer.String())
s.logger.Debugf("debug api: chequebook cheque peer: get peer %s last cheque: %v", peer.String(), err)
s.logger.Errorf("debug api: chequebook cheque peer: can't get peer %s last cheque", peer.String())
jsonhttp.InternalServerError(w, errCantLastChequePeer)
return
}
Expand All @@ -130,18 +130,18 @@ func (s *server) chequebookLastPeerHandler(w http.ResponseWriter, r *http.Reques
})
}

func (s *server) chequebookAllLastHandler(w http.ResponseWriter, r *http.Request) {
lastchequessent, err := s.Swap.LastSentCheques()
func (s *Service) chequebookAllLastHandler(w http.ResponseWriter, r *http.Request) {
lastchequessent, err := s.swap.LastSentCheques()
if err != nil {
s.Logger.Debugf("debug api: chequebook cheque all: get all last cheques: %v", err)
s.Logger.Errorf("debug api: chequebook cheque all: can't get all last cheques")
s.logger.Debugf("debug api: chequebook cheque all: get all last cheques: %v", err)
s.logger.Errorf("debug api: chequebook cheque all: can't get all last cheques")
jsonhttp.InternalServerError(w, errCantLastCheque)
return
}
lastchequesreceived, err := s.Swap.LastReceivedCheques()
lastchequesreceived, err := s.swap.LastReceivedCheques()
if err != nil {
s.Logger.Debugf("debug api: chequebook cheque all: get all last cheques: %v", err)
s.Logger.Errorf("debug api: chequebook cheque all: can't get all last cheques")
s.logger.Debugf("debug api: chequebook cheque all: get all last cheques: %v", err)
s.logger.Errorf("debug api: chequebook cheque all: can't get all last cheques")
jsonhttp.InternalServerError(w, errCantLastCheque)
return
}
Expand Down Expand Up @@ -194,20 +194,20 @@ type swapCashoutResponse struct {
TransactionHash string `json:"transactionHash"`
}

func (s *server) swapCashoutHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) swapCashoutHandler(w http.ResponseWriter, r *http.Request) {
addr := mux.Vars(r)["peer"]
peer, err := swarm.ParseHexAddress(addr)
if err != nil {
s.Logger.Debugf("debug api: cashout peer: invalid peer address %s: %v", addr, err)
s.Logger.Errorf("debug api: cashout peer: invalid peer address %s", addr)
s.logger.Debugf("debug api: cashout peer: invalid peer address %s: %v", addr, err)
s.logger.Errorf("debug api: cashout peer: invalid peer address %s", addr)
jsonhttp.NotFound(w, errInvaliAddress)
return
}

txHash, err := s.Swap.CashCheque(r.Context(), peer)
txHash, err := s.swap.CashCheque(r.Context(), peer)
if err != nil {
s.Logger.Debugf("debug api: cashout peer: cannot cash %s: %v", addr, err)
s.Logger.Errorf("debug api: cashout peer: cannot cash %s", addr)
s.logger.Debugf("debug api: cashout peer: cannot cash %s: %v", addr, err)
s.logger.Errorf("debug api: cashout peer: cannot cash %s", addr)
jsonhttp.InternalServerError(w, errCannotCash)
return
}
Expand All @@ -230,32 +230,32 @@ type swapCashoutStatusResponse struct {
Result *swapCashoutStatusResult `json:"result"`
}

func (s *server) swapCashoutStatusHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) swapCashoutStatusHandler(w http.ResponseWriter, r *http.Request) {
addr := mux.Vars(r)["peer"]
peer, err := swarm.ParseHexAddress(addr)
if err != nil {
s.Logger.Debugf("debug api: cashout status peer: invalid peer address %s: %v", addr, err)
s.Logger.Errorf("debug api: cashout status peer: invalid peer address %s", addr)
s.logger.Debugf("debug api: cashout status peer: invalid peer address %s: %v", addr, err)
s.logger.Errorf("debug api: cashout status peer: invalid peer address %s", addr)
jsonhttp.NotFound(w, errInvaliAddress)
return
}

status, err := s.Swap.CashoutStatus(r.Context(), peer)
status, err := s.swap.CashoutStatus(r.Context(), peer)
if err != nil {
if errors.Is(err, chequebook.ErrNoCheque) {
s.Logger.Debugf("debug api: cashout status peer: %v", addr, err)
s.Logger.Errorf("debug api: cashout status peer: %s", addr)
s.logger.Debugf("debug api: cashout status peer: %v", addr, err)
s.logger.Errorf("debug api: cashout status peer: %s", addr)
jsonhttp.NotFound(w, errNoCheque)
return
}
if errors.Is(err, chequebook.ErrNoCashout) {
s.Logger.Debugf("debug api: cashout status peer: %v", addr, err)
s.Logger.Errorf("debug api: cashout status peer: %s", addr)
s.logger.Debugf("debug api: cashout status peer: %v", addr, err)
s.logger.Errorf("debug api: cashout status peer: %s", addr)
jsonhttp.NotFound(w, errNoCashout)
return
}
s.Logger.Debugf("debug api: cashout status peer: cannot get status %s: %v", addr, err)
s.Logger.Errorf("debug api: cashout status peer: cannot get status %s", addr)
s.logger.Debugf("debug api: cashout status peer: cannot get status %s: %v", addr, err)
s.logger.Errorf("debug api: cashout status peer: cannot get status %s", addr)
jsonhttp.InternalServerError(w, errCannotCashStatus)
return
}
Expand Down Expand Up @@ -283,64 +283,64 @@ type chequebookTxResponse struct {
TransactionHash common.Hash `json:"transactionHash"`
}

func (s *server) chequebookWithdrawHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) chequebookWithdrawHandler(w http.ResponseWriter, r *http.Request) {
amountStr := r.URL.Query().Get("amount")
if amountStr == "" {
jsonhttp.BadRequest(w, errChequebookNoAmount)
s.Logger.Error("debug api: no withdraw amount")
s.logger.Error("debug api: no withdraw amount")
return
}

amount, ok := big.NewInt(0).SetString(amountStr, 10)
if !ok {
jsonhttp.BadRequest(w, errChequebookNoAmount)
s.Logger.Error("debug api: invalid withdraw amount")
s.logger.Error("debug api: invalid withdraw amount")
return
}

txHash, err := s.Chequebook.Withdraw(r.Context(), amount)
txHash, err := s.chequebook.Withdraw(r.Context(), amount)
if errors.Is(err, chequebook.ErrInsufficientFunds) {
jsonhttp.BadRequest(w, errChequebookInsufficientFunds)
s.Logger.Debugf("debug api: chequebook withdraw: %v", err)
s.Logger.Error("debug api: cannot withdraw from chequebook")
s.logger.Debugf("debug api: chequebook withdraw: %v", err)
s.logger.Error("debug api: cannot withdraw from chequebook")
return
}
if err != nil {
jsonhttp.InternalServerError(w, errChequebookNoWithdraw)
s.Logger.Debugf("debug api: chequebook withdraw: %v", err)
s.Logger.Error("debug api: cannot withdraw from chequebook")
s.logger.Debugf("debug api: chequebook withdraw: %v", err)
s.logger.Error("debug api: cannot withdraw from chequebook")
return
}

jsonhttp.OK(w, chequebookTxResponse{TransactionHash: txHash})
}

func (s *server) chequebookDepositHandler(w http.ResponseWriter, r *http.Request) {
func (s *Service) chequebookDepositHandler(w http.ResponseWriter, r *http.Request) {
amountStr := r.URL.Query().Get("amount")
if amountStr == "" {
jsonhttp.BadRequest(w, errChequebookNoAmount)
s.Logger.Error("debug api: no deposit amount")
s.logger.Error("debug api: no deposit amount")
return
}

amount, ok := big.NewInt(0).SetString(amountStr, 10)
if !ok {
jsonhttp.BadRequest(w, errChequebookNoAmount)
s.Logger.Error("debug api: invalid deposit amount")
s.logger.Error("debug api: invalid deposit amount")
return
}

txHash, err := s.Chequebook.Deposit(r.Context(), amount)
txHash, err := s.chequebook.Deposit(r.Context(), amount)
if errors.Is(err, chequebook.ErrInsufficientFunds) {
jsonhttp.BadRequest(w, errChequebookInsufficientFunds)
s.Logger.Debugf("debug api: chequebook deposit: %v", err)
s.Logger.Error("debug api: cannot deposit from chequebook")
s.logger.Debugf("debug api: chequebook deposit: %v", err)
s.logger.Error("debug api: cannot deposit from chequebook")
return
}
if err != nil {
jsonhttp.InternalServerError(w, errChequebookNoDeposit)
s.Logger.Debugf("debug api: chequebook deposit: %v", err)
s.Logger.Error("debug api: cannot deposit from chequebook")
s.logger.Debugf("debug api: chequebook deposit: %v", err)
s.logger.Error("debug api: cannot deposit from chequebook")
return
}

Expand Down
Loading