stress_ethash.go 5.6 KB

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