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

Improve p2ptest.Client #3437

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Improve p2ptest.Client #3437

wants to merge 4 commits into from

Conversation

joshua-kim
Copy link
Contributor

Why this should be merged

  • Simplifies code for p2ptest
  • Zero-value of Client is now useful

How this works

  • Adds an interface and a testing implementation of p2p.ClientInterface

How this was tested

  • UTs

Signed-off-by: Joshua Kim <[email protected]>
Signed-off-by: Joshua Kim <[email protected]>
Signed-off-by: Joshua Kim <[email protected]>
@joshua-kim joshua-kim marked this pull request as ready for review October 2, 2024 04:56
Copy link
Collaborator

@aaronbuchwald aaronbuchwald left a comment

Choose a reason for hiding this comment

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

One small nit

Comment on lines 22 to 23
NodeID ids.NodeID
ServerNodeID ids.NodeID
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can we change to nodeID and peerNodeID or myNodeID and peerNodeID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • NodeID -> MyNodeID seems unnecessary because Client.NodeID already disambiguates the name
  • ServerNodeID -> PeerNodeID I could get behind... but I wouldn't be sure what to name Client as Client talking to Server seems more natural than Client talking to Peer. I guess we could call it Sender?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ya I think you're right client.NodeID should be clear. I prefer PeerNodeID since it's a p2p network rather than single server and updating Handler to PeerHandler or ServerHandler would make it a little more clear.


require.NoError(t, serverNetwork.AddHandler(0, handler))
return clientNetwork.NewClient(0)
c.Handler.AppGossip(ctx, c.NodeID, appGossipBytes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why make AppGossip send to ourselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't, it sends it to the server handler with the sender as us. Maybe I can make this more clear by naming Handler to ServerHandler.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah ya I think renaming would make that more clear

Copy link
Collaborator

@aaronbuchwald aaronbuchwald left a comment

Choose a reason for hiding this comment

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

Minor nit on potential name change - then this LGTM

@joshua-kim joshua-kim self-assigned this Oct 2, 2024
@joshua-kim joshua-kim added the cleanup Code quality improvement label Oct 2, 2024
Comment on lines +146 to +147
RangeProofClient p2p.ClientInterface
ChangeProofClient p2p.ClientInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish there was a better name for this ClientInterface just looks non-go-ie

// Client is a testing implementation of p2p.ClientInterface against a specified
// server handler. The zero value of Client times out AppRequest and drops
// outbound AppGossip messages.
type Client struct {
Copy link
Contributor

@ARR4N ARR4N Oct 3, 2024

Choose a reason for hiding this comment

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

What if you instead created a p2ptest.Sender that wraps a p2p.Handler and then use it in a real p2p.Client? I'm not as familiar with this code as you all are, but this reminded me of the gRPC testing strategy of using a real stack while faking the network connection with bufconn.

type Sender struct {
	Handler p2p.Handler
}

var _ common.AppSender = (*Sender)(nil)

func (s *Sender) SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, appRequestBytes []byte) error {
    // ...
    s.Handler.AppRequest(ctx, nodeIDs.List()[0], time.Time{}, appRequestBytes)
    // How would this plumb to the response / error methods to maintain the expected behaviour?
    // ...
}

func (s *Sender) SendAppGossip(ctx context.Context, config common.SendConfig, appGossipBytes []byte) error {
    // ...
    s.Handler.AppGossip(ctx, config.NodeIDs.List()[0], appGossipBytes)
    // ...
}

// ... response and error methods

Context: @StephenButtolph asked if I could think of a different name for ClientInterface and my response was that I actually think it's a bit non-go-ie to have it defined alongside the *Client.

clientNodeID,
serverNodeID,
)
c := p2ptest.Client{ServerHandler: h}
Copy link
Contributor

Choose a reason for hiding this comment

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

(style) This is too far from its usage to just be called c. I'd move the declaration to the end (as against renaming it) because I spent the whole time trying to understand where it's used. Some other variables (at least the handler) should also be addressed for the same reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants