Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] P2P rudimentary scaling #34

Closed
wants to merge 17 commits into from
Closed

[WIP] P2P rudimentary scaling #34

wants to merge 17 commits into from

Conversation

bfbachmann
Copy link
Member

@bfbachmann bfbachmann commented May 20, 2017

What changed:

  • Added message package
  • Added subnet package
  • Added errors package
  • Refactor and add some functionality to Peer package

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 40.782% when pulling 156dd85 on 17-p2p-scaling into 59bd054 on dev.


// Broadcast sends message to all peers this peer is currently connected to
func (p *Peer) Broadcast(m msg.Message) error {
return errors.New("Function not implemented")
}

// ExtractPeerInfo extracts the peer ID and multiaddress from the
// given multiaddress.
// Returns peer ID (esentially 46 character hash created by the peer)
// and the peer's multiaddress in the form /ip4/<peer IP>/tcp/<CumulusPort>.
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'm not really sure that this function is necessary. I feel like it's doing much more work than it has to.

Copy link
Member

@jordanschalm jordanschalm left a comment

Choose a reason for hiding this comment

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

Highest Quality 👌

// Send the multiaddress of a peer to another peer
PeerInfo = iota
// Request addressess of peers in the remote peer's subnet
RequestPeerInfo = iota
Copy link
Member

Choose a reason for hiding this comment

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

What is the difference between these two? When would we ever need only one peer's info

// ability of your peer to communicate with others.
const (
// Send the multiaddress of a peer to another peer
PeerInfo = iota
Copy link
Member

Choose a reason for hiding this comment

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

You only need to write iota the first time
Like:

const (
  a = iota
  b
  c
)
// a = 0, b = 1, c = 2

// Request chunk of the blockchain from peer
RequestChunk = iota
// Advertise that we have a chunk of the blockchain
AdvertiseChunk = iota
Copy link
Member

Choose a reason for hiding this comment

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

I think we should assume that every node has the whole blockchain. That should be the first thing they do.

// FromString parses a message in the form of a string and returns a pointer
// to a new Message struct made from the contents of the string. Returns error
// if string is malformed.
func FromString(s string) (*Message, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be FromBytes/Marshal instead? Why is it a string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea I think this should be Unmarshal and should take a []byte as an argument.


// Bytes returns JSON representation of message as a byte array, or error if
// message cannot be marshalled.
func (m *Message) Bytes() ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Marshal for consistency

Copy link
Member

Choose a reason for hiding this comment

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

Use gob for en/decoding

peer/peer.go Outdated
log.Debug("\t", mAddrString)
message, err := msg.New([]byte(mAddrString), msg.PeerInfo)
if err != nil {
log.Error("Failed to create message")
Copy link
Member

Choose a reason for hiding this comment

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

Same as above. Use log.WithError(....) Also two instances below

peer/peer.go Outdated
// makeMultiaddr creates a Multiaddress from the given Multiaddress (of the form
// /ip4/<ip address>/tcp/<TCP port>) and the peer id (a hash) and turn them
// into one Multiaddress. Will return error if Multiaddress is invalid.
func makeMultiaddr(iAddr ma.Multiaddr, pid lpeer.ID) (ma.Multiaddr, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Use NewMultiAddr for consistency

peer/peer.go Outdated
func (p *Peer) handleMessage(m msg.Message, s net.Stream) {
switch m.Type() {
case msg.RequestPeerInfo:
p.advertisePeers(s)
Copy link
Member

Choose a reason for hiding this comment

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

Why advertise to everyone when only one peer requests?

subnet/subnet.go Outdated
func (sn *Subnet) RemovePeer(mAddr ma.Multiaddr) {
log.Debugf("Removing peer %s from subnet", mAddr.String())
delete(sn.peers, mAddr)
sn.numPeers--
Copy link
Member

Choose a reason for hiding this comment

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

Don't separately keep track of numPeers, just use len(sn.peers). It's O(1)

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow yea that is just disgusting.

subnet/subnet.go Outdated

// isFull returns true if the number of peers in the sunbet is at or over the
// limit set for that subnet, otherwise returns false.
func (sn *Subnet) isFull() bool {
Copy link
Member

Choose a reason for hiding this comment

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

Full not isFull

peer/peer.go Outdated
// DefaultIP is the IP address new hosts will use if none if provided
DefaultIP = "127.0.0.1"
)

// Peer is a cumulus Peer composed of a host
type Peer struct {
host.Host
subnet sn.Subnet
Copy link
Contributor

Choose a reason for hiding this comment

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

I thiiiiink we're using capital letters, see https://github.com/ubclaunchpad/cumulus/pull/23/files Subnet

Copy link
Member Author

Choose a reason for hiding this comment

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

If it's capitalized its public, so I decided against it.

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.08%) to 70.917% when pulling 5b2f674 on 17-p2p-scaling into dcaafba on dev.

@jordanschalm jordanschalm added this to the Sprint 5 milestone May 27, 2017
@bfbachmann bfbachmann closed this Jun 3, 2017
@jordanschalm jordanschalm deleted the 17-p2p-scaling branch June 17, 2017 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants