github.com/matthieu/go-ethereum@v1.13.2/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/matthieu/go-ethereum/accounts/keystore" 32 "github.com/matthieu/go-ethereum/common" 33 "github.com/matthieu/go-ethereum/common/fdlimit" 34 "github.com/matthieu/go-ethereum/consensus/ethash" 35 "github.com/matthieu/go-ethereum/core" 36 "github.com/matthieu/go-ethereum/core/types" 37 "github.com/matthieu/go-ethereum/crypto" 38 "github.com/matthieu/go-ethereum/eth" 39 "github.com/matthieu/go-ethereum/eth/downloader" 40 "github.com/matthieu/go-ethereum/log" 41 "github.com/matthieu/go-ethereum/miner" 42 "github.com/matthieu/go-ethereum/node" 43 "github.com/matthieu/go-ethereum/p2p" 44 "github.com/matthieu/go-ethereum/p2p/enode" 45 "github.com/matthieu/go-ethereum/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 []*node.Node 65 enodes []*enode.Node 66 ) 67 for i := 0; i < 4; i++ { 68 // Start the node and wait until it's up 69 node, err := makeMiner(genesis) 70 if err != nil { 71 panic(err) 72 } 73 defer node.Close() 74 75 for node.Server().NodeInfo().Ports.Listener == 0 { 76 time.Sleep(250 * time.Millisecond) 77 } 78 // Connect the node to al the previous ones 79 for _, n := range enodes { 80 node.Server().AddPeer(n) 81 } 82 // Start tracking the node and it's enode 83 nodes = append(nodes, node) 84 enodes = append(enodes, node.Server().Self()) 85 86 // Inject the signer key and start sealing with it 87 store := node.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 88 if _, err := store.NewAccount(""); err != nil { 89 panic(err) 90 } 91 } 92 // Iterate over all the nodes and start signing with them 93 time.Sleep(3 * time.Second) 94 95 for _, node := range nodes { 96 var ethereum *eth.Ethereum 97 if err := node.Service(ðereum); err != nil { 98 panic(err) 99 } 100 if err := ethereum.StartMining(1); err != nil { 101 panic(err) 102 } 103 } 104 time.Sleep(3 * time.Second) 105 106 // Start injecting transactions from the faucets like crazy 107 nonces := make([]uint64, len(faucets)) 108 for { 109 index := rand.Intn(len(faucets)) 110 111 // Fetch the accessor for the relevant signer 112 var ethereum *eth.Ethereum 113 if err := nodes[index%len(nodes)].Service(ðereum); err != nil { 114 panic(err) 115 } 116 // Create a self transaction and inject into the pool 117 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]) 118 if err != nil { 119 panic(err) 120 } 121 if err := ethereum.TxPool().AddLocal(tx); err != nil { 122 panic(err) 123 } 124 nonces[index]++ 125 126 // Wait if we're too saturated 127 if pend, _ := ethereum.TxPool().Stats(); pend > 2048 { 128 time.Sleep(100 * time.Millisecond) 129 } 130 } 131 } 132 133 // makeGenesis creates a custom Ethash genesis block based on some pre-defined 134 // faucet accounts. 135 func makeGenesis(faucets []*ecdsa.PrivateKey) *core.Genesis { 136 genesis := core.DefaultRopstenGenesisBlock() 137 genesis.Difficulty = params.MinimumDifficulty 138 genesis.GasLimit = 25000000 139 140 genesis.Config.ChainID = big.NewInt(18) 141 genesis.Config.EIP150Hash = common.Hash{} 142 143 genesis.Alloc = core.GenesisAlloc{} 144 for _, faucet := range faucets { 145 genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{ 146 Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil), 147 } 148 } 149 return genesis 150 } 151 152 func makeMiner(genesis *core.Genesis) (*node.Node, error) { 153 // Define the basic configurations for the Ethereum node 154 datadir, _ := ioutil.TempDir("", "") 155 156 config := &node.Config{ 157 Name: "geth", 158 Version: params.Version, 159 DataDir: datadir, 160 P2P: p2p.Config{ 161 ListenAddr: "0.0.0.0:0", 162 NoDiscovery: true, 163 MaxPeers: 25, 164 }, 165 NoUSB: true, 166 UseLightweightKDF: true, 167 } 168 // Start the node and configure a full Ethereum node on it 169 stack, err := node.New(config) 170 if err != nil { 171 return nil, err 172 } 173 if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { 174 return eth.New(ctx, ð.Config{ 175 Genesis: genesis, 176 NetworkId: genesis.Config.ChainID.Uint64(), 177 SyncMode: downloader.FullSync, 178 DatabaseCache: 256, 179 DatabaseHandles: 256, 180 TxPool: core.DefaultTxPoolConfig, 181 GPO: eth.DefaultConfig.GPO, 182 Ethash: eth.DefaultConfig.Ethash, 183 Miner: miner.Config{ 184 GasFloor: genesis.GasLimit * 9 / 10, 185 GasCeil: genesis.GasLimit * 11 / 10, 186 GasPrice: big.NewInt(1), 187 Recommit: time.Second, 188 }, 189 }) 190 }); err != nil { 191 return nil, err 192 } 193 // Start the node and return if successful 194 return stack, stack.Start() 195 }