Skip to content
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
8 changes: 7 additions & 1 deletion cmd/booster-http/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ func (h *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// TODO: Allow this to be configurable
expectedPaymentAmount := big.NewInt(10)

if hasPaid := checkPaymentChannelBalance(h.nitroRpcClient, chId, expectedPaymentAmount); !hasPaid {
hasPaid, err := checkPaymentChannelBalance(h.nitroRpcClient, chId, expectedPaymentAmount)
if err != nil {
webError(w, err, http.StatusPaymentRequired)
return
}

if !hasPaid {
webError(w, fmt.Errorf("payment of %d required", expectedPaymentAmount.Uint64()), http.StatusPaymentRequired)
return
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ func (s *HttpServer) handleByPieceCid(w http.ResponseWriter, r *http.Request) {
// TODO: Allow this to be configurable
expectedPaymentAmount := big.NewInt(10)

if hasPaid := checkPaymentChannelBalance(s.nitroRpcClient, chId, expectedPaymentAmount); !hasPaid {
writeError(w, r, http.StatusPaymentRequired, "payment required")
hasPaid, err := checkPaymentChannelBalance(s.nitroRpcClient, chId, expectedPaymentAmount)
if err != nil {
writeError(w, r, http.StatusInternalServerError, err.Error())
return
}

if !hasPaid {
writeError(w, r, http.StatusPaymentRequired, fmt.Sprintf("payment of %d required", expectedPaymentAmount.Uint64()))
return
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/booster-http/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"errors"
"math/big"
"net/http"

Expand All @@ -18,13 +19,12 @@ func addCommas(count uint64) string {
}

// checkPaymentChannelBalance checks a payment channel balance and returns true if the AmountPaid is greater than the expected amount
func checkPaymentChannelBalance(rpcClient *rpc.RpcClient, paymentChannelId types.Destination, expectedAmount *big.Int) bool {
func checkPaymentChannelBalance(rpcClient *rpc.RpcClient, paymentChannelId types.Destination, expectedAmount *big.Int) (bool, error) {
if rpcClient == nil {
panic("the rpcClient is nil")
return false, errors.New("the rpcClient is nil")
}
payCh := rpcClient.GetVirtualChannel(paymentChannelId)
return payCh.Balance.PaidSoFar.ToInt().Cmp(expectedAmount) > 0

return (payCh.Balance.PaidSoFar.ToInt().Cmp(expectedAmount) > 0), nil
}

type corsHandler struct {
Expand Down