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

swap: prevent bad cheques from being sent #1925

Draft
wants to merge 51 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
c872714
swap: introduce pending cheques, drop peers on io errors
ralph-pichler Oct 11, 2019
c8aebdd
swap: return if confirm while no pending, add comments to peer getters
ralph-pichler Oct 22, 2019
ff1bbfc
swap: add comments for pending cheques
ralph-pichler Oct 22, 2019
ea75197
swap: add pending cheque key to TestStoreKeys
ralph-pichler Oct 22, 2019
b80884a
swap: use same err in same block
ralph-pichler Oct 22, 2019
7c928ac
swap: check for pending cheque in payment threshold test
ralph-pichler Oct 24, 2019
906f5b4
swap: rename checkPaymentThreshold and add comment about lock
ralph-pichler Oct 25, 2019
af27bb8
swap: adjust comments and error message
ralph-pichler Oct 28, 2019
afda8db
swap: dont crash if there is no last received cheque
ralph-pichler Oct 29, 2019
0d20163
swap: improve logs and error messages
ralph-pichler Oct 29, 2019
d2123a3
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 1, 2019
a498e93
swap: check available balance before sending cheque, add placeholder …
mortelli Nov 1, 2019
927a239
Merge remote-tracking branch 'origin/master' into swap-pending-cheques
ralph-pichler Nov 4, 2019
e22d28e
swap: add pending cheques to sentcheques and availablebalance
ralph-pichler Nov 4, 2019
9ebe71c
Merge remote-tracking branch 'origin/swap-pending-cheques' into swap-…
mortelli Nov 4, 2019
355ba72
swap: include pending cheques in tests
ralph-pichler Nov 4, 2019
a8b3cd1
swap: adjust balance before sending the cheque
ralph-pichler Nov 4, 2019
8f933c2
Merge remote-tracking branch 'origin/swap-pending-cheques' into swap-…
mortelli Nov 4, 2019
d588a86
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 5, 2019
57680d2
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 12, 2019
36c43de
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 12, 2019
5212660
swap: remove loadPendingCheque re-declaration
mortelli Nov 12, 2019
82cdcc3
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 14, 2019
a3f9e18
swap: fix TestAvailableBalance based on cheque logic changes from master
mortelli Nov 14, 2019
e48c165
swap/api: temporarily remove locks from Cheques function
mortelli Nov 14, 2019
ca193f0
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 14, 2019
e97f036
swap: remove placeholder function
mortelli Nov 14, 2019
8b04042
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 18, 2019
56b502d
swap: refactor sendCheque
mortelli Nov 18, 2019
37ceb8a
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 20, 2019
138cac0
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 25, 2019
ea71be9
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 26, 2019
3b10d6e
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Nov 28, 2019
cc6dae6
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Dec 2, 2019
031f935
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Dec 4, 2019
d33e71d
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Dec 5, 2019
dc62938
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Dec 16, 2019
b9385a5
swap: temporarily disable bad cheques check
mortelli Dec 16, 2019
c60fe1e
swap: minor refactor to TestMultiChequeSimulation function
mortelli Dec 16, 2019
3d02aa0
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Dec 23, 2019
472b400
swap: re-enable bad cheques check
mortelli Dec 23, 2019
171e7a2
swap: linter corrections
mortelli Dec 23, 2019
ffc9bdc
swap: fix inverted chequebook deploy amounts in TestSwapLogToFile fun…
mortelli Dec 23, 2019
9cf629e
swap: re-add locks to Cheques function
mortelli Dec 23, 2019
174334a
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Jan 8, 2020
944dbff
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Jan 9, 2020
7489976
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Jan 13, 2020
23cbc92
swap: remove redundant change in TestSwapLogToFile
mortelli Jan 14, 2020
b7d18de
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Jan 21, 2020
aa12503
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Feb 7, 2020
5989e3e
Merge remote-tracking branch 'origin/master' into swap-prevent-bad-ch…
mortelli Feb 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions swap/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ func (s *Swap) Cheques() (map[enode.ID]*PeerCheques, error) {
// get peer cheques from memory
s.peersLock.Lock()
for peer, swapPeer := range s.peers {
swapPeer.lock.Lock()
//swapPeer.lock.Lock() //temporary
pendingCheque := swapPeer.getPendingCheque()
sentCheque := swapPeer.getLastSentCheque()
receivedCheque := swapPeer.getLastReceivedCheque()
// don't add peer to result if there are no cheques
if sentCheque != nil || receivedCheque != nil || pendingCheque != nil {
cheques[peer] = &PeerCheques{pendingCheque, sentCheque, receivedCheque}
}
swapPeer.lock.Unlock()
//swapPeer.lock.Unlock() //temporary
}
s.peersLock.Unlock()

Expand Down
10 changes: 10 additions & 0 deletions swap/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ func (p *Peer) sendCheque() error {
return fmt.Errorf("error while creating cheque: %v", err)
}

chequebookBalance, err := p.swap.AvailableBalance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you just call this availableBalance. ChequebookBalance is not correct, as availableBalance is less than the chequebookBalance (see the defenition of availableBalance)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, fixed

if err != nil {
return fmt.Errorf("error while getting available balance: %v", err)
}

chequeAmount := uint64(-p.getBalance())
if chequebookBalance < chequeAmount {
return fmt.Errorf("cannot send created cheque, amount %d is greater than chequebook available balance %d", chequeAmount, chequebookBalance)
}

err = p.setPendingCheque(cheque)
if err != nil {
return fmt.Errorf("error while saving pending cheque: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion swap/simulations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func TestMultiChequeSimulation(t *testing.T) {
}

// we will send just maxCheques number of cheques
maxCheques := 10
maxCheques := 3

// the peer object used for sending
debitorSvc.lock.Lock()
Expand Down
1 change: 0 additions & 1 deletion swap/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,6 @@ func TestAvailableBalance(t *testing.T) {
if availableBalance != (netDeposit - uint64(chequeAmount)) {
t.Fatalf("availableBalance not equal to deposited minus withdraw. availableBalance: %d, deposit minus withdrawn: %d", availableBalance, depositAmount.Uint64()-withdrawAmount.Uint64())
}

}

// dummyMsgRW implements MessageReader and MessageWriter
Expand Down