mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 15:16:43 +00:00
feat:unify Ping method signature
Signed-off-by: Chen Kai <281165273grape@gmail.com>
This commit is contained in:
parent
b61b1053f0
commit
b26e0a8593
4 changed files with 19 additions and 17 deletions
|
|
@ -163,7 +163,7 @@ func discv4Ping(ctx *cli.Context) error {
|
|||
defer disc.Close()
|
||||
|
||||
start := time.Now()
|
||||
if err := disc.PingWithoutResp(n); err != nil {
|
||||
if _, err := disc.Ping(n); err != nil {
|
||||
return fmt.Errorf("node didn't respond: %v", err)
|
||||
}
|
||||
fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start))
|
||||
|
|
|
|||
|
|
@ -84,7 +84,8 @@ func discv5Ping(ctx *cli.Context) error {
|
|||
disc, _ := startV5(ctx)
|
||||
defer disc.Close()
|
||||
|
||||
fmt.Println(disc.PingWithoutResp(n))
|
||||
_, err := disc.Ping(n)
|
||||
fmt.Println(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -210,12 +210,6 @@ func (t *UDPv4) ourEndpoint() v4wire.Endpoint {
|
|||
return v4wire.NewEndpoint(addr, uint16(node.TCP()))
|
||||
}
|
||||
|
||||
// PingWithoutResp sends a ping message to the given node.
|
||||
func (t *UDPv4) PingWithoutResp(n *enode.Node) error {
|
||||
_, err := t.ping(n)
|
||||
return err
|
||||
}
|
||||
|
||||
// ping sends a ping message to the given node and waits for a reply.
|
||||
func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
|
||||
addr, ok := n.UDPEndpoint()
|
||||
|
|
@ -229,6 +223,19 @@ func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) {
|
|||
return seq, err
|
||||
}
|
||||
|
||||
// Ping calls PING on a node and waits for a PONG response.
|
||||
func (t *UDPv4) Ping(n *enode.Node) (pong *v4wire.Pong, err error) {
|
||||
addr, ok := n.UDPEndpoint()
|
||||
if !ok {
|
||||
return nil, errNoUDPEndpoint
|
||||
}
|
||||
rm := t.sendPing(n.ID(), addr, nil)
|
||||
if err = <-rm.errc; err == nil {
|
||||
pong = rm.reply.(*v4wire.Pong)
|
||||
}
|
||||
return pong, err
|
||||
}
|
||||
|
||||
// sendPing sends a ping message to the given node and invokes the callback
|
||||
// when the reply arrives.
|
||||
func (t *UDPv4) sendPing(toid enode.ID, toaddr netip.AddrPort, callback func()) *replyMatcher {
|
||||
|
|
|
|||
|
|
@ -200,12 +200,6 @@ func (t *UDPv5) Close() {
|
|||
})
|
||||
}
|
||||
|
||||
// PingWithoutResp sends a ping message to the given node.
|
||||
func (t *UDPv5) PingWithoutResp(n *enode.Node) error {
|
||||
_, err := t.ping(n)
|
||||
return err
|
||||
}
|
||||
|
||||
// Resolve searches for a specific node with the given ID and tries to get the most recent
|
||||
// version of the node record for it. It returns n if the node could not be resolved.
|
||||
func (t *UDPv5) Resolve(n *enode.Node) *enode.Node {
|
||||
|
|
@ -400,7 +394,7 @@ func lookupDistances(target, dest enode.ID) (dists []uint) {
|
|||
|
||||
// ping calls PING on a node and waits for a PONG response.
|
||||
func (t *UDPv5) ping(n *enode.Node) (uint64, error) {
|
||||
pong, err := t.PingWithResp(n)
|
||||
pong, err := t.Ping(n)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
@ -408,8 +402,8 @@ func (t *UDPv5) ping(n *enode.Node) (uint64, error) {
|
|||
return pong.ENRSeq, nil
|
||||
}
|
||||
|
||||
// PingWithResp calls PING on a node and waits for a PONG response.
|
||||
func (t *UDPv5) PingWithResp(n *enode.Node) (*v5wire.Pong, error) {
|
||||
// Ping calls PING on a node and waits for a PONG response.
|
||||
func (t *UDPv5) Ping(n *enode.Node) (*v5wire.Pong, error) {
|
||||
req := &v5wire.Ping{ENRSeq: t.localNode.Node().Seq()}
|
||||
resp := t.callToNode(n, v5wire.PongMsg, req)
|
||||
defer t.callDone(resp)
|
||||
|
|
|
|||
Loading…
Reference in a new issue