1 // Copyright (c) 2013-2024 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4 5 package wire
6 7 import (
8 "fmt"
9 "strconv"
10 "strings"
11 )
12 13 // XXX pedro: we will probably need to bump this.
14 const (
15 // ProtocolVersion is the latest protocol version this package supports.
16 ProtocolVersion uint32 = 70016
17 18 // MultipleAddressVersion is the protocol version which added multiple
19 // addresses per message (pver >= MultipleAddressVersion).
20 MultipleAddressVersion uint32 = 209
21 22 // NetAddressTimeVersion is the protocol version which added the
23 // timestamp field (pver >= NetAddressTimeVersion).
24 NetAddressTimeVersion uint32 = 31402
25 26 // BIP0031Version is the protocol version AFTER which a pong message
27 // and nonce field in ping were added (pver > BIP0031Version).
28 BIP0031Version uint32 = 60000
29 30 // BIP0035Version is the protocol version which added the mempool
31 // message (pver >= BIP0035Version).
32 BIP0035Version uint32 = 60002
33 34 // BIP0037Version is the protocol version which added new connection
35 // bloom filtering related messages and extended the version message
36 // with a relay flag (pver >= BIP0037Version).
37 BIP0037Version uint32 = 70001
38 39 // RejectVersion is the protocol version which added a new reject
40 // message.
41 RejectVersion uint32 = 70002
42 43 // BIP0111Version is the protocol version which added the SFNodeBloom
44 // service flag.
45 BIP0111Version uint32 = 70011
46 47 // SendHeadersVersion is the protocol version which added a new
48 // sendheaders message.
49 SendHeadersVersion uint32 = 70012
50 51 // FeeFilterVersion is the protocol version which added a new
52 // feefilter message.
53 FeeFilterVersion uint32 = 70013
54 55 // AddrV2Version is the protocol version which added two new messages.
56 // sendaddrv2 is sent during the version-verack handshake and signals
57 // support for sending and receiving the addrv2 message. In the future,
58 // new messages that occur during the version-verack handshake will not
59 // come with a protocol version bump.
60 AddrV2Version uint32 = 70016
61 )
62 63 const (
64 // NodeNetworkLimitedBlockThreshold is the number of blocks that a node
65 // broadcasting SFNodeNetworkLimited MUST be able to serve from the tip.
66 NodeNetworkLimitedBlockThreshold = 288
67 )
68 69 // ServiceFlag identifies services supported by a bitcoin peer.
70 type ServiceFlag uint64
71 72 const (
73 // SFNodeNetwork is a flag used to indicate a peer is a full node.
74 SFNodeNetwork ServiceFlag = 1 << iota
75 76 // SFNodeGetUTXO is a flag used to indicate a peer supports the
77 // getutxos and utxos commands (BIP0064).
78 SFNodeGetUTXO
79 80 // SFNodeBloom is a flag used to indicate a peer supports bloom
81 // filtering.
82 SFNodeBloom
83 84 // SFNodeWitness is a flag used to indicate a peer supports blocks
85 // and transactions including witness data (BIP0144).
86 SFNodeWitness
87 88 // SFNodeXthin is a flag used to indicate a peer supports xthin blocks.
89 SFNodeXthin
90 91 // SFNodeBit5 is a flag used to indicate a peer supports a service
92 // defined by bit 5.
93 SFNodeBit5
94 95 // SFNodeCF is a flag used to indicate a peer supports committed
96 // filters (CFs).
97 SFNodeCF
98 99 // SFNode2X is a flag used to indicate a peer is running the Segwit2X
100 // software.
101 SFNode2X
102 103 // SFNodeNetWorkLimited is a flag used to indicate a peer supports serving
104 // the last 288 blocks.
105 SFNodeNetworkLimited = 1 << 10
106 107 // SFNodeP2PV2 is a flag used to indicate a peer supports BIP324 v2
108 // connections.
109 SFNodeP2PV2 = 1 << 11
110 )
111 112 // Map of service flags back to their constant names for pretty printing.
113 var sfStrings = map[ServiceFlag]string{
114 SFNodeNetwork: "SFNodeNetwork",
115 SFNodeGetUTXO: "SFNodeGetUTXO",
116 SFNodeBloom: "SFNodeBloom",
117 SFNodeWitness: "SFNodeWitness",
118 SFNodeXthin: "SFNodeXthin",
119 SFNodeBit5: "SFNodeBit5",
120 SFNodeCF: "SFNodeCF",
121 SFNode2X: "SFNode2X",
122 SFNodeNetworkLimited: "SFNodeNetworkLimited",
123 SFNodeP2PV2: "SFNodeP2PV2",
124 }
125 126 // orderedSFStrings is an ordered list of service flags from highest to
127 // lowest.
128 var orderedSFStrings = []ServiceFlag{
129 SFNodeNetwork,
130 SFNodeGetUTXO,
131 SFNodeBloom,
132 SFNodeWitness,
133 SFNodeXthin,
134 SFNodeBit5,
135 SFNodeCF,
136 SFNode2X,
137 SFNodeNetworkLimited,
138 SFNodeP2PV2,
139 }
140 141 // HasFlag returns a bool indicating if the service has the given flag.
142 func (f ServiceFlag) HasFlag(s ServiceFlag) bool {
143 return f&s == s
144 }
145 146 // String returns the ServiceFlag in human-readable form.
147 func (f ServiceFlag) String() string {
148 // No flags are set.
149 if f == 0 {
150 return "0x0"
151 }
152 153 // Add individual bit flags.
154 s := ""
155 for _, flag := range orderedSFStrings {
156 if f&flag == flag {
157 s += sfStrings[flag] + "|"
158 f -= flag
159 }
160 }
161 162 // Add any remaining flags which aren't accounted for as hex.
163 s = strings.TrimRight(s, "|")
164 if f != 0 {
165 s += "|0x" + strconv.FormatUint(uint64(f), 16)
166 }
167 s = strings.TrimLeft(s, "|")
168 return s
169 }
170 171 // BitcoinNet represents which bitcoin network a message belongs to.
172 type BitcoinNet uint32
173 174 // Constants used to indicate the message bitcoin network. They can also be
175 // used to seek to the next message when a stream's state is unknown, but
176 // this package does not provide that functionality since it's generally a
177 // better idea to simply disconnect clients that are misbehaving over TCP.
178 const (
179 // MainNet represents the main bitcoin network.
180 MainNet BitcoinNet = 0xd9b4bef9
181 182 // TestNet represents the regression test network.
183 TestNet BitcoinNet = 0xdab5bffa
184 185 // TestNet3 represents the test network (version 3).
186 TestNet3 BitcoinNet = 0x0709110b
187 188 // TestNet4 represents the test network (version 4).
189 TestNet4 BitcoinNet = 0x283f161c
190 191 // SigNet represents the public default SigNet. For custom signets,
192 // see CustomSignetParams.
193 SigNet BitcoinNet = 0x40CF030A
194 195 // SimNet represents the simulation test network.
196 SimNet BitcoinNet = 0x12141c16
197 )
198 199 // bnStrings is a map of bitcoin networks back to their constant names for
200 // pretty printing.
201 var bnStrings = map[BitcoinNet]string{
202 MainNet: "MainNet",
203 TestNet: "TestNet",
204 TestNet3: "TestNet3",
205 TestNet4: "TestNet4",
206 SigNet: "SigNet",
207 SimNet: "SimNet",
208 }
209 210 // String returns the BitcoinNet in human-readable form.
211 func (n BitcoinNet) String() string {
212 if s, ok := bnStrings[n]; ok {
213 return s
214 }
215 216 return fmt.Sprintf("Unknown BitcoinNet (%d)", uint32(n))
217 }
218