github.com/gilgames000/kcc-geth@v1.0.6/core/rawdb/accessors_chain_test.go (about)

     1  // Copyright 2018 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 rawdb
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"math/big"
    25  	"math/rand"
    26  	"os"
    27  	"reflect"
    28  	"testing"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/params"
    33  	"github.com/ethereum/go-ethereum/rlp"
    34  	"golang.org/x/crypto/sha3"
    35  )
    36  
    37  // Tests block header storage and retrieval operations.
    38  func TestHeaderStorage(t *testing.T) {
    39  	db := NewMemoryDatabase()
    40  
    41  	// Create a test header to move around the database and make sure it's really new
    42  	header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
    43  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    44  		t.Fatalf("Non existent header returned: %v", entry)
    45  	}
    46  	// Write and verify the header in the database
    47  	WriteHeader(db, header)
    48  	if entry := ReadHeader(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 := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
    54  		t.Fatalf("Stored header RLP not found")
    55  	} else {
    56  		hasher := sha3.NewLegacyKeccak256()
    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 := ReadHeader(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 := NewMemoryDatabase()
    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.NewLegacyKeccak256()
    78  	rlp.Encode(hasher, body)
    79  	hash := common.BytesToHash(hasher.Sum(nil))
    80  
    81  	if entry := ReadBody(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  	WriteBody(db, hash, 0, body)
    86  	if entry := ReadBody(db, hash, 0); entry == nil {
    87  		t.Fatalf("Stored body not found")
    88  	} else if types.DeriveSha(types.Transactions(entry.Transactions), newHasher()) != types.DeriveSha(types.Transactions(body.Transactions), newHasher()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
    89  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
    90  	}
    91  	if entry := ReadBodyRLP(db, hash, 0); entry == nil {
    92  		t.Fatalf("Stored body RLP not found")
    93  	} else {
    94  		hasher := sha3.NewLegacyKeccak256()
    95  		hasher.Write(entry)
    96  
    97  		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
    98  			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
    99  		}
   100  	}
   101  	// Delete the body and verify the execution
   102  	DeleteBody(db, hash, 0)
   103  	if entry := ReadBody(db, hash, 0); entry != nil {
   104  		t.Fatalf("Deleted body returned: %v", entry)
   105  	}
   106  }
   107  
   108  // Tests block storage and retrieval operations.
   109  func TestBlockStorage(t *testing.T) {
   110  	db := NewMemoryDatabase()
   111  
   112  	// Create a test block to move around the database and make sure it's really new
   113  	block := types.NewBlockWithHeader(&types.Header{
   114  		Extra:       []byte("test block"),
   115  		UncleHash:   types.EmptyUncleHash,
   116  		TxHash:      types.EmptyRootHash,
   117  		ReceiptHash: types.EmptyRootHash,
   118  	})
   119  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   120  		t.Fatalf("Non existent block returned: %v", entry)
   121  	}
   122  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   123  		t.Fatalf("Non existent header returned: %v", entry)
   124  	}
   125  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   126  		t.Fatalf("Non existent body returned: %v", entry)
   127  	}
   128  	// Write and verify the block in the database
   129  	WriteBlock(db, block)
   130  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   131  		t.Fatalf("Stored block not found")
   132  	} else if entry.Hash() != block.Hash() {
   133  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   134  	}
   135  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
   136  		t.Fatalf("Stored header not found")
   137  	} else if entry.Hash() != block.Header().Hash() {
   138  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
   139  	}
   140  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
   141  		t.Fatalf("Stored body not found")
   142  	} else if types.DeriveSha(types.Transactions(entry.Transactions), newHasher()) != types.DeriveSha(block.Transactions(), newHasher()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
   143  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
   144  	}
   145  	// Delete the block and verify the execution
   146  	DeleteBlock(db, block.Hash(), block.NumberU64())
   147  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   148  		t.Fatalf("Deleted block returned: %v", entry)
   149  	}
   150  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   151  		t.Fatalf("Deleted header returned: %v", entry)
   152  	}
   153  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   154  		t.Fatalf("Deleted body returned: %v", entry)
   155  	}
   156  }
   157  
   158  // Tests that partial block contents don't get reassembled into full blocks.
   159  func TestPartialBlockStorage(t *testing.T) {
   160  	db := NewMemoryDatabase()
   161  	block := types.NewBlockWithHeader(&types.Header{
   162  		Extra:       []byte("test block"),
   163  		UncleHash:   types.EmptyUncleHash,
   164  		TxHash:      types.EmptyRootHash,
   165  		ReceiptHash: types.EmptyRootHash,
   166  	})
   167  	// Store a header and check that it's not recognized as a block
   168  	WriteHeader(db, block.Header())
   169  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   170  		t.Fatalf("Non existent block returned: %v", entry)
   171  	}
   172  	DeleteHeader(db, block.Hash(), block.NumberU64())
   173  
   174  	// Store a body and check that it's not recognized as a block
   175  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   176  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   177  		t.Fatalf("Non existent block returned: %v", entry)
   178  	}
   179  	DeleteBody(db, block.Hash(), block.NumberU64())
   180  
   181  	// Store a header and a body separately and check reassembly
   182  	WriteHeader(db, block.Header())
   183  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   184  
   185  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   186  		t.Fatalf("Stored block not found")
   187  	} else if entry.Hash() != block.Hash() {
   188  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   189  	}
   190  }
   191  
   192  // Tests block storage and retrieval operations.
   193  func TestBadBlockStorage(t *testing.T) {
   194  	db := NewMemoryDatabase()
   195  
   196  	// Create a test block to move around the database and make sure it's really new
   197  	block := types.NewBlockWithHeader(&types.Header{
   198  		Number:      big.NewInt(1),
   199  		Extra:       []byte("bad block"),
   200  		UncleHash:   types.EmptyUncleHash,
   201  		TxHash:      types.EmptyRootHash,
   202  		ReceiptHash: types.EmptyRootHash,
   203  	})
   204  	if entry := ReadBadBlock(db, block.Hash()); entry != nil {
   205  		t.Fatalf("Non existent block returned: %v", entry)
   206  	}
   207  	// Write and verify the block in the database
   208  	WriteBadBlock(db, block)
   209  	if entry := ReadBadBlock(db, block.Hash()); entry == nil {
   210  		t.Fatalf("Stored block not found")
   211  	} else if entry.Hash() != block.Hash() {
   212  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   213  	}
   214  	// Write one more bad block
   215  	blockTwo := types.NewBlockWithHeader(&types.Header{
   216  		Number:      big.NewInt(2),
   217  		Extra:       []byte("bad block two"),
   218  		UncleHash:   types.EmptyUncleHash,
   219  		TxHash:      types.EmptyRootHash,
   220  		ReceiptHash: types.EmptyRootHash,
   221  	})
   222  	WriteBadBlock(db, blockTwo)
   223  
   224  	// Write the block one again, should be filtered out.
   225  	WriteBadBlock(db, block)
   226  	badBlocks := ReadAllBadBlocks(db)
   227  	if len(badBlocks) != 2 {
   228  		t.Fatalf("Failed to load all bad blocks")
   229  	}
   230  
   231  	// Write a bunch of bad blocks, all the blocks are should sorted
   232  	// in reverse order. The extra blocks should be truncated.
   233  	for _, n := range rand.Perm(100) {
   234  		block := types.NewBlockWithHeader(&types.Header{
   235  			Number:      big.NewInt(int64(n)),
   236  			Extra:       []byte("bad block"),
   237  			UncleHash:   types.EmptyUncleHash,
   238  			TxHash:      types.EmptyRootHash,
   239  			ReceiptHash: types.EmptyRootHash,
   240  		})
   241  		WriteBadBlock(db, block)
   242  	}
   243  	badBlocks = ReadAllBadBlocks(db)
   244  	if len(badBlocks) != badBlockToKeep {
   245  		t.Fatalf("The number of persised bad blocks in incorrect %d", len(badBlocks))
   246  	}
   247  	for i := 0; i < len(badBlocks)-1; i++ {
   248  		if badBlocks[i].NumberU64() < badBlocks[i+1].NumberU64() {
   249  			t.Fatalf("The bad blocks are not sorted #[%d](%d) < #[%d](%d)", i, i+1, badBlocks[i].NumberU64(), badBlocks[i+1].NumberU64())
   250  		}
   251  	}
   252  
   253  	// Delete all bad blocks
   254  	DeleteBadBlocks(db)
   255  	badBlocks = ReadAllBadBlocks(db)
   256  	if len(badBlocks) != 0 {
   257  		t.Fatalf("Failed to delete bad blocks")
   258  	}
   259  }
   260  
   261  // Tests block total difficulty storage and retrieval operations.
   262  func TestTdStorage(t *testing.T) {
   263  	db := NewMemoryDatabase()
   264  
   265  	// Create a test TD to move around the database and make sure it's really new
   266  	hash, td := common.Hash{}, big.NewInt(314)
   267  	if entry := ReadTd(db, hash, 0); entry != nil {
   268  		t.Fatalf("Non existent TD returned: %v", entry)
   269  	}
   270  	// Write and verify the TD in the database
   271  	WriteTd(db, hash, 0, td)
   272  	if entry := ReadTd(db, hash, 0); entry == nil {
   273  		t.Fatalf("Stored TD not found")
   274  	} else if entry.Cmp(td) != 0 {
   275  		t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
   276  	}
   277  	// Delete the TD and verify the execution
   278  	DeleteTd(db, hash, 0)
   279  	if entry := ReadTd(db, hash, 0); entry != nil {
   280  		t.Fatalf("Deleted TD returned: %v", entry)
   281  	}
   282  }
   283  
   284  // Tests that canonical numbers can be mapped to hashes and retrieved.
   285  func TestCanonicalMappingStorage(t *testing.T) {
   286  	db := NewMemoryDatabase()
   287  
   288  	// Create a test canonical number and assinged hash to move around
   289  	hash, number := common.Hash{0: 0xff}, uint64(314)
   290  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   291  		t.Fatalf("Non existent canonical mapping returned: %v", entry)
   292  	}
   293  	// Write and verify the TD in the database
   294  	WriteCanonicalHash(db, hash, number)
   295  	if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
   296  		t.Fatalf("Stored canonical mapping not found")
   297  	} else if entry != hash {
   298  		t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
   299  	}
   300  	// Delete the TD and verify the execution
   301  	DeleteCanonicalHash(db, number)
   302  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   303  		t.Fatalf("Deleted canonical mapping returned: %v", entry)
   304  	}
   305  }
   306  
   307  // Tests that head headers and head blocks can be assigned, individually.
   308  func TestHeadStorage(t *testing.T) {
   309  	db := NewMemoryDatabase()
   310  
   311  	blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
   312  	blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
   313  	blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
   314  
   315  	// Check that no head entries are in a pristine database
   316  	if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) {
   317  		t.Fatalf("Non head header entry returned: %v", entry)
   318  	}
   319  	if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
   320  		t.Fatalf("Non head block entry returned: %v", entry)
   321  	}
   322  	if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) {
   323  		t.Fatalf("Non fast head block entry returned: %v", entry)
   324  	}
   325  	// Assign separate entries for the head header and block
   326  	WriteHeadHeaderHash(db, blockHead.Hash())
   327  	WriteHeadBlockHash(db, blockFull.Hash())
   328  	WriteHeadFastBlockHash(db, blockFast.Hash())
   329  
   330  	// Check that both heads are present, and different (i.e. two heads maintained)
   331  	if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() {
   332  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   333  	}
   334  	if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
   335  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   336  	}
   337  	if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() {
   338  		t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
   339  	}
   340  }
   341  
   342  // Tests that receipts associated with a single block can be stored and retrieved.
   343  func TestBlockReceiptStorage(t *testing.T) {
   344  	db := NewMemoryDatabase()
   345  
   346  	// Create a live block since we need metadata to reconstruct the receipt
   347  	tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
   348  	tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)
   349  
   350  	body := &types.Body{Transactions: types.Transactions{tx1, tx2}}
   351  
   352  	// Create the two receipts to manage afterwards
   353  	receipt1 := &types.Receipt{
   354  		Status:            types.ReceiptStatusFailed,
   355  		CumulativeGasUsed: 1,
   356  		Logs: []*types.Log{
   357  			{Address: common.BytesToAddress([]byte{0x11})},
   358  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   359  		},
   360  		TxHash:          tx1.Hash(),
   361  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   362  		GasUsed:         111111,
   363  	}
   364  	receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
   365  
   366  	receipt2 := &types.Receipt{
   367  		PostState:         common.Hash{2}.Bytes(),
   368  		CumulativeGasUsed: 2,
   369  		Logs: []*types.Log{
   370  			{Address: common.BytesToAddress([]byte{0x22})},
   371  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   372  		},
   373  		TxHash:          tx2.Hash(),
   374  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   375  		GasUsed:         222222,
   376  	}
   377  	receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
   378  	receipts := []*types.Receipt{receipt1, receipt2}
   379  
   380  	// Check that no receipt entries are in a pristine database
   381  	hash := common.BytesToHash([]byte{0x03, 0x14})
   382  	if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
   383  		t.Fatalf("non existent receipts returned: %v", rs)
   384  	}
   385  	// Insert the body that corresponds to the receipts
   386  	WriteBody(db, hash, 0, body)
   387  
   388  	// Insert the receipt slice into the database and check presence
   389  	WriteReceipts(db, hash, 0, receipts)
   390  	if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
   391  		t.Fatalf("no receipts returned")
   392  	} else {
   393  		if err := checkReceiptsRLP(rs, receipts); err != nil {
   394  			t.Fatalf(err.Error())
   395  		}
   396  	}
   397  	// Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed)
   398  	DeleteBody(db, hash, 0)
   399  	if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil {
   400  		t.Fatalf("receipts returned when body was deleted: %v", rs)
   401  	}
   402  	// Ensure that receipts without metadata can be returned without the block body too
   403  	if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil {
   404  		t.Fatalf(err.Error())
   405  	}
   406  	// Sanity check that body alone without the receipt is a full purge
   407  	WriteBody(db, hash, 0, body)
   408  
   409  	DeleteReceipts(db, hash, 0)
   410  	if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
   411  		t.Fatalf("deleted receipts returned: %v", rs)
   412  	}
   413  }
   414  
   415  func checkReceiptsRLP(have, want types.Receipts) error {
   416  	if len(have) != len(want) {
   417  		return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want))
   418  	}
   419  	for i := 0; i < len(want); i++ {
   420  		rlpHave, err := rlp.EncodeToBytes(have[i])
   421  		if err != nil {
   422  			return err
   423  		}
   424  		rlpWant, err := rlp.EncodeToBytes(want[i])
   425  		if err != nil {
   426  			return err
   427  		}
   428  		if !bytes.Equal(rlpHave, rlpWant) {
   429  			return fmt.Errorf("receipt #%d: receipt mismatch: have %s, want %s", i, hex.EncodeToString(rlpHave), hex.EncodeToString(rlpWant))
   430  		}
   431  	}
   432  	return nil
   433  }
   434  
   435  func TestAncientStorage(t *testing.T) {
   436  	// Freezer style fast import the chain.
   437  	frdir, err := ioutil.TempDir("", "")
   438  	if err != nil {
   439  		t.Fatalf("failed to create temp freezer dir: %v", err)
   440  	}
   441  	defer os.Remove(frdir)
   442  
   443  	db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "")
   444  	if err != nil {
   445  		t.Fatalf("failed to create database with ancient backend")
   446  	}
   447  	// Create a test block
   448  	block := types.NewBlockWithHeader(&types.Header{
   449  		Number:      big.NewInt(0),
   450  		Extra:       []byte("test block"),
   451  		UncleHash:   types.EmptyUncleHash,
   452  		TxHash:      types.EmptyRootHash,
   453  		ReceiptHash: types.EmptyRootHash,
   454  	})
   455  	// Ensure nothing non-existent will be read
   456  	hash, number := block.Hash(), block.NumberU64()
   457  	if blob := ReadHeaderRLP(db, hash, number); len(blob) > 0 {
   458  		t.Fatalf("non existent header returned")
   459  	}
   460  	if blob := ReadBodyRLP(db, hash, number); len(blob) > 0 {
   461  		t.Fatalf("non existent body returned")
   462  	}
   463  	if blob := ReadReceiptsRLP(db, hash, number); len(blob) > 0 {
   464  		t.Fatalf("non existent receipts returned")
   465  	}
   466  	if blob := ReadTdRLP(db, hash, number); len(blob) > 0 {
   467  		t.Fatalf("non existent td returned")
   468  	}
   469  	// Write and verify the header in the database
   470  	WriteAncientBlock(db, block, nil, big.NewInt(100))
   471  	if blob := ReadHeaderRLP(db, hash, number); len(blob) == 0 {
   472  		t.Fatalf("no header returned")
   473  	}
   474  	if blob := ReadBodyRLP(db, hash, number); len(blob) == 0 {
   475  		t.Fatalf("no body returned")
   476  	}
   477  	if blob := ReadReceiptsRLP(db, hash, number); len(blob) == 0 {
   478  		t.Fatalf("no receipts returned")
   479  	}
   480  	if blob := ReadTdRLP(db, hash, number); len(blob) == 0 {
   481  		t.Fatalf("no td returned")
   482  	}
   483  	// Use a fake hash for data retrieval, nothing should be returned.
   484  	fakeHash := common.BytesToHash([]byte{0x01, 0x02, 0x03})
   485  	if blob := ReadHeaderRLP(db, fakeHash, number); len(blob) != 0 {
   486  		t.Fatalf("invalid header returned")
   487  	}
   488  	if blob := ReadBodyRLP(db, fakeHash, number); len(blob) != 0 {
   489  		t.Fatalf("invalid body returned")
   490  	}
   491  	if blob := ReadReceiptsRLP(db, fakeHash, number); len(blob) != 0 {
   492  		t.Fatalf("invalid receipts returned")
   493  	}
   494  	if blob := ReadTdRLP(db, fakeHash, number); len(blob) != 0 {
   495  		t.Fatalf("invalid td returned")
   496  	}
   497  }
   498  
   499  func TestCanonicalHashIteration(t *testing.T) {
   500  	var cases = []struct {
   501  		from, to uint64
   502  		limit    int
   503  		expect   []uint64
   504  	}{
   505  		{1, 8, 0, nil},
   506  		{1, 8, 1, []uint64{1}},
   507  		{1, 8, 10, []uint64{1, 2, 3, 4, 5, 6, 7}},
   508  		{1, 9, 10, []uint64{1, 2, 3, 4, 5, 6, 7, 8}},
   509  		{2, 9, 10, []uint64{2, 3, 4, 5, 6, 7, 8}},
   510  		{9, 10, 10, nil},
   511  	}
   512  	// Test empty db iteration
   513  	db := NewMemoryDatabase()
   514  	numbers, _ := ReadAllCanonicalHashes(db, 0, 10, 10)
   515  	if len(numbers) != 0 {
   516  		t.Fatalf("No entry should be returned to iterate an empty db")
   517  	}
   518  	// Fill database with testing data.
   519  	for i := uint64(1); i <= 8; i++ {
   520  		WriteCanonicalHash(db, common.Hash{}, i)
   521  		WriteTd(db, common.Hash{}, i, big.NewInt(10)) // Write some interferential data
   522  	}
   523  	for i, c := range cases {
   524  		numbers, _ := ReadAllCanonicalHashes(db, c.from, c.to, c.limit)
   525  		if !reflect.DeepEqual(numbers, c.expect) {
   526  			t.Fatalf("Case %d failed, want %v, got %v", i, c.expect, numbers)
   527  		}
   528  	}
   529  }