github.com/bamzi/go-ethereum@v1.6.7-0.20170704111104-138f26c93af1/core/database_util_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"math/big"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/crypto/sha3"
    30  	"github.com/ethereum/go-ethereum/ethdb"
    31  	"github.com/ethereum/go-ethereum/params"
    32  	"github.com/ethereum/go-ethereum/rlp"
    33  )
    34  
    35  // Tests block header storage and retrieval operations.
    36  func TestHeaderStorage(t *testing.T) {
    37  	db, _ := ethdb.NewMemDatabase()
    38  
    39  	// Create a test header to move around the database and make sure it's really new
    40  	header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
    41  	if entry := GetHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    42  		t.Fatalf("Non existent header returned: %v", entry)
    43  	}
    44  	// Write and verify the header in the database
    45  	if err := WriteHeader(db, header); err != nil {
    46  		t.Fatalf("Failed to write header into database: %v", err)
    47  	}
    48  	if entry := GetHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
    49  		t.Fatalf("Stored header not found")
    50  	} else if entry.Hash() != header.Hash() {
    51  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
    52  	}
    53  	if entry := GetHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
    54  		t.Fatalf("Stored header RLP not found")
    55  	} else {
    56  		hasher := sha3.NewKeccak256()
    57  		hasher.Write(entry)
    58  
    59  		if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
    60  			t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
    61  		}
    62  	}
    63  	// Delete the header and verify the execution
    64  	DeleteHeader(db, header.Hash(), header.Number.Uint64())
    65  	if entry := GetHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    66  		t.Fatalf("Deleted header returned: %v", entry)
    67  	}
    68  }
    69  
    70  // Tests block body storage and retrieval operations.
    71  func TestBodyStorage(t *testing.T) {
    72  	db, _ := ethdb.NewMemDatabase()
    73  
    74  	// Create a test body to move around the database and make sure it's really new
    75  	body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
    76  
    77  	hasher := sha3.NewKeccak256()
    78  	rlp.Encode(hasher, body)
    79  	hash := common.BytesToHash(hasher.Sum(nil))
    80  
    81  	if entry := GetBody(db, hash, 0); entry != nil {
    82  		t.Fatalf("Non existent body returned: %v", entry)
    83  	}
    84  	// Write and verify the body in the database
    85  	if err := WriteBody(db, hash, 0, body); err != nil {
    86  		t.Fatalf("Failed to write body into database: %v", err)
    87  	}
    88  	if entry := GetBody(db, hash, 0); entry == nil {
    89  		t.Fatalf("Stored body not found")
    90  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
    91  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
    92  	}
    93  	if entry := GetBodyRLP(db, hash, 0); entry == nil {
    94  		t.Fatalf("Stored body RLP not found")
    95  	} else {
    96  		hasher := sha3.NewKeccak256()
    97  		hasher.Write(entry)
    98  
    99  		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
   100  			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
   101  		}
   102  	}
   103  	// Delete the body and verify the execution
   104  	DeleteBody(db, hash, 0)
   105  	if entry := GetBody(db, hash, 0); entry != nil {
   106  		t.Fatalf("Deleted body returned: %v", entry)
   107  	}
   108  }
   109  
   110  // Tests block storage and retrieval operations.
   111  func TestBlockStorage(t *testing.T) {
   112  	db, _ := ethdb.NewMemDatabase()
   113  
   114  	// Create a test block to move around the database and make sure it's really new
   115  	block := types.NewBlockWithHeader(&types.Header{
   116  		Extra:       []byte("test block"),
   117  		UncleHash:   types.EmptyUncleHash,
   118  		TxHash:      types.EmptyRootHash,
   119  		ReceiptHash: types.EmptyRootHash,
   120  	})
   121  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   122  		t.Fatalf("Non existent block returned: %v", entry)
   123  	}
   124  	if entry := GetHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   125  		t.Fatalf("Non existent header returned: %v", entry)
   126  	}
   127  	if entry := GetBody(db, block.Hash(), block.NumberU64()); entry != nil {
   128  		t.Fatalf("Non existent body returned: %v", entry)
   129  	}
   130  	// Write and verify the block in the database
   131  	if err := WriteBlock(db, block); err != nil {
   132  		t.Fatalf("Failed to write block into database: %v", err)
   133  	}
   134  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   135  		t.Fatalf("Stored block not found")
   136  	} else if entry.Hash() != block.Hash() {
   137  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   138  	}
   139  	if entry := GetHeader(db, block.Hash(), block.NumberU64()); entry == nil {
   140  		t.Fatalf("Stored header not found")
   141  	} else if entry.Hash() != block.Header().Hash() {
   142  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
   143  	}
   144  	if entry := GetBody(db, block.Hash(), block.NumberU64()); entry == nil {
   145  		t.Fatalf("Stored body not found")
   146  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
   147  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
   148  	}
   149  	// Delete the block and verify the execution
   150  	DeleteBlock(db, block.Hash(), block.NumberU64())
   151  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   152  		t.Fatalf("Deleted block returned: %v", entry)
   153  	}
   154  	if entry := GetHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   155  		t.Fatalf("Deleted header returned: %v", entry)
   156  	}
   157  	if entry := GetBody(db, block.Hash(), block.NumberU64()); entry != nil {
   158  		t.Fatalf("Deleted body returned: %v", entry)
   159  	}
   160  }
   161  
   162  // Tests that partial block contents don't get reassembled into full blocks.
   163  func TestPartialBlockStorage(t *testing.T) {
   164  	db, _ := ethdb.NewMemDatabase()
   165  	block := types.NewBlockWithHeader(&types.Header{
   166  		Extra:       []byte("test block"),
   167  		UncleHash:   types.EmptyUncleHash,
   168  		TxHash:      types.EmptyRootHash,
   169  		ReceiptHash: types.EmptyRootHash,
   170  	})
   171  	// Store a header and check that it's not recognized as a block
   172  	if err := WriteHeader(db, block.Header()); err != nil {
   173  		t.Fatalf("Failed to write header into database: %v", err)
   174  	}
   175  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   176  		t.Fatalf("Non existent block returned: %v", entry)
   177  	}
   178  	DeleteHeader(db, block.Hash(), block.NumberU64())
   179  
   180  	// Store a body and check that it's not recognized as a block
   181  	if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
   182  		t.Fatalf("Failed to write body into database: %v", err)
   183  	}
   184  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   185  		t.Fatalf("Non existent block returned: %v", entry)
   186  	}
   187  	DeleteBody(db, block.Hash(), block.NumberU64())
   188  
   189  	// Store a header and a body separately and check reassembly
   190  	if err := WriteHeader(db, block.Header()); err != nil {
   191  		t.Fatalf("Failed to write header into database: %v", err)
   192  	}
   193  	if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
   194  		t.Fatalf("Failed to write body into database: %v", err)
   195  	}
   196  	if entry := GetBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   197  		t.Fatalf("Stored block not found")
   198  	} else if entry.Hash() != block.Hash() {
   199  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   200  	}
   201  }
   202  
   203  // Tests block total difficulty storage and retrieval operations.
   204  func TestTdStorage(t *testing.T) {
   205  	db, _ := ethdb.NewMemDatabase()
   206  
   207  	// Create a test TD to move around the database and make sure it's really new
   208  	hash, td := common.Hash{}, big.NewInt(314)
   209  	if entry := GetTd(db, hash, 0); entry != nil {
   210  		t.Fatalf("Non existent TD returned: %v", entry)
   211  	}
   212  	// Write and verify the TD in the database
   213  	if err := WriteTd(db, hash, 0, td); err != nil {
   214  		t.Fatalf("Failed to write TD into database: %v", err)
   215  	}
   216  	if entry := GetTd(db, hash, 0); entry == nil {
   217  		t.Fatalf("Stored TD not found")
   218  	} else if entry.Cmp(td) != 0 {
   219  		t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
   220  	}
   221  	// Delete the TD and verify the execution
   222  	DeleteTd(db, hash, 0)
   223  	if entry := GetTd(db, hash, 0); entry != nil {
   224  		t.Fatalf("Deleted TD returned: %v", entry)
   225  	}
   226  }
   227  
   228  // Tests that canonical numbers can be mapped to hashes and retrieved.
   229  func TestCanonicalMappingStorage(t *testing.T) {
   230  	db, _ := ethdb.NewMemDatabase()
   231  
   232  	// Create a test canonical number and assinged hash to move around
   233  	hash, number := common.Hash{0: 0xff}, uint64(314)
   234  	if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
   235  		t.Fatalf("Non existent canonical mapping returned: %v", entry)
   236  	}
   237  	// Write and verify the TD in the database
   238  	if err := WriteCanonicalHash(db, hash, number); err != nil {
   239  		t.Fatalf("Failed to write canonical mapping into database: %v", err)
   240  	}
   241  	if entry := GetCanonicalHash(db, number); entry == (common.Hash{}) {
   242  		t.Fatalf("Stored canonical mapping not found")
   243  	} else if entry != hash {
   244  		t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
   245  	}
   246  	// Delete the TD and verify the execution
   247  	DeleteCanonicalHash(db, number)
   248  	if entry := GetCanonicalHash(db, number); entry != (common.Hash{}) {
   249  		t.Fatalf("Deleted canonical mapping returned: %v", entry)
   250  	}
   251  }
   252  
   253  // Tests that head headers and head blocks can be assigned, individually.
   254  func TestHeadStorage(t *testing.T) {
   255  	db, _ := ethdb.NewMemDatabase()
   256  
   257  	blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
   258  	blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
   259  	blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
   260  
   261  	// Check that no head entries are in a pristine database
   262  	if entry := GetHeadHeaderHash(db); entry != (common.Hash{}) {
   263  		t.Fatalf("Non head header entry returned: %v", entry)
   264  	}
   265  	if entry := GetHeadBlockHash(db); entry != (common.Hash{}) {
   266  		t.Fatalf("Non head block entry returned: %v", entry)
   267  	}
   268  	if entry := GetHeadFastBlockHash(db); entry != (common.Hash{}) {
   269  		t.Fatalf("Non fast head block entry returned: %v", entry)
   270  	}
   271  	// Assign separate entries for the head header and block
   272  	if err := WriteHeadHeaderHash(db, blockHead.Hash()); err != nil {
   273  		t.Fatalf("Failed to write head header hash: %v", err)
   274  	}
   275  	if err := WriteHeadBlockHash(db, blockFull.Hash()); err != nil {
   276  		t.Fatalf("Failed to write head block hash: %v", err)
   277  	}
   278  	if err := WriteHeadFastBlockHash(db, blockFast.Hash()); err != nil {
   279  		t.Fatalf("Failed to write fast head block hash: %v", err)
   280  	}
   281  	// Check that both heads are present, and different (i.e. two heads maintained)
   282  	if entry := GetHeadHeaderHash(db); entry != blockHead.Hash() {
   283  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   284  	}
   285  	if entry := GetHeadBlockHash(db); entry != blockFull.Hash() {
   286  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   287  	}
   288  	if entry := GetHeadFastBlockHash(db); entry != blockFast.Hash() {
   289  		t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
   290  	}
   291  }
   292  
   293  // Tests that transactions and associated metadata can be stored and retrieved.
   294  func TestTransactionStorage(t *testing.T) {
   295  	db, _ := ethdb.NewMemDatabase()
   296  
   297  	tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), big.NewInt(1111), big.NewInt(11111), []byte{0x11, 0x11, 0x11})
   298  	tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), big.NewInt(2222), big.NewInt(22222), []byte{0x22, 0x22, 0x22})
   299  	tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), big.NewInt(3333), big.NewInt(33333), []byte{0x33, 0x33, 0x33})
   300  	txs := []*types.Transaction{tx1, tx2, tx3}
   301  
   302  	block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil)
   303  
   304  	// Check that no transactions entries are in a pristine database
   305  	for i, tx := range txs {
   306  		if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
   307  			t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn)
   308  		}
   309  	}
   310  	// Insert all the transactions into the database, and verify contents
   311  	if err := WriteTransactions(db, block); err != nil {
   312  		t.Fatalf("failed to write transactions: %v", err)
   313  	}
   314  	for i, tx := range txs {
   315  		if txn, hash, number, index := GetTransaction(db, tx.Hash()); txn == nil {
   316  			t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
   317  		} else {
   318  			if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
   319  				t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i)
   320  			}
   321  			if tx.String() != txn.String() {
   322  				t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
   323  			}
   324  		}
   325  	}
   326  	// Delete the transactions and check purge
   327  	for i, tx := range txs {
   328  		DeleteTransaction(db, tx.Hash())
   329  		if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
   330  			t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
   331  		}
   332  	}
   333  }
   334  
   335  // Tests that receipts can be stored and retrieved.
   336  func TestReceiptStorage(t *testing.T) {
   337  	db, _ := ethdb.NewMemDatabase()
   338  
   339  	receipt1 := &types.Receipt{
   340  		PostState:         []byte{0x01},
   341  		CumulativeGasUsed: big.NewInt(1),
   342  		Logs: []*types.Log{
   343  			{Address: common.BytesToAddress([]byte{0x11})},
   344  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   345  		},
   346  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
   347  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   348  		GasUsed:         big.NewInt(111111),
   349  	}
   350  	receipt2 := &types.Receipt{
   351  		PostState:         []byte{0x02},
   352  		CumulativeGasUsed: big.NewInt(2),
   353  		Logs: []*types.Log{
   354  			{Address: common.BytesToAddress([]byte{0x22})},
   355  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   356  		},
   357  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
   358  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   359  		GasUsed:         big.NewInt(222222),
   360  	}
   361  	receipts := []*types.Receipt{receipt1, receipt2}
   362  
   363  	// Check that no receipt entries are in a pristine database
   364  	for i, receipt := range receipts {
   365  		if r := GetReceipt(db, receipt.TxHash); r != nil {
   366  			t.Fatalf("receipt #%d [%x]: non existent receipt returned: %v", i, receipt.TxHash, r)
   367  		}
   368  	}
   369  	// Insert all the receipts into the database, and verify contents
   370  	if err := WriteReceipts(db, receipts); err != nil {
   371  		t.Fatalf("failed to write receipts: %v", err)
   372  	}
   373  	for i, receipt := range receipts {
   374  		if r := GetReceipt(db, receipt.TxHash); r == nil {
   375  			t.Fatalf("receipt #%d [%x]: receipt not found", i, receipt.TxHash)
   376  		} else {
   377  			rlpHave, _ := rlp.EncodeToBytes(r)
   378  			rlpWant, _ := rlp.EncodeToBytes(receipt)
   379  
   380  			if !bytes.Equal(rlpHave, rlpWant) {
   381  				t.Fatalf("receipt #%d [%x]: receipt mismatch: have %v, want %v", i, receipt.TxHash, r, receipt)
   382  			}
   383  		}
   384  	}
   385  	// Delete the receipts and check purge
   386  	for i, receipt := range receipts {
   387  		DeleteReceipt(db, receipt.TxHash)
   388  		if r := GetReceipt(db, receipt.TxHash); r != nil {
   389  			t.Fatalf("receipt #%d [%x]: deleted receipt returned: %v", i, receipt.TxHash, r)
   390  		}
   391  	}
   392  }
   393  
   394  // Tests that receipts associated with a single block can be stored and retrieved.
   395  func TestBlockReceiptStorage(t *testing.T) {
   396  	db, _ := ethdb.NewMemDatabase()
   397  
   398  	receipt1 := &types.Receipt{
   399  		PostState:         []byte{0x01},
   400  		CumulativeGasUsed: big.NewInt(1),
   401  		Logs: []*types.Log{
   402  			{Address: common.BytesToAddress([]byte{0x11})},
   403  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   404  		},
   405  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
   406  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   407  		GasUsed:         big.NewInt(111111),
   408  	}
   409  	receipt2 := &types.Receipt{
   410  		PostState:         []byte{0x02},
   411  		CumulativeGasUsed: big.NewInt(2),
   412  		Logs: []*types.Log{
   413  			{Address: common.BytesToAddress([]byte{0x22})},
   414  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   415  		},
   416  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
   417  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   418  		GasUsed:         big.NewInt(222222),
   419  	}
   420  	receipts := []*types.Receipt{receipt1, receipt2}
   421  
   422  	// Check that no receipt entries are in a pristine database
   423  	hash := common.BytesToHash([]byte{0x03, 0x14})
   424  	if rs := GetBlockReceipts(db, hash, 0); len(rs) != 0 {
   425  		t.Fatalf("non existent receipts returned: %v", rs)
   426  	}
   427  	// Insert the receipt slice into the database and check presence
   428  	if err := WriteBlockReceipts(db, hash, 0, receipts); err != nil {
   429  		t.Fatalf("failed to write block receipts: %v", err)
   430  	}
   431  	if rs := GetBlockReceipts(db, hash, 0); len(rs) == 0 {
   432  		t.Fatalf("no receipts returned")
   433  	} else {
   434  		for i := 0; i < len(receipts); i++ {
   435  			rlpHave, _ := rlp.EncodeToBytes(rs[i])
   436  			rlpWant, _ := rlp.EncodeToBytes(receipts[i])
   437  
   438  			if !bytes.Equal(rlpHave, rlpWant) {
   439  				t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
   440  			}
   441  		}
   442  	}
   443  	// Delete the receipt slice and check purge
   444  	DeleteBlockReceipts(db, hash, 0)
   445  	if rs := GetBlockReceipts(db, hash, 0); len(rs) != 0 {
   446  		t.Fatalf("deleted receipts returned: %v", rs)
   447  	}
   448  }
   449  
   450  func TestMipmapBloom(t *testing.T) {
   451  	db, _ := ethdb.NewMemDatabase()
   452  
   453  	receipt1 := new(types.Receipt)
   454  	receipt1.Logs = []*types.Log{
   455  		{Address: common.BytesToAddress([]byte("test"))},
   456  		{Address: common.BytesToAddress([]byte("address"))},
   457  	}
   458  	receipt2 := new(types.Receipt)
   459  	receipt2.Logs = []*types.Log{
   460  		{Address: common.BytesToAddress([]byte("test"))},
   461  		{Address: common.BytesToAddress([]byte("address1"))},
   462  	}
   463  
   464  	WriteMipmapBloom(db, 1, types.Receipts{receipt1})
   465  	WriteMipmapBloom(db, 2, types.Receipts{receipt2})
   466  
   467  	for _, level := range MIPMapLevels {
   468  		bloom := GetMipmapBloom(db, 2, level)
   469  		if !bloom.Test(new(big.Int).SetBytes([]byte("address1"))) {
   470  			t.Error("expected test to be included on level:", level)
   471  		}
   472  	}
   473  
   474  	// reset
   475  	db, _ = ethdb.NewMemDatabase()
   476  	receipt := new(types.Receipt)
   477  	receipt.Logs = []*types.Log{
   478  		{Address: common.BytesToAddress([]byte("test"))},
   479  	}
   480  	WriteMipmapBloom(db, 999, types.Receipts{receipt1})
   481  
   482  	receipt = new(types.Receipt)
   483  	receipt.Logs = []*types.Log{
   484  		{Address: common.BytesToAddress([]byte("test 1"))},
   485  	}
   486  	WriteMipmapBloom(db, 1000, types.Receipts{receipt})
   487  
   488  	bloom := GetMipmapBloom(db, 1000, 1000)
   489  	if bloom.TestBytes([]byte("test")) {
   490  		t.Error("test should not have been included")
   491  	}
   492  }
   493  
   494  func TestMipmapChain(t *testing.T) {
   495  	dir, err := ioutil.TempDir("", "mipmap")
   496  	if err != nil {
   497  		t.Fatal(err)
   498  	}
   499  	defer os.RemoveAll(dir)
   500  
   501  	var (
   502  		db, _   = ethdb.NewLDBDatabase(dir, 0, 0)
   503  		key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
   504  		addr    = crypto.PubkeyToAddress(key1.PublicKey)
   505  		addr2   = common.BytesToAddress([]byte("jeff"))
   506  
   507  		hash1 = common.BytesToHash([]byte("topic1"))
   508  	)
   509  	defer db.Close()
   510  
   511  	gspec := &Genesis{
   512  		Config: params.TestChainConfig,
   513  		Alloc:  GenesisAlloc{addr: {Balance: big.NewInt(1000000)}},
   514  	}
   515  	genesis := gspec.MustCommit(db)
   516  	chain, receipts := GenerateChain(params.TestChainConfig, genesis, db, 1010, func(i int, gen *BlockGen) {
   517  		var receipts types.Receipts
   518  		switch i {
   519  		case 1:
   520  			receipt := types.NewReceipt(nil, new(big.Int))
   521  			receipt.Logs = []*types.Log{{Address: addr, Topics: []common.Hash{hash1}}}
   522  			gen.AddUncheckedReceipt(receipt)
   523  			receipts = types.Receipts{receipt}
   524  		case 1000:
   525  			receipt := types.NewReceipt(nil, new(big.Int))
   526  			receipt.Logs = []*types.Log{{Address: addr2}}
   527  			gen.AddUncheckedReceipt(receipt)
   528  			receipts = types.Receipts{receipt}
   529  
   530  		}
   531  
   532  		// store the receipts
   533  		err := WriteReceipts(db, receipts)
   534  		if err != nil {
   535  			t.Fatal(err)
   536  		}
   537  		WriteMipmapBloom(db, uint64(i+1), receipts)
   538  	})
   539  	for i, block := range chain {
   540  		WriteBlock(db, block)
   541  		if err := WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil {
   542  			t.Fatalf("failed to insert block number: %v", err)
   543  		}
   544  		if err := WriteHeadBlockHash(db, block.Hash()); err != nil {
   545  			t.Fatalf("failed to insert block number: %v", err)
   546  		}
   547  		if err := WriteBlockReceipts(db, block.Hash(), block.NumberU64(), receipts[i]); err != nil {
   548  			t.Fatal("error writing block receipts:", err)
   549  		}
   550  	}
   551  
   552  	bloom := GetMipmapBloom(db, 0, 1000)
   553  	if bloom.TestBytes(addr2[:]) {
   554  		t.Error("address was included in bloom and should not have")
   555  	}
   556  }