github.com/MetalBlockchain/metalgo@v1.11.9/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  	"time"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/proto/pb/p2p"
    13  	"github.com/MetalBlockchain/metalgo/utils/timer/mockable"
    14  	"github.com/MetalBlockchain/metalgo/version"
    15  )
    16  
    17  var (
    18  	disconnected  = &Disconnected{}
    19  	gossipRequest = &GossipRequest{}
    20  	timeout       = &Timeout{}
    21  
    22  	_ fmt.Stringer    = (*GetStateSummaryFrontierFailed)(nil)
    23  	_ chainIDGetter   = (*GetStateSummaryFrontierFailed)(nil)
    24  	_ requestIDGetter = (*GetStateSummaryFrontierFailed)(nil)
    25  
    26  	_ fmt.Stringer    = (*GetAcceptedStateSummaryFailed)(nil)
    27  	_ chainIDGetter   = (*GetAcceptedStateSummaryFailed)(nil)
    28  	_ requestIDGetter = (*GetAcceptedStateSummaryFailed)(nil)
    29  
    30  	_ fmt.Stringer    = (*GetAcceptedFrontierFailed)(nil)
    31  	_ chainIDGetter   = (*GetAcceptedFrontierFailed)(nil)
    32  	_ requestIDGetter = (*GetAcceptedFrontierFailed)(nil)
    33  
    34  	_ fmt.Stringer    = (*GetAcceptedFailed)(nil)
    35  	_ chainIDGetter   = (*GetAcceptedFailed)(nil)
    36  	_ requestIDGetter = (*GetAcceptedFailed)(nil)
    37  
    38  	_ fmt.Stringer     = (*GetAncestorsFailed)(nil)
    39  	_ chainIDGetter    = (*GetAncestorsFailed)(nil)
    40  	_ requestIDGetter  = (*GetAncestorsFailed)(nil)
    41  	_ engineTypeGetter = (*GetAncestorsFailed)(nil)
    42  
    43  	_ fmt.Stringer    = (*GetFailed)(nil)
    44  	_ chainIDGetter   = (*GetFailed)(nil)
    45  	_ requestIDGetter = (*GetFailed)(nil)
    46  
    47  	_ fmt.Stringer    = (*QueryFailed)(nil)
    48  	_ chainIDGetter   = (*QueryFailed)(nil)
    49  	_ requestIDGetter = (*QueryFailed)(nil)
    50  
    51  	_ fmt.Stringer        = (*CrossChainAppRequest)(nil)
    52  	_ sourceChainIDGetter = (*CrossChainAppRequest)(nil)
    53  	_ chainIDGetter       = (*CrossChainAppRequest)(nil)
    54  	_ requestIDGetter     = (*CrossChainAppRequest)(nil)
    55  
    56  	_ fmt.Stringer        = (*CrossChainAppRequestFailed)(nil)
    57  	_ sourceChainIDGetter = (*CrossChainAppRequestFailed)(nil)
    58  	_ chainIDGetter       = (*CrossChainAppRequestFailed)(nil)
    59  	_ requestIDGetter     = (*CrossChainAppRequestFailed)(nil)
    60  
    61  	_ fmt.Stringer        = (*CrossChainAppResponse)(nil)
    62  	_ sourceChainIDGetter = (*CrossChainAppResponse)(nil)
    63  	_ chainIDGetter       = (*CrossChainAppResponse)(nil)
    64  	_ requestIDGetter     = (*CrossChainAppResponse)(nil)
    65  
    66  	_ fmt.Stringer = (*Disconnected)(nil)
    67  
    68  	_ fmt.Stringer = (*GossipRequest)(nil)
    69  
    70  	_ fmt.Stringer = (*Timeout)(nil)
    71  )
    72  
    73  type GetStateSummaryFrontierFailed struct {
    74  	ChainID   ids.ID `json:"chain_id,omitempty"`
    75  	RequestID uint32 `json:"request_id,omitempty"`
    76  }
    77  
    78  func (m *GetStateSummaryFrontierFailed) String() string {
    79  	return fmt.Sprintf(
    80  		"ChainID: %s RequestID: %d",
    81  		m.ChainID, m.RequestID,
    82  	)
    83  }
    84  
    85  func (m *GetStateSummaryFrontierFailed) GetChainId() []byte {
    86  	return m.ChainID[:]
    87  }
    88  
    89  func (m *GetStateSummaryFrontierFailed) GetRequestId() uint32 {
    90  	return m.RequestID
    91  }
    92  
    93  func InternalGetStateSummaryFrontierFailed(
    94  	nodeID ids.NodeID,
    95  	chainID ids.ID,
    96  	requestID uint32,
    97  ) InboundMessage {
    98  	return &inboundMessage{
    99  		nodeID: nodeID,
   100  		op:     GetStateSummaryFrontierFailedOp,
   101  		message: &GetStateSummaryFrontierFailed{
   102  			ChainID:   chainID,
   103  			RequestID: requestID,
   104  		},
   105  		expiration: mockable.MaxTime,
   106  	}
   107  }
   108  
   109  type GetAcceptedStateSummaryFailed struct {
   110  	ChainID   ids.ID `json:"chain_id,omitempty"`
   111  	RequestID uint32 `json:"request_id,omitempty"`
   112  }
   113  
   114  func (m *GetAcceptedStateSummaryFailed) String() string {
   115  	return fmt.Sprintf(
   116  		"ChainID: %s RequestID: %d",
   117  		m.ChainID, m.RequestID,
   118  	)
   119  }
   120  
   121  func (m *GetAcceptedStateSummaryFailed) GetChainId() []byte {
   122  	return m.ChainID[:]
   123  }
   124  
   125  func (m *GetAcceptedStateSummaryFailed) GetRequestId() uint32 {
   126  	return m.RequestID
   127  }
   128  
   129  func InternalGetAcceptedStateSummaryFailed(
   130  	nodeID ids.NodeID,
   131  	chainID ids.ID,
   132  	requestID uint32,
   133  ) InboundMessage {
   134  	return &inboundMessage{
   135  		nodeID: nodeID,
   136  		op:     GetAcceptedStateSummaryFailedOp,
   137  		message: &GetAcceptedStateSummaryFailed{
   138  			ChainID:   chainID,
   139  			RequestID: requestID,
   140  		},
   141  		expiration: mockable.MaxTime,
   142  	}
   143  }
   144  
   145  type GetAcceptedFrontierFailed struct {
   146  	ChainID   ids.ID `json:"chain_id,omitempty"`
   147  	RequestID uint32 `json:"request_id,omitempty"`
   148  }
   149  
   150  func (m *GetAcceptedFrontierFailed) String() string {
   151  	return fmt.Sprintf(
   152  		"ChainID: %s RequestID: %d",
   153  		m.ChainID, m.RequestID,
   154  	)
   155  }
   156  
   157  func (m *GetAcceptedFrontierFailed) GetChainId() []byte {
   158  	return m.ChainID[:]
   159  }
   160  
   161  func (m *GetAcceptedFrontierFailed) GetRequestId() uint32 {
   162  	return m.RequestID
   163  }
   164  
   165  func InternalGetAcceptedFrontierFailed(
   166  	nodeID ids.NodeID,
   167  	chainID ids.ID,
   168  	requestID uint32,
   169  ) InboundMessage {
   170  	return &inboundMessage{
   171  		nodeID: nodeID,
   172  		op:     GetAcceptedFrontierFailedOp,
   173  		message: &GetAcceptedFrontierFailed{
   174  			ChainID:   chainID,
   175  			RequestID: requestID,
   176  		},
   177  		expiration: mockable.MaxTime,
   178  	}
   179  }
   180  
   181  type GetAcceptedFailed struct {
   182  	ChainID   ids.ID `json:"chain_id,omitempty"`
   183  	RequestID uint32 `json:"request_id,omitempty"`
   184  }
   185  
   186  func (m *GetAcceptedFailed) String() string {
   187  	return fmt.Sprintf(
   188  		"ChainID: %s RequestID: %d",
   189  		m.ChainID, m.RequestID,
   190  	)
   191  }
   192  
   193  func (m *GetAcceptedFailed) GetChainId() []byte {
   194  	return m.ChainID[:]
   195  }
   196  
   197  func (m *GetAcceptedFailed) GetRequestId() uint32 {
   198  	return m.RequestID
   199  }
   200  
   201  func InternalGetAcceptedFailed(
   202  	nodeID ids.NodeID,
   203  	chainID ids.ID,
   204  	requestID uint32,
   205  ) InboundMessage {
   206  	return &inboundMessage{
   207  		nodeID: nodeID,
   208  		op:     GetAcceptedFailedOp,
   209  		message: &GetAcceptedFailed{
   210  			ChainID:   chainID,
   211  			RequestID: requestID,
   212  		},
   213  		expiration: mockable.MaxTime,
   214  	}
   215  }
   216  
   217  type GetAncestorsFailed struct {
   218  	ChainID    ids.ID         `json:"chain_id,omitempty"`
   219  	RequestID  uint32         `json:"request_id,omitempty"`
   220  	EngineType p2p.EngineType `json:"engine_type,omitempty"`
   221  }
   222  
   223  func (m *GetAncestorsFailed) String() string {
   224  	return fmt.Sprintf(
   225  		"ChainID: %s RequestID: %d EngineType: %s",
   226  		m.ChainID, m.RequestID, m.EngineType,
   227  	)
   228  }
   229  
   230  func (m *GetAncestorsFailed) GetChainId() []byte {
   231  	return m.ChainID[:]
   232  }
   233  
   234  func (m *GetAncestorsFailed) GetRequestId() uint32 {
   235  	return m.RequestID
   236  }
   237  
   238  func (m *GetAncestorsFailed) GetEngineType() p2p.EngineType {
   239  	return m.EngineType
   240  }
   241  
   242  func InternalGetAncestorsFailed(
   243  	nodeID ids.NodeID,
   244  	chainID ids.ID,
   245  	requestID uint32,
   246  	engineType p2p.EngineType,
   247  ) InboundMessage {
   248  	return &inboundMessage{
   249  		nodeID: nodeID,
   250  		op:     GetAncestorsFailedOp,
   251  		message: &GetAncestorsFailed{
   252  			ChainID:    chainID,
   253  			RequestID:  requestID,
   254  			EngineType: engineType,
   255  		},
   256  		expiration: mockable.MaxTime,
   257  	}
   258  }
   259  
   260  type GetFailed struct {
   261  	ChainID   ids.ID `json:"chain_id,omitempty"`
   262  	RequestID uint32 `json:"request_id,omitempty"`
   263  }
   264  
   265  func (m *GetFailed) String() string {
   266  	return fmt.Sprintf(
   267  		"ChainID: %s RequestID: %d",
   268  		m.ChainID, m.RequestID,
   269  	)
   270  }
   271  
   272  func (m *GetFailed) GetChainId() []byte {
   273  	return m.ChainID[:]
   274  }
   275  
   276  func (m *GetFailed) GetRequestId() uint32 {
   277  	return m.RequestID
   278  }
   279  
   280  func InternalGetFailed(
   281  	nodeID ids.NodeID,
   282  	chainID ids.ID,
   283  	requestID uint32,
   284  ) InboundMessage {
   285  	return &inboundMessage{
   286  		nodeID: nodeID,
   287  		op:     GetFailedOp,
   288  		message: &GetFailed{
   289  			ChainID:   chainID,
   290  			RequestID: requestID,
   291  		},
   292  		expiration: mockable.MaxTime,
   293  	}
   294  }
   295  
   296  type QueryFailed struct {
   297  	ChainID   ids.ID `json:"chain_id,omitempty"`
   298  	RequestID uint32 `json:"request_id,omitempty"`
   299  }
   300  
   301  func (m *QueryFailed) String() string {
   302  	return fmt.Sprintf(
   303  		"ChainID: %s RequestID: %d",
   304  		m.ChainID, m.RequestID,
   305  	)
   306  }
   307  
   308  func (m *QueryFailed) GetChainId() []byte {
   309  	return m.ChainID[:]
   310  }
   311  
   312  func (m *QueryFailed) GetRequestId() uint32 {
   313  	return m.RequestID
   314  }
   315  
   316  func InternalQueryFailed(
   317  	nodeID ids.NodeID,
   318  	chainID ids.ID,
   319  	requestID uint32,
   320  ) InboundMessage {
   321  	return &inboundMessage{
   322  		nodeID: nodeID,
   323  		op:     QueryFailedOp,
   324  		message: &QueryFailed{
   325  			ChainID:   chainID,
   326  			RequestID: requestID,
   327  		},
   328  		expiration: mockable.MaxTime,
   329  	}
   330  }
   331  
   332  type CrossChainAppRequest struct {
   333  	SourceChainID      ids.ID `json:"source_chain_id,omitempty"`
   334  	DestinationChainID ids.ID `json:"destination_chain_id,omitempty"`
   335  	RequestID          uint32 `json:"request_id,omitempty"`
   336  	Message            []byte `json:"message,omitempty"`
   337  }
   338  
   339  func (m *CrossChainAppRequest) String() string {
   340  	return fmt.Sprintf(
   341  		"SourceChainID: %s DestinationChainID: %s RequestID: %d Message: 0x%x",
   342  		m.SourceChainID, m.DestinationChainID, m.RequestID, m.Message,
   343  	)
   344  }
   345  
   346  func (m *CrossChainAppRequest) GetSourceChainID() ids.ID {
   347  	return m.SourceChainID
   348  }
   349  
   350  func (m *CrossChainAppRequest) GetChainId() []byte {
   351  	return m.DestinationChainID[:]
   352  }
   353  
   354  func (m *CrossChainAppRequest) GetRequestId() uint32 {
   355  	return m.RequestID
   356  }
   357  
   358  func InternalCrossChainAppRequest(
   359  	nodeID ids.NodeID,
   360  	sourceChainID ids.ID,
   361  	destinationChainID ids.ID,
   362  	requestID uint32,
   363  	deadline time.Duration,
   364  	msg []byte,
   365  ) InboundMessage {
   366  	return &inboundMessage{
   367  		nodeID: nodeID,
   368  		op:     CrossChainAppRequestOp,
   369  		message: &CrossChainAppRequest{
   370  			SourceChainID:      sourceChainID,
   371  			DestinationChainID: destinationChainID,
   372  			RequestID:          requestID,
   373  			Message:            msg,
   374  		},
   375  		expiration: time.Now().Add(deadline),
   376  	}
   377  }
   378  
   379  type CrossChainAppRequestFailed struct {
   380  	SourceChainID      ids.ID `json:"source_chain_id,omitempty"`
   381  	DestinationChainID ids.ID `json:"destination_chain_id,omitempty"`
   382  	RequestID          uint32 `json:"request_id,omitempty"`
   383  	ErrorCode          int32  `json:"error_code,omitempty"`
   384  	ErrorMessage       string `json:"error_message,omitempty"`
   385  }
   386  
   387  func (m *CrossChainAppRequestFailed) String() string {
   388  	return fmt.Sprintf(
   389  		"SourceChainID: %s DestinationChainID: %s RequestID: %d",
   390  		m.SourceChainID, m.DestinationChainID, m.RequestID,
   391  	)
   392  }
   393  
   394  func (m *CrossChainAppRequestFailed) GetSourceChainID() ids.ID {
   395  	return m.SourceChainID
   396  }
   397  
   398  func (m *CrossChainAppRequestFailed) GetChainId() []byte {
   399  	return m.DestinationChainID[:]
   400  }
   401  
   402  func (m *CrossChainAppRequestFailed) GetRequestId() uint32 {
   403  	return m.RequestID
   404  }
   405  
   406  func InternalCrossChainAppError(
   407  	nodeID ids.NodeID,
   408  	sourceChainID ids.ID,
   409  	destinationChainID ids.ID,
   410  	requestID uint32,
   411  	errorCode int32,
   412  	errorMessage string,
   413  ) InboundMessage {
   414  	return &inboundMessage{
   415  		nodeID: nodeID,
   416  		op:     CrossChainAppErrorOp,
   417  		message: &CrossChainAppRequestFailed{
   418  			SourceChainID:      sourceChainID,
   419  			DestinationChainID: destinationChainID,
   420  			RequestID:          requestID,
   421  			ErrorCode:          errorCode,
   422  			ErrorMessage:       errorMessage,
   423  		},
   424  		expiration: mockable.MaxTime,
   425  	}
   426  }
   427  
   428  type CrossChainAppResponse struct {
   429  	SourceChainID      ids.ID `json:"source_chain_id,omitempty"`
   430  	DestinationChainID ids.ID `json:"destination_chain_id,omitempty"`
   431  	RequestID          uint32 `json:"request_id,omitempty"`
   432  	Message            []byte `json:"message,omitempty"`
   433  }
   434  
   435  func (m *CrossChainAppResponse) String() string {
   436  	return fmt.Sprintf(
   437  		"SourceChainID: %s DestinationChainID: %s RequestID: %d Message: 0x%x",
   438  		m.SourceChainID, m.DestinationChainID, m.RequestID, m.Message,
   439  	)
   440  }
   441  
   442  func (m *CrossChainAppResponse) GetSourceChainID() ids.ID {
   443  	return m.SourceChainID
   444  }
   445  
   446  func (m *CrossChainAppResponse) GetChainId() []byte {
   447  	return m.DestinationChainID[:]
   448  }
   449  
   450  func (m *CrossChainAppResponse) GetRequestId() uint32 {
   451  	return m.RequestID
   452  }
   453  
   454  func InternalCrossChainAppResponse(
   455  	nodeID ids.NodeID,
   456  	sourceChainID ids.ID,
   457  	destinationChainID ids.ID,
   458  	requestID uint32,
   459  	msg []byte,
   460  ) InboundMessage {
   461  	return &inboundMessage{
   462  		nodeID: nodeID,
   463  		op:     CrossChainAppResponseOp,
   464  		message: &CrossChainAppResponse{
   465  			SourceChainID:      sourceChainID,
   466  			DestinationChainID: destinationChainID,
   467  			RequestID:          requestID,
   468  			Message:            msg,
   469  		},
   470  		expiration: mockable.MaxTime,
   471  	}
   472  }
   473  
   474  type Connected struct {
   475  	NodeVersion *version.Application `json:"node_version,omitempty"`
   476  }
   477  
   478  func (m *Connected) String() string {
   479  	return fmt.Sprintf(
   480  		"NodeVersion: %s",
   481  		m.NodeVersion,
   482  	)
   483  }
   484  
   485  func InternalConnected(nodeID ids.NodeID, nodeVersion *version.Application) InboundMessage {
   486  	return &inboundMessage{
   487  		nodeID: nodeID,
   488  		op:     ConnectedOp,
   489  		message: &Connected{
   490  			NodeVersion: nodeVersion,
   491  		},
   492  		expiration: mockable.MaxTime,
   493  	}
   494  }
   495  
   496  // ConnectedSubnet contains the subnet ID of the subnet that the node is
   497  // connected to.
   498  type ConnectedSubnet struct {
   499  	SubnetID ids.ID `json:"subnet_id,omitempty"`
   500  }
   501  
   502  func (m *ConnectedSubnet) String() string {
   503  	return fmt.Sprintf(
   504  		"SubnetID: %s",
   505  		m.SubnetID,
   506  	)
   507  }
   508  
   509  // InternalConnectedSubnet returns a message that indicates the node with [nodeID] is
   510  // connected to the subnet with the given [subnetID].
   511  func InternalConnectedSubnet(nodeID ids.NodeID, subnetID ids.ID) InboundMessage {
   512  	return &inboundMessage{
   513  		nodeID: nodeID,
   514  		op:     ConnectedSubnetOp,
   515  		message: &ConnectedSubnet{
   516  			SubnetID: subnetID,
   517  		},
   518  		expiration: mockable.MaxTime,
   519  	}
   520  }
   521  
   522  type Disconnected struct{}
   523  
   524  func (Disconnected) String() string {
   525  	return ""
   526  }
   527  
   528  func InternalDisconnected(nodeID ids.NodeID) InboundMessage {
   529  	return &inboundMessage{
   530  		nodeID:     nodeID,
   531  		op:         DisconnectedOp,
   532  		message:    disconnected,
   533  		expiration: mockable.MaxTime,
   534  	}
   535  }
   536  
   537  type VMMessage struct {
   538  	Notification uint32 `json:"notification,omitempty"`
   539  }
   540  
   541  func (m *VMMessage) String() string {
   542  	return fmt.Sprintf(
   543  		"Notification: %d",
   544  		m.Notification,
   545  	)
   546  }
   547  
   548  func InternalVMMessage(
   549  	nodeID ids.NodeID,
   550  	notification uint32,
   551  ) InboundMessage {
   552  	return &inboundMessage{
   553  		nodeID: nodeID,
   554  		op:     NotifyOp,
   555  		message: &VMMessage{
   556  			Notification: notification,
   557  		},
   558  		expiration: mockable.MaxTime,
   559  	}
   560  }
   561  
   562  type GossipRequest struct{}
   563  
   564  func (GossipRequest) String() string {
   565  	return ""
   566  }
   567  
   568  func InternalGossipRequest(
   569  	nodeID ids.NodeID,
   570  ) InboundMessage {
   571  	return &inboundMessage{
   572  		nodeID:     nodeID,
   573  		op:         GossipRequestOp,
   574  		message:    gossipRequest,
   575  		expiration: mockable.MaxTime,
   576  	}
   577  }
   578  
   579  type Timeout struct{}
   580  
   581  func (Timeout) String() string {
   582  	return ""
   583  }
   584  
   585  func InternalTimeout(nodeID ids.NodeID) InboundMessage {
   586  	return &inboundMessage{
   587  		nodeID:     nodeID,
   588  		op:         TimeoutOp,
   589  		message:    timeout,
   590  		expiration: mockable.MaxTime,
   591  	}
   592  }