bloom

package
v0.0.0-...-9cd94f0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2015 License: ISC Imports: 7 Imported by: 2

README

bloom

[Build Status] (https://travis-ci.org/btcsuite/btcutil)

Package bloom provides an API for dealing with bitcoin-specific bloom filters.

A comprehensive suite of tests is provided to ensure proper functionality. See test_coverage.txt for the gocov coverage report. Alternatively, if you are running a POSIX OS, you can run the cov_report.sh script for a real-time report. Package coinset is licensed under the liberal ISC license.

Documentation

[GoDoc] (http://godoc.org/github.com/btcsuite/btcutil/bloom)

Full go doc style documentation for the project can be viewed online without installing this package by using the GoDoc site here: http://godoc.org/github.com/btcsuite/btcutil/bloom

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/btcsuite/btcutil/bloom

Installation

$ go get github.com/btcsuite/btcutil/bloom

Examples

License

Package bloom is licensed under the copyfree ISC License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MurmurHash3

func MurmurHash3(seed uint32, data []byte) uint32

MurmurHash3 implements a non-cryptographic hash function using the MurmurHash3 algorithm. This implementation yields a 32-bit hash value which is suitable for general hash-based lookups. The seed can be used to effectively randomize the hash function. This makes it ideal for use in bloom filters which need multiple independent hash functions.

func NewMerkleBlock

func NewMerkleBlock(block *btcutil.Block, filter *Filter) (*btcwire.MsgMerkleBlock, []*btcwire.ShaHash)

NewMerkleBlock returns a new *btcwire.MsgMerkleBlock and an array of the matched transaction hashes based on the passed block and filter.

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter defines a bitcoin bloom filter that provides easy manipulation of raw filter data.

func LoadFilter

func LoadFilter(filter *btcwire.MsgFilterLoad) *Filter

LoadFilter creates a new Filter instance with the given underlying btcwire.MsgFilterLoad.

func NewFilter

func NewFilter(elements, tweak uint32, fprate float64, flags btcwire.BloomUpdateType) *Filter

NewFilter creates a new bloom filter instance, mainly to be used by SPV clients. The tweak parameter is a random value added to the seed value. The false positive rate is the probability of a false positive where 1.0 is "match everything" and zero is unachievable. Thus, providing any false positive rates less than 0 or greater than 1 will be adjusted to the valid range.

For more information on what values to use for both elements and fprate, see https://en.wikipedia.org/wiki/Bloom_filter.

Example

This example demonstrates how to create a new bloom filter, add a transaction hash to it, and check if the filter matches the transaction.

package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/PointCoin/btcutil/bloom"
	"github.com/PointCoin/btcwire"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	tweak := rand.Uint32()

	// Create a new bloom filter intended to hold 10 elements with a 0.01%
	// false positive rate and does not include any automatic update
	// functionality when transactions are matched.
	filter := bloom.NewFilter(10, tweak, 0.0001, btcwire.BloomUpdateNone)

	// Create a transaction hash and add it to the filter.  This particular
	// trasaction is the first transaction in block 310,000 of the main
	// bitcoin block chain.
	txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45"
	txHash, err := btcwire.NewShaHashFromStr(txHashStr)
	if err != nil {
		fmt.Println(err)
		return
	}
	filter.AddShaHash(txHash)

	// Show that the filter matches.
	matches := filter.Matches(txHash.Bytes())
	fmt.Println("Filter Matches?:", matches)

}
Output:

Filter Matches?: true

func (*Filter) Add

func (bf *Filter) Add(data []byte)

Add adds the passed byte slice to the bloom filter.

This function is safe for concurrent access.

func (*Filter) AddOutPoint

func (bf *Filter) AddOutPoint(outpoint *btcwire.OutPoint)

AddOutPoint adds the passed transaction outpoint to the bloom filter.

This function is safe for concurrent access.

func (*Filter) AddShaHash

func (bf *Filter) AddShaHash(sha *btcwire.ShaHash)

AddShaHash adds the passed btcwire.ShaHash to the Filter.

This function is safe for concurrent access.

func (*Filter) IsLoaded

func (bf *Filter) IsLoaded() bool

IsLoaded returns true if a filter is loaded, otherwise false.

This function is safe for concurrent access.

func (*Filter) MatchTxAndUpdate

func (bf *Filter) MatchTxAndUpdate(tx *btcutil.Tx) bool

MatchTxAndUpdate returns true if the bloom filter matches data within the passed transaction, otherwise false is returned. If the filter does match the passed transaction, it will also update the filter depending on the bloom update flags set via the loaded filter if needed.

This function is safe for concurrent access.

func (*Filter) Matches

func (bf *Filter) Matches(data []byte) bool

Matches returns true if the bloom filter might contain the passed data and false if it definitely does not.

This function is safe for concurrent access.

func (*Filter) MatchesOutPoint

func (bf *Filter) MatchesOutPoint(outpoint *btcwire.OutPoint) bool

MatchesOutPoint returns true if the bloom filter might contain the passed outpoint and false if it definitely does not.

This function is safe for concurrent access.

func (*Filter) MsgFilterLoad

func (bf *Filter) MsgFilterLoad() *btcwire.MsgFilterLoad

MsgFilterLoad returns the underlying btcwire.MsgFilterLoad for the bloom filter.

This function is safe for concurrent access.

func (*Filter) Reload

func (bf *Filter) Reload(filter *btcwire.MsgFilterLoad)

Reload loads a new filter replacing any existing filter.

This function is safe for concurrent access.

func (*Filter) Unload

func (bf *Filter) Unload()

Unload unloads the bloom filter.

This function is safe for concurrent access.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL