gitee.com/liu-zhao234568/cntest@v1.0.0/miner/stress/ethash/main.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 // This file contains a miner stress test based on the Ethash consensus engine. 18 package main 19 20 import ( 21 "crypto/ecdsa" 22 "io/ioutil" 23 "math/big" 24 "math/rand" 25 "os" 26 "path/filepath" 27 "time" 28 29 "gitee.com/liu-zhao234568/cntest/accounts/keystore" 30 "gitee.com/liu-zhao234568/cntest/common" 31 "gitee.com/liu-zhao234568/cntest/common/fdlimit" 32 "gitee.com/liu-zhao234568/cntest/consensus/ethash" 33 "gitee.com/liu-zhao234568/cntest/core" 34 "gitee.com/liu-zhao234568/cntest/core/types" 35 "gitee.com/liu-zhao234568/cntest/crypto" 36 "gitee.com/liu-zhao234568/cntest/eth" 37 "gitee.com/liu-zhao234568/cntest/eth/downloader" 38 "gitee.com/liu-zhao234568/cntest/eth/ethconfig" 39 "gitee.com/liu-zhao234568/cntest/log" 40 "gitee.com/liu-zhao234568/cntest/miner" 41 "gitee.com/liu-zhao234568/cntest/node" 42 "gitee.com/liu-zhao234568/cntest/p2p" 43 "gitee.com/liu-zhao234568/cntest/p2p/enode" 44 "gitee.com/liu-zhao234568/cntest/params" 45 ) 46 47 func main() { 48 log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) 49 fdlimit.Raise(2048) 50 51 // Generate a batch of accounts to seal and fund with 52 faucets := make([]*ecdsa.PrivateKey, 128) 53 for i := 0; i < len(faucets); i++ { 54 faucets[i], _ = crypto.GenerateKey() 55 } 56 // Pre-generate the ethash mining DAG so we don't race 57 ethash.MakeDataset(1, filepath.Join(os.Getenv("HOME"), ".ethash")) 58 59 // Create an Ethash network based off of the Ropsten config 60 genesis := makeGenesis(faucets) 61 62 var ( 63 nodes []*eth.Ethereum 64 enodes []*enode.Node 65 ) 66 for i := 0; i < 4; i++ { 67 // Start the node and wait until it's up 68 stack, ethBackend, err := makeMiner(genesis) 69 if err != nil { 70 panic(err) 71 } 72 defer stack.Close() 73 74 for stack.Server().NodeInfo().Ports.Listener == 0 { 75 time.Sleep(250 * time.Millisecond) 76 } 77 // Connect the node to all the previous ones 78 for _, n := range enodes { 79 stack.Server().AddPeer(n) 80 } 81 // Start tracking the node and its enode 82 nodes = append(nodes, ethBackend) 83 enodes = append(enodes, stack.Server().Self()) 84 85 // Inject the signer key and start sealing with it 86 store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 87 if _, err := store.NewAccount(""); err != nil { 88 panic(err) 89 } 90 } 91 92 // Iterate over all the nodes and start mining 93 time.Sleep(3 * time.Second) 94 for _, node := range nodes { 95 if err := node.StartMining(1); err != nil { 96 panic(err) 97 } 98 } 99 time.Sleep(3 * time.Second) 100 101 // Start injecting transactions from the faucets like crazy 102 nonces := make([]uint64, len(faucets)) 103 for { 104 // Pick a random mining node 105 index := rand.Intn(len(faucets)) 106 backend := nodes[index%len(nodes)] 107 108 // Create a self transaction and inject into the pool 109 tx, err := types.SignTx(types.NewTransaction(nonces[index], crypto.PubkeyToAddress(faucets[index].PublicKey), new(big.Int), 21000, big.NewInt(100000000000+rand.Int63n(65536)), nil), types.HomesteadSigner{}, faucets[index]) 110 if err != nil { 111 panic(err) 112 } 113 if err := backend.TxPool().AddLocal(tx); err != nil { 114 panic(err) 115 } 116 nonces[index]++ 117 118 // Wait if we're too saturated 119 if pend, _ := backend.TxPool().Stats(); pend > 2048 { 120 time.Sleep(100 * time.Millisecond) 121 } 122 } 123 } 124 125 // makeGenesis creates a custom Ethash genesis block based on some pre-defined 126 // faucet accounts. 127 func makeGenesis(faucets []*ecdsa.PrivateKey) *core.Genesis { 128 genesis := core.DefaultRopstenGenesisBlock() 129 genesis.Difficulty = params.MinimumDifficulty 130 genesis.GasLimit = 25000000 131 132 genesis.Config.ChainID = big.NewInt(18) 133 genesis.Config.EIP150Hash = common.Hash{} 134 135 genesis.Alloc = core.GenesisAlloc{} 136 for _, faucet := range faucets { 137 genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{ 138 Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil), 139 } 140 } 141 return genesis 142 } 143 144 func makeMiner(genesis *core.Genesis) (*node.Node, *eth.Ethereum, error) { 145 // Define the basic configurations for the Ethereum node 146 datadir, _ := ioutil.TempDir("", "") 147 148 config := &node.Config{ 149 Name: "geth", 150 Version: params.Version, 151 DataDir: datadir, 152 P2P: p2p.Config{ 153 ListenAddr: "0.0.0.0:0", 154 NoDiscovery: true, 155 MaxPeers: 25, 156 }, 157 UseLightweightKDF: true, 158 } 159 // Create the node and configure a full Ethereum node on it 160 stack, err := node.New(config) 161 if err != nil { 162 return nil, nil, err 163 } 164 ethBackend, err := eth.New(stack, ðconfig.Config{ 165 Genesis: genesis, 166 NetworkId: genesis.Config.ChainID.Uint64(), 167 SyncMode: downloader.FullSync, 168 DatabaseCache: 256, 169 DatabaseHandles: 256, 170 TxPool: core.DefaultTxPoolConfig, 171 GPO: ethconfig.Defaults.GPO, 172 Ethash: ethconfig.Defaults.Ethash, 173 Miner: miner.Config{ 174 GasFloor: genesis.GasLimit * 9 / 10, 175 GasCeil: genesis.GasLimit * 11 / 10, 176 GasPrice: big.NewInt(1), 177 Recommit: time.Second, 178 }, 179 }) 180 if err != nil { 181 return nil, nil, err 182 } 183 184 err = stack.Start() 185 return stack, ethBackend, err 186 }