2015-12-02 20:53:24 +01:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
2014-01-03 15:32:43 +01:00
|
|
|
"fmt"
|
2013-05-08 21:31:00 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-07-27 23:31:47 +02:00
|
|
|
// ProtocolVersion is the latest protocol version this package supports.
|
2016-08-25 21:08:32 +02:00
|
|
|
ProtocolVersion uint32 = 70013
|
2013-07-27 23:31:47 +02:00
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// MultipleAddressVersion is the protocol version which added multiple
|
|
|
|
// addresses per message (pver >= MultipleAddressVersion).
|
|
|
|
MultipleAddressVersion uint32 = 209
|
|
|
|
|
|
|
|
// NetAddressTimeVersion is the protocol version which added the
|
|
|
|
// timestamp field (pver >= NetAddressTimeVersion).
|
|
|
|
NetAddressTimeVersion uint32 = 31402
|
|
|
|
|
|
|
|
// BIP0031Version is the protocol version AFTER which a pong message
|
|
|
|
// and nonce field in ping were added (pver > BIP0031Version).
|
|
|
|
BIP0031Version uint32 = 60000
|
|
|
|
|
|
|
|
// BIP0035Version is the protocol version which added the mempool
|
|
|
|
// message (pver >= BIP0035Version).
|
|
|
|
BIP0035Version uint32 = 60002
|
2013-05-10 07:54:56 +02:00
|
|
|
|
|
|
|
// BIP0037Version is the protocol version which added new connection
|
|
|
|
// bloom filtering related messages and extended the version message
|
|
|
|
// with a relay flag (pver >= BIP0037Version).
|
|
|
|
BIP0037Version uint32 = 70001
|
2013-11-12 06:12:07 +01:00
|
|
|
|
2016-08-25 21:08:32 +02:00
|
|
|
// RejectVersion is the protocol version which added a new reject
|
|
|
|
// message.
|
|
|
|
RejectVersion uint32 = 70002
|
|
|
|
|
2015-08-24 17:48:59 +02:00
|
|
|
// BIP0111Version is the protocol version which added the SFNodeBloom
|
|
|
|
// service flag.
|
|
|
|
BIP0111Version uint32 = 70011
|
|
|
|
|
2015-12-02 20:53:24 +01:00
|
|
|
// SendHeadersVersion is the protocol version which added a new
|
|
|
|
// sendheaders message.
|
|
|
|
SendHeadersVersion uint32 = 70012
|
|
|
|
|
2016-08-25 21:08:32 +02:00
|
|
|
// FeeFilterVersion is the protocol version which added a new
|
|
|
|
// feefilter message.
|
|
|
|
FeeFilterVersion uint32 = 70013
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServiceFlag identifies services supported by a bitcoin peer.
|
|
|
|
type ServiceFlag uint64
|
|
|
|
|
|
|
|
const (
|
2013-07-27 23:31:47 +02:00
|
|
|
// SFNodeNetwork is a flag used to indicate a peer is a full node.
|
2013-05-08 21:31:00 +02:00
|
|
|
SFNodeNetwork ServiceFlag = 1 << iota
|
2015-08-21 21:29:38 +02:00
|
|
|
|
|
|
|
// SFNodeGetUTXO is a flag used to indicate a peer supports the
|
|
|
|
// getutxos and utxos commands (BIP0064).
|
|
|
|
SFNodeGetUTXO
|
2015-08-24 17:48:59 +02:00
|
|
|
|
2017-05-30 16:59:51 +02:00
|
|
|
// SFNodeBloom is a flag used to indicate a peer supports bloom
|
2015-08-24 17:48:59 +02:00
|
|
|
// filtering.
|
|
|
|
SFNodeBloom
|
2016-10-19 00:54:27 +02:00
|
|
|
|
|
|
|
// SFNodeWitness is a flag used to indicate a peer supports blocks
|
|
|
|
// and transactions including witness data (BIP0144).
|
|
|
|
SFNodeWitness
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Map of service flags back to their constant names for pretty printing.
|
|
|
|
var sfStrings = map[ServiceFlag]string{
|
|
|
|
SFNodeNetwork: "SFNodeNetwork",
|
2015-08-21 21:29:38 +02:00
|
|
|
SFNodeGetUTXO: "SFNodeGetUTXO",
|
2015-08-24 17:48:59 +02:00
|
|
|
SFNodeBloom: "SFNodeBloom",
|
2016-10-19 00:54:27 +02:00
|
|
|
SFNodeWitness: "SFNodeWitness",
|
2015-08-21 21:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// orderedSFStrings is an ordered list of service flags from highest to
|
|
|
|
// lowest.
|
|
|
|
var orderedSFStrings = []ServiceFlag{
|
|
|
|
SFNodeNetwork,
|
|
|
|
SFNodeGetUTXO,
|
2015-08-24 17:48:59 +02:00
|
|
|
SFNodeBloom,
|
2016-10-19 00:54:27 +02:00
|
|
|
SFNodeWitness,
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the ServiceFlag in human-readable form.
|
|
|
|
func (f ServiceFlag) String() string {
|
|
|
|
// No flags are set.
|
|
|
|
if f == 0 {
|
|
|
|
return "0x0"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add individual bit flags.
|
|
|
|
s := ""
|
2015-08-21 21:29:38 +02:00
|
|
|
for _, flag := range orderedSFStrings {
|
2013-05-08 21:31:00 +02:00
|
|
|
if f&flag == flag {
|
2015-08-21 21:29:38 +02:00
|
|
|
s += sfStrings[flag] + "|"
|
2013-05-08 21:31:00 +02:00
|
|
|
f -= flag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any remaining flags which aren't accounted for as hex.
|
|
|
|
s = strings.TrimRight(s, "|")
|
|
|
|
if f != 0 {
|
|
|
|
s += "|0x" + strconv.FormatUint(uint64(f), 16)
|
|
|
|
}
|
|
|
|
s = strings.TrimLeft(s, "|")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// BitcoinNet represents which bitcoin network a message belongs to.
|
|
|
|
type BitcoinNet uint32
|
|
|
|
|
|
|
|
// Constants used to indicate the message bitcoin network. They can also be
|
|
|
|
// used to seek to the next message when a stream's state is unknown, but
|
|
|
|
// this package does not provide that functionality since it's generally a
|
|
|
|
// better idea to simply disconnect clients that are misbehaving over TCP.
|
|
|
|
const (
|
2013-07-27 23:18:13 +02:00
|
|
|
// MainNet represents the main bitcoin network.
|
2013-07-26 02:04:58 +02:00
|
|
|
MainNet BitcoinNet = 0xd9b4bef9
|
|
|
|
|
2013-07-27 23:18:13 +02:00
|
|
|
// TestNet represents the regression test network.
|
2013-07-26 02:04:58 +02:00
|
|
|
TestNet BitcoinNet = 0xdab5bffa
|
|
|
|
|
2013-07-27 23:18:13 +02:00
|
|
|
// TestNet3 represents the test network (version 3).
|
2013-05-08 21:31:00 +02:00
|
|
|
TestNet3 BitcoinNet = 0x0709110b
|
2014-05-28 17:28:10 +02:00
|
|
|
|
|
|
|
// SimNet represents the simulation test network.
|
|
|
|
SimNet BitcoinNet = 0x12141c16
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
2014-01-03 15:32:43 +01:00
|
|
|
|
|
|
|
// bnStrings is a map of bitcoin networks back to their constant names for
|
|
|
|
// pretty printing.
|
|
|
|
var bnStrings = map[BitcoinNet]string{
|
|
|
|
MainNet: "MainNet",
|
|
|
|
TestNet: "TestNet",
|
|
|
|
TestNet3: "TestNet3",
|
2014-05-28 17:28:10 +02:00
|
|
|
SimNet: "SimNet",
|
2014-01-03 15:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the BitcoinNet in human-readable form.
|
|
|
|
func (n BitcoinNet) String() string {
|
|
|
|
if s, ok := bnStrings[n]; ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Unknown BitcoinNet (%d)", uint32(n))
|
|
|
|
}
|