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

swap: refactor lastReceivedCheque, lastSentCheque, balances to peer #1725

Merged
merged 9 commits into from
Sep 10, 2019

Conversation

ralph-pichler
Copy link
Member

@ralph-pichler ralph-pichler commented Sep 3, 2019

This PR

  • moves lastSentCheque to Peer
  • moves balances to Peer
  • loads cheques and balance when the Peer is created
  • always adds a peer to peers when it is created
  • introduces a lock per peer and removes the balanceLock, chequesLock and accountingLock
  • moves sendCheque and createCheque to Peer

closes #1604

swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/protocol.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mortelli mortelli left a comment

Choose a reason for hiding this comment

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

this PR is definitely moving in the right direction, great work so far 👍

i did not pay much attention to the tests, just made sure that they were passing. i will for sure check back on them on a later (non-draft) iteration.

although it probably will be hard, we should try to find a way to make sure that the peer locks are behaving the way we want them to... not sure that we can capture this with a unit test, though.

e.g.: call Add for the same peer twice and check that the execution of these routines is not concurrent, but check that it is when calling Add for 2 different peers simultaneously.

swap/peer.go Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Show resolved Hide resolved
swap/peer.go Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/swap_test.go Outdated Show resolved Hide resolved
swap/swap_test.go Outdated Show resolved Hide resolved
swap/swap_test.go Outdated Show resolved Hide resolved
swap/swap_test.go Outdated Show resolved Hide resolved
Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

This PR is in great shape. just one major and two minor points

swap/peer.go Show resolved Hide resolved
swap/peer.go Outdated
return fmt.Errorf("error while creating cheque: %v", err)
}

log.Info("sending cheque", "honey", cheque.Honey, "cumulativePayout", cheque.ChequeParams.CumulativePayout, "beneficiary", cheque.Beneficiary, "contract", cheque.Contract)
Copy link
Member

Choose a reason for hiding this comment

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

put this after updateBalance, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -134,17 +136,22 @@ func (s *Swap) removePeer(p *Peer) {
delete(s.peers, p.ID())
}

func (s *Swap) addPeer(p *Peer) {
func (s *Swap) addPeer(protoPeer *protocols.Peer, beneficiary common.Address, contractAddress common.Address) (*Peer, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I would keep addPeer as it was and put the peer initialisation in the run function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like to keep peer initialisation and adding it to the swaps peer mapping in one common function as creating a Peer without adding it to the map is always a mistake (this only happens at one place in the main code but would lead to a lot of duplicate code in the tests). I used to have this in a separate separate function but @holisticode recommended doing this in addPeer (see #1725 (comment)).

Copy link
Member

@zelig zelig left a comment

Choose a reason for hiding this comment

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

great work

swap/peer.go Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
swap/swap.go Show resolved Hide resolved
swap/swap.go Outdated Show resolved Hide resolved
swap/peer.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mortelli mortelli left a comment

Choose a reason for hiding this comment

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

ready for merge as far as i'm concerned 👍

// Caller must be careful that the same resources aren't concurrently read and written by multiple routines
func (p *Peer) createCheque() (*Cheque, error) {
var cheque *Cheque
var err error
Copy link
Contributor

Choose a reason for hiding this comment

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

if i understand our coding standards correctly, we could use named parameters here so as not to have to initialize these variables (as long as we don't use naked returns).

however, i don't know if this is still acceptable for relatively long functions like this one.

Copy link
Member

Choose a reason for hiding this comment

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

you dont need this declarations, just put a comma after cheque on line 138

swap/swap.go Show resolved Hide resolved
swap/swap.go Show resolved Hide resolved
s.peersLock.RLock()
defer s.peersLock.RUnlock()
peer, ok := s.peers[id]
return peer, ok
peer := s.peers[id]
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we removing the ok check here? What if the peer is not in the map? Do you just want to force the caller to check for nil?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this was suggested here (#1725 (comment))

Copy link
Contributor

@holisticode holisticode left a comment

Choose a reason for hiding this comment

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

Well done! LGTM.

Please just clarify my small question in the last comment I left.

@ralph-pichler ralph-pichler merged commit fe24d12 into master Sep 10, 2019
@ralph-pichler ralph-pichler deleted the swap-peer branch September 11, 2019 11:10
This was referenced Sep 11, 2019
@skylenet skylenet added this to the 0.5.0 milestone Sep 17, 2019
chadsr added a commit to chadsr/swarm that referenced this pull request Sep 23, 2019
* 'master' of github.com:ethersphere/swarm:
  all: support go 1.13 change to testing/flag packages (ethersphere#1710)
  swap: refactor lastReceivedCheque, lastSentCheque, balances to peer (ethersphere#1725)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor lastSentCheque according to lastReceivedCheque
6 participants