Change AddressKey from string to port+ipv6 address (#1458)

This commit is contained in:
Elichai Turkel 2021-01-27 16:09:32 +02:00 committed by GitHub
parent 2075c585da
commit 2823461fe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@
package addressmanager package addressmanager
import ( import (
"encoding/binary" "net"
"sync" "sync"
"github.com/kaspanet/kaspad/app/appmessage" "github.com/kaspanet/kaspad/app/appmessage"
@ -18,9 +18,11 @@ type AddressRandomizer interface {
RandomAddresses(addresses []*appmessage.NetAddress, count int) []*appmessage.NetAddress RandomAddresses(addresses []*appmessage.NetAddress, count int) []*appmessage.NetAddress
} }
// AddressKey represents a "string" key of the ip addresses // AddressKey represents a pair of IP and port, the IP is always in V6 representation
// for use as keys in maps. type AddressKey struct {
type AddressKey string port uint16
address [net.IPv6len]byte
}
// ErrAddressNotFound is an error returned from some functions when a // ErrAddressNotFound is an error returned from some functions when a
// given address is not found in the address manager // given address is not found in the address manager
@ -28,13 +30,10 @@ var ErrAddressNotFound = errors.New("address not found")
// NetAddressKey returns a key of the ip address to use it in maps. // NetAddressKey returns a key of the ip address to use it in maps.
func netAddressKey(netAddress *appmessage.NetAddress) AddressKey { func netAddressKey(netAddress *appmessage.NetAddress) AddressKey {
port := make([]byte, 2, 2) key := AddressKey{port: netAddress.Port}
binary.LittleEndian.PutUint16(port, netAddress.Port) // all IPv4 can be represented as IPv6.
copy(key.address[:], netAddress.IP.To16())
key := make([]byte, len(netAddress.IP), len(netAddress.IP)+len(port)) return key
copy(key, netAddress.IP)
return AddressKey(append(key, port...))
} }
// netAddressKeys returns a key of the ip address to use it in maps. // netAddressKeys returns a key of the ip address to use it in maps.