stress_ethash.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 []*node.Node
  57. enodes []*enode.Node
  58. )
  59. for i := 0; i < 4; i++ {
  60. // Start the node and wait until it's up
  61. node, err := makeMiner(genesis)
  62. if err != nil {
  63. panic(err)
  64. }
  65. defer node.Close()
  66. for node.Server().NodeInfo().Ports.Listener == 0 {
  67. time.Sleep(250 * time.Millisecond)
  68. }
  69. // Connect the node to al the previous ones
  70. for _, n := range enodes {
  71. node.Server().AddPeer(n)
  72. }
  73. // Start tracking the node and it's enode
  74. nodes = append(nodes, node)
  75. enodes = append(enodes, node.Server().Self())
  76. // Inject the signer key and start sealing with it
  77. store := node.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 signing with them
  83. time.Sleep(3 * time.Second)
  84. for _, node := range nodes {
  85. var ethereum *eth.Ethereum
  86. if err := node.Service(&ethereum); err != nil {
  87. panic(err)
  88. }
  89. if err := ethereum.StartMining(1); err != nil {
  90. panic(err)
  91. }
  92. }
  93. time.Sleep(3 * time.Second)
  94. // Start injecting transactions from the faucets like crazy
  95. nonces := make([]uint64, len(faucets))
  96. for {
  97. index := rand.Intn(len(faucets))
  98. // Fetch the accessor for the relevant signer
  99. var ethereum *eth.Ethereum
  100. if err := nodes[index%len(nodes)].Service(&ethereum); err != nil {
  101. panic(err)
  102. }
  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 := ethereum.TxPool().AddLocal(tx); err != nil {
  109. panic(err)
  110. }
  111. nonces[index]++
  112. // Wait if we're too saturated
  113. if pend, _ := ethereum.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, error) {
  135. // Define the basic configurations for the Ethereum node
  136. datadir, _ := ioutil.TempDir("", "")
  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. NoUSB: true,
  147. UseLightweightKDF: true,
  148. }
  149. // Start the node and configure a full Ethereum node on it
  150. stack, err := node.New(config)
  151. if err != nil {
  152. return nil, err
  153. }
  154. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  155. return eth.New(ctx, &eth.Config{
  156. Genesis: genesis,
  157. NetworkId: genesis.Config.ChainID.Uint64(),
  158. SyncMode: downloader.FullSync,
  159. DatabaseCache: 256,
  160. DatabaseHandles: 256,
  161. TxPool: core.DefaultTxPoolConfig,
  162. GPO: eth.DefaultConfig.GPO,
  163. Ethash: eth.DefaultConfig.Ethash,
  164. Miner: miner.Config{
  165. GasFloor: genesis.GasLimit * 9 / 10,
  166. GasCeil: genesis.GasLimit * 11 / 10,
  167. GasPrice: big.NewInt(1),
  168. Recommit: time.Second,
  169. },
  170. })
  171. }); err != nil {
  172. return nil, err
  173. }
  174. // Start the node and return if successful
  175. return stack, stack.Start()
  176. }