forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
80 lines (62 loc) · 2.02 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// (c) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"errors"
"fmt"
"github.com/haowang0402/avalanchego/codec"
"github.com/ethereum/go-ethereum/common"
"github.com/haowang0402/avalanchego/ids"
"github.com/haowang0402/avalanchego/utils/units"
)
const (
// EthMsgSoftCapSize is the ideal size of encoded transaction bytes we send in
// any [EthTxsGossip] or [AtomicTxGossip] message. We do not limit inbound messages to
// this size, however. Max inbound message size is enforced by the codec
// (512KB).
EthMsgSoftCapSize = common.StorageSize(64 * units.KiB)
)
var (
_ GossipMessage = AtomicTxGossip{}
_ GossipMessage = EthTxsGossip{}
errUnexpectedCodecVersion = errors.New("unexpected codec version")
)
type GossipMessage interface {
// types implementing GossipMessage should also implement fmt.Stringer for logging purposes.
fmt.Stringer
// Handle this gossip message with the gossip handler.
Handle(handler GossipHandler, nodeID ids.ShortID) error
}
type AtomicTxGossip struct {
Tx []byte `serialize:"true"`
}
func (msg AtomicTxGossip) Handle(handler GossipHandler, nodeID ids.ShortID) error {
return handler.HandleAtomicTx(nodeID, msg)
}
func (msg AtomicTxGossip) String() string {
return fmt.Sprintf("AtomicTxGossip(Len=%d)", len(msg.Tx))
}
type EthTxsGossip struct {
Txs []byte `serialize:"true"`
}
func (msg EthTxsGossip) Handle(handler GossipHandler, nodeID ids.ShortID) error {
return handler.HandleEthTxs(nodeID, msg)
}
func (msg EthTxsGossip) String() string {
return fmt.Sprintf("EthTxsGossip(Len=%d)", len(msg.Txs))
}
func ParseGossipMessage(codec codec.Manager, bytes []byte) (GossipMessage, error) {
var msg GossipMessage
version, err := codec.Unmarshal(bytes, &msg)
if err != nil {
return nil, err
}
if version != Version {
return nil, errUnexpectedCodecVersion
}
return msg, nil
}
func BuildGossipMessage(codec codec.Manager, msg GossipMessage) ([]byte, error) {
bytes, err := codec.Marshal(Version, &msg)
return bytes, err
}