stress_ethash.go 5.8 KB

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