example_test.go raw

   1  package bloom_test
   2  
   3  import (
   4  	"fmt"
   5  	"math/rand"
   6  	"time"
   7  	
   8  	"github.com/p9c/p9/pkg/bloom"
   9  	"github.com/p9c/p9/pkg/chainhash"
  10  	"github.com/p9c/p9/pkg/wire"
  11  )
  12  
  13  // This example demonstrates how to create a new bloom filter, add a transaction hash to it, and check if the filter
  14  // matches the transaction.
  15  func ExampleNewFilter() {
  16  	rand.Seed(time.Now().UnixNano())
  17  	tweak := rand.Uint32()
  18  	// Create a new bloom filter intended to hold 10 elements with a 0.01% false positive rate and does not include any
  19  	// automatic update functionality when transactions are matched.
  20  	filter := bloom.NewFilter(10, tweak, 0.0001, wire.BloomUpdateNone)
  21  	// Create a transaction hash and add it to the filter. This particular trasaction is the first transaction in block
  22  	// 310,000 of the main bitcoin block chain.
  23  	txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45"
  24  	txHash, e := chainhash.NewHashFromStr(txHashStr)
  25  	if e != nil {
  26  		fmt.Println(e)
  27  		return
  28  	}
  29  	filter.AddHash(txHash)
  30  	// Show that the filter matches.
  31  	matches := filter.Matches(txHash[:])
  32  	fmt.Println("Filter Matches?:", matches)
  33  	// Output:
  34  	// Filter Matches?: true
  35  }
  36