@@ -18,12 +18,15 @@ package core
18
18
19
19
import (
20
20
"encoding/json"
21
+ "io/ioutil"
21
22
"math/big"
22
23
"os"
23
24
"testing"
24
25
25
26
"github.com/ethereum/go-ethereum/common"
26
27
"github.com/ethereum/go-ethereum/core/types"
28
+ "github.com/ethereum/go-ethereum/core/vm"
29
+ "github.com/ethereum/go-ethereum/crypto"
27
30
"github.com/ethereum/go-ethereum/crypto/sha3"
28
31
"github.com/ethereum/go-ethereum/ethdb"
29
32
"github.com/ethereum/go-ethereum/rlp"
@@ -318,3 +321,112 @@ func TestHeadStorage(t *testing.T) {
318
321
t .Fatalf ("Head block hash mismatch: have %v, want %v" , entry , blockFull .Hash ())
319
322
}
320
323
}
324
+
325
+ func TestMipmapBloom (t * testing.T ) {
326
+ db , _ := ethdb .NewMemDatabase ()
327
+
328
+ receipt1 := new (types.Receipt )
329
+ receipt1 .SetLogs (vm.Logs {
330
+ & vm.Log {Address : common .BytesToAddress ([]byte ("test" ))},
331
+ & vm.Log {Address : common .BytesToAddress ([]byte ("address" ))},
332
+ })
333
+ receipt2 := new (types.Receipt )
334
+ receipt2 .SetLogs (vm.Logs {
335
+ & vm.Log {Address : common .BytesToAddress ([]byte ("test" ))},
336
+ & vm.Log {Address : common .BytesToAddress ([]byte ("address1" ))},
337
+ })
338
+
339
+ WriteMipmapBloom (db , 1 , types.Receipts {receipt1 })
340
+ WriteMipmapBloom (db , 2 , types.Receipts {receipt2 })
341
+
342
+ for _ , level := range MIPMapLevels {
343
+ bloom := GetMipmapBloom (db , 2 , level )
344
+ if ! bloom .Test (new (big.Int ).SetBytes ([]byte ("address1" ))) {
345
+ t .Error ("expected test to be included on level:" , level )
346
+ }
347
+ }
348
+
349
+ // reset
350
+ db , _ = ethdb .NewMemDatabase ()
351
+ receipt := new (types.Receipt )
352
+ receipt .SetLogs (vm.Logs {
353
+ & vm.Log {Address : common .BytesToAddress ([]byte ("test" ))},
354
+ })
355
+ WriteMipmapBloom (db , 999 , types.Receipts {receipt1 })
356
+
357
+ receipt = new (types.Receipt )
358
+ receipt .SetLogs (vm.Logs {
359
+ & vm.Log {Address : common .BytesToAddress ([]byte ("test 1" ))},
360
+ })
361
+ WriteMipmapBloom (db , 1000 , types.Receipts {receipt })
362
+
363
+ bloom := GetMipmapBloom (db , 1000 , 1000 )
364
+ if bloom .TestBytes ([]byte ("test" )) {
365
+ t .Error ("test should not have been included" )
366
+ }
367
+ }
368
+
369
+ func TestMipmapChain (t * testing.T ) {
370
+ dir , err := ioutil .TempDir ("" , "mipmap" )
371
+ if err != nil {
372
+ t .Fatal (err )
373
+ }
374
+ defer os .RemoveAll (dir )
375
+
376
+ var (
377
+ db , _ = ethdb .NewLDBDatabase (dir , 16 )
378
+ key1 , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
379
+ addr = crypto .PubkeyToAddress (key1 .PublicKey )
380
+ addr2 = common .BytesToAddress ([]byte ("jeff" ))
381
+
382
+ hash1 = common .BytesToHash ([]byte ("topic1" ))
383
+ )
384
+ defer db .Close ()
385
+
386
+ genesis := WriteGenesisBlockForTesting (db , GenesisAccount {addr , big .NewInt (1000000 )})
387
+ chain := GenerateChain (genesis , db , 1010 , func (i int , gen * BlockGen ) {
388
+ var receipts types.Receipts
389
+ switch i {
390
+ case 1 :
391
+ receipt := types .NewReceipt (nil , new (big.Int ))
392
+ receipt .SetLogs (vm.Logs {
393
+ & vm.Log {
394
+ Address : addr ,
395
+ Topics : []common.Hash {hash1 },
396
+ },
397
+ })
398
+ gen .AddUncheckedReceipt (receipt )
399
+ receipts = types.Receipts {receipt }
400
+ case 1000 :
401
+ receipt := types .NewReceipt (nil , new (big.Int ))
402
+ receipt .SetLogs (vm.Logs {& vm.Log {Address : addr2 }})
403
+ gen .AddUncheckedReceipt (receipt )
404
+ receipts = types.Receipts {receipt }
405
+
406
+ }
407
+
408
+ // store the receipts
409
+ err := PutReceipts (db , receipts )
410
+ if err != nil {
411
+ t .Fatal (err )
412
+ }
413
+ WriteMipmapBloom (db , uint64 (i + 1 ), receipts )
414
+ })
415
+ for _ , block := range chain {
416
+ WriteBlock (db , block )
417
+ if err := WriteCanonicalHash (db , block .Hash (), block .NumberU64 ()); err != nil {
418
+ t .Fatalf ("failed to insert block number: %v" , err )
419
+ }
420
+ if err := WriteHeadBlockHash (db , block .Hash ()); err != nil {
421
+ t .Fatalf ("failed to insert block number: %v" , err )
422
+ }
423
+ if err := PutBlockReceipts (db , block , block .Receipts ()); err != nil {
424
+ t .Fatal ("error writing block receipts:" , err )
425
+ }
426
+ }
427
+
428
+ bloom := GetMipmapBloom (db , 0 , 1000 )
429
+ if bloom .TestBytes (addr2 [:]) {
430
+ t .Error ("address was included in bloom and should not have" )
431
+ }
432
+ }
0 commit comments