github.com/btcsuite/btcd@v0.24.0/wire/protocol.go (about)

     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  // 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  
   108  // Map of service flags back to their constant names for pretty printing.
   109  var sfStrings = map[ServiceFlag]string{
   110  	SFNodeNetwork:        "SFNodeNetwork",
   111  	SFNodeGetUTXO:        "SFNodeGetUTXO",
   112  	SFNodeBloom:          "SFNodeBloom",
   113  	SFNodeWitness:        "SFNodeWitness",
   114  	SFNodeXthin:          "SFNodeXthin",
   115  	SFNodeBit5:           "SFNodeBit5",
   116  	SFNodeCF:             "SFNodeCF",
   117  	SFNode2X:             "SFNode2X",
   118  	SFNodeNetworkLimited: "SFNodeNetworkLimited",
   119  }
   120  
   121  // orderedSFStrings is an ordered list of service flags from highest to
   122  // lowest.
   123  var orderedSFStrings = []ServiceFlag{
   124  	SFNodeNetwork,
   125  	SFNodeGetUTXO,
   126  	SFNodeBloom,
   127  	SFNodeWitness,
   128  	SFNodeXthin,
   129  	SFNodeBit5,
   130  	SFNodeCF,
   131  	SFNode2X,
   132  	SFNodeNetworkLimited,
   133  }
   134  
   135  // HasFlag returns a bool indicating if the service has the given flag.
   136  func (f ServiceFlag) HasFlag(s ServiceFlag) bool {
   137  	return f&s == s
   138  }
   139  
   140  // String returns the ServiceFlag in human-readable form.
   141  func (f ServiceFlag) String() string {
   142  	// No flags are set.
   143  	if f == 0 {
   144  		return "0x0"
   145  	}
   146  
   147  	// Add individual bit flags.
   148  	s := ""
   149  	for _, flag := range orderedSFStrings {
   150  		if f&flag == flag {
   151  			s += sfStrings[flag] + "|"
   152  			f -= flag
   153  		}
   154  	}
   155  
   156  	// Add any remaining flags which aren't accounted for as hex.
   157  	s = strings.TrimRight(s, "|")
   158  	if f != 0 {
   159  		s += "|0x" + strconv.FormatUint(uint64(f), 16)
   160  	}
   161  	s = strings.TrimLeft(s, "|")
   162  	return s
   163  }
   164  
   165  // BitcoinNet represents which bitcoin network a message belongs to.
   166  type BitcoinNet uint32
   167  
   168  // Constants used to indicate the message bitcoin network.  They can also be
   169  // used to seek to the next message when a stream's state is unknown, but
   170  // this package does not provide that functionality since it's generally a
   171  // better idea to simply disconnect clients that are misbehaving over TCP.
   172  const (
   173  	// MainNet represents the main bitcoin network.
   174  	MainNet BitcoinNet = 0xd9b4bef9
   175  
   176  	// TestNet represents the regression test network.
   177  	TestNet BitcoinNet = 0xdab5bffa
   178  
   179  	// TestNet3 represents the test network (version 3).
   180  	TestNet3 BitcoinNet = 0x0709110b
   181  
   182  	// SimNet represents the simulation test network.
   183  	SimNet BitcoinNet = 0x12141c16
   184  )
   185  
   186  // bnStrings is a map of bitcoin networks back to their constant names for
   187  // pretty printing.
   188  var bnStrings = map[BitcoinNet]string{
   189  	MainNet:  "MainNet",
   190  	TestNet:  "TestNet",
   191  	TestNet3: "TestNet3",
   192  	SimNet:   "SimNet",
   193  }
   194  
   195  // String returns the BitcoinNet in human-readable form.
   196  func (n BitcoinNet) String() string {
   197  	if s, ok := bnStrings[n]; ok {
   198  		return s
   199  	}
   200  
   201  	return fmt.Sprintf("Unknown BitcoinNet (%d)", uint32(n))
   202  }