github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/payload/version.go (about)

     1  package payload
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/config/netmode"
     7  	"github.com/nspcc-dev/neo-go/pkg/io"
     8  	"github.com/nspcc-dev/neo-go/pkg/network/capability"
     9  )
    10  
    11  // MaxUserAgentLength is the limit for the user agent field.
    12  const MaxUserAgentLength = 1024
    13  
    14  // Version payload.
    15  type Version struct {
    16  	// NetMode of the node
    17  	Magic netmode.Magic
    18  	// currently the version of the protocol is 0
    19  	Version uint32
    20  	// timestamp
    21  	Timestamp uint32
    22  	// it's used to distinguish several nodes using the same public IP (or different ones)
    23  	Nonce uint32
    24  	// client id
    25  	UserAgent []byte
    26  	// List of available network services
    27  	Capabilities capability.Capabilities
    28  }
    29  
    30  // NewVersion returns a pointer to a Version payload.
    31  func NewVersion(magic netmode.Magic, id uint32, ua string, c []capability.Capability) *Version {
    32  	return &Version{
    33  		Magic:        magic,
    34  		Version:      0,
    35  		Timestamp:    uint32(time.Now().UTC().Unix()),
    36  		Nonce:        id,
    37  		UserAgent:    []byte(ua),
    38  		Capabilities: c,
    39  	}
    40  }
    41  
    42  // DecodeBinary implements the Serializable interface.
    43  func (p *Version) DecodeBinary(br *io.BinReader) {
    44  	p.Magic = netmode.Magic(br.ReadU32LE())
    45  	p.Version = br.ReadU32LE()
    46  	p.Timestamp = br.ReadU32LE()
    47  	p.Nonce = br.ReadU32LE()
    48  	p.UserAgent = br.ReadVarBytes(MaxUserAgentLength)
    49  	p.Capabilities.DecodeBinary(br)
    50  }
    51  
    52  // EncodeBinary implements the Serializable interface.
    53  func (p *Version) EncodeBinary(bw *io.BinWriter) {
    54  	bw.WriteU32LE(uint32(p.Magic))
    55  	bw.WriteU32LE(p.Version)
    56  	bw.WriteU32LE(p.Timestamp)
    57  	bw.WriteU32LE(p.Nonce)
    58  	bw.WriteVarBytes(p.UserAgent)
    59  	p.Capabilities.EncodeBinary(bw)
    60  }