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