github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/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  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/PlatONnetwork/PlatON-Go/common"
    25  	"github.com/PlatONnetwork/PlatON-Go/core/types"
    26  	"github.com/PlatONnetwork/PlatON-Go/crypto/sha3"
    27  	"github.com/PlatONnetwork/PlatON-Go/ethdb"
    28  	"github.com/PlatONnetwork/PlatON-Go/rlp"
    29  )
    30  
    31  // Tests block header storage and retrieval operations.
    32  func TestHeaderStorage(t *testing.T) {
    33  	db := ethdb.NewMemDatabase()
    34  
    35  	// Create a test header to move around the database and make sure it's really new
    36  	header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
    37  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    38  		t.Fatalf("Non existent header returned: %v", entry)
    39  	}
    40  	// Write and verify the header in the database
    41  	WriteHeader(db, header)
    42  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
    43  		t.Fatalf("Stored header not found")
    44  	} else if entry.Hash() != header.Hash() {
    45  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
    46  	}
    47  	if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
    48  		t.Fatalf("Stored header RLP not found")
    49  	} else {
    50  		hasher := sha3.NewKeccak256()
    51  		hasher.Write(entry)
    52  
    53  		if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
    54  			t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
    55  		}
    56  	}
    57  	// Delete the header and verify the execution
    58  	DeleteHeader(db, header.Hash(), header.Number.Uint64())
    59  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    60  		t.Fatalf("Deleted header returned: %v", entry)
    61  	}
    62  }
    63  
    64  // Tests block body storage and retrieval operations.
    65  func TestBodyStorage(t *testing.T) {
    66  	db := ethdb.NewMemDatabase()
    67  
    68  	// Create a test body to move around the database and make sure it's really new
    69  	body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
    70  
    71  	hasher := sha3.NewKeccak256()
    72  	rlp.Encode(hasher, body)
    73  	hash := common.BytesToHash(hasher.Sum(nil))
    74  
    75  	if entry := ReadBody(db, hash, 0); entry != nil {
    76  		t.Fatalf("Non existent body returned: %v", entry)
    77  	}
    78  	// Write and verify the body in the database
    79  	WriteBody(db, hash, 0, body)
    80  	if entry := ReadBody(db, hash, 0); entry == nil {
    81  		t.Fatalf("Stored body not found")
    82  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
    83  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
    84  	}
    85  	if entry := ReadBodyRLP(db, hash, 0); entry == nil {
    86  		t.Fatalf("Stored body RLP not found")
    87  	} else {
    88  		hasher := sha3.NewKeccak256()
    89  		hasher.Write(entry)
    90  
    91  		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
    92  			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
    93  		}
    94  	}
    95  	// Delete the body and verify the execution
    96  	DeleteBody(db, hash, 0)
    97  	if entry := ReadBody(db, hash, 0); entry != nil {
    98  		t.Fatalf("Deleted body returned: %v", entry)
    99  	}
   100  }
   101  
   102  // Tests block storage and retrieval operations.
   103  func TestBlockStorage(t *testing.T) {
   104  	db := ethdb.NewMemDatabase()
   105  
   106  	// Create a test block to move around the database and make sure it's really new
   107  	block := types.NewBlockWithHeader(&types.Header{
   108  		Extra:       []byte("test block"),
   109  		UncleHash:   types.EmptyUncleHash,
   110  		TxHash:      types.EmptyRootHash,
   111  		ReceiptHash: types.EmptyRootHash,
   112  	})
   113  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   114  		t.Fatalf("Non existent block returned: %v", entry)
   115  	}
   116  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   117  		t.Fatalf("Non existent header returned: %v", entry)
   118  	}
   119  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   120  		t.Fatalf("Non existent body returned: %v", entry)
   121  	}
   122  	// Write and verify the block in the database
   123  	WriteBlock(db, block)
   124  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   125  		t.Fatalf("Stored block not found")
   126  	} else if entry.Hash() != block.Hash() {
   127  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   128  	}
   129  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
   130  		t.Fatalf("Stored header not found")
   131  	} else if entry.Hash() != block.Header().Hash() {
   132  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
   133  	}
   134  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
   135  		t.Fatalf("Stored body not found")
   136  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
   137  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
   138  	}
   139  	// Delete the block and verify the execution
   140  	DeleteBlock(db, block.Hash(), block.NumberU64())
   141  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   142  		t.Fatalf("Deleted block returned: %v", entry)
   143  	}
   144  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   145  		t.Fatalf("Deleted header returned: %v", entry)
   146  	}
   147  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   148  		t.Fatalf("Deleted body returned: %v", entry)
   149  	}
   150  }
   151  
   152  // Tests that partial block contents don't get reassembled into full blocks.
   153  func TestPartialBlockStorage(t *testing.T) {
   154  	db := ethdb.NewMemDatabase()
   155  	block := types.NewBlockWithHeader(&types.Header{
   156  		Extra:       []byte("test block"),
   157  		UncleHash:   types.EmptyUncleHash,
   158  		TxHash:      types.EmptyRootHash,
   159  		ReceiptHash: types.EmptyRootHash,
   160  	})
   161  	// Store a header and check that it's not recognized as a block
   162  	WriteHeader(db, block.Header())
   163  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   164  		t.Fatalf("Non existent block returned: %v", entry)
   165  	}
   166  	DeleteHeader(db, block.Hash(), block.NumberU64())
   167  
   168  	// Store a body and check that it's not recognized as a block
   169  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   170  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   171  		t.Fatalf("Non existent block returned: %v", entry)
   172  	}
   173  	DeleteBody(db, block.Hash(), block.NumberU64())
   174  
   175  	// Store a header and a body separately and check reassembly
   176  	WriteHeader(db, block.Header())
   177  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   178  
   179  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   180  		t.Fatalf("Stored block not found")
   181  	} else if entry.Hash() != block.Hash() {
   182  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   183  	}
   184  }
   185  
   186  // Tests that canonical numbers can be mapped to hashes and retrieved.
   187  func TestCanonicalMappingStorage(t *testing.T) {
   188  	db := ethdb.NewMemDatabase()
   189  
   190  	// Create a test canonical number and assinged hash to move around
   191  	hash, number := common.Hash{0: 0xff}, uint64(314)
   192  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   193  		t.Fatalf("Non existent canonical mapping returned: %v", entry)
   194  	}
   195  	// Write and verify the TD in the database
   196  	WriteCanonicalHash(db, hash, number)
   197  	if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
   198  		t.Fatalf("Stored canonical mapping not found")
   199  	} else if entry != hash {
   200  		t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
   201  	}
   202  	// Delete the TD and verify the execution
   203  	DeleteCanonicalHash(db, number)
   204  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   205  		t.Fatalf("Deleted canonical mapping returned: %v", entry)
   206  	}
   207  }
   208  
   209  // Tests that head headers and head blocks can be assigned, individually.
   210  func TestHeadStorage(t *testing.T) {
   211  	db := ethdb.NewMemDatabase()
   212  
   213  	blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
   214  	blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
   215  	blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
   216  
   217  	// Check that no head entries are in a pristine database
   218  	if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) {
   219  		t.Fatalf("Non head header entry returned: %v", entry)
   220  	}
   221  	if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
   222  		t.Fatalf("Non head block entry returned: %v", entry)
   223  	}
   224  	if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) {
   225  		t.Fatalf("Non fast head block entry returned: %v", entry)
   226  	}
   227  	// Assign separate entries for the head header and block
   228  	WriteHeadHeaderHash(db, blockHead.Hash())
   229  	WriteHeadBlockHash(db, blockFull.Hash())
   230  	WriteHeadFastBlockHash(db, blockFast.Hash())
   231  
   232  	// Check that both heads are present, and different (i.e. two heads maintained)
   233  	if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() {
   234  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   235  	}
   236  	if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
   237  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   238  	}
   239  	if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() {
   240  		t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
   241  	}
   242  }
   243  
   244  // Tests that receipts associated with a single block can be stored and retrieved.
   245  func TestBlockReceiptStorage(t *testing.T) {
   246  	db := ethdb.NewMemDatabase()
   247  
   248  	receipt1 := &types.Receipt{
   249  		Status:            types.ReceiptStatusFailed,
   250  		CumulativeGasUsed: 1,
   251  		Logs: []*types.Log{
   252  			{Address: common.BytesToAddress([]byte{0x11})},
   253  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   254  		},
   255  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
   256  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   257  		GasUsed:         111111,
   258  	}
   259  	receipt2 := &types.Receipt{
   260  		PostState:         common.Hash{2}.Bytes(),
   261  		CumulativeGasUsed: 2,
   262  		Logs: []*types.Log{
   263  			{Address: common.BytesToAddress([]byte{0x22})},
   264  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   265  		},
   266  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
   267  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   268  		GasUsed:         222222,
   269  	}
   270  	receipts := []*types.Receipt{receipt1, receipt2}
   271  
   272  	// Check that no receipt entries are in a pristine database
   273  	hash := common.BytesToHash([]byte{0x03, 0x14})
   274  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   275  		t.Fatalf("non existent receipts returned: %v", rs)
   276  	}
   277  	// Insert the receipt slice into the database and check presence
   278  	WriteReceipts(db, hash, 0, receipts)
   279  	if rs := ReadReceipts(db, hash, 0); len(rs) == 0 {
   280  		t.Fatalf("no receipts returned")
   281  	} else {
   282  		for i := 0; i < len(receipts); i++ {
   283  			rlpHave, _ := rlp.EncodeToBytes(rs[i])
   284  			rlpWant, _ := rlp.EncodeToBytes(receipts[i])
   285  
   286  			if !bytes.Equal(rlpHave, rlpWant) {
   287  				t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
   288  			}
   289  		}
   290  	}
   291  	// Delete the receipt slice and check purge
   292  	DeleteReceipts(db, hash, 0)
   293  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   294  		t.Fatalf("deleted receipts returned: %v", rs)
   295  	}
   296  }