github.com/decred/dcrlnd@v0.7.6/lnwire/init_message.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  )
     7  
     8  // Init is the first message reveals the features supported or required by this
     9  // node. Nodes wait for receipt of the other's features to simplify error
    10  // diagnosis where features are incompatible. Each node MUST wait to receive
    11  // init before sending any other messages.
    12  type Init struct {
    13  	// GlobalFeatures is a legacy feature vector used for backwards
    14  	// compatibility with older nodes. Any features defined here should be
    15  	// merged with those presented in Features.
    16  	GlobalFeatures *RawFeatureVector
    17  
    18  	// Features is a feature vector containing the features supported by
    19  	// the remote node.
    20  	//
    21  	// NOTE: Older nodes may place some features in GlobalFeatures, but all
    22  	// new features are to be added in Features. When handling an Init
    23  	// message, any GlobalFeatures should be merged into the unified
    24  	// Features field.
    25  	Features *RawFeatureVector
    26  
    27  	// ExtraData is the set of data that was appended to this message to
    28  	// fill out the full maximum transport message size. These fields can
    29  	// be used to specify optional data such as custom TLV fields.
    30  	ExtraData ExtraOpaqueData
    31  }
    32  
    33  // NewInitMessage creates new instance of init message object.
    34  func NewInitMessage(gf *RawFeatureVector, f *RawFeatureVector) *Init {
    35  	return &Init{
    36  		GlobalFeatures: gf,
    37  		Features:       f,
    38  		ExtraData:      make([]byte, 0),
    39  	}
    40  }
    41  
    42  // A compile time check to ensure Init implements the lnwire.Message
    43  // interface.
    44  var _ Message = (*Init)(nil)
    45  
    46  // Decode deserializes a serialized Init message stored in the passed
    47  // io.Reader observing the specified protocol version.
    48  //
    49  // This is part of the lnwire.Message interface.
    50  func (msg *Init) Decode(r io.Reader, pver uint32) error {
    51  	return ReadElements(r,
    52  		&msg.GlobalFeatures,
    53  		&msg.Features,
    54  		&msg.ExtraData,
    55  	)
    56  }
    57  
    58  // Encode serializes the target Init into the passed io.Writer observing
    59  // the protocol version specified.
    60  //
    61  // This is part of the lnwire.Message interface.
    62  func (msg *Init) Encode(w *bytes.Buffer, pver uint32) error {
    63  	if err := WriteRawFeatureVector(w, msg.GlobalFeatures); err != nil {
    64  		return err
    65  	}
    66  
    67  	if err := WriteRawFeatureVector(w, msg.Features); err != nil {
    68  		return err
    69  	}
    70  
    71  	return WriteBytes(w, msg.ExtraData)
    72  }
    73  
    74  // MsgType returns the integer uniquely identifying this message type on the
    75  // wire.
    76  //
    77  // This is part of the lnwire.Message interface.
    78  func (msg *Init) MsgType() MessageType {
    79  	return MsgInit
    80  }