protocol.go raw

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