github.com/ava-labs/avalanchego@v1.11.11/message/internal_msg_builder.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  //nolint:stylecheck // proto generates interfaces that fail linting
     5  package message
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/ava-labs/avalanchego/ids"
    11  	"github.com/ava-labs/avalanchego/proto/pb/p2p"
    12  	"github.com/ava-labs/avalanchego/utils/timer/mockable"
    13  	"github.com/ava-labs/avalanchego/version"
    14  )
    15  
    16  var (
    17  	disconnected  = &Disconnected{}
    18  	gossipRequest = &GossipRequest{}
    19  	timeout       = &Timeout{}
    20  
    21  	_ fmt.Stringer    = (*GetStateSummaryFrontierFailed)(nil)
    22  	_ chainIDGetter   = (*GetStateSummaryFrontierFailed)(nil)
    23  	_ requestIDGetter = (*GetStateSummaryFrontierFailed)(nil)
    24  
    25  	_ fmt.Stringer    = (*GetAcceptedStateSummaryFailed)(nil)
    26  	_ chainIDGetter   = (*GetAcceptedStateSummaryFailed)(nil)
    27  	_ requestIDGetter = (*GetAcceptedStateSummaryFailed)(nil)
    28  
    29  	_ fmt.Stringer    = (*GetAcceptedFrontierFailed)(nil)
    30  	_ chainIDGetter   = (*GetAcceptedFrontierFailed)(nil)
    31  	_ requestIDGetter = (*GetAcceptedFrontierFailed)(nil)
    32  
    33  	_ fmt.Stringer    = (*GetAcceptedFailed)(nil)
    34  	_ chainIDGetter   = (*GetAcceptedFailed)(nil)
    35  	_ requestIDGetter = (*GetAcceptedFailed)(nil)
    36  
    37  	_ fmt.Stringer     = (*GetAncestorsFailed)(nil)
    38  	_ chainIDGetter    = (*GetAncestorsFailed)(nil)
    39  	_ requestIDGetter  = (*GetAncestorsFailed)(nil)
    40  	_ engineTypeGetter = (*GetAncestorsFailed)(nil)
    41  
    42  	_ fmt.Stringer    = (*GetFailed)(nil)
    43  	_ chainIDGetter   = (*GetFailed)(nil)
    44  	_ requestIDGetter = (*GetFailed)(nil)
    45  
    46  	_ fmt.Stringer    = (*QueryFailed)(nil)
    47  	_ chainIDGetter   = (*QueryFailed)(nil)
    48  	_ requestIDGetter = (*QueryFailed)(nil)
    49  
    50  	_ fmt.Stringer = (*Disconnected)(nil)
    51  
    52  	_ fmt.Stringer = (*GossipRequest)(nil)
    53  
    54  	_ fmt.Stringer = (*Timeout)(nil)
    55  )
    56  
    57  type GetStateSummaryFrontierFailed struct {
    58  	ChainID   ids.ID `json:"chain_id,omitempty"`
    59  	RequestID uint32 `json:"request_id,omitempty"`
    60  }
    61  
    62  func (m *GetStateSummaryFrontierFailed) String() string {
    63  	return fmt.Sprintf(
    64  		"ChainID: %s RequestID: %d",
    65  		m.ChainID, m.RequestID,
    66  	)
    67  }
    68  
    69  func (m *GetStateSummaryFrontierFailed) GetChainId() []byte {
    70  	return m.ChainID[:]
    71  }
    72  
    73  func (m *GetStateSummaryFrontierFailed) GetRequestId() uint32 {
    74  	return m.RequestID
    75  }
    76  
    77  func InternalGetStateSummaryFrontierFailed(
    78  	nodeID ids.NodeID,
    79  	chainID ids.ID,
    80  	requestID uint32,
    81  ) InboundMessage {
    82  	return &inboundMessage{
    83  		nodeID: nodeID,
    84  		op:     GetStateSummaryFrontierFailedOp,
    85  		message: &GetStateSummaryFrontierFailed{
    86  			ChainID:   chainID,
    87  			RequestID: requestID,
    88  		},
    89  		expiration: mockable.MaxTime,
    90  	}
    91  }
    92  
    93  type GetAcceptedStateSummaryFailed struct {
    94  	ChainID   ids.ID `json:"chain_id,omitempty"`
    95  	RequestID uint32 `json:"request_id,omitempty"`
    96  }
    97  
    98  func (m *GetAcceptedStateSummaryFailed) String() string {
    99  	return fmt.Sprintf(
   100  		"ChainID: %s RequestID: %d",
   101  		m.ChainID, m.RequestID,
   102  	)
   103  }
   104  
   105  func (m *GetAcceptedStateSummaryFailed) GetChainId() []byte {
   106  	return m.ChainID[:]
   107  }
   108  
   109  func (m *GetAcceptedStateSummaryFailed) GetRequestId() uint32 {
   110  	return m.RequestID
   111  }
   112  
   113  func InternalGetAcceptedStateSummaryFailed(
   114  	nodeID ids.NodeID,
   115  	chainID ids.ID,
   116  	requestID uint32,
   117  ) InboundMessage {
   118  	return &inboundMessage{
   119  		nodeID: nodeID,
   120  		op:     GetAcceptedStateSummaryFailedOp,
   121  		message: &GetAcceptedStateSummaryFailed{
   122  			ChainID:   chainID,
   123  			RequestID: requestID,
   124  		},
   125  		expiration: mockable.MaxTime,
   126  	}
   127  }
   128  
   129  type GetAcceptedFrontierFailed struct {
   130  	ChainID   ids.ID `json:"chain_id,omitempty"`
   131  	RequestID uint32 `json:"request_id,omitempty"`
   132  }
   133  
   134  func (m *GetAcceptedFrontierFailed) String() string {
   135  	return fmt.Sprintf(
   136  		"ChainID: %s RequestID: %d",
   137  		m.ChainID, m.RequestID,
   138  	)
   139  }
   140  
   141  func (m *GetAcceptedFrontierFailed) GetChainId() []byte {
   142  	return m.ChainID[:]
   143  }
   144  
   145  func (m *GetAcceptedFrontierFailed) GetRequestId() uint32 {
   146  	return m.RequestID
   147  }
   148  
   149  func InternalGetAcceptedFrontierFailed(
   150  	nodeID ids.NodeID,
   151  	chainID ids.ID,
   152  	requestID uint32,
   153  ) InboundMessage {
   154  	return &inboundMessage{
   155  		nodeID: nodeID,
   156  		op:     GetAcceptedFrontierFailedOp,
   157  		message: &GetAcceptedFrontierFailed{
   158  			ChainID:   chainID,
   159  			RequestID: requestID,
   160  		},
   161  		expiration: mockable.MaxTime,
   162  	}
   163  }
   164  
   165  type GetAcceptedFailed struct {
   166  	ChainID   ids.ID `json:"chain_id,omitempty"`
   167  	RequestID uint32 `json:"request_id,omitempty"`
   168  }
   169  
   170  func (m *GetAcceptedFailed) String() string {
   171  	return fmt.Sprintf(
   172  		"ChainID: %s RequestID: %d",
   173  		m.ChainID, m.RequestID,
   174  	)
   175  }
   176  
   177  func (m *GetAcceptedFailed) GetChainId() []byte {
   178  	return m.ChainID[:]
   179  }
   180  
   181  func (m *GetAcceptedFailed) GetRequestId() uint32 {
   182  	return m.RequestID
   183  }
   184  
   185  func InternalGetAcceptedFailed(
   186  	nodeID ids.NodeID,
   187  	chainID ids.ID,
   188  	requestID uint32,
   189  ) InboundMessage {
   190  	return &inboundMessage{
   191  		nodeID: nodeID,
   192  		op:     GetAcceptedFailedOp,
   193  		message: &GetAcceptedFailed{
   194  			ChainID:   chainID,
   195  			RequestID: requestID,
   196  		},
   197  		expiration: mockable.MaxTime,
   198  	}
   199  }
   200  
   201  type GetAncestorsFailed struct {
   202  	ChainID    ids.ID         `json:"chain_id,omitempty"`
   203  	RequestID  uint32         `json:"request_id,omitempty"`
   204  	EngineType p2p.EngineType `json:"engine_type,omitempty"`
   205  }
   206  
   207  func (m *GetAncestorsFailed) String() string {
   208  	return fmt.Sprintf(
   209  		"ChainID: %s RequestID: %d EngineType: %s",
   210  		m.ChainID, m.RequestID, m.EngineType,
   211  	)
   212  }
   213  
   214  func (m *GetAncestorsFailed) GetChainId() []byte {
   215  	return m.ChainID[:]
   216  }
   217  
   218  func (m *GetAncestorsFailed) GetRequestId() uint32 {
   219  	return m.RequestID
   220  }
   221  
   222  func (m *GetAncestorsFailed) GetEngineType() p2p.EngineType {
   223  	return m.EngineType
   224  }
   225  
   226  func InternalGetAncestorsFailed(
   227  	nodeID ids.NodeID,
   228  	chainID ids.ID,
   229  	requestID uint32,
   230  	engineType p2p.EngineType,
   231  ) InboundMessage {
   232  	return &inboundMessage{
   233  		nodeID: nodeID,
   234  		op:     GetAncestorsFailedOp,
   235  		message: &GetAncestorsFailed{
   236  			ChainID:    chainID,
   237  			RequestID:  requestID,
   238  			EngineType: engineType,
   239  		},
   240  		expiration: mockable.MaxTime,
   241  	}
   242  }
   243  
   244  type GetFailed struct {
   245  	ChainID   ids.ID `json:"chain_id,omitempty"`
   246  	RequestID uint32 `json:"request_id,omitempty"`
   247  }
   248  
   249  func (m *GetFailed) String() string {
   250  	return fmt.Sprintf(
   251  		"ChainID: %s RequestID: %d",
   252  		m.ChainID, m.RequestID,
   253  	)
   254  }
   255  
   256  func (m *GetFailed) GetChainId() []byte {
   257  	return m.ChainID[:]
   258  }
   259  
   260  func (m *GetFailed) GetRequestId() uint32 {
   261  	return m.RequestID
   262  }
   263  
   264  func InternalGetFailed(
   265  	nodeID ids.NodeID,
   266  	chainID ids.ID,
   267  	requestID uint32,
   268  ) InboundMessage {
   269  	return &inboundMessage{
   270  		nodeID: nodeID,
   271  		op:     GetFailedOp,
   272  		message: &GetFailed{
   273  			ChainID:   chainID,
   274  			RequestID: requestID,
   275  		},
   276  		expiration: mockable.MaxTime,
   277  	}
   278  }
   279  
   280  type QueryFailed struct {
   281  	ChainID   ids.ID `json:"chain_id,omitempty"`
   282  	RequestID uint32 `json:"request_id,omitempty"`
   283  }
   284  
   285  func (m *QueryFailed) String() string {
   286  	return fmt.Sprintf(
   287  		"ChainID: %s RequestID: %d",
   288  		m.ChainID, m.RequestID,
   289  	)
   290  }
   291  
   292  func (m *QueryFailed) GetChainId() []byte {
   293  	return m.ChainID[:]
   294  }
   295  
   296  func (m *QueryFailed) GetRequestId() uint32 {
   297  	return m.RequestID
   298  }
   299  
   300  func InternalQueryFailed(
   301  	nodeID ids.NodeID,
   302  	chainID ids.ID,
   303  	requestID uint32,
   304  ) InboundMessage {
   305  	return &inboundMessage{
   306  		nodeID: nodeID,
   307  		op:     QueryFailedOp,
   308  		message: &QueryFailed{
   309  			ChainID:   chainID,
   310  			RequestID: requestID,
   311  		},
   312  		expiration: mockable.MaxTime,
   313  	}
   314  }
   315  
   316  type Connected struct {
   317  	NodeVersion *version.Application `json:"node_version,omitempty"`
   318  }
   319  
   320  func (m *Connected) String() string {
   321  	return fmt.Sprintf(
   322  		"NodeVersion: %s",
   323  		m.NodeVersion,
   324  	)
   325  }
   326  
   327  func InternalConnected(nodeID ids.NodeID, nodeVersion *version.Application) InboundMessage {
   328  	return &inboundMessage{
   329  		nodeID: nodeID,
   330  		op:     ConnectedOp,
   331  		message: &Connected{
   332  			NodeVersion: nodeVersion,
   333  		},
   334  		expiration: mockable.MaxTime,
   335  	}
   336  }
   337  
   338  // ConnectedSubnet contains the subnet ID of the subnet that the node is
   339  // connected to.
   340  type ConnectedSubnet struct {
   341  	SubnetID ids.ID `json:"subnet_id,omitempty"`
   342  }
   343  
   344  func (m *ConnectedSubnet) String() string {
   345  	return fmt.Sprintf(
   346  		"SubnetID: %s",
   347  		m.SubnetID,
   348  	)
   349  }
   350  
   351  // InternalConnectedSubnet returns a message that indicates the node with [nodeID] is
   352  // connected to the subnet with the given [subnetID].
   353  func InternalConnectedSubnet(nodeID ids.NodeID, subnetID ids.ID) InboundMessage {
   354  	return &inboundMessage{
   355  		nodeID: nodeID,
   356  		op:     ConnectedSubnetOp,
   357  		message: &ConnectedSubnet{
   358  			SubnetID: subnetID,
   359  		},
   360  		expiration: mockable.MaxTime,
   361  	}
   362  }
   363  
   364  type Disconnected struct{}
   365  
   366  func (Disconnected) String() string {
   367  	return ""
   368  }
   369  
   370  func InternalDisconnected(nodeID ids.NodeID) InboundMessage {
   371  	return &inboundMessage{
   372  		nodeID:     nodeID,
   373  		op:         DisconnectedOp,
   374  		message:    disconnected,
   375  		expiration: mockable.MaxTime,
   376  	}
   377  }
   378  
   379  type VMMessage struct {
   380  	Notification uint32 `json:"notification,omitempty"`
   381  }
   382  
   383  func (m *VMMessage) String() string {
   384  	return fmt.Sprintf(
   385  		"Notification: %d",
   386  		m.Notification,
   387  	)
   388  }
   389  
   390  func InternalVMMessage(
   391  	nodeID ids.NodeID,
   392  	notification uint32,
   393  ) InboundMessage {
   394  	return &inboundMessage{
   395  		nodeID: nodeID,
   396  		op:     NotifyOp,
   397  		message: &VMMessage{
   398  			Notification: notification,
   399  		},
   400  		expiration: mockable.MaxTime,
   401  	}
   402  }
   403  
   404  type GossipRequest struct{}
   405  
   406  func (GossipRequest) String() string {
   407  	return ""
   408  }
   409  
   410  func InternalGossipRequest(
   411  	nodeID ids.NodeID,
   412  ) InboundMessage {
   413  	return &inboundMessage{
   414  		nodeID:     nodeID,
   415  		op:         GossipRequestOp,
   416  		message:    gossipRequest,
   417  		expiration: mockable.MaxTime,
   418  	}
   419  }
   420  
   421  type Timeout struct{}
   422  
   423  func (Timeout) String() string {
   424  	return ""
   425  }
   426  
   427  func InternalTimeout(nodeID ids.NodeID) InboundMessage {
   428  	return &inboundMessage{
   429  		nodeID:     nodeID,
   430  		op:         TimeoutOp,
   431  		message:    timeout,
   432  		expiration: mockable.MaxTime,
   433  	}
   434  }