forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fields.go
151 lines (144 loc) · 4.41 KB
/
fields.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"github.com/MetalBlockchain/metalgo/utils/wrappers"
)
// Field that may be packed into a message
type Field uint32
// Fields that may be packed. These values are not sent over the wire.
const (
VersionStr Field = iota // Used in handshake
NetworkID // Used in handshake
NodeID // TODO: remove NodeID. Used in handshake
MyTime // Used in handshake
IP // Used in handshake
ChainID // Used for dispatching
RequestID // Used for all messages
Deadline // Used for request messages
ContainerID // Used for querying
ContainerBytes // Used for gossiping
ContainerIDs // Used for querying
MultiContainerBytes // Used in Ancestors
SigBytes // Used in handshake / peer gossiping
VersionTime // Used in handshake / peer gossiping
Peers // Used in peer gossiping
TrackedSubnets // Used in handshake / peer gossiping
AppBytes // Used at application level
VMMessage // Used internally
Uptime // Used for Pong
SummaryBytes // Used for state sync
SummaryHeights // Used for state sync
SummaryIDs // Used for state sync
VersionStruct // Used internally
)
// Packer returns the packer function that can be used to pack this field.
func (f Field) Packer() func(*wrappers.Packer, interface{}) {
switch f {
case VersionStr:
return wrappers.TryPackStr
case NetworkID, NodeID, RequestID:
return wrappers.TryPackInt
case MyTime, Deadline, VersionTime:
return wrappers.TryPackLong
case IP:
return wrappers.TryPackIP
case ChainID, ContainerID: // TODO: This will be shortened to use a modified varint spec
return wrappers.TryPackHash
case ContainerBytes, AppBytes, SigBytes, SummaryBytes:
return wrappers.TryPackBytes
case ContainerIDs, TrackedSubnets, SummaryIDs:
return wrappers.TryPackHashes
case MultiContainerBytes:
return wrappers.TryPack2DBytes
case Peers:
return wrappers.TryPackClaimedIPPortList
case Uptime:
return wrappers.TryPackByte
case SummaryHeights:
return wrappers.TryPackUint64Slice
default:
return nil
}
}
// Unpacker returns the unpacker function that can be used to unpack this field.
func (f Field) Unpacker() func(*wrappers.Packer) interface{} {
switch f {
case VersionStr:
return wrappers.TryUnpackStr
case NetworkID, NodeID, RequestID:
return wrappers.TryUnpackInt
case MyTime, Deadline, VersionTime:
return wrappers.TryUnpackLong
case IP:
return wrappers.TryUnpackIP
case ChainID, ContainerID: // TODO: This will be shortened to use a modified varint spec
return wrappers.TryUnpackHash
case ContainerBytes, AppBytes, SigBytes, SummaryBytes:
return wrappers.TryUnpackBytes
case ContainerIDs, TrackedSubnets, SummaryIDs:
return wrappers.TryUnpackHashes
case MultiContainerBytes:
return wrappers.TryUnpack2DBytes
case Peers:
return wrappers.TryUnpackClaimedIPPortList
case Uptime:
return wrappers.TryUnpackByte
case SummaryHeights:
return wrappers.TryUnpackUint64Slice
default:
return nil
}
}
func (f Field) String() string {
switch f {
case VersionStr:
return "VersionStr"
case NetworkID:
return "NetworkID"
case NodeID:
return "NodeID"
case MyTime:
return "MyTime"
case IP:
return "IP"
case ChainID:
return "ChainID"
case RequestID:
return "RequestID"
case Deadline:
return "Deadline"
case ContainerID:
return "ContainerID"
case ContainerBytes:
return "Container Bytes"
case ContainerIDs:
return "Container IDs"
case MultiContainerBytes:
return "MultiContainerBytes"
case AppBytes:
return "AppBytes"
case SigBytes:
return "SigBytes"
case VersionTime:
return "VersionTime"
case Peers:
return "Peers"
case TrackedSubnets:
return "TrackedSubnets"
case VMMessage:
return "VMMessage"
case Uptime:
return "Uptime"
case SummaryBytes:
return "Summary"
case SummaryHeights:
return "SummaryHeights"
case SummaryIDs:
return "SummaryIDs"
case VersionStruct:
return "VersionStruct"
default:
return "Unknown Field"
}
}