main.go 5.6 KB

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