2015-05-01 19:41:58 +02:00
|
|
|
// Copyright (c) 2014 The btcsuite developers
|
2014-07-10 03:25:28 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package bloom_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
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/bloom"
|
2014-07-10 03:25:28 +02:00
|
|
|
)
|
|
|
|
|
2014-07-10 03:41:22 +02:00
|
|
|
// This example demonstrates how to create a new bloom filter, add a transaction
|
|
|
|
// hash to it, and check if the filter matches the transaction.
|
2014-07-10 03:25:28 +02:00
|
|
|
func ExampleNewFilter() {
|
|
|
|
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.
|
2015-02-05 21:48:38 +01:00
|
|
|
filter := bloom.NewFilter(10, tweak, 0.0001, wire.BloomUpdateNone)
|
2014-07-10 03:25:28 +02:00
|
|
|
|
|
|
|
// 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"
|
2015-02-05 21:48:38 +01:00
|
|
|
txHash, err := wire.NewShaHashFromStr(txHashStr)
|
2014-07-10 03:25:28 +02:00
|
|
|
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
|
|
|
|
}
|