From d3a7978641c79db315df989edc5b2f4098b1dc76 Mon Sep 17 00:00:00 2001 From: Ralph Pichler Date: Mon, 22 Jul 2019 17:34:59 +0200 Subject: [PATCH] load and save received cheques, verify amount and serial are higher --- swap/peer.go | 40 +++++++++++++++++++++++++++++++++++----- swap/swap.go | 12 ++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/swap/peer.go b/swap/peer.go index 532cbc822e..83d2e2c02b 100644 --- a/swap/peer.go +++ b/swap/peer.go @@ -36,10 +36,11 @@ var ErrDontOwe = errors.New("no negative balance") // Peer is a devp2p peer for the Swap protocol type Peer struct { *protocols.Peer - swap *Swap - backend cswap.Backend - beneficiary common.Address - contractAddress common.Address + swap *Swap + backend cswap.Backend + beneficiary common.Address + contractAddress common.Address + lastReceivedCheque *Cheque } // NewPeer creates a new swap Peer instance @@ -96,7 +97,22 @@ func (sp *Peer) handleEmitChequeMsg(ctx context.Context, msg *EmitChequeMsg) err return fmt.Errorf("wrong cheque parameters: expected timeout to be 0, was: %d", cheque.Timeout) } - // TODO: check serial and balance are higher + lastCheque := sp.loadLastReceivedCheque() + + if lastCheque != nil { + if cheque.Serial <= lastCheque.Serial { + return fmt.Errorf("wrong cheque parameters: expected serial larger than %d, was: %d", lastCheque.Serial, cheque.Serial) + } + + if cheque.Amount <= lastCheque.Amount { + return fmt.Errorf("wrong cheque parameters: expected amount larger than %d, was: %d", lastCheque.Amount, cheque.Amount) + } + } + + if err := sp.saveLastReceivedCheque(cheque); err != nil { + log.Error("error while saving last received cheque", "peer", sp.ID().String(), "err", err.Error()) + // TODO: what do we do here? + } // reset balance by amount // as this is done by the creditor, receiving the cheque, the amount should be negative, @@ -149,3 +165,17 @@ func (sp *Peer) handleConfirmMsg(ctx context.Context, msg interface{}) error { log.Info("received confirm msg") return nil } + +// loadLastReceivedCheque gets the last received cheque for this peer +// cheque gets loaded from database if not already in memory +func (sp *Peer) loadLastReceivedCheque() *Cheque { + if sp.lastReceivedCheque != nil { + sp.lastReceivedCheque = sp.swap.loadLastReceivedCheque(sp.ID()) + } + return sp.lastReceivedCheque +} + +// saveLastReceivedCheque saves cheque as the last received cheque for this peer +func (sp *Peer) saveLastReceivedCheque(cheque *Cheque) error { + return sp.swap.saveLastReceivedCheque(sp.ID(), cheque) +} diff --git a/swap/swap.go b/swap/swap.go index 4109c2d9f7..49cda500e6 100644 --- a/swap/swap.go +++ b/swap/swap.go @@ -329,6 +329,18 @@ func (s *Swap) loadCheque(peer enode.ID) (err error) { return } +// saveLastReceivedCheque loads the last received cheque for peer +func (s *Swap) loadLastReceivedCheque(peer enode.ID) *Cheque { + var cheque *Cheque + s.stateStore.Get(peer.String()+"_cheques", &cheque) + return cheque +} + +// saveLastReceivedCheque saves cheque as the last received cheque for peer +func (s *Swap) saveLastReceivedCheque(peer enode.ID, cheque *Cheque) error { + return s.stateStore.Put(peer.String()+"_cheques", cheque) +} + // Close cleans up swap func (s *Swap) Close() { s.stateStore.Close()