From eaf4c92c0c66c8a5846250e3c8069d27b2f2a5d6 Mon Sep 17 00:00:00 2001 From: mortelli Date: Mon, 23 Mar 2020 15:09:52 -0300 Subject: [PATCH] protocols, swap: add ErrNotAccPeer, drop peer in Send and handleMsg funcs if this error appears --- p2p/protocols/protocol.go | 15 +++++++++++++-- swap/swap.go | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index 5fc95d48ed..b678e25af9 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -44,6 +44,9 @@ import ( "github.com/ethersphere/swarm/tracing" ) +// ErrNotAccPeer indicates that the peer in question is not registered as an accounting-enabled peer +var ErrNotAccPeer = errors.New("peer %s not an accounting-enabled peer") + // MsgPauser can be used to pause run execution // IMPORTANT: should be used only for tests type MsgPauser interface { @@ -307,8 +310,12 @@ func (p *Peer) Send(ctx context.Context, msg interface{}) error { if p.spec.Hook != nil { // validate that this operation would succeed... costToLocalNode, err := p.spec.Hook.Validate(p, uint32(size), wmsg, Sender) + // ...because if it would fail, we return and don't send the message if err != nil { - // ...because if it would fail, we return and don't send the message + // drop peer if not accounting-enabled to avoid pointless message exchange in the future + if err == fmt.Errorf(ErrNotAccPeer.Error(), p.ID().String()) { + p.Drop(err.Error()) + } return err } // seems like accounting would succeed, thus send the message first... @@ -376,8 +383,12 @@ func (p *Peer) handleMsg(msg p2p.Msg, handle func(ctx context.Context, msg inter // validate that the accounting call would succeed... costToLocalNode, err := p.spec.Hook.Validate(p, size, val, Receiver) + // ...because if it would fail, we return and don't handle the message if err != nil { - // ...because if it would fail, we return and don't handle the message + // drop peer if not accounting-enabled to avoid pointless message exchange in the future + if err == fmt.Errorf(ErrNotAccPeer.Error(), p.ID().String()) { + p.Drop(err.Error()) + } return Break(err) } diff --git a/swap/swap.go b/swap/swap.go index 391ff44b7a..42053d914b 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -286,7 +286,7 @@ func (s *Swap) modifyBalanceOk(amount int64, swapPeer *Peer) (err error) { func (s *Swap) Check(amount int64, peer *protocols.Peer) (err error) { swapPeer := s.getPeer(peer.ID()) if swapPeer == nil { - return fmt.Errorf("peer %s not a swap enabled peer", peer.ID().String()) + return fmt.Errorf(protocols.ErrNotAccPeer.Error(), peer.ID().String()) } swapPeer.lock.Lock() @@ -300,7 +300,7 @@ func (s *Swap) Check(amount int64, peer *protocols.Peer) (err error) { func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) { swapPeer := s.getPeer(peer.ID()) if swapPeer == nil { - return fmt.Errorf("peer %s not a swap enabled peer", peer.ID().String()) + return fmt.Errorf(protocols.ErrNotAccPeer.Error(), peer.ID().String()) } swapPeer.lock.Lock()