github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/les/catalyst/api_test.go (about) 1 // Copyright 2022 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 catalyst 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethxdao/go-ethereum/consensus/ethash" 24 "github.com/ethxdao/go-ethereum/core" 25 //"github.com/ethxdao/go-ethereum/core/beacon" 26 "github.com/ethxdao/go-ethereum/core/rawdb" 27 "github.com/ethxdao/go-ethereum/core/types" 28 "github.com/ethxdao/go-ethereum/crypto" 29 "github.com/ethxdao/go-ethereum/eth/downloader" 30 "github.com/ethxdao/go-ethereum/eth/ethconfig" 31 "github.com/ethxdao/go-ethereum/les" 32 "github.com/ethxdao/go-ethereum/node" 33 "github.com/ethxdao/go-ethereum/params" 34 ) 35 36 var ( 37 // testKey is a private key to use for funding a tester account. 38 testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 39 40 // testAddr is the Ethereum address of the tester account. 41 testAddr = crypto.PubkeyToAddress(testKey.PublicKey) 42 43 testBalance = big.NewInt(2e18) 44 ) 45 46 func generatePreMergeChain(n int) (*core.Genesis, []*types.Header, []*types.Block) { 47 db := rawdb.NewMemoryDatabase() 48 config := params.AllEthashProtocolChanges 49 genesis := &core.Genesis{ 50 Config: config, 51 Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, 52 ExtraData: []byte("test genesis"), 53 Timestamp: 9000, 54 BaseFee: big.NewInt(params.InitialBaseFee), 55 } 56 gblock := genesis.MustCommit(db) 57 engine := ethash.NewFaker() 58 blocks, _ := core.GenerateChain(config, gblock, engine, db, n, nil) 59 totalDifficulty := big.NewInt(0) 60 61 var headers []*types.Header 62 for _, b := range blocks { 63 totalDifficulty.Add(totalDifficulty, b.Difficulty()) 64 headers = append(headers, b.Header()) 65 } 66 config.TerminalTotalDifficulty = totalDifficulty 67 68 return genesis, headers, blocks 69 } 70 71 func TestEth2DeepReorg(t *testing.T) { 72 // TODO (MariusVanDerWijden) TestEth2DeepReorg is currently broken, because it tries to reorg 73 // before the totalTerminalDifficulty threshold 74 /* 75 genesis, preMergeBlocks := generatePreMergeChain(core.TriesInMemory * 2) 76 n, ethservice := startEthService(t, genesis, preMergeBlocks) 77 defer n.Close() 78 79 var ( 80 api = NewConsensusAPI(ethservice, nil) 81 parent = preMergeBlocks[len(preMergeBlocks)-core.TriesInMemory-1] 82 head = ethservice.BlockChain().CurrentBlock().NumberU64() 83 ) 84 if ethservice.BlockChain().HasBlockAndState(parent.Hash(), parent.NumberU64()) { 85 t.Errorf("Block %d not pruned", parent.NumberU64()) 86 } 87 for i := 0; i < 10; i++ { 88 execData, err := api.assembleBlock(AssembleBlockParams{ 89 ParentHash: parent.Hash(), 90 Timestamp: parent.Time() + 5, 91 }) 92 if err != nil { 93 t.Fatalf("Failed to create the executable data %v", err) 94 } 95 block, err := ExecutableDataToBlock(ethservice.BlockChain().Config(), parent.Header(), *execData) 96 if err != nil { 97 t.Fatalf("Failed to convert executable data to block %v", err) 98 } 99 newResp, err := api.ExecutePayload(*execData) 100 if err != nil || newResp.Status != "VALID" { 101 t.Fatalf("Failed to insert block: %v", err) 102 } 103 if ethservice.BlockChain().CurrentBlock().NumberU64() != head { 104 t.Fatalf("Chain head shouldn't be updated") 105 } 106 if err := api.setCanonical(block.Hash()); err != nil { 107 t.Fatalf("Failed to set head: %v", err) 108 } 109 if ethservice.BlockChain().CurrentBlock().NumberU64() != block.NumberU64() { 110 t.Fatalf("Chain head should be updated") 111 } 112 parent, head = block, block.NumberU64() 113 } 114 */ 115 } 116 117 // startEthService creates a full node instance for testing. 118 func startLesService(t *testing.T, genesis *core.Genesis, headers []*types.Header) (*node.Node, *les.LightEthereum) { 119 t.Helper() 120 121 n, err := node.New(&node.Config{}) 122 if err != nil { 123 t.Fatal("can't create node:", err) 124 } 125 ethcfg := ðconfig.Config{ 126 Genesis: genesis, 127 Ethash: ethash.Config{PowMode: ethash.ModeFake}, 128 SyncMode: downloader.LightSync, 129 TrieDirtyCache: 256, 130 TrieCleanCache: 256, 131 LightPeers: 10, 132 } 133 lesService, err := les.New(n, ethcfg) 134 if err != nil { 135 t.Fatal("can't create eth service:", err) 136 } 137 if err := n.Start(); err != nil { 138 t.Fatal("can't start node:", err) 139 } 140 if _, err := lesService.BlockChain().InsertHeaderChain(headers, 0); err != nil { 141 n.Close() 142 t.Fatal("can't import test headers:", err) 143 } 144 return n, lesService 145 } 146 147 func encodeTransactions(txs []*types.Transaction) [][]byte { 148 var enc = make([][]byte, len(txs)) 149 for i, tx := range txs { 150 enc[i], _ = tx.MarshalBinary() 151 } 152 return enc 153 }