Commit fb7dcddc authored by Ralph Pichler's avatar Ralph Pichler Committed by GitHub

actually disconnect on no swap beneficiary (#786)

parent d64cc230
...@@ -287,7 +287,7 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey, ...@@ -287,7 +287,7 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
if o.SwapEnable { if o.SwapEnable {
swapProtocol := swapprotocol.New(p2ps, logger, overlayEthAddress) swapProtocol := swapprotocol.New(p2ps, logger, overlayEthAddress)
swapAddressBook := swap.NewAddressbook(stateStore) swapAddressBook := swap.NewAddressbook(stateStore)
swapService = swap.New(swapProtocol, logger, stateStore, chequebookService, chequeStore, swapAddressBook, networkID, cashoutService) swapService = swap.New(swapProtocol, logger, stateStore, chequebookService, chequeStore, swapAddressBook, networkID, cashoutService, p2ps)
swapProtocol.SetSwap(swapService) swapProtocol.SetSwap(swapService)
if err = p2ps.AddProtocol(swapProtocol.Protocol()); err != nil { if err = p2ps.AddProtocol(swapProtocol.Protocol()); err != nil {
return nil, fmt.Errorf("swap protocol: %w", err) return nil, fmt.Errorf("swap protocol: %w", err)
......
...@@ -54,12 +54,13 @@ type Service struct { ...@@ -54,12 +54,13 @@ type Service struct {
chequebook chequebook.Service chequebook chequebook.Service
chequeStore chequebook.ChequeStore chequeStore chequebook.ChequeStore
cashout chequebook.CashoutService cashout chequebook.CashoutService
p2pService p2p.Service
addressbook Addressbook addressbook Addressbook
networkID uint64 networkID uint64
} }
// New creates a new swap Service. // New creates a new swap Service.
func New(proto swapprotocol.Interface, logger logging.Logger, store storage.StateStorer, chequebook chequebook.Service, chequeStore chequebook.ChequeStore, addressbook Addressbook, networkID uint64, cashout chequebook.CashoutService) *Service { func New(proto swapprotocol.Interface, logger logging.Logger, store storage.StateStorer, chequebook chequebook.Service, chequeStore chequebook.ChequeStore, addressbook Addressbook, networkID uint64, cashout chequebook.CashoutService, p2pService p2p.Service) *Service {
return &Service{ return &Service{
proto: proto, proto: proto,
logger: logger, logger: logger,
...@@ -70,6 +71,7 @@ func New(proto swapprotocol.Interface, logger logging.Logger, store storage.Stat ...@@ -70,6 +71,7 @@ func New(proto swapprotocol.Interface, logger logging.Logger, store storage.Stat
addressbook: addressbook, addressbook: addressbook,
networkID: networkID, networkID: networkID,
cashout: cashout, cashout: cashout,
p2pService: p2pService,
} }
} }
...@@ -108,7 +110,12 @@ func (s *Service) Pay(ctx context.Context, peer swarm.Address, amount uint64) er ...@@ -108,7 +110,12 @@ func (s *Service) Pay(ctx context.Context, peer swarm.Address, amount uint64) er
return err return err
} }
if !known { if !known {
return p2p.NewDisconnectError(ErrUnknownBeneficary) s.logger.Warning("disconnecting non-swap peer %v", peer)
err = s.p2pService.Disconnect(peer)
if err != nil {
return err
}
return ErrUnknownBeneficary
} }
err = s.chequebook.Issue(ctx, beneficiary, big.NewInt(int64(amount)), func(signedCheque *chequebook.SignedCheque) error { err = s.chequebook.Issue(ctx, beneficiary, big.NewInt(int64(amount)), func(signedCheque *chequebook.SignedCheque) error {
return s.proto.EmitCheque(ctx, peer, signedCheque) return s.proto.EmitCheque(ctx, peer, signedCheque)
......
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
mockp2p "github.com/ethersphere/bee/pkg/p2p/mock"
"github.com/ethersphere/bee/pkg/settlement/swap" "github.com/ethersphere/bee/pkg/settlement/swap"
"github.com/ethersphere/bee/pkg/settlement/swap/chequebook" "github.com/ethersphere/bee/pkg/settlement/swap/chequebook"
mockchequebook "github.com/ethersphere/bee/pkg/settlement/swap/chequebook/mock" mockchequebook "github.com/ethersphere/bee/pkg/settlement/swap/chequebook/mock"
...@@ -139,6 +140,7 @@ func TestReceiveCheque(t *testing.T) { ...@@ -139,6 +140,7 @@ func TestReceiveCheque(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
observer := &testObserver{} observer := &testObserver{}
...@@ -201,6 +203,7 @@ func TestReceiveChequeReject(t *testing.T) { ...@@ -201,6 +203,7 @@ func TestReceiveChequeReject(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
observer := &testObserver{} observer := &testObserver{}
...@@ -252,6 +255,7 @@ func TestReceiveChequeWrongChequebook(t *testing.T) { ...@@ -252,6 +255,7 @@ func TestReceiveChequeWrongChequebook(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
observer := &testObserver{} observer := &testObserver{}
...@@ -324,6 +328,7 @@ func TestPay(t *testing.T) { ...@@ -324,6 +328,7 @@ func TestPay(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
err := swap.Pay(context.Background(), peer, amount) err := swap.Pay(context.Background(), peer, amount)
...@@ -374,6 +379,7 @@ func TestPayIssueError(t *testing.T) { ...@@ -374,6 +379,7 @@ func TestPayIssueError(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
err := swap.Pay(context.Background(), peer, amount) err := swap.Pay(context.Background(), peer, amount)
...@@ -398,6 +404,7 @@ func TestPayUnknownBeneficiary(t *testing.T) { ...@@ -398,6 +404,7 @@ func TestPayUnknownBeneficiary(t *testing.T) {
}, },
} }
var disconnectCalled bool
swapService := swap.New( swapService := swap.New(
&swapProtocolMock{}, &swapProtocolMock{},
logger, logger,
...@@ -407,12 +414,25 @@ func TestPayUnknownBeneficiary(t *testing.T) { ...@@ -407,12 +414,25 @@ func TestPayUnknownBeneficiary(t *testing.T) {
addressbook, addressbook,
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(
mockp2p.WithDisconnectFunc(func(disconnectPeer swarm.Address) error {
if !peer.Equal(disconnectPeer) {
t.Fatalf("disconnecting wrong peer. wanted %v, got %v", peer, disconnectPeer)
}
disconnectCalled = true
return nil
}),
),
) )
err := swapService.Pay(context.Background(), peer, amount) err := swapService.Pay(context.Background(), peer, amount)
if !errors.Is(err, swap.ErrUnknownBeneficary) { if !errors.Is(err, swap.ErrUnknownBeneficary) {
t.Fatalf("wrong error. wanted %v, got %v", swap.ErrUnknownBeneficary, err) t.Fatalf("wrong error. wanted %v, got %v", swap.ErrUnknownBeneficary, err)
} }
if !disconnectCalled {
t.Fatal("disconnect was not called")
}
} }
func TestHandshake(t *testing.T) { func TestHandshake(t *testing.T) {
...@@ -441,6 +461,7 @@ func TestHandshake(t *testing.T) { ...@@ -441,6 +461,7 @@ func TestHandshake(t *testing.T) {
}, },
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
err := swapService.Handshake(peer, beneficiary) err := swapService.Handshake(peer, beneficiary)
...@@ -479,6 +500,7 @@ func TestHandshakeNewPeer(t *testing.T) { ...@@ -479,6 +500,7 @@ func TestHandshakeNewPeer(t *testing.T) {
}, },
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
err := swapService.Handshake(peer, beneficiary) err := swapService.Handshake(peer, beneficiary)
...@@ -508,6 +530,7 @@ func TestHandshakeWrongBeneficiary(t *testing.T) { ...@@ -508,6 +530,7 @@ func TestHandshakeWrongBeneficiary(t *testing.T) {
&addressbookMock{}, &addressbookMock{},
networkID, networkID,
&cashoutMock{}, &cashoutMock{},
mockp2p.New(),
) )
err := swapService.Handshake(peer, beneficiary) err := swapService.Handshake(peer, beneficiary)
...@@ -556,6 +579,7 @@ func TestCashout(t *testing.T) { ...@@ -556,6 +579,7 @@ func TestCashout(t *testing.T) {
return txHash, nil return txHash, nil
}, },
}, },
mockp2p.New(),
) )
returnedHash, err := swapService.CashCheque(context.Background(), peer) returnedHash, err := swapService.CashCheque(context.Background(), peer)
...@@ -601,6 +625,7 @@ func TestCashoutStatus(t *testing.T) { ...@@ -601,6 +625,7 @@ func TestCashoutStatus(t *testing.T) {
return expectedStatus, nil return expectedStatus, nil
}, },
}, },
mockp2p.New(),
) )
returnedStatus, err := swapService.CashoutStatus(context.Background(), peer) returnedStatus, err := swapService.CashoutStatus(context.Background(), peer)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment