dcrd/wire/protocol.go
Josh Rickmar 71500c80f2 multi: Add initial committed filter (CF) support
This change begins the work of bringing committed filters to the
network consensus daemon.  Committed filters are designed to enable
light wallets without many of the privacy issues associated with
server-side bloom filtering.

The new gcs package provides the primitives for creating and matching
against Golomb-coded sets (GCS) filters while the blockcf package
provides creation of filters and filter entries for data structures
found in blocks.

The wire package has been updated to define a new protocol version and
service flag for advertising CF support and includes types for the
following new messages: cfheaders, cfilter, cftypes, getcfheaders,
getcfilter, getcftypes.  The peer package and server implementation
have been updated to include support for the new protocol version and
messages.

Filters are created using a collision probability of 2^-20 and are
saved to a new optional database index when running with committed
filter support enabled (the default).  At first startup, if support is
not disabled, the index will be created and populated with filters and
filter headers for all preexisting blocks, and new filters will be
recorded for processed blocks.

Multiple filter types are supported.  The regular filter commits to
output scripts and previous outpoints that any non-voting wallet will
require access to.  Scripts and previous outpoints that can only be
spent by votes and revocations are not committed to the filter.  The
extended filter is a supplementary filter which commits to all
transaction hashes and script data pushes from the input scripts of
non-coinbase regular and ticket purchase transactions.  Creating these
filters is based on the algorithm defined by BIP0158 but is modified
to only commit "regular" data in stake transactions to prevent
committed filters from being used to create SPV voting wallets.
2018-03-30 13:52:12 -04:00

138 lines
3.5 KiB
Go

// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wire
import (
"fmt"
"strconv"
"strings"
)
const (
// InitialProcotolVersion is the initial protocol version for the
// network.
InitialProcotolVersion uint32 = 1
// ProtocolVersion is the latest protocol version this package supports.
ProtocolVersion uint32 = 6
// NodeBloomVersion is the protocol version which added the SFNodeBloom
// service flag.
NodeBloomVersion uint32 = 2
// SendHeadersVersion is the protocol version which added a new
// sendheaders message.
SendHeadersVersion uint32 = 3
// MaxBlockSizeVersion is the protocol version which increased the
// original blocksize.
MaxBlockSizeVersion uint32 = 4
// FeeFilterVersion is the protocol version which added a new
// feefilter message.
FeeFilterVersion uint32 = 5
// NodeCFVersion is the protocol version which adds the SFNodeCF service
// flag and the cfheaders, cfilter, cftypes, getcfheaders, getcfilter and
// getcftypes messages.
NodeCFVersion uint32 = 6
)
// ServiceFlag identifies services supported by a Decred peer.
type ServiceFlag uint64
const (
// SFNodeNetwork is a flag used to indicate a peer is a full node.
SFNodeNetwork ServiceFlag = 1 << iota
// SFNodeBloom is a flag used to indiciate a peer supports bloom
// filtering.
SFNodeBloom
// SFNodeCF is a flag used to indicate a peer supports committed
// filters (CFs).
SFNodeCF
)
// Map of service flags back to their constant names for pretty printing.
var sfStrings = map[ServiceFlag]string{
SFNodeNetwork: "SFNodeNetwork",
SFNodeBloom: "SFNodeBloom",
SFNodeCF: "SFNodeCF",
}
// orderedSFStrings is an ordered list of service flags from highest to
// lowest.
var orderedSFStrings = []ServiceFlag{
SFNodeNetwork,
SFNodeBloom,
SFNodeCF,
}
// 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 := ""
for _, flag := range orderedSFStrings {
if f&flag == flag {
s += sfStrings[flag] + "|"
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
}
// CurrencyNet represents which Decred network a message belongs to.
type CurrencyNet uint32
// Constants used to indicate the message Decred 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 (
// MainNet represents the main Decred network.
MainNet CurrencyNet = 0xd9b400f9
// RegTest represents the regression test network.
RegTest CurrencyNet = 0xdab500fa
// TestNet2 represents the 2nd test network.
TestNet2 CurrencyNet = 0x48e7a065
// SimNet represents the simulation test network.
SimNet CurrencyNet = 0x12141c16
)
// bnStrings is a map of Decred networks back to their constant names for
// pretty printing.
var bnStrings = map[CurrencyNet]string{
MainNet: "MainNet",
TestNet2: "TestNet2",
RegTest: "RegNet",
SimNet: "SimNet",
}
// String returns the CurrencyNet in human-readable form.
func (n CurrencyNet) String() string {
if s, ok := bnStrings[n]; ok {
return s
}
return fmt.Sprintf("Unknown CurrencyNet (%d)", uint32(n))
}