// TestFilterFPRange checks that new filters made with out of range // false positive targets result in either max or min false positive rates. func TestFilterFPRange(t *testing.T) { tests := []struct { name string hash string want string filter *bloom.Filter }{ { name: "fprates > 1 should be clipped at 1", hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041", want: "00000000000000000001", filter: bloom.NewFilter(1, 0, 20.9999999769, wire.BloomUpdateAll), }, { name: "fprates less than 1e-9 should be clipped at min", hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041", want: "0566d97a91a91b0000000000000001", filter: bloom.NewFilter(1, 0, 0, wire.BloomUpdateAll), }, { name: "negative fprates should be clipped at min", hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041", want: "0566d97a91a91b0000000000000001", filter: bloom.NewFilter(1, 0, -1, wire.BloomUpdateAll), }, } for _, test := range tests { // Convert test input to appropriate types. hash, err := chainhash.NewHashFromStr(test.hash) if err != nil { t.Errorf("NewHashFromStr unexpected error: %v", err) continue } want, err := hex.DecodeString(test.want) if err != nil { t.Errorf("DecodeString unexpected error: %v\n", err) continue } // Add the test hash to the bloom filter and ensure the // filter serializes to the expected bytes. f := test.filter f.AddHash(hash) got := bytes.NewBuffer(nil) err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("BtcDecode unexpected error: %v\n", err) continue } if !bytes.Equal(got.Bytes(), want) { t.Errorf("serialized filter mismatch: got %x want %x\n", got.Bytes(), want) continue } } }
// This example demonstrates how to create a new bloom filter, add a transaction // hash to it, and check if the filter matches the transaction. 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. filter := bloom.NewFilter(10, tweak, 0.0001, wire.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 := chainhash.NewHashFromStr(txHashStr) if err != nil { fmt.Println(err) return } filter.AddHash(txHash) // Show that the filter matches. matches := filter.Matches(txHash[:]) fmt.Println("Filter Matches?:", matches) // Output: // Filter Matches?: true }
// TestFilterInsertKey ensures inserting public keys and addresses works as // expected. func TestFilterInsertKey(t *testing.T) { secret := "PtWU93QdrNBasyWA7GDJ3ycEN5aQRF69EynXJfmnyWDS4G7pzpEvN" wif, err := dcrutil.DecodeWIF(secret) if err != nil { t.Errorf("TestFilterInsertKey DecodeWIF failed: %v", err) return } f := bloom.NewFilter(2, 0, 0.001, wire.BloomUpdateAll) f.Add(wif.SerializePubKey()) f.Add(dcrutil.Hash160(wif.SerializePubKey())) want, err := hex.DecodeString("03323f6e080000000000000001") if err != nil { t.Errorf("TestFilterInsertWithTweak DecodeString failed: %v\n", err) return } got := bytes.NewBuffer(nil) err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestFilterInsertWithTweak BtcDecode failed: %v\n", err) return } if !bytes.Equal(got.Bytes(), want) { t.Errorf("TestFilterInsertWithTweak failure: got %v want %v\n", got.Bytes(), want) return } }
// TestFilterLarge ensures a maximum sized filter can be created. func TestFilterLarge(t *testing.T) { f := bloom.NewFilter(100000000, 0, 0.01, wire.BloomUpdateNone) if len(f.MsgFilterLoad().Filter) > wire.MaxFilterLoadFilterSize { t.Errorf("TestFilterLarge test failed: %d > %d", len(f.MsgFilterLoad().Filter), wire.MaxFilterLoadFilterSize) } }
func TestFilterReload(t *testing.T) { f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) bFilter := bloom.LoadFilter(f.MsgFilterLoad()) if bFilter.MsgFilterLoad() == nil { t.Errorf("TestFilterReload LoadFilter test failed") return } bFilter.Reload(nil) if bFilter.MsgFilterLoad() != nil { t.Errorf("TestFilterReload Reload test failed") } }
// TestFilterInsert ensures inserting data into the filter causes that data // to be matched and the resulting serialized MsgFilterLoad is the expected // value. func TestFilterInsert(t *testing.T) { var tests = []struct { hex string insert bool }{ {"99108ad8ed9bb6274d3980bab5a85c048f0950c8", true}, {"19108ad8ed9bb6274d3980bab5a85c048f0950c8", false}, {"b5a2c786d9ef4658287ced5914b37a1b4aa32eee", true}, {"b9300670b4c5366e95b2699e8b18bc75e5f729c5", true}, } f := bloom.NewFilter(3, 0, 0.01, wire.BloomUpdateAll) for i, test := range tests { data, err := hex.DecodeString(test.hex) if err != nil { t.Errorf("TestFilterInsert DecodeString failed: %v\n", err) return } if test.insert { f.Add(data) } result := f.Matches(data) if test.insert != result { t.Errorf("TestFilterInsert Matches test #%d failure: got %v want %v\n", i, result, test.insert) return } } want, err := hex.DecodeString("03614e9b050000000000000001") if err != nil { t.Errorf("TestFilterInsert DecodeString failed: %v\n", err) return } got := bytes.NewBuffer(nil) err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestFilterInsert BtcDecode failed: %v\n", err) return } if !bytes.Equal(got.Bytes(), want) { t.Errorf("TestFilterInsert failure: got %v want %v\n", got.Bytes(), want) return } }
func TestFilterInsertUpdateNone(t *testing.T) { f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone) // Add the generation pubkey inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" + "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" + "2252247d97a46a91" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) // Add the output address for the 4th transaction inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" sha, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } }
func TestFilterInsertUpdateNone(t *testing.T) { f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone) // Add the generation pubkey inputStr := "0270c906c3ba64ba5eb3943cc012a3b142ef169f066002515bf9ec1bd9b7e27f0d" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) // Add the output address for the 4th transaction inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err) return } f.Add(inputBytes) inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" hash, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(hash, 0, wire.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(hash, 0, wire.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) return } }
func TestMerkleBlock3(t *testing.T) { blockStr := "0100000073cf056852529ffadc50b49589218795adc4d3f24170950d49f201000000000033fd46dda0acfa5c0651c58bee00362b04186c5b4d1045d37751b25779148649000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000ffff011b00c2eb0b00000000000100007e0100006614b956bee4fc44442bf144050552b3010000000000000000000000000000000000000000000000000000000101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff00ffffffff03fa1a981200000000000017a914f5916158e3e2c4551c1796708db8367207ed13bb8700000000000000000000266a24000100000000000000000000000000000000000000000000000000004dfb774daa8f3a76dea1906f0000000000001976a914b74b7476bdbcf03d18f12cca1766b0ddfd030cdd88ac000000000000000001d8bc28820000000000000000ffffffff0800002f646372642f00" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } blk, err := dcrutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestMerkleBlock3 NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "4986147957b25177d345104d5b6c18042b3600ee8bc551065cfaaca0dd46fd33" hash, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlock3 NewHashFromStr failed: %v", err) return } f.AddHash(hash) mBlock, _ := bloom.NewMerkleBlock(blk, f) wantStr := "0100000073cf056852529ffadc50b49589218795adc4d3f24170950d49f201000000000033fd46dda0acfa5c0651c58bee00362b04186c5b4d1045d37751b25779148649000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000ffff011b00c2eb0b00000000000100007e0100006614b956bee4fc44442bf144050552b3010000000000000000000000000000000000000000000000000000000100000001d0f51e5a4978d736eb3d4a8d615bee74943756b4745d29b27ab2943bc1307cc800000000000100" want, err := hex.DecodeString(wantStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } got := bytes.NewBuffer(nil) err = mBlock.BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestMerkleBlock3 BtcEncode failed: %v", err) return } if !bytes.Equal(want, got.Bytes()) { t.Errorf("TestMerkleBlock3 failed merkle block comparison: "+ "got %v want %v", got.Bytes(), want) return } }
func TestFilterInsertP2PubKeyOnly(t *testing.T) { blockStr := "000000003ffad804971ce6b8a13c8162287222d91395fa77b6bea6b4" + "626b4827000000004259bd8a4d5d5f8469f390903d27b9cab2ea03822fbe" + "616478756ada751a283be8e4aa58eeb75810e22031cc2756442b7f68c77b" + "3735522fc5490be1676253c301005f031703bb61050000005c1300006fb2" + "381c30dd35680000000091940000fc0900000cadf15600d9781d3a5f237b" + "083799410f00000000000000000000000000000000000000000000000000" + "000002010000000100000000000000000000000000000000000000000000" + "00000000000000000000ffffffff00ffffffff032727750c000000000000" + "17a9144fa6cbd0dbe5ec407fe4c8ad374e667771fa0d4487000000000000" + "00000000266a249194000000000000000000000000000000000000000000" + "0000000000b90c53023ee736e6d7eebe4a0000000000001976a914bfe0f6" + "3eda5d7db3ee05661051c026f5296ffc7888ac0000000000000000011512" + "34570000000000000000ffffffff0800002f646372642f01000000020c57" + "441e66eaa72bc76ab54faaa0ec87941f4423a463c5e34d7f23f247d65e9d" + "0200000001ffffffff31b7ffaec935e787dc03670d4d13ee0066717cb750" + "2cb4d925b09b7f692a73580400000001ffffffff0200ca9a3b0000000000" + "0023210351b64c01163b9184637671e27d6d29b3a205203710f6fbc5a6e1" + "b646a11984d6ac81001f060000000000001976a9144feba3e04d91d9dc90" + "ebb13ce91bf9bcfe32ff9288ac000000000000000002c29f493300000000" + "8f9400000a0000006a4730440220186741de60d6fe75d2206b62780edabe" + "8a0a13f823aaeca58f0d5a8dedfa99f202201dc84a1bd26d14723ddf2fe4" + "216d4a24d788597fa22255d144ce119d7544ce39012103879a3b3666bf03" + "88ab33e6e24e77c3c219044f7c5d778b71ac1837817095830da72e700e00" + "000000909400000e0000006b4830450221008cc441f58be0eedc713b867d" + "c8dfff21e2f6c2e38a906a0af5aa0625b2683f6b0220308bcaf6628b3c68" + "19da76ebb2ca5e5d1ddde6e5035193d75e3e667731deadc4012102a871fe" + "4e4f77121f1cbaf2db62190539687092b465e252a3bd732791ca442d5405" + "010000000200000000000000000000000000000000000000000000000000" + "00000000000000ffffffff00ffffffff91ed802c4a23cca0517dbc2d89f7" + "b6f8a2d1491a27ed25e3d3f621bac03316da0000000001ffffffff030000" + "0000000000000000266a243ffad804971ce6b8a13c8162287222d91395fa" + "77b6bea6b4626b4827000000009094000000000000000000000000046a02" + "010003d0db190100000000001abb76a914339f1f6a41d7ef65ffd80bbb8c" + "daaa215a16472d88ac000000000000000002e47d79070000000000000000" + "ffffffff04deadbeef1f52621201000000938b0000090000006a47304402" + "207184b4d7a95a559b1142f50cfde3e40bf460572aea4d5eeabad062e45c" + "9a8f5102202304acbcc9cc55e58770e51d4fc9ac81af9c790a65d251ea4d" + "556bc7ee96fb47012103c46bdbf6b24be97eac230ca9b23e928e2e76750d" + "3d3c8b29e15cebb64ba01f86010000000200000000000000000000000000" + "00000000000000000000000000000000000000ffffffff00ffffffff17be" + "9d003549030c20c018a71745af6f6a02d96637f49f5a548ee7fe0ac84420" + "0000000001ffffffff0300000000000000000000266a243ffad804971ce6" + "b8a13c8162287222d91395fa77b6bea6b4626b4827000000009094000000" + "000000000000000000046a02010071c339cf0000000000001abb76a9147f" + "1aaac9f04febbf9dadd262b1c710729970445988ac000000000000000002" + "e47d79070000000000000000ffffffff04deadbeef8d45c0c70000000099" + "8f0000070000006a473044022054a09af013f1a74960686bf5c36f3eb822" + "5ffc96e76a54dc1aac9fdea65cff2b022010359dfcd16ac77f3a6ab47309" + "e106a885fa2d031d5bac0824f4432a77ccdae001210389e445603d66ce44" + "92ec74d9b1974268a2e83413c84df87895184bc6f46ae1d7010000000200" + "000000000000000000000000000000000000000000000000000000000000" + "00ffffffff00ffffffff49a883465f82c0483b3ff7da057cab44a42ecbc8" + "27c344a960a2a33c95263f760000000001ffffffff030000000000000000" + "0000266a243ffad804971ce6b8a13c8162287222d91395fa77b6bea6b462" + "6b4827000000009094000000000000000000000000046a0201002c9d63bf" + "0000000000001abb76a91449d402e31fc08414f565febaa1b69eccc2fb8d" + "2688ac000000000000000002e47d79070000000000000000ffffffff04de" + "adbeef481feab70000000064870000120000006a473044022008daa49081" + "d7dc6c466a4d3fd4184927e8b77fd1f5721f206bf0d13a2c00b07802202d" + "89841ad5346de14db95c3e5b15a0d82e0df1f50a235f67aeec08b98f4b36" + "7a012102907261a4670a9d0ff918724af50f2ecb7947831dad30ac285afa" + "1b90549aa282010000000200000000000000000000000000000000000000" + "00000000000000000000000000ffffffff00ffffffffb6df4ea74c958f39" + "65ecc90b9226cfdc2d7986b1df30998cf3e827d7e9fbf4b80000000001ff" + "ffffff0300000000000000000000266a243ffad804971ce6b8a13c816228" + "7222d91395fa77b6bea6b4626b4827000000009094000000000000000000" + "000000046a020100f3a613b90000000000001abb76a9147f9321c65ee805" + "d87f67b0b0e9dac4f0779c1d3a88ac000000000000000002e47d79070000" + "000000000000ffffffff04deadbeef0f299ab100000000408d0000130000" + "006b4830450221008c5759dd9f0c40c61f61f5b67c0b3f860d4d59859fb2" + "451dbeab9956b3ceb05602203e81d5b88b83e279dc7feca13c8e5eb58676" + "158243058ec43d6366933397be4a0121031e6757ee86e94a02e567db7d4e" + "c4ba5088f6b5fdbfe9b2794b70b42b52b879200100000002000000000000" + "0000000000000000000000000000000000000000000000000000ffffffff" + "00ffffffff9e74ead771c313f0c0bb739860eba39d04a8cce201cda626dc" + "6a7dd4e83b3f770000000001ffffffff0400000000000000000000266a24" + "3ffad804971ce6b8a13c8162287222d91395fa77b6bea6b4626b48270000" + "00009094000000000000000000000000046a0201000793a0e00000000000" + "001abb76a914c8c5a2cbd183871718dcb14b5f198561450d157e88ac596e" + "8f250000000000001abb76a9147112f7d015baeb129e732b9c4805e492ef" + "d0a2fb88ac000000000000000002e47d79070000000000000000ffffffff" + "04deadbeef7d83b6fe0000000022920000070000006a4730440220160ff7" + "bd84190d8d837ac6bd782be475d23d50832872626453bd70ea63d9a6a002" + "20603afdd51fc8e3fead1fd9dbae410b8d0617969ad5119e6dc2b0855d7b" + "48e73501210297ab850cae270e9438693353e40444c7e714e179505e8b12" + "1c042f4869e42072" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } block, err := dcrutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateP2PubkeyOnly) // Generation pubkey inputStr := "0351b64c01163b9184637671e27d6d29b3a205203710f6fbc5a6e1b646a11984d6" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Output address of 2nd transaction inputStr = "4feba3e04d91d9dc90ebb13ce91bf9bcfe32ff92" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Ignore return value -- this is just used to update the filter. _, _ = bloom.NewMerkleBlock(block, f) // We should match the generation pubkey inputStr = "8199f30ccc006056ed79cf0a3cd0b67a195ffd46903d42adc7babe2ed2f2e371" hash, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(hash, 0, wire.TxTreeRegular) if !f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+ "outpoint %s", inputStr) return } // We should not match the 4th transaction, which is not p2pk inputStr = "d7314aaf54253c651e8258c7d22c574af8804611f0dcea79fd9a47f4565d85ad" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(hash, 0, wire.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr) return } }
func TestFilterBloomMatch(t *testing.T) { // tx 2 from blk 10000 str := "0100000001a4fbbbca2416ba4c10c94be9f4a650d37fc4f9a1a4ecded9cc2" + "714aa0a529a750000000000ffffffff02c2d0b32f0000000000001976a91" + "499678d10a90c8df40e4c9af742aa6ebc7764a60e88acbe01611c0000000" + "000001976a9147701528df10cf0c14f9e53925031bd398796c1f988ac000" + "000000000000001e0b52b4c0000000003270000020000006b48304502210" + "08003ce072e4b67f9a98129ac2f58e3de6e06f47a15e248d4375d19dfb52" + "7a02d02204ab0a0dfe7c69024ae8e524e01d1c45183efda945a0d411e4e9" + "4b69be21efbe601210270c906c3ba64ba5eb3943cc012a3b142ef169f066" + "002515bf9ec1bd9b7e27f0d" strBytes, err := hex.DecodeString(str) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failure: %v", err) return } tx, err := dcrutil.NewTxFromBytes(strBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } spendingStr := "01000000018c5e1f62f83d750a0ee228c228731eae241e6b483e5b63be199" + "12846eb2d11500000000000ffffffff02ed44871d0000000000001976a91" + "461788151a27fad1a9c609fa29a2bd43886e2dd4088ac75a815120000000" + "000001976a91483419547ee3db5c0ee29f347740ff7f448e8ab2c88ac000" + "000000000000001c2d0b32f0000000010270000010000006b48304502210" + "0aca38b780893b6be3287efa908ace8bb8b91af0477ab433f101889b86bb" + "d9c2d0220789a177956f91c75141ea527573294a20f6fc0ea8bd5cc33550" + "4a0654ae197e30121025516815b900e10e51824ea1f451fd197fb11209af" + "60c5c52f9a8cf3edad5dc09" spendingTxBytes, err := hex.DecodeString(spendingStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failure: %v", err) return } spendingTx, err := dcrutil.NewTxFromBytes(spendingTxBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "50112deb46289119be635b3e486b1e24ae1e7328c228e20e0a753df8621f5e8c" hash, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err) return } f.AddHash(hash) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "8c5e1f62f83d750a0ee228c228731eae241e6b483e5b63be19912846eb2d1150" hashBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "30450221008003ce072e4b67f9a98129ac2f58e3de6e06f47a15e248d43" + "75d19dfb527a02d02204ab0a0dfe7c69024ae8e524e01d1c45183efda945a0" + "d411e4e94b69be21efbe601" hashBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "0270c906c3ba64ba5eb3943cc012a3b142ef169f066002515bf9ec1bd9b7e27f0d" hashBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "99678d10a90c8df40e4c9af742aa6ebc7764a60e" hashBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } if !f.MatchTxAndUpdate(spendingTx) { t.Errorf("TestFilterBloomMatch spendingTx didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "7701528df10cf0c14f9e53925031bd398796c1f9" hashBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "759a520aaa1427ccd9deeca4a1f9c47fd350a6f4e94bc9104cba1624cabbfba4" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err) return } outpoint := wire.NewOutPoint(hash, 0, wire.TxTreeRegular) f.AddOutPoint(outpoint) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr) } // XXX unchanged from btcd f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err) return } f.AddHash(hash) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched hash %s", inputStr) } // XXX unchanged from btcd f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431" hashBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(hashBytes) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "759a520aaa1427ccd9deeca4a1f9c47fd350a6f4e94bc9104cba1624cabbfba4" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(hash, 1, wire.TxTreeRegular) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } // XXX unchanged from btcd f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" hash, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(hash, 0, wire.TxTreeRegular) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } }
func TestFilterInsertP2PubKeyOnly(t *testing.T) { blockStr := "000000004ad131bae9cb9f74b8bcd928" + "a60dfe4dadabeb31b1e79403385f9ac4" + "ccc28b7400429e56f7df2872aaaa0c16" + "221cb09059bd3ea897de156ff51202ff" + "72b2cd8d000000000000000000000000" + "00000000000000000000000000000000" + "00000000010000000000000000000000" + "22000000ffff7f20002d310100000000" + "640000007601000063a0815601000000" + "00000000000000000000000000000000" + "00000000000000000000000000000000" + "00000000010100000001000000000000" + "00000000000000000000000000000000" + "00000000000000000000ffffffff00ff" + "ffffff0380b2e60e00000000000017a9" + "144fa6cbd0dbe5ec407fe4c8ad374e66" + "7771fa0d448700000000000000000000" + "226a2000000000000000000000000000" + "0000009e0453a6ab10610e17a7a5fadc" + "f6c34f002f68590000000000001976a9" + "141b79e6496226f89ad4e049667c1344" + "c16a75815188ac000000000000000001" + "000000000000000000000000ffffffff" + "04deadbeef00" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } block, err := dcrutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateP2PubkeyOnly) // Generation pubkey inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" + "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" + "2252247d97a46a91" inputBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Output address of 4th transaction inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21" inputBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err) return } f.Add(inputBytes) // Ignore return value -- this is just used to update the filter. _, _ = bloom.NewMerkleBlock(block, f) // We should match the generation pubkey inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" sha, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) return } outpoint := wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) if !f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+ "outpoint %s", inputStr) return } // We should not match the 4th transaction, which is not p2pk inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) return } outpoint = wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) if f.MatchesOutPoint(outpoint) { t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr) return } }
func TestFilterBloomMatch(t *testing.T) { str := "0100000001cd24a3a215761757d2b565994b5f544be8cc7996e7835a0b326" + "feed9f8a3072802000000016a47304402200d1f455ff952f3fae2f9b3d5c" + "471371ba9d09789abb5cd80f01c6576c6d760e70220283aaa6e18a043a3b" + "5d105d6fccaca8edf31b705b75d82488414366ca48bdda60121030e233e9" + "6e98d98336bd2b3f4ec23aaa3c9b5c9b57de6d408490a9ce36c9d4326fff" + "fffff0600ca9a3b000000001976a914e6719fd3749c5ed40f193928e8b2a" + "d0dd163efe588acf82f357a0a0000001976a91468447782004ae8718618f" + "d752f1d02e285f897d488ac00ca9a3b000000001976a9148d2b19f048851" + "857afd872acde7ccf93bf16f5d488ac00ca9a3b000000001976a914e5aaf" + "268a042555daabf1e33030f12f61d663ea088ac00ca9a3b000000001976a" + "914f02e3cb22c39d3e6e0179241e52f21df097b17ae88ac00ca9a3b00000" + "0001976a9142ea99ce64965bf8cfb40e8e1b7bf4d2cea89db1988ac00000" + "000" strBytes, err := hex.DecodeString(str) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failure: %v", err) return } tx, err := dcrutil.NewTxFromBytes(strBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } spendingTxBytes := []byte{0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00} spendingTx, err := dcrutil.NewTxFromBytes(spendingTxBytes) if err != nil { t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b" sha, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } f.AddShaHash(sha) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4" shaBytes, err := hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065" + "f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643" + "ac4cb7cb3c462aced7f14711a01" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95" + "c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe" + "76036c339" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "04943fdd508053c75000106d3bc6e2754dbcff19" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } if !f.MatchTxAndUpdate(spendingTx) { t.Errorf("TestFilterBloomMatch spendingTx didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "a266436d2965547608b9e15d9032a7b9d64fa431" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint := wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) f.AddOutPoint(outpoint) if !f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } f.AddShaHash(sha) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched sha %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431" shaBytes, err = hex.DecodeString(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) return } f.Add(shaBytes) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched address %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(sha, 1, dcrutil.TxTreeRegular) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" sha, err = chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) return } outpoint = wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular) f.AddOutPoint(outpoint) if f.MatchTxAndUpdate(tx) { t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) } }
func TestMerkleBlock3(t *testing.T) { blockStr := "000000004ad131bae9cb9f74b8bcd928" + "a60dfe4dadabeb31b1e79403385f9ac4" + "ccc28b7400429e56f7df2872aaaa0c16" + "221cb09059bd3ea897de156ff51202ff" + "72b2cd8d000000000000000000000000" + "00000000000000000000000000000000" + "00000000010000000000000000000000" + "22000000ffff7f20002d310100000000" + "640000007601000063a0815601000000" + "00000000000000000000000000000000" + "00000000000000000000000000000000" + "00000000010100000001000000000000" + "00000000000000000000000000000000" + "00000000000000000000ffffffff00ff" + "ffffff0380b2e60e00000000000017a9" + "144fa6cbd0dbe5ec407fe4c8ad374e66" + "7771fa0d448700000000000000000000" + "226a2000000000000000000000000000" + "0000009e0453a6ab10610e17a7a5fadc" + "f6c34f002f68590000000000001976a9" + "141b79e6496226f89ad4e049667c1344" + "c16a75815188ac000000000000000001" + "000000000000000000000000ffffffff" + "04deadbeef00" blockBytes, err := hex.DecodeString(blockStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } blk, err := dcrutil.NewBlockFromBytes(blockBytes) if err != nil { t.Errorf("TestMerkleBlock3 NewBlockFromBytes failed: %v", err) return } f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) inputStr := "4797be83be7b4c4f833739c3542c2c1c403ffb01f0b721b5bc5dee3ff655a856" sha, err := chainhash.NewHashFromStr(inputStr) if err != nil { t.Errorf("TestMerkleBlock3 NewShaHashFromStr failed: %v", err) return } f.AddShaHash(sha) mBlock, _ := bloom.NewMerkleBlock(blk, f) wantStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4" + "b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc" + "96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50c" + "c069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196" + "30101" want, err := hex.DecodeString(wantStr) if err != nil { t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err) return } got := bytes.NewBuffer(nil) err = mBlock.BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestMerkleBlock3 BtcEncode failed: %v", err) return } if !bytes.Equal(want, got.Bytes()) { t.Errorf("TestMerkleBlock3 failed merkle block comparison: "+ "got %v want %v", got.Bytes, want) return } }