-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Reviewing file https://github.com/WhoSoup/factom-p2p/blob/master/controller_routing.go
- If I understand correctly
ToNetwork/FromNetworkare 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 typeTypeMessage/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.
-
manageDatamethod: I think it's better if there is as little logic as possible within the cases of the switch. In theTypePingcase 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 theTypePeerRequestcase 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 usingfallthrough? -
Broadcastwhy 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. -
randomPeersandrandomPeersConditionalreturn results that are a bit surprising/inconsistent to me in the case you have less elements thancount, 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 exactlycountpeers and you cannot meet that demand then make it clear by returning an empty slice (or nil). Also you separate the caselen(peers) == 0but 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.