mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
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.
139 lines
4.2 KiB
Go
139 lines
4.2 KiB
Go
// Copyright (c) 2017 The btcsuite developers
|
|
// Copyright (c) 2017 The Lightning Network Developers
|
|
// Copyright (c) 2018 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"
|
|
"io"
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
)
|
|
|
|
// MsgGetCFHeaders is a message similar to MsgGetHeaders, but for committed
|
|
// filter headers. It allows to set the FilterType field to get headers in the
|
|
// chain of basic (0x00) or extended (0x01) headers.
|
|
type MsgGetCFHeaders struct {
|
|
BlockLocatorHashes []*chainhash.Hash
|
|
HashStop chainhash.Hash
|
|
FilterType FilterType
|
|
}
|
|
|
|
// AddBlockLocatorHash adds a new block locator hash to the message.
|
|
func (msg *MsgGetCFHeaders) AddBlockLocatorHash(hash *chainhash.Hash) error {
|
|
if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg {
|
|
str := fmt.Sprintf("too many block locator hashes for message [max %v]",
|
|
MaxBlockLocatorsPerMsg)
|
|
return messageError("MsgGetCFHeaders.AddBlockLocatorHash", str)
|
|
}
|
|
|
|
msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash)
|
|
return nil
|
|
}
|
|
|
|
// BtcDecode decodes r using the wire protocol encoding into the receiver.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgGetCFHeaders) BtcDecode(r io.Reader, pver uint32) error {
|
|
if pver < NodeCFVersion {
|
|
str := fmt.Sprintf("getcfheaders message invalid for protocol "+
|
|
"version %d", pver)
|
|
return messageError("MsgCFHeaders.BtcDecode", str)
|
|
}
|
|
|
|
// Read num block locator hashes and limit to max.
|
|
count, err := ReadVarInt(r, pver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > MaxBlockLocatorsPerMsg {
|
|
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
return messageError("MsgGetHeaders.BtcDecode", str)
|
|
}
|
|
|
|
// Create a contiguous slice of hashes to deserialize into in order to
|
|
// reduce the number of allocations.
|
|
locatorHashes := make([]chainhash.Hash, count)
|
|
msg.BlockLocatorHashes = make([]*chainhash.Hash, 0, count)
|
|
for i := uint64(0); i < count; i++ {
|
|
hash := &locatorHashes[i]
|
|
err := readElement(r, hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
msg.AddBlockLocatorHash(hash)
|
|
}
|
|
|
|
err = readElement(r, &msg.HashStop)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return readElement(r, (*uint8)(&msg.FilterType))
|
|
}
|
|
|
|
// BtcEncode encodes the receiver to w using the wire protocol encoding.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgGetCFHeaders) BtcEncode(w io.Writer, pver uint32) error {
|
|
if pver < NodeCFVersion {
|
|
str := fmt.Sprintf("getcfheaders message invalid for protocol "+
|
|
"version %d", pver)
|
|
return messageError("MsgCFHeaders.BtcEncode", str)
|
|
}
|
|
|
|
// Limit to max block locator hashes per message.
|
|
count := len(msg.BlockLocatorHashes)
|
|
if count > MaxBlockLocatorsPerMsg {
|
|
str := fmt.Sprintf("too many block locator hashes for message "+
|
|
"[count %v, max %v]", count, MaxBlockLocatorsPerMsg)
|
|
return messageError("MsgGetHeaders.BtcEncode", str)
|
|
}
|
|
|
|
err := WriteVarInt(w, pver, uint64(count))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, hash := range msg.BlockLocatorHashes {
|
|
err := writeElement(w, hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err = writeElement(w, &msg.HashStop)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return binarySerializer.PutUint8(w, uint8(msg.FilterType))
|
|
}
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
// of the Message interface implementation.
|
|
func (msg *MsgGetCFHeaders) Command() string {
|
|
return CmdGetCFHeaders
|
|
}
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
// receiver. This is part of the Message interface implementation.
|
|
func (msg *MsgGetCFHeaders) MaxPayloadLength(pver uint32) uint32 {
|
|
// Num block locator hashes (varInt) + max allowed
|
|
// block locators + hash stop + filter type 1 byte.
|
|
return MaxVarIntPayload + (MaxBlockLocatorsPerMsg *
|
|
chainhash.HashSize) + chainhash.HashSize + 1
|
|
}
|
|
|
|
// NewMsgGetCFHeaders returns a new getcfheader message that conforms to the
|
|
// Message interface using the passed parameters and defaults for the remaining
|
|
// fields.
|
|
func NewMsgGetCFHeaders() *MsgGetCFHeaders {
|
|
return &MsgGetCFHeaders{
|
|
BlockLocatorHashes: make([]*chainhash.Hash, 0,
|
|
MaxBlockLocatorsPerMsg),
|
|
}
|
|
}
|