gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/core/chain_makers_test.go (about) 1 // Copyright 2019 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain 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 aquachain 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 aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "gitlab.com/aquachain/aquachain/aquadb" 21 "gitlab.com/aquachain/aquachain/common" 22 "gitlab.com/aquachain/aquachain/consensus" 23 "gitlab.com/aquachain/aquachain/core/types" 24 "gitlab.com/aquachain/aquachain/core/vm" 25 "gitlab.com/aquachain/aquachain/params" 26 ) 27 28 // So we can deterministically seed different blockchains 29 const ( 30 canonicalSeed = 1 31 forkSeed = 2 32 ) 33 34 // newCanonical creates a chain database, and injects a deterministic canonical 35 // chain. Depending on the full flag, if creates either a full block chain or a 36 // header only chain. 37 func newCanonical(engine consensus.Engine, n int, full bool) (aquadb.Database, *BlockChain, error) { 38 // Initialize a fresh chain with only a genesis block 39 gspec := new(Genesis) 40 db := aquadb.NewMemDatabase() 41 genesis := gspec.MustCommit(db) 42 43 blockchain, _ := NewBlockChain(db, nil, params.AllAquahashProtocolChanges, engine, vm.Config{}) 44 // Create and inject the requested chain 45 if n == 0 { 46 return db, blockchain, nil 47 } 48 if full { 49 // Full block-chain requested 50 blocks := makeBlockChain(genesis, n, engine, db, canonicalSeed) 51 _, err := blockchain.InsertChain(blocks) 52 return db, blockchain, err 53 } 54 // Header-only chain requested 55 headers := makeHeaderChain(genesis.Header(), n, engine, db, canonicalSeed) 56 _, err := blockchain.InsertHeaderChain(headers, 1) 57 return db, blockchain, err 58 } 59 60 // makeHeaderChain creates a deterministic chain of headers rooted at parent. 61 func makeHeaderChain(parent *types.Header, n int, engine consensus.Engine, db aquadb.Database, seed int) []*types.Header { 62 blocks := makeBlockChain(types.NewBlockWithHeader(parent), n, engine, db, seed) 63 headers := make([]*types.Header, len(blocks)) 64 for i, block := range blocks { 65 headers[i] = block.Header() 66 headers[i].Version = params.TestChainConfig.GetBlockVersion(headers[i].Number) 67 } 68 return headers 69 } 70 71 // makeBlockChain creates a deterministic chain of blocks rooted at parent. 72 func makeBlockChain(parent *types.Block, n int, engine consensus.Engine, db aquadb.Database, seed int) []*types.Block { 73 blocks, _ := GenerateChain(params.TestChainConfig, parent, engine, db, n, func(i int, b *BlockGen) { 74 b.header.Version = b.config.GetBlockVersion(b.Number()) 75 b.SetCoinbase(common.Address{0: byte(seed), 19: byte(i)}) 76 }) 77 return blocks 78 }