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