github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/protocol.go (about)

     1  package gossip
     2  
     3  import (
     4  	"github.com/unicornultrafoundation/go-helios/hash"
     5  	"github.com/unicornultrafoundation/go-helios/native/idx"
     6  	"github.com/unicornultrafoundation/go-u2u/common"
     7  	"github.com/unicornultrafoundation/go-u2u/core/types"
     8  	notify "github.com/unicornultrafoundation/go-u2u/event"
     9  
    10  	"github.com/unicornultrafoundation/go-u2u/evmcore"
    11  	"github.com/unicornultrafoundation/go-u2u/gossip/emitter"
    12  	"github.com/unicornultrafoundation/go-u2u/native"
    13  	"github.com/unicornultrafoundation/go-u2u/native/ibr"
    14  	"github.com/unicornultrafoundation/go-u2u/native/iep"
    15  )
    16  
    17  // Constants to match up U2U Protocol versions and messages
    18  const (
    19  	UP01            = 1
    20  	ProtocolVersion = UP01
    21  )
    22  
    23  // ProtocolName is the official short name of the protocol used during capability negotiation.
    24  const ProtocolName = "u2u"
    25  
    26  // ProtocolVersions are the supported versions of the protocol (first is primary).
    27  var ProtocolVersions = []uint{UP01}
    28  
    29  // protocolLengths are the number of implemented message corresponding to different protocol versions.
    30  var protocolLengths = map[uint]uint64{UP01: EventsStreamResponse + 1}
    31  
    32  const protocolMaxMsgSize = native.ProtocolMaxMsgSize // Maximum cap on the size of a protocol message
    33  
    34  // protocol message codes
    35  const (
    36  	HandshakeMsg = 0
    37  
    38  	// Signals about the current synchronization status.
    39  	// The current peer's status is used during packs downloading,
    40  	// and to estimate may peer be interested in the new event or not
    41  	// (based on peer's epoch).
    42  	ProgressMsg = 1
    43  
    44  	EvmTxsMsg         = 2
    45  	NewEvmTxHashesMsg = 3
    46  	GetEvmTxsMsg      = 4
    47  
    48  	// Non-aggressive events propagation. Signals about newly-connected
    49  	// batch of events, sending only their IDs.
    50  	NewEventIDsMsg = 5
    51  
    52  	// Request the batch of events by IDs
    53  	GetEventsMsg = 6
    54  	// Contains the batch of events.
    55  	// May be an answer to GetEventsMsg, or be sent during aggressive events propagation.
    56  	EventsMsg = 7
    57  
    58  	// Request a range of events by a selector
    59  	RequestEventsStream = 8
    60  	// Contains the requested events by RequestEventsStream
    61  	EventsStreamResponse = 9
    62  
    63  	RequestBVsStream  = 10
    64  	BVsStreamResponse = 11
    65  	RequestBRsStream  = 12
    66  	BRsStreamResponse = 13
    67  	RequestEPsStream  = 14
    68  	EPsStreamResponse = 15
    69  )
    70  
    71  type errCode int
    72  
    73  const (
    74  	ErrMsgTooLarge = iota
    75  	ErrDecode
    76  	ErrInvalidMsgCode
    77  	ErrProtocolVersionMismatch
    78  	ErrNetworkIDMismatch
    79  	ErrGenesisMismatch
    80  	ErrNoStatusMsg
    81  	ErrExtraStatusMsg
    82  	ErrSuspendedPeer
    83  	ErrEmptyMessage = 0xf00
    84  )
    85  
    86  func (e errCode) String() string {
    87  	return errorToString[int(e)]
    88  }
    89  
    90  // XXX change once legacy code is out
    91  var errorToString = map[int]string{
    92  	ErrMsgTooLarge:             "Message too long",
    93  	ErrDecode:                  "Invalid message",
    94  	ErrInvalidMsgCode:          "Invalid message code",
    95  	ErrProtocolVersionMismatch: "Protocol version mismatch",
    96  	ErrNetworkIDMismatch:       "NetworkId mismatch",
    97  	ErrGenesisMismatch:         "Genesis object mismatch",
    98  	ErrNoStatusMsg:             "No status message",
    99  	ErrExtraStatusMsg:          "Extra status message",
   100  	ErrSuspendedPeer:           "Suspended peer",
   101  	ErrEmptyMessage:            "Empty message",
   102  }
   103  
   104  type TxPool interface {
   105  	emitter.TxPool
   106  	SubscribeNewTxsNotify(chan<- evmcore.NewTxsNotify) notify.Subscription
   107  	// AddRemotes should add the given transactions to the pool.
   108  	AddRemotes([]*types.Transaction) []error
   109  	AddLocals(txs []*types.Transaction) []error
   110  	AddLocal(tx *types.Transaction) error
   111  
   112  	Get(common.Hash) *types.Transaction
   113  
   114  	OnlyNotExisting(hashes []common.Hash) []common.Hash
   115  	SampleHashes(max int) []common.Hash
   116  
   117  	Nonce(addr common.Address) uint64
   118  	Stats() (int, int)
   119  	Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
   120  	ContentFrom(addr common.Address) (types.Transactions, types.Transactions)
   121  }
   122  
   123  // handshakeData is the network packet for the initial handshake message
   124  type handshakeData struct {
   125  	ProtocolVersion uint32
   126  	NetworkID       uint64
   127  	Genesis         common.Hash
   128  }
   129  
   130  // PeerProgress is synchronization status of a peer
   131  type PeerProgress struct {
   132  	Epoch            idx.Epoch
   133  	LastBlockIdx     idx.Block
   134  	LastBlockAtropos hash.Event
   135  	// Currently unused
   136  	HighestLamport idx.Lamport
   137  }
   138  
   139  type dagChunk struct {
   140  	SessionID uint32
   141  	Done      bool
   142  	IDs       hash.Events
   143  	Events    native.EventPayloads
   144  }
   145  
   146  type bvsChunk struct {
   147  	SessionID uint32
   148  	Done      bool
   149  	BVs       []native.LlrSignedBlockVotes
   150  }
   151  
   152  type brsChunk struct {
   153  	SessionID uint32
   154  	Done      bool
   155  	BRs       []ibr.LlrIdxFullBlockRecord
   156  }
   157  
   158  type epsChunk struct {
   159  	SessionID uint32
   160  	Done      bool
   161  	EPs       []iep.LlrEpochPack
   162  }