github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/rawdb/accessors_chain_test.go (about)

     1  package rawdb
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/neatio-net/neatio/chain/core/types"
     9  	"github.com/neatio-net/neatio/utilities/common"
    10  	"github.com/neatio-net/neatio/utilities/rlp"
    11  	"golang.org/x/crypto/sha3"
    12  )
    13  
    14  func TestHeaderStorage(t *testing.T) {
    15  	db := NewMemoryDatabase()
    16  
    17  	header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
    18  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    19  		t.Fatalf("Non existent header returned: %v", entry)
    20  	}
    21  	WriteHeader(db, header)
    22  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
    23  		t.Fatalf("Stored header not found")
    24  	} else if entry.Hash() != header.Hash() {
    25  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
    26  	}
    27  	if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
    28  		t.Fatalf("Stored header RLP not found")
    29  	} else {
    30  		hasher := sha3.NewLegacyKeccak256()
    31  		hasher.Write(entry)
    32  
    33  		if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
    34  			t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
    35  		}
    36  	}
    37  	DeleteHeader(db, header.Hash(), header.Number.Uint64())
    38  	if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
    39  		t.Fatalf("Deleted header returned: %v", entry)
    40  	}
    41  }
    42  
    43  func TestBodyStorage(t *testing.T) {
    44  	db := NewMemoryDatabase()
    45  
    46  	body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
    47  
    48  	hasher := sha3.NewLegacyKeccak256()
    49  	rlp.Encode(hasher, body)
    50  	hash := common.BytesToHash(hasher.Sum(nil))
    51  
    52  	if entry := ReadBody(db, hash, 0); entry != nil {
    53  		t.Fatalf("Non existent body returned: %v", entry)
    54  	}
    55  	WriteBody(db, hash, 0, body)
    56  	if entry := ReadBody(db, hash, 0); entry == nil {
    57  		t.Fatalf("Stored body not found")
    58  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
    59  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
    60  	}
    61  	if entry := ReadBodyRLP(db, hash, 0); entry == nil {
    62  		t.Fatalf("Stored body RLP not found")
    63  	} else {
    64  		hasher := sha3.NewLegacyKeccak256()
    65  		hasher.Write(entry)
    66  
    67  		if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
    68  			t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
    69  		}
    70  	}
    71  	DeleteBody(db, hash, 0)
    72  	if entry := ReadBody(db, hash, 0); entry != nil {
    73  		t.Fatalf("Deleted body returned: %v", entry)
    74  	}
    75  }
    76  
    77  func TestBlockStorage(t *testing.T) {
    78  	db := NewMemoryDatabase()
    79  
    80  	block := types.NewBlockWithHeader(&types.Header{
    81  		Extra:       []byte("test block"),
    82  		UncleHash:   types.EmptyUncleHash,
    83  		TxHash:      types.EmptyRootHash,
    84  		ReceiptHash: types.EmptyRootHash,
    85  	})
    86  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
    87  		t.Fatalf("Non existent block returned: %v", entry)
    88  	}
    89  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
    90  		t.Fatalf("Non existent header returned: %v", entry)
    91  	}
    92  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
    93  		t.Fatalf("Non existent body returned: %v", entry)
    94  	}
    95  	WriteBlock(db, block)
    96  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
    97  		t.Fatalf("Stored block not found")
    98  	} else if entry.Hash() != block.Hash() {
    99  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   100  	}
   101  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
   102  		t.Fatalf("Stored header not found")
   103  	} else if entry.Hash() != block.Header().Hash() {
   104  		t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
   105  	}
   106  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
   107  		t.Fatalf("Stored body not found")
   108  	} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(block.Transactions()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
   109  		t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
   110  	}
   111  	DeleteBlock(db, block.Hash(), block.NumberU64())
   112  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   113  		t.Fatalf("Deleted block returned: %v", entry)
   114  	}
   115  	if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
   116  		t.Fatalf("Deleted header returned: %v", entry)
   117  	}
   118  	if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
   119  		t.Fatalf("Deleted body returned: %v", entry)
   120  	}
   121  }
   122  
   123  func TestPartialBlockStorage(t *testing.T) {
   124  	db := NewMemoryDatabase()
   125  	block := types.NewBlockWithHeader(&types.Header{
   126  		Extra:       []byte("test block"),
   127  		UncleHash:   types.EmptyUncleHash,
   128  		TxHash:      types.EmptyRootHash,
   129  		ReceiptHash: types.EmptyRootHash,
   130  	})
   131  	WriteHeader(db, block.Header())
   132  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   133  		t.Fatalf("Non existent block returned: %v", entry)
   134  	}
   135  	DeleteHeader(db, block.Hash(), block.NumberU64())
   136  
   137  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   138  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
   139  		t.Fatalf("Non existent block returned: %v", entry)
   140  	}
   141  	DeleteBody(db, block.Hash(), block.NumberU64())
   142  
   143  	WriteHeader(db, block.Header())
   144  	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
   145  
   146  	if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
   147  		t.Fatalf("Stored block not found")
   148  	} else if entry.Hash() != block.Hash() {
   149  		t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
   150  	}
   151  }
   152  
   153  func TestTdStorage(t *testing.T) {
   154  	db := NewMemoryDatabase()
   155  
   156  	hash, td := common.Hash{}, big.NewInt(314)
   157  	if entry := ReadTd(db, hash, 0); entry != nil {
   158  		t.Fatalf("Non existent TD returned: %v", entry)
   159  	}
   160  	WriteTd(db, hash, 0, td)
   161  	if entry := ReadTd(db, hash, 0); entry == nil {
   162  		t.Fatalf("Stored TD not found")
   163  	} else if entry.Cmp(td) != 0 {
   164  		t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
   165  	}
   166  	DeleteTd(db, hash, 0)
   167  	if entry := ReadTd(db, hash, 0); entry != nil {
   168  		t.Fatalf("Deleted TD returned: %v", entry)
   169  	}
   170  }
   171  
   172  func TestCanonicalMappingStorage(t *testing.T) {
   173  	db := NewMemoryDatabase()
   174  
   175  	hash, number := common.Hash{0: 0xff}, uint64(314)
   176  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   177  		t.Fatalf("Non existent canonical mapping returned: %v", entry)
   178  	}
   179  	WriteCanonicalHash(db, hash, number)
   180  	if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
   181  		t.Fatalf("Stored canonical mapping not found")
   182  	} else if entry != hash {
   183  		t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
   184  	}
   185  	DeleteCanonicalHash(db, number)
   186  	if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
   187  		t.Fatalf("Deleted canonical mapping returned: %v", entry)
   188  	}
   189  }
   190  
   191  func TestHeadStorage(t *testing.T) {
   192  	db := NewMemoryDatabase()
   193  
   194  	blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
   195  	blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
   196  	blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
   197  
   198  	if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) {
   199  		t.Fatalf("Non head header entry returned: %v", entry)
   200  	}
   201  	if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
   202  		t.Fatalf("Non head block entry returned: %v", entry)
   203  	}
   204  	if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) {
   205  		t.Fatalf("Non fast head block entry returned: %v", entry)
   206  	}
   207  	WriteHeadHeaderHash(db, blockHead.Hash())
   208  	WriteHeadBlockHash(db, blockFull.Hash())
   209  	WriteHeadFastBlockHash(db, blockFast.Hash())
   210  
   211  	if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() {
   212  		t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
   213  	}
   214  	if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
   215  		t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
   216  	}
   217  	if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() {
   218  		t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
   219  	}
   220  }
   221  
   222  func TestBlockReceiptStorage(t *testing.T) {
   223  	db := NewMemoryDatabase()
   224  
   225  	receipt1 := &types.Receipt{
   226  		Status:            types.ReceiptStatusFailed,
   227  		CumulativeGasUsed: 1,
   228  		Logs: []*types.Log{
   229  			{Address: common.BytesToAddress([]byte{0x11})},
   230  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   231  		},
   232  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
   233  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   234  		GasUsed:         111111,
   235  	}
   236  	receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
   237  	receipt2 := &types.Receipt{
   238  		PostState:         common.Hash{2}.Bytes(),
   239  		CumulativeGasUsed: 2,
   240  		Logs: []*types.Log{
   241  			{Address: common.BytesToAddress([]byte{0x22})},
   242  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   243  		},
   244  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
   245  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   246  		GasUsed:         222222,
   247  	}
   248  	receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
   249  	receipts := []*types.Receipt{receipt1, receipt2}
   250  
   251  	hash := common.BytesToHash([]byte{0x03, 0x14})
   252  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   253  		t.Fatalf("non existent receipts returned: %v", rs)
   254  	}
   255  	WriteReceipts(db, hash, 0, receipts)
   256  	if rs := ReadReceipts(db, hash, 0); len(rs) == 0 {
   257  		t.Fatalf("no receipts returned")
   258  	} else {
   259  		for i := 0; i < len(receipts); i++ {
   260  			rlpHave, _ := rlp.EncodeToBytes(rs[i])
   261  			rlpWant, _ := rlp.EncodeToBytes(receipts[i])
   262  
   263  			if !bytes.Equal(rlpHave, rlpWant) {
   264  				t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i])
   265  			}
   266  		}
   267  	}
   268  	DeleteReceipts(db, hash, 0)
   269  	if rs := ReadReceipts(db, hash, 0); len(rs) != 0 {
   270  		t.Fatalf("deleted receipts returned: %v", rs)
   271  	}
   272  }