github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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 "math/big" 23 "math/rand" 24 "os" 25 "os/signal" 26 "time" 27 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/common/fdlimit" 30 "github.com/ethereum/go-ethereum/consensus/ethash" 31 "github.com/ethereum/go-ethereum/core" 32 "github.com/ethereum/go-ethereum/core/types" 33 "github.com/ethereum/go-ethereum/crypto" 34 "github.com/ethereum/go-ethereum/eth" 35 "github.com/ethereum/go-ethereum/eth/downloader" 36 "github.com/ethereum/go-ethereum/eth/ethconfig" 37 "github.com/ethereum/go-ethereum/log" 38 "github.com/ethereum/go-ethereum/miner" 39 "github.com/ethereum/go-ethereum/node" 40 "github.com/ethereum/go-ethereum/p2p" 41 "github.com/ethereum/go-ethereum/p2p/enode" 42 "github.com/ethereum/go-ethereum/params" 43 ) 44 45 func main() { 46 log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) 47 fdlimit.Raise(2048) 48 49 // Generate a batch of accounts to seal and fund with 50 faucets := make([]*ecdsa.PrivateKey, 128) 51 for i := 0; i < len(faucets); i++ { 52 faucets[i], _ = crypto.GenerateKey() 53 } 54 // Pre-generate the ethash mining DAG so we don't race 55 ethash.MakeDataset(1, ethconfig.Defaults.Ethash.DatasetDir) 56 57 // Create an Ethash network based off of the Ropsten config 58 genesis := makeGenesis(faucets) 59 60 // Handle interrupts. 61 interruptCh := make(chan os.Signal, 5) 62 signal.Notify(interruptCh, os.Interrupt) 63 64 var ( 65 stacks []*node.Node 66 nodes []*eth.Ethereum 67 enodes []*enode.Node 68 ) 69 for i := 0; i < 4; i++ { 70 // Start the node and wait until it's up 71 stack, ethBackend, err := makeMiner(genesis) 72 if err != nil { 73 panic(err) 74 } 75 defer stack.Close() 76 77 for stack.Server().NodeInfo().Ports.Listener == 0 { 78 time.Sleep(250 * time.Millisecond) 79 } 80 // Connect the node to all the previous ones 81 for _, n := range enodes { 82 stack.Server().AddPeer(n) 83 } 84 // Start tracking the node and its enode 85 stacks = append(stacks, stack) 86 nodes = append(nodes, ethBackend) 87 enodes = append(enodes, stack.Server().Self()) 88 } 89 90 // Iterate over all the nodes and start mining 91 time.Sleep(3 * time.Second) 92 for _, node := range nodes { 93 if err := node.StartMining(1); err != nil { 94 panic(err) 95 } 96 } 97 time.Sleep(3 * time.Second) 98 99 // Start injecting transactions from the faucets like crazy 100 nonces := make([]uint64, len(faucets)) 101 for { 102 // Stop when interrupted. 103 select { 104 case <-interruptCh: 105 for _, node := range stacks { 106 node.Close() 107 } 108 return 109 default: 110 } 111 112 // Pick a random mining node 113 index := rand.Intn(len(faucets)) 114 backend := nodes[index%len(nodes)] 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 := backend.TxPool().AddLocal(tx); err != nil { 122 panic(err) 123 } 124 nonces[index]++ 125 126 // Wait if we're too saturated 127 if pend, _ := backend.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, *eth.Ethereum, error) { 153 // Define the basic configurations for the Ethereum node 154 datadir, _ := os.MkdirTemp("", "") 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 UseLightweightKDF: true, 166 } 167 // Create the node and configure a full Ethereum node on it 168 stack, err := node.New(config) 169 if err != nil { 170 return nil, nil, err 171 } 172 ethBackend, err := eth.New(stack, ðconfig.Config{ 173 Genesis: genesis, 174 NetworkId: genesis.Config.ChainID.Uint64(), 175 SyncMode: downloader.FullSync, 176 DatabaseCache: 256, 177 DatabaseHandles: 256, 178 TxPool: core.DefaultTxPoolConfig, 179 GPO: ethconfig.Defaults.GPO, 180 Ethash: ethconfig.Defaults.Ethash, 181 Miner: miner.Config{ 182 Etherbase: common.Address{1}, 183 GasCeil: genesis.GasLimit * 11 / 10, 184 GasPrice: big.NewInt(1), 185 Recommit: time.Second, 186 }, 187 }) 188 if err != nil { 189 return nil, nil, err 190 } 191 192 err = stack.Start() 193 return stack, ethBackend, err 194 }