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

fix: runtime panic #2836

Merged
merged 5 commits into from
Mar 7, 2022
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
1 change: 0 additions & 1 deletion cmd/bee/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (c *command) initDeployCmd() error {
chequebookFactory,
swapInitialDeposit,
deployGasPrice,
true,
)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/debugapi/chequebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/postage/postagecontract"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/settlement/swap"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
Expand Down Expand Up @@ -68,6 +69,12 @@ type chequebookLastChequesResponse struct {

func (s *Service) chequebookBalanceHandler(w http.ResponseWriter, r *http.Request) {
balance, err := s.chequebook.Balance(r.Context())
if errors.Is(err, postagecontract.ErrChainDisabled) {
jsonhttp.MethodNotAllowed(w, err)
s.logger.Debugf("debug api: chequebook balance: %v", err)
s.logger.Error("debug api: cannot get chequebook balance")
return
}
if err != nil {
jsonhttp.InternalServerError(w, errChequebookBalance)
s.logger.Debugf("debug api: chequebook balance: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions pkg/debugapi/settlements.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ethersphere/bee/pkg/bigint"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/postage/postagecontract"
"github.com/ethersphere/bee/pkg/settlement"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/gorilla/mux"
Expand All @@ -36,6 +37,12 @@ type settlementsResponse struct {
func (s *Service) settlementsHandler(w http.ResponseWriter, r *http.Request) {

settlementsSent, err := s.swap.SettlementsSent()
if errors.Is(err, postagecontract.ErrChainDisabled) {
jsonhttp.MethodNotAllowed(w, err)
s.logger.Debugf("debug api: sent settlements: %v", err)
s.logger.Error("debug api: can not get sent settlements")
return
}
if err != nil {
jsonhttp.InternalServerError(w, errCantSettlements)
s.logger.Debugf("debug api: sent settlements: %v", err)
Expand Down
22 changes: 9 additions & 13 deletions pkg/node/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/p2p/libp2p"
"github.com/ethersphere/bee/pkg/postage/postagecontract"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/settlement"
"github.com/ethersphere/bee/pkg/settlement/swap"
Expand Down Expand Up @@ -160,12 +161,7 @@ func InitChequebookService(
chequebookFactory chequebook.Factory,
initialDeposit string,
deployGasPrice string,
chainEnabled bool,
) (chequebook.Service, error) {
if !chainEnabled {
return new(noOpChequebookService), nil
}

chequeSigner := chequebook.NewChequeSigner(signer, chainID)

deposit, ok := new(big.Int).SetString(initialDeposit, 10)
Expand Down Expand Up @@ -347,31 +343,31 @@ func GetTxNextBlock(ctx context.Context, logger logging.Logger, backend transact
type noOpChequebookService struct{}

func (m *noOpChequebookService) Deposit(context.Context, *big.Int) (hash common.Hash, err error) {
return hash, errors.New("chain disabled")
return hash, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) Withdraw(context.Context, *big.Int) (hash common.Hash, err error) {
return hash, errors.New("chain disabled")
return hash, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) WaitForDeposit(context.Context, common.Hash) error {
return errors.New("chain disabled")
return postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) Balance(context.Context) (*big.Int, error) {
return nil, errors.New("chain disabled")
return nil, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) AvailableBalance(context.Context) (*big.Int, error) {
return nil, errors.New("chain disabled")
return nil, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) Address() common.Address {
return common.Address{}
}
func (m *noOpChequebookService) Issue(context.Context, common.Address, *big.Int, chequebook.SendChequeFunc) (*big.Int, error) {
return nil, errors.New("chain disabled")
return nil, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) LastCheque(common.Address) (*chequebook.SignedCheque, error) {
return nil, errors.New("chain disabled")
return nil, postagecontract.ErrChainDisabled
}
func (m *noOpChequebookService) LastCheques() (map[common.Address]*chequebook.SignedCheque, error) {
return nil, errors.New("chain disabled")
return nil, postagecontract.ErrChainDisabled
}

// noOpChainBackend is a noOp implementation for transaction.Backend interface.
Expand Down
14 changes: 10 additions & 4 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
transactionService transaction.Service
transactionMonitor transaction.Monitor
chequebookFactory chequebook.Factory
chequebookService chequebook.Service
chequebookService chequebook.Service = new(noOpChequebookService)
chequeStore chequebook.ChequeStore
cashoutService chequebook.CashoutService
pollingInterval = time.Duration(o.BlockTime) * time.Second
Expand Down Expand Up @@ -343,7 +343,7 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
return nil, fmt.Errorf("factory fail: %w", err)
}

if o.ChequebookEnable {
if o.ChequebookEnable && chainEnabled {
chequebookService, err = InitChequebookService(
p2pCtx,
logger,
Expand All @@ -356,7 +356,6 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
chequebookFactory,
o.SwapInitialDeposit,
o.DeployGasPrice,
chainEnabled,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -869,8 +868,15 @@ func NewBee(addr string, publicKey *ecdsa.PublicKey, signer crypto.Signer, netwo
if chainSyncer != nil {
debugAPIService.MustRegisterMetrics(chainSyncer.Metrics()...)
}

var debugSwapService swap.Interface = swapService

if !chainEnabled {
debugSwapService = new(swap.NoOpSwap)
}

// inject dependencies and configure full debug api http path routes
debugAPIService.Configure(swarmAddress, p2ps, pingPong, kad, lightNodes, storer, tagService, acc, pseudosettleService, o.SwapEnable, o.ChequebookEnable, swapService, chequebookService, batchStore, post, postageContractService, traversalService)
debugAPIService.Configure(swarmAddress, p2ps, pingPong, kad, lightNodes, storer, tagService, acc, pseudosettleService, o.SwapEnable, o.ChequebookEnable, debugSwapService, chequebookService, batchStore, post, postageContractService, traversalService)
}

if err := kad.Start(p2pCtx); err != nil {
Expand Down
52 changes: 52 additions & 0 deletions pkg/settlement/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/postage/postagecontract"
"github.com/ethersphere/bee/pkg/settlement"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
"github.com/ethersphere/bee/pkg/settlement/swap/swapprotocol"
Expand Down Expand Up @@ -380,3 +381,54 @@ func (s *Service) GetDeductionByPeer(peer swarm.Address) (bool, error) {
func (s *Service) AddDeductionByPeer(peer swarm.Address) error {
return s.addressbook.AddDeductionBy(peer)
}

type NoOpSwap struct {
}

func (*NoOpSwap) TotalSent(peer swarm.Address) (totalSent *big.Int, err error) {
return nil, postagecontract.ErrChainDisabled
}

// TotalReceived returns the total amount received from a peer
func (*NoOpSwap) TotalReceived(peer swarm.Address) (totalSent *big.Int, err error) {
return nil, postagecontract.ErrChainDisabled
}

// SettlementsSent returns sent settlements for each individual known peer
func (*NoOpSwap) SettlementsSent() (map[string]*big.Int, error) {
return nil, postagecontract.ErrChainDisabled
}

// SettlementsReceived returns received settlements for each individual known peer
func (*NoOpSwap) SettlementsReceived() (map[string]*big.Int, error) {
return nil, postagecontract.ErrChainDisabled
}

func (*NoOpSwap) LastSentCheque(peer swarm.Address) (*chequebook.SignedCheque, error) {
return nil, postagecontract.ErrChainDisabled
}

// LastSentCheques returns the list of last sent cheques for all peers
func (*NoOpSwap) LastSentCheques() (map[string]*chequebook.SignedCheque, error) {
return nil, postagecontract.ErrChainDisabled
}

// LastReceivedCheque returns the last received cheque for the peer
func (*NoOpSwap) LastReceivedCheque(peer swarm.Address) (*chequebook.SignedCheque, error) {
return nil, postagecontract.ErrChainDisabled
}

// LastReceivedCheques returns the list of last received cheques for all peers
func (*NoOpSwap) LastReceivedCheques() (map[string]*chequebook.SignedCheque, error) {
return nil, postagecontract.ErrChainDisabled
}

// CashCheque sends a cashing transaction for the last cheque of the peer
func (*NoOpSwap) CashCheque(ctx context.Context, peer swarm.Address) (common.Hash, error) {
return common.Hash{}, postagecontract.ErrChainDisabled
}

// CashoutStatus gets the status of the latest cashout transaction for the peers chequebook
func (*NoOpSwap) CashoutStatus(ctx context.Context, peer swarm.Address) (*chequebook.CashoutStatus, error) {
return nil, postagecontract.ErrChainDisabled
}