github.com/gochain-io/gochain@v2.2.26+incompatible/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/gochain-io/gochain/common"
    25  	"github.com/gochain-io/gochain/core/types"
    26  	"github.com/gochain-io/gochain/crypto/sha3"
    27  	"github.com/gochain-io/gochain/ethdb"
    28  	"github.com/gochain-io/gochain/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.GlobalTable(), db.HeaderTable(), 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.GlobalTable(), db.HeaderTable(), 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.GlobalTable(), db.HeaderTable(), block.Header())
   177  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   178  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   179  		t.Fatalf("Stored block not found")
   180  	} else if entry.Hash() != block.Hash() {
   181  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   182  	}
   183  }
   184  
   185  // Tests block total difficulty storage and retrieval operations.
   186  func TestTdStorage(t *testing.T) {
   187  	db := ethdb.NewMemDatabase()
   188  
   189  	// Create a test TD to move around the database and make sure it's really new
   190  	hash, td := common.Hash{}, big.NewInt(314)
   191  	if entry := ReadTd(db, hash, 0); entry != nil {
   192  		t.Fatalf("Non existent TD returned: %v", entry)
   193  	}
   194  	// Write and verify the TD in the database
   195  	WriteTd(db, hash, 0, td)
   196  	if entry := ReadTd(db, hash, 0); entry == nil {
   197  		t.Fatalf("Stored TD not found")
   198  	} else if entry.Cmp(td) != 0 {
   199  		t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
   200  	}
   201  	// Delete the TD and verify the execution
   202  	DeleteTd(db, hash, 0)
   203  	if entry := ReadTd(db, hash, 0); entry != nil {
   204  		t.Fatalf("Deleted TD returned: %v", entry)
   205  	}
   206  }
   207  
   208  // Tests that canonical numbers can be mapped to hashes and retrieved.
   209  func TestCanonicalMappingStorage(t *testing.T) {
   210  	db := ethdb.NewMemDatabase()
   211  
   212  	// Create a test canonical number and assinged hash to move around
   213  	hash, number := common.Hash{0: 0xff}, uint64(314)
   214  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   215  		t.Fatalf("Non existent canonical mapping returned: %v", entry)
   216  	}
   217  	// Write and verify the TD in the database
   218  	WriteCanonicalHash(db, hash, number)
   219  	if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
   220  		t.Fatalf("Stored canonical mapping not found")
   221  	} else if entry != hash {
   222  		t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
   223  	}
   224  	// Delete the TD and verify the execution
   225  	DeleteCanonicalHash(db, number)
   226  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   227  		t.Fatalf("Deleted canonical mapping returned: %v", entry)
   228  	}
   229  }
   230  
   231  // Tests that head headers and head blocks can be assigned, individually.
   232  func TestHeadStorage(t *testing.T) {
   233  	db := ethdb.NewMemDatabase()
   234  
   235  	blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
   236  	blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
   237  	blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
   238  
   239  	// Check that no head entries are in a pristine database
   240  	if entry := ReadHeadHeaderHash(db.GlobalTable()); entry != (common.Hash{}) {
   241  		t.Fatalf("Non head header entry returned: %v", entry)
   242  	}
   243  	if entry := ReadHeadBlockHash(db.GlobalTable()); entry != (common.Hash{}) {
   244  		t.Fatalf("Non head block entry returned: %v", entry)
   245  	}
   246  	if entry := ReadHeadFastBlockHash(db.GlobalTable()); entry != (common.Hash{}) {
   247  		t.Fatalf("Non fast head block entry returned: %v", entry)
   248  	}
   249  	// Assign separate entries for the head header and block
   250  	WriteHeadHeaderHash(db.GlobalTable(), blockHead.Hash())
   251  	WriteHeadBlockHash(db.GlobalTable(), blockFull.Hash())
   252  	WriteHeadFastBlockHash(db, blockFast.Hash())
   253  
   254  	// Check that both heads are present, and different (i.e. two heads maintained)
   255  	if entry := ReadHeadHeaderHash(db.GlobalTable()); entry != blockHead.Hash() {
   256  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   257  	}
   258  	if entry := ReadHeadBlockHash(db.GlobalTable()); entry != blockFull.Hash() {
   259  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   260  	}
   261  	if entry := ReadHeadFastBlockHash(db.GlobalTable()); entry != blockFast.Hash() {
   262  		t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
   263  	}
   264  }
   265  
   266  // Tests that receipts associated with a single block can be stored and retrieved.
   267  func TestBlockReceiptStorage(t *testing.T) {
   268  	db := ethdb.NewMemDatabase()
   269  
   270  	receipt1 := &types.Receipt{
   271  		Status:            types.ReceiptStatusFailed,
   272  		CumulativeGasUsed: 1,
   273  		Logs: []*types.Log{
   274  			{Address: common.BytesToAddress([]byte{0x11})},
   275  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   276  		},
   277  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
   278  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   279  		GasUsed:         111111,
   280  	}
   281  	receipt2 := &types.Receipt{
   282  		PostState:         common.Hash{2}.Bytes(),
   283  		CumulativeGasUsed: 2,
   284  		Logs: []*types.Log{
   285  			{Address: common.BytesToAddress([]byte{0x22})},
   286  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   287  		},
   288  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
   289  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   290  		GasUsed:         222222,
   291  	}
   292  	receipts := []*types.Receipt{receipt1, receipt2}
   293  
   294  	// Check that no receipt entries are in a pristine database
   295  	hash := common.BytesToHash([]byte{0x03, 0x14})
   296  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   297  		t.Fatalf("non existent receipts returned: %v", rs)
   298  	}
   299  	// Insert the receipt slice into the database and check presence
   300  	WriteReceipts(db, hash, 0, receipts)
   301  	if rs := ReadReceipts(db, hash, 0); len(rs) == 0 {
   302  		t.Fatalf("no receipts returned")
   303  	} else {
   304  		for i := 0; i < len(receipts); i++ {
   305  			rlpHave, _ := rlp.EncodeToBytes(rs[i])
   306  			rlpWant, _ := rlp.EncodeToBytes(receipts[i])
   307  
   308  			if !bytes.Equal(rlpHave, rlpWant) {
   309  				t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
   310  			}
   311  		}
   312  	}
   313  	// Delete the receipt slice and check purge
   314  	DeleteReceipts(db, hash, 0)
   315  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   316  		t.Fatalf("deleted receipts returned: %v", rs)
   317  	}
   318  }