Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

protocols, swap: drop unincentivized peers when sending/receiving messages #2142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions p2p/protocols/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down