github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/intfoundation/intchain/common"
    25  	"github.com/intfoundation/intchain/core/types"
    26  	"github.com/intfoundation/intchain/rlp"
    27  	"golang.org/x/crypto/sha3"
    28  )
    29  
    30  // Tests block header storage and retrieval operations.
    31  func TestHeaderStorage(t *testing.T) {
    32  	db := NewMemoryDatabase()
    33  
    34  	// Create a test header to move around the database and make sure it's really new
    35  	header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
    36  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    37  		t.Fatalf("Non existent header returned: %v", entry)
    38  	}
    39  	// Write and verify the header in the database
    40  	WriteHeader(db, header)
    41  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
    42  		t.Fatalf("Stored header not found")
    43  	} else if entry.Hash() != header.Hash() {
    44  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
    45  	}
    46  	if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
    47  		t.Fatalf("Stored header RLP not found")
    48  	} else {
    49  		hasher := sha3.NewLegacyKeccak256()
    50  		hasher.Write(entry)
    51  
    52  		if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
    53  			t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
    54  		}
    55  	}
    56  	// Delete the header and verify the execution
    57  	DeleteHeader(db, header.Hash(), header.Number.Uint64())
    58  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    59  		t.Fatalf("Deleted header returned: %v", entry)
    60  	}
    61  }
    62  
    63  // Tests block body storage and retrieval operations.
    64  func TestBodyStorage(t *testing.T) {
    65  	db := NewMemoryDatabase()
    66  
    67  	// Create a test body to move around the database and make sure it's really new
    68  	body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
    69  
    70  	hasher := sha3.NewLegacyKeccak256()
    71  	rlp.Encode(hasher, body)
    72  	hash := common.BytesToHash(hasher.Sum(nil))
    73  
    74  	if entry := ReadBody(db, hash, 0); entry != nil {
    75  		t.Fatalf("Non existent body returned: %v", entry)
    76  	}
    77  	// Write and verify the body in the database
    78  	WriteBody(db, hash, 0, body)
    79  	if entry := ReadBody(db, hash, 0); entry == nil {
    80  		t.Fatalf("Stored body not found")
    81  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
    82  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
    83  	}
    84  	if entry := ReadBodyRLP(db, hash, 0); entry == nil {
    85  		t.Fatalf("Stored body RLP not found")
    86  	} else {
    87  		hasher := sha3.NewLegacyKeccak256()
    88  		hasher.Write(entry)
    89  
    90  		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
    91  			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
    92  		}
    93  	}
    94  	// Delete the body and verify the execution
    95  	DeleteBody(db, hash, 0)
    96  	if entry := ReadBody(db, hash, 0); entry != nil {
    97  		t.Fatalf("Deleted body returned: %v", entry)
    98  	}
    99  }
   100  
   101  // Tests block storage and retrieval operations.
   102  func TestBlockStorage(t *testing.T) {
   103  	db := NewMemoryDatabase()
   104  
   105  	// Create a test block to move around the database and make sure it's really new
   106  	block := types.NewBlockWithHeader(&types.Header{
   107  		Extra:       []byte("test block"),
   108  		UncleHash:   types.EmptyUncleHash,
   109  		TxHash:      types.EmptyRootHash,
   110  		ReceiptHash: types.EmptyRootHash,
   111  	})
   112  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   113  		t.Fatalf("Non existent block returned: %v", entry)
   114  	}
   115  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   116  		t.Fatalf("Non existent header returned: %v", entry)
   117  	}
   118  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   119  		t.Fatalf("Non existent body returned: %v", entry)
   120  	}
   121  	// Write and verify the block in the database
   122  	WriteBlock(db, block)
   123  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   124  		t.Fatalf("Stored block not found")
   125  	} else if entry.Hash() != block.Hash() {
   126  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   127  	}
   128  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
   129  		t.Fatalf("Stored header not found")
   130  	} else if entry.Hash() != block.Header().Hash() {
   131  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
   132  	}
   133  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
   134  		t.Fatalf("Stored body not found")
   135  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
   136  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
   137  	}
   138  	// Delete the block and verify the execution
   139  	DeleteBlock(db, block.Hash(), block.NumberU64())
   140  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   141  		t.Fatalf("Deleted block returned: %v", entry)
   142  	}
   143  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   144  		t.Fatalf("Deleted header returned: %v", entry)
   145  	}
   146  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   147  		t.Fatalf("Deleted body returned: %v", entry)
   148  	}
   149  }
   150  
   151  // Tests that partial block contents don't get reassembled into full blocks.
   152  func TestPartialBlockStorage(t *testing.T) {
   153  	db := NewMemoryDatabase()
   154  	block := types.NewBlockWithHeader(&types.Header{
   155  		Extra:       []byte("test block"),
   156  		UncleHash:   types.EmptyUncleHash,
   157  		TxHash:      types.EmptyRootHash,
   158  		ReceiptHash: types.EmptyRootHash,
   159  	})
   160  	// Store a header and check that it's not recognized as a block
   161  	WriteHeader(db, block.Header())
   162  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   163  		t.Fatalf("Non existent block returned: %v", entry)
   164  	}
   165  	DeleteHeader(db, block.Hash(), block.NumberU64())
   166  
   167  	// Store a body and check that it's not recognized as a block
   168  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   169  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   170  		t.Fatalf("Non existent block returned: %v", entry)
   171  	}
   172  	DeleteBody(db, block.Hash(), block.NumberU64())
   173  
   174  	// Store a header and a body separately and check reassembly
   175  	WriteHeader(db, block.Header())
   176  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   177  
   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 := NewMemoryDatabase()
   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 := NewMemoryDatabase()
   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 := NewMemoryDatabase()
   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); entry != (common.Hash{}) {
   241  		t.Fatalf("Non head header entry returned: %v", entry)
   242  	}
   243  	if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
   244  		t.Fatalf("Non head block entry returned: %v", entry)
   245  	}
   246  	if entry := ReadHeadFastBlockHash(db); 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, blockHead.Hash())
   251  	WriteHeadBlockHash(db, 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); entry != blockHead.Hash() {
   256  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   257  	}
   258  	if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
   259  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   260  	}
   261  	if entry := ReadHeadFastBlockHash(db); 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 := NewMemoryDatabase()
   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  	receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
   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  	receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
   294  	receipts := []*types.Receipt{receipt1, receipt2}
   295  
   296  	// Check that no receipt entries are in a pristine database
   297  	hash := common.BytesToHash([]byte{0x03, 0x14})
   298  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   299  		t.Fatalf("non existent receipts returned: %v", rs)
   300  	}
   301  	// Insert the receipt slice into the database and check presence
   302  	WriteReceipts(db, hash, 0, receipts)
   303  	if rs := ReadReceipts(db, hash, 0); len(rs) == 0 {
   304  		t.Fatalf("no receipts returned")
   305  	} else {
   306  		for i := 0; i < len(receipts); i++ {
   307  			rlpHave, _ := rlp.EncodeToBytes(rs[i])
   308  			rlpWant, _ := rlp.EncodeToBytes(receipts[i])
   309  
   310  			if !bytes.Equal(rlpHave, rlpWant) {
   311  				t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
   312  			}
   313  		}
   314  	}
   315  	// Delete the receipt slice and check purge
   316  	DeleteReceipts(db, hash, 0)
   317  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   318  		t.Fatalf("deleted receipts returned: %v", rs)
   319  	}
   320  }