@ -20,6 +20,7 @@ import (
"fmt"
"net"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
@ -32,6 +33,10 @@ type Packet interface {
Kind ( ) byte // Kind returns the message type.
RequestID ( ) [ ] byte // Returns the request ID.
SetRequestID ( [ ] byte ) // Sets the request ID.
// AppendLogInfo returns its argument 'ctx' with additional fields
// appended for logging purposes.
AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { }
}
// Message types.
@ -44,9 +49,6 @@ const (
TalkResponseMsg
RequestTicketMsg
TicketMsg
RegtopicMsg
RegconfirmationMsg
TopicQueryMsg
UnknownPacket = byte ( 255 ) // any non-decryptable packet
WhoareyouPacket = byte ( 254 ) // the WHOAREYOU packet
@ -59,7 +61,7 @@ type (
Nonce Nonce
}
// Whoareyou contains the handshake challenge.
// WHOAREYOU contains the handshake challenge.
Whoareyou struct {
ChallengeData [ ] byte // Encoded challenge
Nonce Nonce // Nonce of request packet
@ -73,13 +75,13 @@ type (
sent mclock . AbsTime // for handshake GC.
}
// Ping is sent during liveness checks.
// PING is sent during liveness checks.
Ping struct {
ReqID [ ] byte
ENRSeq uint64
}
// Pong is the reply to Ping .
// PONG is the reply to PING .
Pong struct {
ReqID [ ] byte
ENRSeq uint64
@ -87,62 +89,35 @@ type (
ToPort uint16 // packet, which provides a way to discover the external address (after NAT).
}
// Findnode is a query for nodes in the given bucket.
// FINDNODE is a query for nodes in the given bucket.
Findnode struct {
ReqID [ ] byte
Distances [ ] uint
// OpID is for debugging purposes and is not part of the packet encoding.
// It identifies the 'operation' on behalf of which the request was sent.
OpID uint64 ` rlp:"-" `
}
// Nodes is the reply to Findnode and Topicquery.
// NODES is a response to FINDNODE .
Nodes struct {
ReqID [ ] byte
Total uint8
Nodes [ ] * enr . Record
ReqID [ ] byte
RespCount uint8 // total number of responses to the request
Nodes [ ] * enr . Record
}
// TalkRequest is an application-level request.
// TALKREQ is an application-level request.
TalkRequest struct {
ReqID [ ] byte
Protocol string
Message [ ] byte
}
// TalkResponse is the reply to TalkRequest .
// TALKRESP is the reply to TALKREQ .
TalkResponse struct {
ReqID [ ] byte
Message [ ] byte
}
// RequestTicket requests a ticket for a topic queue.
RequestTicket struct {
ReqID [ ] byte
Topic [ ] byte
}
// Ticket is the response to RequestTicket.
Ticket struct {
ReqID [ ] byte
Ticket [ ] byte
}
// Regtopic registers the sender in a topic queue using a ticket.
Regtopic struct {
ReqID [ ] byte
Ticket [ ] byte
ENR * enr . Record
}
// Regconfirmation is the reply to Regtopic.
Regconfirmation struct {
ReqID [ ] byte
Registered bool
}
// TopicQuery asks for nodes with the given topic.
TopicQuery struct {
ReqID [ ] byte
Topic [ ] byte
}
)
// DecodeMessage decodes the message body of a packet.
@ -161,16 +136,6 @@ func DecodeMessage(ptype byte, body []byte) (Packet, error) {
dec = new ( TalkRequest )
case TalkResponseMsg :
dec = new ( TalkResponse )
case RequestTicketMsg :
dec = new ( RequestTicket )
case TicketMsg :
dec = new ( Ticket )
case RegtopicMsg :
dec = new ( Regtopic )
case RegconfirmationMsg :
dec = new ( Regconfirmation )
case TopicQueryMsg :
dec = new ( TopicQuery )
default :
return nil , fmt . Errorf ( "unknown packet type %d" , ptype )
}
@ -188,62 +153,77 @@ func (*Whoareyou) Kind() byte { return WhoareyouPacket }
func ( * Whoareyou ) RequestID ( ) [ ] byte { return nil }
func ( * Whoareyou ) SetRequestID ( [ ] byte ) { }
func ( * Whoareyou ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return ctx
}
func ( * Unknown ) Name ( ) string { return "UNKNOWN/v5" }
func ( * Unknown ) Kind ( ) byte { return UnknownPacket }
func ( * Unknown ) RequestID ( ) [ ] byte { return nil }
func ( * Unknown ) SetRequestID ( [ ] byte ) { }
func ( * Unknown ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return ctx
}
func ( * Ping ) Name ( ) string { return "PING/v5" }
func ( * Ping ) Kind ( ) byte { return PingMsg }
func ( p * Ping ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Ping ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( p * Ping ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return append ( ctx , "req" , hexutil . Bytes ( p . ReqID ) , "enrseq" , p . ENRSeq )
}
func ( * Pong ) Name ( ) string { return "PONG/v5" }
func ( * Pong ) Kind ( ) byte { return PongMsg }
func ( p * Pong ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Pong ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * Findnode ) Name ( ) string { return "FINDNODE/v5" }
func ( * Findnode ) Kind ( ) byte { return FindnodeMsg }
func ( p * Pong ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return append ( ctx , "req" , hexutil . Bytes ( p . ReqID ) , "enrseq" , p . ENRSeq )
}
func ( p * Findnode ) Name ( ) string { return "FINDNODE/v5" }
func ( p * Findnode ) Kind ( ) byte { return FindnodeMsg }
func ( p * Findnode ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Findnode ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( p * Findnode ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
ctx = append ( ctx , "req" , hexutil . Bytes ( p . ReqID ) )
if p . OpID != 0 {
ctx = append ( ctx , "opid" , p . OpID )
}
return ctx
}
func ( * Nodes ) Name ( ) string { return "NODES/v5" }
func ( * Nodes ) Kind ( ) byte { return NodesMsg }
func ( p * Nodes ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Nodes ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( p * Nodes ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return append ( ctx ,
"req" , hexutil . Bytes ( p . ReqID ) ,
"tot" , p . RespCount ,
"n" , len ( p . Nodes ) ,
)
}
func ( * TalkRequest ) Name ( ) string { return "TALKREQ/v5" }
func ( * TalkRequest ) Kind ( ) byte { return TalkRequestMsg }
func ( p * TalkRequest ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * TalkRequest ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( p * TalkRequest ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return append ( ctx , "proto" , p . Protocol , "reqid" , hexutil . Bytes ( p . ReqID ) , "len" , len ( p . Message ) )
}
func ( * TalkResponse ) Name ( ) string { return "TALKRESP/v5" }
func ( * TalkResponse ) Kind ( ) byte { return TalkResponseMsg }
func ( p * TalkResponse ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * TalkResponse ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * RequestTicket ) Name ( ) string { return "REQTICKET/v5" }
func ( * RequestTicket ) Kind ( ) byte { return RequestTicketMsg }
func ( p * RequestTicket ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * RequestTicket ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * Regtopic ) Name ( ) string { return "REGTOPIC/v5" }
func ( * Regtopic ) Kind ( ) byte { return RegtopicMsg }
func ( p * Regtopic ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Regtopic ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * Ticket ) Name ( ) string { return "TICKET/v5" }
func ( * Ticket ) Kind ( ) byte { return TicketMsg }
func ( p * Ticket ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Ticket ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * Regconfirmation ) Name ( ) string { return "REGCONFIRMATION/v5" }
func ( * Regconfirmation ) Kind ( ) byte { return RegconfirmationMsg }
func ( p * Regconfirmation ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * Regconfirmation ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( * TopicQuery ) Name ( ) string { return "TOPICQUERY/v5" }
func ( * TopicQuery ) Kind ( ) byte { return TopicQueryMsg }
func ( p * TopicQuery ) RequestID ( ) [ ] byte { return p . ReqID }
func ( p * TopicQuery ) SetRequestID ( id [ ] byte ) { p . ReqID = id }
func ( p * TalkResponse ) AppendLogInfo ( ctx [ ] interface { } ) [ ] interface { } {
return append ( ctx , "req" , p . ReqID , "len" , len ( p . Message ) )
}