github.com/dim4egster/coreth@v0.10.2/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/dim4egster/qmallgo/codec" 11 12 "github.com/ethereum/go-ethereum/common" 13 14 "github.com/dim4egster/qmallgo/ids" 15 "github.com/dim4egster/qmallgo/utils/units" 16 ) 17 18 const ( 19 // EthMsgSoftCapSize is the ideal size of encoded transaction bytes we send in 20 // any [EthTxsGossip] or [AtomicTxGossip] message. We do not limit inbound messages to 21 // this size, however. Max inbound message size is enforced by the codec 22 // (512KB). 23 EthMsgSoftCapSize = common.StorageSize(64 * units.KiB) 24 ) 25 26 var ( 27 _ GossipMessage = AtomicTxGossip{} 28 _ GossipMessage = EthTxsGossip{} 29 30 errUnexpectedCodecVersion = errors.New("unexpected codec version") 31 ) 32 33 type GossipMessage interface { 34 // types implementing GossipMessage should also implement fmt.Stringer for logging purposes. 35 fmt.Stringer 36 37 // Handle this gossip message with the gossip handler. 38 Handle(handler GossipHandler, nodeID ids.NodeID) error 39 } 40 41 type AtomicTxGossip struct { 42 Tx []byte `serialize:"true"` 43 } 44 45 func (msg AtomicTxGossip) Handle(handler GossipHandler, nodeID ids.NodeID) error { 46 return handler.HandleAtomicTx(nodeID, msg) 47 } 48 49 func (msg AtomicTxGossip) String() string { 50 return fmt.Sprintf("AtomicTxGossip(Len=%d)", len(msg.Tx)) 51 } 52 53 type EthTxsGossip struct { 54 Txs []byte `serialize:"true"` 55 } 56 57 func (msg EthTxsGossip) Handle(handler GossipHandler, nodeID ids.NodeID) error { 58 return handler.HandleEthTxs(nodeID, msg) 59 } 60 61 func (msg EthTxsGossip) String() string { 62 return fmt.Sprintf("EthTxsGossip(Len=%d)", len(msg.Txs)) 63 } 64 65 func ParseGossipMessage(codec codec.Manager, bytes []byte) (GossipMessage, error) { 66 var msg GossipMessage 67 version, err := codec.Unmarshal(bytes, &msg) 68 if err != nil { 69 return nil, err 70 } 71 if version != Version { 72 return nil, errUnexpectedCodecVersion 73 } 74 return msg, nil 75 } 76 77 func BuildGossipMessage(codec codec.Manager, msg GossipMessage) ([]byte, error) { 78 bytes, err := codec.Marshal(Version, &msg) 79 return bytes, err 80 }