Full SPVPong parsing and tests against prod server.

This commit is contained in:
Jeffrey Picard 2021-11-24 17:58:05 -05:00
parent 4e6b47c2a3
commit 0a1ba43d66
3 changed files with 94 additions and 53 deletions

View file

@ -73,10 +73,11 @@ func (s *Server) getNumSubs() int64 {
// pings it, so we can determine our own external IP address. // pings it, so we can determine our own external IP address.
func (s *Server) getAndSetExternalIp(msg *pb.ServerMessage) error { func (s *Server) getAndSetExternalIp(msg *pb.ServerMessage) error {
log.Println(msg) log.Println(msg)
myIp, _, err := UDPPing(msg.Address, msg.Port) pong, err := UDPPing(msg.Address, msg.Port)
if err != nil { if err != nil {
return err return err
} }
myIp := pong.DecodeAddress()
log.Println("my ip: ", myIp) log.Println("my ip: ", myIp)
s.ExternalIP = myIp s.ExternalIP = myIp

View file

@ -153,48 +153,64 @@ func (pong *SPVPong) DecodeCountry() string {
return pb.Location_Country_name[int32(pong.country)] return pb.Location_Country_name[int32(pong.country)]
} }
func (pong *SPVPong) DecodeProtocolVersion() int {
return int(pong.protocolVersion)
}
func (pong *SPVPong) DecodeHeight() int {
return int(pong.height)
}
func (pong *SPVPong) DecodeTip() []byte {
return pong.tip
}
func (pong *SPVPong) DecodeFlags() byte {
return pong.flags
}
// UDPPing sends a ping over udp to another hub and returns the ip address of // UDPPing sends a ping over udp to another hub and returns the ip address of
// this hub. // this hub.
func UDPPing(ip, port string) (net.IP, string, error) { func UDPPing(ip, port string) (*SPVPong, error) {
address := ip + ":" + port address := ip + ":" + port
addr, err := net.ResolveUDPAddr("udp", address) addr, err := net.ResolveUDPAddr("udp", address)
if err != nil { if err != nil {
return net.IP{}, "", err return nil, err
} }
conn, err := net.DialUDP("udp", nil, addr) conn, err := net.DialUDP("udp", nil, addr)
if err != nil { if err != nil {
return net.IP{}, "", err return nil, err
} }
defer conn.Close() defer conn.Close()
_, err = conn.Write(encodeSPVPing()) _, err = conn.Write(encodeSPVPing())
if err != nil { if err != nil {
return net.IP{}, "", err return nil, err
} }
buffer := make([]byte, maxBufferSize) buffer := make([]byte, maxBufferSize)
deadline := time.Now().Add(time.Second) deadline := time.Now().Add(time.Second)
err = conn.SetReadDeadline(deadline) err = conn.SetReadDeadline(deadline)
if err != nil { if err != nil {
return net.IP{}, "", err return nil, err
} }
n, _, err := conn.ReadFromUDP(buffer) n, _, err := conn.ReadFromUDP(buffer)
if err != nil { if err != nil {
return net.IP{}, "", err return nil, err
} }
pong := decodeSPVPong(buffer[:n]) pong := decodeSPVPong(buffer[:n])
if pong == nil { if pong == nil {
return net.IP{}, "", errors.Base("Pong decoding failed") return nil, errors.Base("Pong decoding failed")
} }
myAddr := pong.DecodeAddress() // myAddr := pong.DecodeAddress()
country := pong.DecodeCountry() // country := pong.DecodeCountry()
return myAddr, country, nil return pong, nil
} }
// UDPServer is a goroutine that starts an udp server that implements the hubs // UDPServer is a goroutine that starts an udp server that implements the hubs

View file

@ -16,11 +16,17 @@ func TestUDPPing(t *testing.T) {
name string name string
wantIP string wantIP string
wantCountry string wantCountry string
wantProtocolVersion int
wantHeightMin int
wantFlags byte
} { } {
{ {
name: "Get the right ip from production server.", name: "Correctly parse information from production server.",
wantIP: "SETME", wantIP: "SETME",
wantCountry: "US", wantCountry: "US",
wantProtocolVersion: 1,
wantHeightMin: 1060000,
wantFlags: 1,
}, },
} }
@ -30,8 +36,8 @@ func TestUDPPing(t *testing.T) {
toAddr := "spv16.lbry.com" toAddr := "spv16.lbry.com"
toPort := "50001" toPort := "50001"
ip, country, err := UDPPing(toAddr, toPort) pong, err := UDPPing(toAddr, toPort)
gotCountry := country gotCountry := pong.DecodeCountry()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
@ -43,15 +49,33 @@ func TestUDPPing(t *testing.T) {
} }
digIP := strings.TrimSpace(string(res)) digIP := strings.TrimSpace(string(res))
udpIP := ip.String() udpIP := pong.DecodeAddress().String()
tt.wantIP = digIP tt.wantIP = digIP
log.Println("Height:", pong.DecodeHeight())
log.Printf("Flags: %x\n", pong.DecodeFlags())
log.Println("ProtocolVersion:", pong.DecodeProtocolVersion())
log.Printf("Tip: %x\n", pong.DecodeTip())
gotHeight := pong.DecodeHeight()
gotProtocolVersion := pong.DecodeProtocolVersion()
gotFlags := pong.DecodeFlags()
gotIP := udpIP gotIP := udpIP
if gotIP != tt.wantIP { if gotIP != tt.wantIP {
t.Errorf("got: '%s', want: '%s'\n", gotIP, tt.wantIP) t.Errorf("ip: got: '%s', want: '%s'\n", gotIP, tt.wantIP)
} }
if gotCountry != tt.wantCountry { if gotCountry != tt.wantCountry {
t.Errorf("got: '%s', want: '%s'\n", gotCountry, tt.wantCountry) t.Errorf("country: got: '%s', want: '%s'\n", gotCountry, tt.wantCountry)
}
if gotHeight < tt.wantHeightMin {
t.Errorf("height: got: %d, want >=: %d\n", gotHeight, tt.wantHeightMin)
}
if gotProtocolVersion != tt.wantProtocolVersion {
t.Errorf("protocolVersion: got: %d, want: %d\n", gotProtocolVersion, tt.wantProtocolVersion)
}
if gotFlags != tt.wantFlags {
t.Errorf("flags: got: %d, want: %d\n", gotFlags, tt.wantFlags)
} }
}) })
} }