2015-05-01 19:41:58 +02:00
|
|
|
// Copyright (c) 2013, 2014 The btcsuite developers
|
2014-07-10 03:01:38 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-09 04:59:33 +02:00
|
|
|
package bloom
|
|
|
|
|
|
|
|
import (
|
2015-01-30 23:15:57 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2015-02-05 21:48:38 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 22:13:38 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2014-07-09 04:59:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// merkleBlock is used to house intermediate information needed to generate a
|
2015-02-05 21:48:38 +01:00
|
|
|
// wire.MsgMerkleBlock according to a filter.
|
2014-07-09 04:59:33 +02:00
|
|
|
type merkleBlock struct {
|
|
|
|
numTx uint32
|
2015-02-05 21:48:38 +01:00
|
|
|
allHashes []*wire.ShaHash
|
|
|
|
finalHashes []*wire.ShaHash
|
2014-07-09 04:59:33 +02:00
|
|
|
matchedBits []byte
|
|
|
|
bits []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcTreeWidth calculates and returns the the number of nodes (width) or a
|
|
|
|
// merkle tree at the given depth-first height.
|
|
|
|
func (m *merkleBlock) calcTreeWidth(height uint32) uint32 {
|
|
|
|
return (m.numTx + (1 << height) - 1) >> height
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcHash returns the hash for a sub-tree given a depth-first height and
|
|
|
|
// node position.
|
2015-02-05 21:48:38 +01:00
|
|
|
func (m *merkleBlock) calcHash(height, pos uint32) *wire.ShaHash {
|
2014-07-09 04:59:33 +02:00
|
|
|
if height == 0 {
|
|
|
|
return m.allHashes[pos]
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:48:38 +01:00
|
|
|
var right *wire.ShaHash
|
2014-07-09 04:59:33 +02:00
|
|
|
left := m.calcHash(height-1, pos*2)
|
|
|
|
if pos*2+1 < m.calcTreeWidth(height-1) {
|
|
|
|
right = m.calcHash(height-1, pos*2+1)
|
|
|
|
} else {
|
|
|
|
right = left
|
|
|
|
}
|
2015-01-30 23:15:57 +01:00
|
|
|
return blockchain.HashMerkleBranches(left, right)
|
2014-07-09 04:59:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// traverseAndBuild builds a partial merkle tree using a recursive depth-first
|
|
|
|
// approach. As it calculates the hashes, it also saves whether or not each
|
|
|
|
// node is a parent node and a list of final hashes to be included in the
|
|
|
|
// merkle block.
|
|
|
|
func (m *merkleBlock) traverseAndBuild(height, pos uint32) {
|
|
|
|
// Determine whether this node is a parent of a matched node.
|
|
|
|
var isParent byte
|
|
|
|
for i := pos << height; i < (pos+1)<<height && i < m.numTx; i++ {
|
|
|
|
isParent |= m.matchedBits[i]
|
|
|
|
}
|
|
|
|
m.bits = append(m.bits, isParent)
|
|
|
|
|
|
|
|
// When the node is a leaf node or not a parent of a matched node,
|
|
|
|
// append the hash to the list that will be part of the final merkle
|
|
|
|
// block.
|
|
|
|
if height == 0 || isParent == 0x00 {
|
|
|
|
m.finalHashes = append(m.finalHashes, m.calcHash(height, pos))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the node is an internal node and it is the parent of
|
|
|
|
// of an included leaf node.
|
|
|
|
|
|
|
|
// Descend into the left child and process its sub-tree.
|
|
|
|
m.traverseAndBuild(height-1, pos*2)
|
|
|
|
|
|
|
|
// Descend into the right child and process its sub-tree if
|
|
|
|
// there is one.
|
|
|
|
if pos*2+1 < m.calcTreeWidth(height-1) {
|
|
|
|
m.traverseAndBuild(height-1, pos*2+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:48:38 +01:00
|
|
|
// NewMerkleBlock returns a new *wire.MsgMerkleBlock and an array of the matched
|
2015-07-28 11:02:49 +02:00
|
|
|
// transaction index numbers based on the passed block and filter.
|
|
|
|
func NewMerkleBlock(block *btcutil.Block, filter *Filter) (*wire.MsgMerkleBlock, []uint32) {
|
2014-07-09 04:59:33 +02:00
|
|
|
numTx := uint32(len(block.Transactions()))
|
|
|
|
mBlock := merkleBlock{
|
|
|
|
numTx: numTx,
|
2015-02-05 21:48:38 +01:00
|
|
|
allHashes: make([]*wire.ShaHash, 0, numTx),
|
2014-07-09 04:59:33 +02:00
|
|
|
matchedBits: make([]byte, 0, numTx),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find and keep track of any transactions that match the filter.
|
2015-07-28 11:02:49 +02:00
|
|
|
var matchedIndices []uint32
|
|
|
|
for txIndex, tx := range block.Transactions() {
|
2014-07-09 04:59:33 +02:00
|
|
|
if filter.MatchTxAndUpdate(tx) {
|
|
|
|
mBlock.matchedBits = append(mBlock.matchedBits, 0x01)
|
2015-07-28 11:02:49 +02:00
|
|
|
matchedIndices = append(matchedIndices, uint32(txIndex))
|
2014-07-09 04:59:33 +02:00
|
|
|
} else {
|
|
|
|
mBlock.matchedBits = append(mBlock.matchedBits, 0x00)
|
|
|
|
}
|
|
|
|
mBlock.allHashes = append(mBlock.allHashes, tx.Sha())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the number of merkle branches (height) in the tree.
|
|
|
|
height := uint32(0)
|
|
|
|
for mBlock.calcTreeWidth(height) > 1 {
|
|
|
|
height++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the depth-first partial merkle tree.
|
|
|
|
mBlock.traverseAndBuild(height, 0)
|
|
|
|
|
|
|
|
// Create and return the merkle block.
|
2015-02-05 21:48:38 +01:00
|
|
|
msgMerkleBlock := wire.MsgMerkleBlock{
|
2014-07-09 04:59:33 +02:00
|
|
|
Header: block.MsgBlock().Header,
|
|
|
|
Transactions: uint32(mBlock.numTx),
|
2015-02-05 21:48:38 +01:00
|
|
|
Hashes: make([]*wire.ShaHash, 0, len(mBlock.finalHashes)),
|
2014-07-09 04:59:33 +02:00
|
|
|
Flags: make([]byte, (len(mBlock.bits)+7)/8),
|
|
|
|
}
|
|
|
|
for _, sha := range mBlock.finalHashes {
|
|
|
|
msgMerkleBlock.AddTxHash(sha)
|
|
|
|
}
|
|
|
|
for i := uint32(0); i < uint32(len(mBlock.bits)); i++ {
|
|
|
|
msgMerkleBlock.Flags[i/8] |= mBlock.bits[i] << (i % 8)
|
|
|
|
}
|
2015-07-28 11:02:49 +02:00
|
|
|
return &msgMerkleBlock, matchedIndices
|
2014-07-09 04:59:33 +02:00
|
|
|
}
|