mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-13 17:46:39 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
135 lines
3.8 KiB
Go
135 lines
3.8 KiB
Go
// Copyright (c) 2013-2015 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package wire
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"io"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/util/binaryserializer"
|
|
)
|
|
|
|
// maxNetAddressPayload returns the max payload size for a kaspa NetAddress
|
|
// based on the protocol version.
|
|
func maxNetAddressPayload(pver uint32) uint32 {
|
|
// Services 8 bytes + ip 16 bytes + port 2 bytes + timestamp 8 bytes.
|
|
return uint32(34)
|
|
}
|
|
|
|
// NetAddress defines information about a peer on the network including the time
|
|
// it was last seen, the services it supports, its IP address, and port.
|
|
type NetAddress struct {
|
|
// Last time the address was seen.
|
|
Timestamp time.Time
|
|
|
|
// Bitfield which identifies the services supported by the address.
|
|
Services ServiceFlag
|
|
|
|
// IP address of the peer.
|
|
IP net.IP
|
|
|
|
// Port the peer is using. This is encoded in big endian on the wire
|
|
// which differs from most everything else.
|
|
Port uint16
|
|
}
|
|
|
|
// HasService returns whether the specified service is supported by the address.
|
|
func (na *NetAddress) HasService(service ServiceFlag) bool {
|
|
return na.Services&service == service
|
|
}
|
|
|
|
// AddService adds service as a supported service by the peer generating the
|
|
// message.
|
|
func (na *NetAddress) AddService(service ServiceFlag) {
|
|
na.Services |= service
|
|
}
|
|
|
|
// NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and
|
|
// supported services with defaults for the remaining fields.
|
|
func NewNetAddressIPPort(ip net.IP, port uint16, services ServiceFlag) *NetAddress {
|
|
return NewNetAddressTimestamp(time.Now(), services, ip, port)
|
|
}
|
|
|
|
// NewNetAddressTimestamp returns a new NetAddress using the provided
|
|
// timestamp, IP, port, and supported services. The timestamp is rounded to
|
|
// single second precision.
|
|
func NewNetAddressTimestamp(
|
|
timestamp time.Time, services ServiceFlag, ip net.IP, port uint16) *NetAddress {
|
|
// Limit the timestamp to one second precision since the protocol
|
|
// doesn't support better.
|
|
na := NetAddress{
|
|
Timestamp: time.Unix(timestamp.Unix(), 0),
|
|
Services: services,
|
|
IP: ip,
|
|
Port: port,
|
|
}
|
|
return &na
|
|
}
|
|
|
|
// NewNetAddress returns a new NetAddress using the provided TCP address and
|
|
// supported services with defaults for the remaining fields.
|
|
func NewNetAddress(addr *net.TCPAddr, services ServiceFlag) *NetAddress {
|
|
return NewNetAddressIPPort(addr.IP, uint16(addr.Port), services)
|
|
}
|
|
|
|
// readNetAddress reads an encoded NetAddress from r depending on the protocol
|
|
// version and whether or not the timestamp is included per ts. Some messages
|
|
// like version do not include the timestamp.
|
|
func readNetAddress(r io.Reader, pver uint32, na *NetAddress, ts bool) error {
|
|
var ip [16]byte
|
|
|
|
if ts {
|
|
err := ReadElement(r, (*int64Time)(&na.Timestamp))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err := readElements(r, &na.Services, &ip)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Sigh. Bitcoin protocol mixes little and big endian.
|
|
port, err := binaryserializer.Uint16(r, bigEndian)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*na = NetAddress{
|
|
Timestamp: na.Timestamp,
|
|
Services: na.Services,
|
|
IP: net.IP(ip[:]),
|
|
Port: port,
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// writeNetAddress serializes a NetAddress to w depending on the protocol
|
|
// version and whether or not the timestamp is included per ts. Some messages
|
|
// like version do not include the timestamp.
|
|
func writeNetAddress(w io.Writer, pver uint32, na *NetAddress, ts bool) error {
|
|
if ts {
|
|
err := WriteElement(w, int64(na.Timestamp.Unix()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Ensure to always write 16 bytes even if the ip is nil.
|
|
var ip [16]byte
|
|
if na.IP != nil {
|
|
copy(ip[:], na.IP.To16())
|
|
}
|
|
err := writeElements(w, na.Services, ip)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Sigh. Bitcoin protocol mixes little and big endian.
|
|
return binary.Write(w, bigEndian, na.Port)
|
|
}
|