OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / wire / protocol.go
1 // Copyright (c) 2013-2016 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 const (
14         // ProtocolVersion is the latest protocol version this package supports.
15         ProtocolVersion uint32 = 70013
16
17         // MultipleAddressVersion is the protocol version which added multiple
18         // addresses per message (pver >= MultipleAddressVersion).
19         MultipleAddressVersion uint32 = 209
20
21         // NetAddressTimeVersion is the protocol version which added the
22         // timestamp field (pver >= NetAddressTimeVersion).
23         NetAddressTimeVersion uint32 = 31402
24
25         // BIP0031Version is the protocol version AFTER which a pong message
26         // and nonce field in ping were added (pver > BIP0031Version).
27         BIP0031Version uint32 = 60000
28
29         // BIP0035Version is the protocol version which added the mempool
30         // message (pver >= BIP0035Version).
31         BIP0035Version uint32 = 60002
32
33         // BIP0037Version is the protocol version which added new connection
34         // bloom filtering related messages and extended the version message
35         // with a relay flag (pver >= BIP0037Version).
36         BIP0037Version uint32 = 70001
37
38         // RejectVersion is the protocol version which added a new reject
39         // message.
40         RejectVersion uint32 = 70002
41
42         // BIP0111Version is the protocol version which added the SFNodeBloom
43         // service flag.
44         BIP0111Version uint32 = 70011
45
46         // SendHeadersVersion is the protocol version which added a new
47         // sendheaders message.
48         SendHeadersVersion uint32 = 70012
49
50         // FeeFilterVersion is the protocol version which added a new
51         // feefilter message.
52         FeeFilterVersion uint32 = 70013
53 )
54
55 // ServiceFlag identifies services supported by a bitcoin peer.
56 type ServiceFlag uint64
57
58 const (
59         // SFNodeNetwork is a flag used to indicate a peer is a full node.
60         SFNodeNetwork ServiceFlag = 1 << iota
61
62         // SFNodeGetUTXO is a flag used to indicate a peer supports the
63         // getutxos and utxos commands (BIP0064).
64         SFNodeGetUTXO
65
66         // SFNodeBloom is a flag used to indicate a peer supports bloom
67         // filtering.
68         SFNodeBloom
69
70         // SFNodeWitness is a flag used to indicate a peer supports blocks
71         // and transactions including witness data (BIP0144).
72         SFNodeWitness
73 )
74
75 // Map of service flags back to their constant names for pretty printing.
76 var sfStrings = map[ServiceFlag]string{
77         SFNodeNetwork: "SFNodeNetwork",
78         SFNodeGetUTXO: "SFNodeGetUTXO",
79         SFNodeBloom:   "SFNodeBloom",
80         SFNodeWitness: "SFNodeWitness",
81 }
82
83 // orderedSFStrings is an ordered list of service flags from highest to
84 // lowest.
85 var orderedSFStrings = []ServiceFlag{
86         SFNodeNetwork,
87         SFNodeGetUTXO,
88         SFNodeBloom,
89         SFNodeWitness,
90 }
91
92 // String returns the ServiceFlag in human-readable form.
93 func (f ServiceFlag) String() string {
94         // No flags are set.
95         if f == 0 {
96                 return "0x0"
97         }
98
99         // Add individual bit flags.
100         s := ""
101         for _, flag := range orderedSFStrings {
102                 if f&flag == flag {
103                         s += sfStrings[flag] + "|"
104                         f -= flag
105                 }
106         }
107
108         // Add any remaining flags which aren't accounted for as hex.
109         s = strings.TrimRight(s, "|")
110         if f != 0 {
111                 s += "|0x" + strconv.FormatUint(uint64(f), 16)
112         }
113         s = strings.TrimLeft(s, "|")
114         return s
115 }
116
117 // BitcoinNet represents which bitcoin network a message belongs to.
118 type BitcoinNet uint32
119
120 // Constants used to indicate the message bitcoin network.  They can also be
121 // used to seek to the next message when a stream's state is unknown, but
122 // this package does not provide that functionality since it's generally a
123 // better idea to simply disconnect clients that are misbehaving over TCP.
124 const (
125         // MainNet represents the main bitcoin network.
126         MainNet BitcoinNet = 0xd9b4bef9
127
128         // TestNet represents the regression test network.
129         TestNet BitcoinNet = 0xdab5bffa
130
131         // TestNet3 represents the test network (version 3).
132         TestNet3 BitcoinNet = 0x0709110b
133
134         // SimNet represents the simulation test network.
135         SimNet BitcoinNet = 0x12141c16
136 )
137
138 // bnStrings is a map of bitcoin networks back to their constant names for
139 // pretty printing.
140 var bnStrings = map[BitcoinNet]string{
141         MainNet:  "MainNet",
142         TestNet:  "TestNet",
143         TestNet3: "TestNet3",
144         SimNet:   "SimNet",
145 }
146
147 // String returns the BitcoinNet in human-readable form.
148 func (n BitcoinNet) String() string {
149         if s, ok := bnStrings[n]; ok {
150                 return s
151         }
152
153         return fmt.Sprintf("Unknown BitcoinNet (%d)", uint32(n))
154 }