123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804 |
- // Copyright 2018 The go-ethereum Authors
- // This file is part of the go-ethereum library.
- //
- // The go-ethereum library is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // The go-ethereum library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
- package rawdb
- import (
- "bytes"
- "encoding/hex"
- "fmt"
- "io/ioutil"
- "math/big"
- "math/rand"
- "os"
- "reflect"
- "testing"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/log"
- "github.com/ethereum/go-ethereum/params"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/stretchr/testify/assert"
- "golang.org/x/crypto/sha3"
- )
- // Tests block header storage and retrieval operations.
- func TestHeaderStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test header to move around the database and make sure it's really new
- header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")}
- if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
- t.Fatalf("Non existent header returned: %v", entry)
- }
- // Write and verify the header in the database
- WriteHeader(db, header)
- if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry == nil {
- t.Fatalf("Stored header not found")
- } else if entry.Hash() != header.Hash() {
- t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, header)
- }
- if entry := ReadHeaderRLP(db, header.Hash(), header.Number.Uint64()); entry == nil {
- t.Fatalf("Stored header RLP not found")
- } else {
- hasher := sha3.NewLegacyKeccak256()
- hasher.Write(entry)
- if hash := common.BytesToHash(hasher.Sum(nil)); hash != header.Hash() {
- t.Fatalf("Retrieved RLP header mismatch: have %v, want %v", entry, header)
- }
- }
- // Delete the header and verify the execution
- DeleteHeader(db, header.Hash(), header.Number.Uint64())
- if entry := ReadHeader(db, header.Hash(), header.Number.Uint64()); entry != nil {
- t.Fatalf("Deleted header returned: %v", entry)
- }
- }
- // Tests block body storage and retrieval operations.
- func TestBodyStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test body to move around the database and make sure it's really new
- body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}}
- hasher := sha3.NewLegacyKeccak256()
- rlp.Encode(hasher, body)
- hash := common.BytesToHash(hasher.Sum(nil))
- if entry := ReadBody(db, hash, 0); entry != nil {
- t.Fatalf("Non existent body returned: %v", entry)
- }
- // Write and verify the body in the database
- WriteBody(db, hash, 0, body)
- if entry := ReadBody(db, hash, 0); entry == nil {
- t.Fatalf("Stored body not found")
- } else if types.DeriveSha(types.Transactions(entry.Transactions), newHasher()) != types.DeriveSha(types.Transactions(body.Transactions), newHasher()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
- t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
- }
- if entry := ReadBodyRLP(db, hash, 0); entry == nil {
- t.Fatalf("Stored body RLP not found")
- } else {
- hasher := sha3.NewLegacyKeccak256()
- hasher.Write(entry)
- if calc := common.BytesToHash(hasher.Sum(nil)); calc != hash {
- t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
- }
- }
- // Delete the body and verify the execution
- DeleteBody(db, hash, 0)
- if entry := ReadBody(db, hash, 0); entry != nil {
- t.Fatalf("Deleted body returned: %v", entry)
- }
- }
- // Tests block storage and retrieval operations.
- func TestBlockStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test block to move around the database and make sure it's really new
- block := types.NewBlockWithHeader(&types.Header{
- Extra: []byte("test block"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Non existent block returned: %v", entry)
- }
- if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Non existent header returned: %v", entry)
- }
- if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Non existent body returned: %v", entry)
- }
- // Write and verify the block in the database
- WriteBlock(db, block)
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
- t.Fatalf("Stored block not found")
- } else if entry.Hash() != block.Hash() {
- t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
- }
- if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry == nil {
- t.Fatalf("Stored header not found")
- } else if entry.Hash() != block.Header().Hash() {
- t.Fatalf("Retrieved header mismatch: have %v, want %v", entry, block.Header())
- }
- if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry == nil {
- t.Fatalf("Stored body not found")
- } else if types.DeriveSha(types.Transactions(entry.Transactions), newHasher()) != types.DeriveSha(block.Transactions(), newHasher()) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(block.Uncles()) {
- t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, block.Body())
- }
- // Delete the block and verify the execution
- DeleteBlock(db, block.Hash(), block.NumberU64())
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Deleted block returned: %v", entry)
- }
- if entry := ReadHeader(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Deleted header returned: %v", entry)
- }
- if entry := ReadBody(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Deleted body returned: %v", entry)
- }
- }
- // Tests that partial block contents don't get reassembled into full blocks.
- func TestPartialBlockStorage(t *testing.T) {
- db := NewMemoryDatabase()
- block := types.NewBlockWithHeader(&types.Header{
- Extra: []byte("test block"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- // Store a header and check that it's not recognized as a block
- WriteHeader(db, block.Header())
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Non existent block returned: %v", entry)
- }
- DeleteHeader(db, block.Hash(), block.NumberU64())
- // Store a body and check that it's not recognized as a block
- WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry != nil {
- t.Fatalf("Non existent block returned: %v", entry)
- }
- DeleteBody(db, block.Hash(), block.NumberU64())
- // Store a header and a body separately and check reassembly
- WriteHeader(db, block.Header())
- WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
- if entry := ReadBlock(db, block.Hash(), block.NumberU64()); entry == nil {
- t.Fatalf("Stored block not found")
- } else if entry.Hash() != block.Hash() {
- t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
- }
- }
- // Tests block storage and retrieval operations.
- func TestBadBlockStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test block to move around the database and make sure it's really new
- block := types.NewBlockWithHeader(&types.Header{
- Number: big.NewInt(1),
- Extra: []byte("bad block"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- if entry := ReadBadBlock(db, block.Hash()); entry != nil {
- t.Fatalf("Non existent block returned: %v", entry)
- }
- // Write and verify the block in the database
- WriteBadBlock(db, block)
- if entry := ReadBadBlock(db, block.Hash()); entry == nil {
- t.Fatalf("Stored block not found")
- } else if entry.Hash() != block.Hash() {
- t.Fatalf("Retrieved block mismatch: have %v, want %v", entry, block)
- }
- // Write one more bad block
- blockTwo := types.NewBlockWithHeader(&types.Header{
- Number: big.NewInt(2),
- Extra: []byte("bad block two"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- WriteBadBlock(db, blockTwo)
- // Write the block one again, should be filtered out.
- WriteBadBlock(db, block)
- badBlocks := ReadAllBadBlocks(db)
- if len(badBlocks) != 2 {
- t.Fatalf("Failed to load all bad blocks")
- }
- // Write a bunch of bad blocks, all the blocks are should sorted
- // in reverse order. The extra blocks should be truncated.
- for _, n := range rand.Perm(100) {
- block := types.NewBlockWithHeader(&types.Header{
- Number: big.NewInt(int64(n)),
- Extra: []byte("bad block"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- WriteBadBlock(db, block)
- }
- badBlocks = ReadAllBadBlocks(db)
- if len(badBlocks) != badBlockToKeep {
- t.Fatalf("The number of persised bad blocks in incorrect %d", len(badBlocks))
- }
- for i := 0; i < len(badBlocks)-1; i++ {
- if badBlocks[i].NumberU64() < badBlocks[i+1].NumberU64() {
- t.Fatalf("The bad blocks are not sorted #[%d](%d) < #[%d](%d)", i, i+1, badBlocks[i].NumberU64(), badBlocks[i+1].NumberU64())
- }
- }
- // Delete all bad blocks
- DeleteBadBlocks(db)
- badBlocks = ReadAllBadBlocks(db)
- if len(badBlocks) != 0 {
- t.Fatalf("Failed to delete bad blocks")
- }
- }
- // Tests block total difficulty storage and retrieval operations.
- func TestTdStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test TD to move around the database and make sure it's really new
- hash, td := common.Hash{}, big.NewInt(314)
- if entry := ReadTd(db, hash, 0); entry != nil {
- t.Fatalf("Non existent TD returned: %v", entry)
- }
- // Write and verify the TD in the database
- WriteTd(db, hash, 0, td)
- if entry := ReadTd(db, hash, 0); entry == nil {
- t.Fatalf("Stored TD not found")
- } else if entry.Cmp(td) != 0 {
- t.Fatalf("Retrieved TD mismatch: have %v, want %v", entry, td)
- }
- // Delete the TD and verify the execution
- DeleteTd(db, hash, 0)
- if entry := ReadTd(db, hash, 0); entry != nil {
- t.Fatalf("Deleted TD returned: %v", entry)
- }
- }
- // Tests that canonical numbers can be mapped to hashes and retrieved.
- func TestCanonicalMappingStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a test canonical number and assinged hash to move around
- hash, number := common.Hash{0: 0xff}, uint64(314)
- if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
- t.Fatalf("Non existent canonical mapping returned: %v", entry)
- }
- // Write and verify the TD in the database
- WriteCanonicalHash(db, hash, number)
- if entry := ReadCanonicalHash(db, number); entry == (common.Hash{}) {
- t.Fatalf("Stored canonical mapping not found")
- } else if entry != hash {
- t.Fatalf("Retrieved canonical mapping mismatch: have %v, want %v", entry, hash)
- }
- // Delete the TD and verify the execution
- DeleteCanonicalHash(db, number)
- if entry := ReadCanonicalHash(db, number); entry != (common.Hash{}) {
- t.Fatalf("Deleted canonical mapping returned: %v", entry)
- }
- }
- // Tests that head headers and head blocks can be assigned, individually.
- func TestHeadStorage(t *testing.T) {
- db := NewMemoryDatabase()
- blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")})
- blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")})
- blockFast := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block fast")})
- // Check that no head entries are in a pristine database
- if entry := ReadHeadHeaderHash(db); entry != (common.Hash{}) {
- t.Fatalf("Non head header entry returned: %v", entry)
- }
- if entry := ReadHeadBlockHash(db); entry != (common.Hash{}) {
- t.Fatalf("Non head block entry returned: %v", entry)
- }
- if entry := ReadHeadFastBlockHash(db); entry != (common.Hash{}) {
- t.Fatalf("Non fast head block entry returned: %v", entry)
- }
- // Assign separate entries for the head header and block
- WriteHeadHeaderHash(db, blockHead.Hash())
- WriteHeadBlockHash(db, blockFull.Hash())
- WriteHeadFastBlockHash(db, blockFast.Hash())
- // Check that both heads are present, and different (i.e. two heads maintained)
- if entry := ReadHeadHeaderHash(db); entry != blockHead.Hash() {
- t.Fatalf("Head header hash mismatch: have %v, want %v", entry, blockHead.Hash())
- }
- if entry := ReadHeadBlockHash(db); entry != blockFull.Hash() {
- t.Fatalf("Head block hash mismatch: have %v, want %v", entry, blockFull.Hash())
- }
- if entry := ReadHeadFastBlockHash(db); entry != blockFast.Hash() {
- t.Fatalf("Fast head block hash mismatch: have %v, want %v", entry, blockFast.Hash())
- }
- }
- // Tests that receipts associated with a single block can be stored and retrieved.
- func TestBlockReceiptStorage(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a live block since we need metadata to reconstruct the receipt
- tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
- tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)
- body := &types.Body{Transactions: types.Transactions{tx1, tx2}}
- // Create the two receipts to manage afterwards
- receipt1 := &types.Receipt{
- Status: types.ReceiptStatusFailed,
- CumulativeGasUsed: 1,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x11})},
- {Address: common.BytesToAddress([]byte{0x01, 0x11})},
- },
- TxHash: tx1.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
- GasUsed: 111111,
- }
- receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
- receipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- }
- receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
- receipts := []*types.Receipt{receipt1, receipt2}
- // Check that no receipt entries are in a pristine database
- hash := common.BytesToHash([]byte{0x03, 0x14})
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("non existent receipts returned: %v", rs)
- }
- // Insert the body that corresponds to the receipts
- WriteBody(db, hash, 0, body)
- // Insert the receipt slice into the database and check presence
- WriteReceipts(db, hash, 0, receipts)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
- t.Fatalf("no receipts returned")
- } else {
- if err := checkReceiptsRLP(rs, receipts); err != nil {
- t.Fatalf(err.Error())
- }
- // check that the raw data does not contain the quorumExtraData array (since the prepared receipts do not have any quorumExtraData)
- receiptData := ReadReceiptsRLP(db, hash, 0)
- _, extraData, err := rlp.SplitList(receiptData)
- assert.NoError(t, err)
- assert.Empty(t, extraData)
- }
- // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed)
- DeleteBody(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil {
- t.Fatalf("receipts returned when body was deleted: %v", rs)
- }
- // Ensure that receipts without metadata can be returned without the block body too
- if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil {
- t.Fatalf(err.Error())
- }
- // Sanity check that body alone without the receipt is a full purge
- WriteBody(db, hash, 0, body)
- DeleteReceipts(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("deleted receipts returned: %v", rs)
- }
- }
- func TestBlockReceiptStorageWithLegacyMPSV1EncodingWithMPSData(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a live block since we need metadata to reconstruct the receipt
- tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
- tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)
- tx2.SetPrivate()
- body := &types.Body{Transactions: types.Transactions{tx1, tx2}}
- // Create the two receipts to manage afterwards
- receipt1 := &types.Receipt{
- Status: types.ReceiptStatusFailed,
- CumulativeGasUsed: 1,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x11})},
- {Address: common.BytesToAddress([]byte{0x01, 0x11})},
- },
- TxHash: tx1.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
- GasUsed: 111111,
- }
- receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
- psiReceipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- }
- receipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- QuorumReceiptExtraData: types.QuorumReceiptExtraData{
- PSReceipts: map[types.PrivateStateIdentifier]*types.Receipt{types.PrivateStateIdentifier("psi1"): psiReceipt2},
- },
- }
- receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
- receipts := []*types.Receipt{receipt1, receipt2}
- // Check that no receipt entries are in a pristine database
- hash := common.BytesToHash([]byte{0x03, 0x14})
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("non existent receipts returned: %v", rs)
- }
- // Insert the body that corresponds to the receipts
- WriteBody(db, hash, 0, body)
- // Insert the receipt slice into the database and check presence
- WriteReceiptsMPSV1(db, hash, 0, receipts)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
- t.Fatalf("no receipts returned")
- } else {
- if err := checkReceiptsRLP(rs, receipts); err != nil {
- t.Fatalf(err.Error())
- }
- rec2 := rs[1]
- assert.Len(t, rec2.PSReceipts, 1)
- psRec2 := rec2.PSReceipts[types.PrivateStateIdentifier("psi1")]
- assert.NotNil(t, psRec2)
- }
- // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed)
- DeleteBody(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil {
- t.Fatalf("receipts returned when body was deleted: %v", rs)
- }
- // Ensure that receipts without metadata can be returned without the block body too
- if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil {
- t.Fatalf(err.Error())
- }
- }
- func TestBlockReceiptStorageWithLegacyMPSV1EncodingWithoutMPSData(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a live block since we need metadata to reconstruct the receipt
- tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
- tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)
- tx2.SetPrivate()
- body := &types.Body{Transactions: types.Transactions{tx1, tx2}}
- // Create the two receipts to manage afterwards
- receipt1 := &types.Receipt{
- Status: types.ReceiptStatusFailed,
- CumulativeGasUsed: 1,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x11})},
- {Address: common.BytesToAddress([]byte{0x01, 0x11})},
- },
- TxHash: tx1.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
- GasUsed: 111111,
- }
- receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
- receipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- }
- receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
- receipts := []*types.Receipt{receipt1, receipt2}
- // Check that no receipt entries are in a pristine database
- hash := common.BytesToHash([]byte{0x03, 0x14})
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("non existent receipts returned: %v", rs)
- }
- // Insert the body that corresponds to the receipts
- WriteBody(db, hash, 0, body)
- // Insert the receipt slice into the database and check presence
- WriteReceiptsMPSV1(db, hash, 0, receipts)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
- t.Fatalf("no receipts returned")
- } else {
- if err := checkReceiptsRLP(rs, receipts); err != nil {
- t.Fatalf(err.Error())
- }
- rec2 := rs[1]
- assert.Len(t, rec2.PSReceipts, 0)
- }
- // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed)
- DeleteBody(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil {
- t.Fatalf("receipts returned when body was deleted: %v", rs)
- }
- // Ensure that receipts without metadata can be returned without the block body too
- if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil {
- t.Fatalf(err.Error())
- }
- }
- // Tests that receipts associated with a single block can be stored and retrieved.
- func TestBlockReceiptStorageWithQuorumExtraData(t *testing.T) {
- db := NewMemoryDatabase()
- // Create a live block since we need metadata to reconstruct the receipt
- tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
- tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)
- tx2.SetPrivate()
- body := &types.Body{Transactions: types.Transactions{tx1, tx2}}
- // Create the two receipts to manage afterwards
- receipt1 := &types.Receipt{
- Status: types.ReceiptStatusFailed,
- CumulativeGasUsed: 1,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x11})},
- {Address: common.BytesToAddress([]byte{0x01, 0x11})},
- },
- TxHash: tx1.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
- GasUsed: 111111,
- }
- receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
- psiReceipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- QuorumReceiptExtraData: types.QuorumReceiptExtraData{
- RevertReason: []byte("arbitraryvalue"),
- },
- }
- receipt2 := &types.Receipt{
- PostState: common.Hash{2}.Bytes(),
- CumulativeGasUsed: 2,
- Logs: []*types.Log{
- {Address: common.BytesToAddress([]byte{0x22})},
- {Address: common.BytesToAddress([]byte{0x02, 0x22})},
- },
- TxHash: tx2.Hash(),
- ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
- GasUsed: 222222,
- QuorumReceiptExtraData: types.QuorumReceiptExtraData{
- RevertReason: []byte("arbitraryvalue"),
- PSReceipts: map[types.PrivateStateIdentifier]*types.Receipt{types.PrivateStateIdentifier("psi1"): psiReceipt2},
- },
- }
- receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
- receipts := []*types.Receipt{receipt1, receipt2}
- // Check that no receipt entries are in a pristine database
- hash := common.BytesToHash([]byte{0x03, 0x14})
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("non existent receipts returned: %v", rs)
- }
- // Insert the body that corresponds to the receipts
- WriteBody(db, hash, 0, body)
- // Insert the receipt slice into the database and check presence
- WriteReceipts(db, hash, 0, receipts)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 {
- t.Fatalf("no receipts returned")
- } else {
- if err := checkReceiptsRLP(rs, receipts); err != nil {
- t.Fatalf(err.Error())
- }
- rec2 := rs[1]
- assert.Len(t, rec2.PSReceipts, 1)
- assert.Equal(t, rec2.RevertReason, []byte("arbitraryvalue"))
- psRec2 := rec2.PSReceipts[types.PrivateStateIdentifier("psi1")]
- assert.Equal(t, psRec2.RevertReason, []byte("arbitraryvalue"))
- }
- // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed)
- DeleteBody(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil {
- t.Fatalf("receipts returned when body was deleted: %v", rs)
- }
- // Ensure that receipts without metadata can be returned without the block body too
- if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil {
- t.Fatalf(err.Error())
- }
- // Sanity check that body alone without the receipt is a full purge
- WriteBody(db, hash, 0, body)
- DeleteReceipts(db, hash, 0)
- if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 {
- t.Fatalf("deleted receipts returned: %v", rs)
- }
- }
- func checkReceiptsRLP(have, want types.Receipts) error {
- if len(have) != len(want) {
- return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want))
- }
- for i := 0; i < len(want); i++ {
- rlpHave, err := rlp.EncodeToBytes(have[i])
- if err != nil {
- return err
- }
- rlpWant, err := rlp.EncodeToBytes(want[i])
- if err != nil {
- return err
- }
- if !bytes.Equal(rlpHave, rlpWant) {
- return fmt.Errorf("receipt #%d: receipt mismatch: have %s, want %s", i, hex.EncodeToString(rlpHave), hex.EncodeToString(rlpWant))
- }
- }
- return nil
- }
- func TestAncientStorage(t *testing.T) {
- // Freezer style fast import the chain.
- frdir, err := ioutil.TempDir("", "")
- if err != nil {
- t.Fatalf("failed to create temp freezer dir: %v", err)
- }
- defer os.Remove(frdir)
- db, err := NewDatabaseWithFreezer(NewMemoryDatabase(), frdir, "", false)
- if err != nil {
- t.Fatalf("failed to create database with ancient backend")
- }
- // Create a test block
- block := types.NewBlockWithHeader(&types.Header{
- Number: big.NewInt(0),
- Extra: []byte("test block"),
- UncleHash: types.EmptyUncleHash,
- TxHash: types.EmptyRootHash,
- ReceiptHash: types.EmptyRootHash,
- })
- // Ensure nothing non-existent will be read
- hash, number := block.Hash(), block.NumberU64()
- if blob := ReadHeaderRLP(db, hash, number); len(blob) > 0 {
- t.Fatalf("non existent header returned")
- }
- if blob := ReadBodyRLP(db, hash, number); len(blob) > 0 {
- t.Fatalf("non existent body returned")
- }
- if blob := ReadReceiptsRLP(db, hash, number); len(blob) > 0 {
- t.Fatalf("non existent receipts returned")
- }
- if blob := ReadTdRLP(db, hash, number); len(blob) > 0 {
- t.Fatalf("non existent td returned")
- }
- // Write and verify the header in the database
- WriteAncientBlock(db, block, nil, big.NewInt(100))
- if blob := ReadHeaderRLP(db, hash, number); len(blob) == 0 {
- t.Fatalf("no header returned")
- }
- if blob := ReadBodyRLP(db, hash, number); len(blob) == 0 {
- t.Fatalf("no body returned")
- }
- if blob := ReadReceiptsRLP(db, hash, number); len(blob) == 0 {
- t.Fatalf("no receipts returned")
- }
- if blob := ReadTdRLP(db, hash, number); len(blob) == 0 {
- t.Fatalf("no td returned")
- }
- // Use a fake hash for data retrieval, nothing should be returned.
- fakeHash := common.BytesToHash([]byte{0x01, 0x02, 0x03})
- if blob := ReadHeaderRLP(db, fakeHash, number); len(blob) != 0 {
- t.Fatalf("invalid header returned")
- }
- if blob := ReadBodyRLP(db, fakeHash, number); len(blob) != 0 {
- t.Fatalf("invalid body returned")
- }
- if blob := ReadReceiptsRLP(db, fakeHash, number); len(blob) != 0 {
- t.Fatalf("invalid receipts returned")
- }
- if blob := ReadTdRLP(db, fakeHash, number); len(blob) != 0 {
- t.Fatalf("invalid td returned")
- }
- }
- func TestCanonicalHashIteration(t *testing.T) {
- var cases = []struct {
- from, to uint64
- limit int
- expect []uint64
- }{
- {1, 8, 0, nil},
- {1, 8, 1, []uint64{1}},
- {1, 8, 10, []uint64{1, 2, 3, 4, 5, 6, 7}},
- {1, 9, 10, []uint64{1, 2, 3, 4, 5, 6, 7, 8}},
- {2, 9, 10, []uint64{2, 3, 4, 5, 6, 7, 8}},
- {9, 10, 10, nil},
- }
- // Test empty db iteration
- db := NewMemoryDatabase()
- numbers, _ := ReadAllCanonicalHashes(db, 0, 10, 10)
- if len(numbers) != 0 {
- t.Fatalf("No entry should be returned to iterate an empty db")
- }
- // Fill database with testing data.
- for i := uint64(1); i <= 8; i++ {
- WriteCanonicalHash(db, common.Hash{}, i)
- WriteTd(db, common.Hash{}, i, big.NewInt(10)) // Write some interferential data
- }
- for i, c := range cases {
- numbers, _ := ReadAllCanonicalHashes(db, c.from, c.to, c.limit)
- if !reflect.DeepEqual(numbers, c.expect) {
- t.Fatalf("Case %d failed, want %v, got %v", i, c.expect, numbers)
- }
- }
- }
- func WriteReceiptsMPSV1(db ethdb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) {
- // Convert the receipts into their storage form and serialize them
- storageReceipts := make([]*types.ReceiptForStorageMPSV1, len(receipts))
- for i, receipt := range receipts {
- storageReceipts[i] = (*types.ReceiptForStorageMPSV1)(receipt)
- }
- bytes, err := rlp.EncodeToBytes(storageReceipts)
- if err != nil {
- log.Crit("Failed to encode block receipts", "err", err)
- }
- // Store the flattened receipt slice
- if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
- log.Crit("Failed to store block receipts", "err", err)
- }
- }
|