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