github.com/ethereum/go-ethereum@v1.16.1/cmd/utils/history_test.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package utils 18 19 import ( 20 "bytes" 21 "crypto/sha256" 22 "io" 23 "math/big" 24 "os" 25 "path/filepath" 26 "strings" 27 "testing" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/consensus/ethash" 31 "github.com/ethereum/go-ethereum/core" 32 "github.com/ethereum/go-ethereum/core/rawdb" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/crypto" 35 "github.com/ethereum/go-ethereum/internal/era" 36 "github.com/ethereum/go-ethereum/params" 37 "github.com/ethereum/go-ethereum/trie" 38 "github.com/ethereum/go-ethereum/triedb" 39 ) 40 41 var ( 42 count uint64 = 128 43 step uint64 = 16 44 ) 45 46 func TestHistoryImportAndExport(t *testing.T) { 47 var ( 48 key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 49 address = crypto.PubkeyToAddress(key.PublicKey) 50 genesis = &core.Genesis{ 51 Config: params.TestChainConfig, 52 Alloc: types.GenesisAlloc{address: {Balance: big.NewInt(1000000000000000000)}}, 53 } 54 signer = types.LatestSigner(genesis.Config) 55 ) 56 57 // Generate chain. 58 db, blocks, _ := core.GenerateChainWithGenesis(genesis, ethash.NewFaker(), int(count), func(i int, g *core.BlockGen) { 59 if i == 0 { 60 return 61 } 62 tx, err := types.SignNewTx(key, signer, &types.DynamicFeeTx{ 63 ChainID: genesis.Config.ChainID, 64 Nonce: uint64(i - 1), 65 GasTipCap: common.Big0, 66 GasFeeCap: g.PrevBlock(0).BaseFee(), 67 Gas: 50000, 68 To: &common.Address{0xaa}, 69 Value: big.NewInt(int64(i)), 70 Data: nil, 71 AccessList: nil, 72 }) 73 if err != nil { 74 t.Fatalf("error creating tx: %v", err) 75 } 76 g.AddTx(tx) 77 }) 78 79 // Initialize BlockChain. 80 chain, err := core.NewBlockChain(db, genesis, ethash.NewFaker(), nil) 81 if err != nil { 82 t.Fatalf("unable to initialize chain: %v", err) 83 } 84 if _, err := chain.InsertChain(blocks); err != nil { 85 t.Fatalf("error inserting chain: %v", err) 86 } 87 88 // Make temp directory for era files. 89 dir := t.TempDir() 90 91 // Export history to temp directory. 92 if err := ExportHistory(chain, dir, 0, count, step); err != nil { 93 t.Fatalf("error exporting history: %v", err) 94 } 95 96 // Read checksums. 97 b, err := os.ReadFile(filepath.Join(dir, "checksums.txt")) 98 if err != nil { 99 t.Fatalf("failed to read checksums: %v", err) 100 } 101 checksums := strings.Split(string(b), "\n") 102 103 // Verify each Era. 104 entries, _ := era.ReadDir(dir, "mainnet") 105 for i, filename := range entries { 106 func() { 107 f, err := os.Open(filepath.Join(dir, filename)) 108 if err != nil { 109 t.Fatalf("error opening era file: %v", err) 110 } 111 var ( 112 h = sha256.New() 113 buf = bytes.NewBuffer(nil) 114 ) 115 if _, err := io.Copy(h, f); err != nil { 116 t.Fatalf("unable to recalculate checksum: %v", err) 117 } 118 if got, want := common.BytesToHash(h.Sum(buf.Bytes()[:])).Hex(), checksums[i]; got != want { 119 t.Fatalf("checksum %d does not match: got %s, want %s", i, got, want) 120 } 121 e, err := era.From(f) 122 if err != nil { 123 t.Fatalf("error opening era: %v", err) 124 } 125 defer e.Close() 126 it, err := era.NewIterator(e) 127 if err != nil { 128 t.Fatalf("error making era reader: %v", err) 129 } 130 for j := 0; it.Next(); j++ { 131 n := i*int(step) + j 132 if it.Error() != nil { 133 t.Fatalf("error reading block entry %d: %v", n, it.Error()) 134 } 135 block, receipts, err := it.BlockAndReceipts() 136 if err != nil { 137 t.Fatalf("error reading block entry %d: %v", n, err) 138 } 139 want := chain.GetBlockByNumber(uint64(n)) 140 if want, got := uint64(n), block.NumberU64(); want != got { 141 t.Fatalf("blocks out of order: want %d, got %d", want, got) 142 } 143 if want.Hash() != block.Hash() { 144 t.Fatalf("block hash mismatch %d: want %s, got %s", n, want.Hash().Hex(), block.Hash().Hex()) 145 } 146 if got := types.DeriveSha(block.Transactions(), trie.NewStackTrie(nil)); got != want.TxHash() { 147 t.Fatalf("tx hash %d mismatch: want %s, got %s", n, want.TxHash(), got) 148 } 149 if got := types.CalcUncleHash(block.Uncles()); got != want.UncleHash() { 150 t.Fatalf("uncle hash %d mismatch: want %s, got %s", n, want.UncleHash(), got) 151 } 152 if got := types.DeriveSha(receipts, trie.NewStackTrie(nil)); got != want.ReceiptHash() { 153 t.Fatalf("receipt root %d mismatch: want %s, got %s", n, want.ReceiptHash(), got) 154 } 155 } 156 }() 157 } 158 159 // Now import Era. 160 db2, err := rawdb.Open(rawdb.NewMemoryDatabase(), rawdb.OpenOptions{}) 161 if err != nil { 162 panic(err) 163 } 164 t.Cleanup(func() { 165 db2.Close() 166 }) 167 168 genesis.MustCommit(db2, triedb.NewDatabase(db2, triedb.HashDefaults)) 169 imported, err := core.NewBlockChain(db2, genesis, ethash.NewFaker(), nil) 170 if err != nil { 171 t.Fatalf("unable to initialize chain: %v", err) 172 } 173 if err := ImportHistory(imported, dir, "mainnet"); err != nil { 174 t.Fatalf("failed to import chain: %v", err) 175 } 176 if have, want := imported.CurrentHeader(), chain.CurrentHeader(); have.Hash() != want.Hash() { 177 t.Fatalf("imported chain does not match expected, have (%d, %s) want (%d, %s)", have.Number, have.Hash(), want.Number, want.Hash()) 178 } 179 }