github.com/MetalBlockchain/subnet-evm@v0.4.9/plugin/evm/message/message.go (about)

     1  // (c) 2019-2021, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package message
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/MetalBlockchain/metalgo/codec"
    11  
    12  	"github.com/ethereum/go-ethereum/common"
    13  
    14  	"github.com/MetalBlockchain/metalgo/ids"
    15  	"github.com/MetalBlockchain/metalgo/utils/units"
    16  )
    17  
    18  const (
    19  	// TxMsgSoftCapSize is the ideal size of encoded transaction bytes we send in
    20  	// any [Txs] message. We do not limit inbound messages to
    21  	// this size, however. Max inbound message size is enforced by the codec
    22  	// (512KB).
    23  	TxMsgSoftCapSize = common.StorageSize(64 * units.KiB)
    24  )
    25  
    26  var (
    27  	_ GossipMessage = TxsGossip{}
    28  
    29  	errUnexpectedCodecVersion = errors.New("unexpected codec version")
    30  )
    31  
    32  type GossipMessage interface {
    33  	// types implementing GossipMessage should also implement fmt.Stringer for logging purposes.
    34  	fmt.Stringer
    35  
    36  	// Handle this gossip message with the gossip handler.
    37  	Handle(handler GossipHandler, nodeID ids.NodeID) error
    38  }
    39  
    40  type TxsGossip struct {
    41  	Txs []byte `serialize:"true"`
    42  }
    43  
    44  func (msg TxsGossip) Handle(handler GossipHandler, nodeID ids.NodeID) error {
    45  	return handler.HandleTxs(nodeID, msg)
    46  }
    47  
    48  func (msg TxsGossip) String() string {
    49  	return fmt.Sprintf("TxsGossip(Len=%d)", len(msg.Txs))
    50  }
    51  
    52  func ParseGossipMessage(codec codec.Manager, bytes []byte) (GossipMessage, error) {
    53  	var msg GossipMessage
    54  	version, err := codec.Unmarshal(bytes, &msg)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	if version != Version {
    59  		return nil, errUnexpectedCodecVersion
    60  	}
    61  	return msg, nil
    62  }
    63  
    64  func BuildGossipMessage(codec codec.Manager, msg GossipMessage) ([]byte, error) {
    65  	bytes, err := codec.Marshal(Version, &msg)
    66  	return bytes, err
    67  }