Skip to content

review of file controller_routing #28

@PaulBernier

Description

@PaulBernier

Reviewing file https://github.com/WhoSoup/factom-p2p/blob/master/controller_routing.go

  1. If I understand correctly ToNetwork/FromNetwork are the interfaces between the application and the p2p package. I think those communication channels should not use the Parcel struct but its own structure specific to the communication with the application layer; Parcel is an object that belongs to the networking layer and should not leak outside. Here are some elements that brought me to this conclusion:
    -first just from a layering point of view it's better to have things well defined and isolated. It's very similar to the classic DTO vs DAO pattern
    -the only Parcel that should go in/out to the Application must be of type TypeMessage/TypeMessagePart, having a specific type for application communication would definitively rule out any possibility to mistakenly send in/out unwanted Parcel types (prevent bugs ahead thanks to typing mechanism).
  • You have a comment next to Address in your Parcel struct "" or nil for broadcast, otherwise the destination peer's hash. but that's not completely true, when coming from the application it is filled with "", "" or "". That's another hint that you may be overloading your Parcel struct with 2 different meanings in 2 different contexts.
  1. manageData method: I think it's better if there is as little logic as possible within the cases of the switch. In the TypePing case I see a comment "// Move": I say yes! Let's create a Pong method of peer and just call that in in the switch case (also maybe create a Ping method for Peer for symmetry). Similarly the TypePeerRequest case contains a bit too much logic for my personal taste that would be better encapsulated. It seems you don't need that distinction between TypeMessage and TypeMessagePart any more, I guess you can merge them instead of using fallthrough?

  2. Broadcast why does it take a boolean as an argument while the type could be determined within by using the Parcel type? Technically speaking this method could receive inconsistent information between this boolean and the parcel type, by relying solely on the type you protect programmers against this risk.

  3. randomPeers and randomPeersConditional return results that are a bit surprising/inconsistent to me in the case you have less elements than count, you will not fulfill your promise of randomness, but still return some peers. It looks like a half-baked results. If the contract is to returned exactly count peers and you cannot meet that demand then make it clear by returning an empty slice (or nil). Also you separate the case len(peers) == 0 but it seems to me it fits the general case, it's not particularly special.

	if uint(len(regular)) < count {
		return append(special, regular...)
	}

This snippet looks suspicious to me as len(special)+len(regular) may end up greater than count, should that be a possibility? I'd find that surprising as a client of this function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions