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