This implements new getcfilterv2 and cfilterv2 messages which are used to request and deliver version 2 committed gcs filters along with their associated header commitment inclusion proof. The getcfilterv2 message requests a version 2 gcs filter and proof for a given block hash. The cfilterv2 message is sent in response with the requested filter and proof. It should be noted that since all header commitments require consensus changes, the proof can only be used once a consensus vote passes and the header actually commits to the filter. Finally, it also bumps the minor default user agent version for the wire package to account for the updates.
166 lines
4.7 KiB
Go
166 lines
4.7 KiB
Go
// Copyright (c) 2019 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"
|
|
)
|
|
|
|
// MaxHeaderProofHashes is the maximum number of header commitment inclusion
|
|
// proof hashes that can be in a single message. It is based on the fact that
|
|
// the proofs are logarithmic in nature and hence a value of 32 supports proofs
|
|
// of up to 2^32 commitments.
|
|
const MaxHeaderProofHashes = 32
|
|
|
|
// MsgCFilterV2 implements the Message interface and represents a cfilterv2
|
|
// message. It is used to deliver a version 2 committed gcs filter for a given
|
|
// block along with a proof that can be used to prove the filter is committed to
|
|
// by the block header. Note that the proof is only useful once the vote to
|
|
// enable header commitments is active.
|
|
//
|
|
// It is delivered in response to a getcfilterv2 message (MsgGetCFilterV2).
|
|
// Unknown blocks are ignored.
|
|
type MsgCFilterV2 struct {
|
|
BlockHash chainhash.Hash
|
|
Data []byte
|
|
ProofIndex uint32
|
|
ProofHashes []chainhash.Hash
|
|
}
|
|
|
|
// BtcDecode decodes r using the Decred protocol encoding into the receiver.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgCFilterV2) BtcDecode(r io.Reader, pver uint32) error {
|
|
if pver < CFilterV2Version {
|
|
str := fmt.Sprintf("%s message invalid for protocol version %d",
|
|
msg.Command(), pver)
|
|
return messageError("MsgCFilterV2.BtcDecode", str)
|
|
}
|
|
|
|
err := readElement(r, &msg.BlockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
msg.Data, err = ReadVarBytes(r, pver, MaxCFilterDataSize, "cfilterv2 data")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = readElement(r, &msg.ProofIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Read num proof hashes and limit to max.
|
|
count, err := ReadVarInt(r, pver)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > MaxHeaderProofHashes {
|
|
str := fmt.Sprintf("too many proof hashes for message [count %v, "+
|
|
"max %v]", count, MaxHeaderProofHashes)
|
|
return messageError("MsgCFilterV2.BtcDecode", str)
|
|
}
|
|
|
|
// Create a contiguous slice of hashes to deserialize into in order to
|
|
// reduce the number of allocations.
|
|
msg.ProofHashes = make([]chainhash.Hash, count)
|
|
for i := uint64(0); i < count; i++ {
|
|
err := readElement(r, &msg.ProofHashes[i])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// BtcEncode encodes the receiver to w using the Decred protocol encoding.
|
|
// This is part of the Message interface implementation.
|
|
func (msg *MsgCFilterV2) BtcEncode(w io.Writer, pver uint32) error {
|
|
if pver < CFilterV2Version {
|
|
str := fmt.Sprintf("%s message invalid for protocol version %d",
|
|
msg.Command(), pver)
|
|
return messageError("MsgCFilterV2.BtcEncode", str)
|
|
}
|
|
|
|
size := len(msg.Data)
|
|
if size > MaxCFilterDataSize {
|
|
str := fmt.Sprintf("filter size too large for message [size %v, "+
|
|
"max %v]", size, MaxCFilterDataSize)
|
|
return messageError("MsgCFilterV2.BtcEncode", str)
|
|
}
|
|
|
|
numHashes := len(msg.ProofHashes)
|
|
if numHashes > MaxHeaderProofHashes {
|
|
str := fmt.Sprintf("too many proof hashes for message [count %v, "+
|
|
"max %v]", numHashes, MaxHeaderProofHashes)
|
|
return messageError("MsgCFilterV2.BtcEncode", str)
|
|
}
|
|
|
|
err := writeElement(w, &msg.BlockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = WriteVarBytes(w, pver, msg.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = writeElement(w, msg.ProofIndex)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = WriteVarInt(w, pver, uint64(numHashes))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, hash := range msg.ProofHashes {
|
|
err := writeElement(w, hash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
// of the Message interface implementation.
|
|
func (msg *MsgCFilterV2) Command() string {
|
|
return CmdCFilterV2
|
|
}
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
// receiver. This is part of the Message interface implementation.
|
|
func (msg *MsgCFilterV2) MaxPayloadLength(pver uint32) uint32 {
|
|
// Block hash + max filter data (including varint) +
|
|
// proof index + max num proof hashes (including varint).
|
|
return chainhash.HashSize +
|
|
uint32(VarIntSerializeSize(MaxCFilterDataSize)) +
|
|
MaxCFilterDataSize + 4 +
|
|
uint32(VarIntSerializeSize(MaxHeaderProofHashes)) +
|
|
(MaxHeaderProofHashes * chainhash.HashSize)
|
|
}
|
|
|
|
// NewMsgCFilterV2 returns a new cfilterv2 message that conforms to the Message
|
|
// interface using the passed parameters and defaults for the remaining fields.
|
|
func NewMsgCFilterV2(blockHash *chainhash.Hash, filterData []byte,
|
|
proofIndex uint32, proofHashes []chainhash.Hash) *MsgCFilterV2 {
|
|
|
|
return &MsgCFilterV2{
|
|
BlockHash: *blockHash,
|
|
Data: filterData,
|
|
ProofIndex: proofIndex,
|
|
ProofHashes: proofHashes,
|
|
}
|
|
}
|