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

Commit

Permalink
load and save received cheques, verify amount and serial are higher
Browse files Browse the repository at this point in the history
  • Loading branch information
ralph-pichler committed Jul 22, 2019
1 parent fdadcbe commit d3a7978
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
40 changes: 35 additions & 5 deletions swap/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
12 changes: 12 additions & 0 deletions swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d3a7978

Please sign in to comment.