stress_ethash.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "fmt"
  22. "io/ioutil"
  23. "math/big"
  24. "math/rand"
  25. "os"
  26. "path/filepath"
  27. "time"
  28. "github.com/ethereum/go-ethereum/accounts/keystore"
  29. "github.com/ethereum/go-ethereum/common"
  30. "github.com/ethereum/go-ethereum/common/fdlimit"
  31. "github.com/ethereum/go-ethereum/consensus/ethash"
  32. "github.com/ethereum/go-ethereum/core"
  33. "github.com/ethereum/go-ethereum/core/types"
  34. "github.com/ethereum/go-ethereum/crypto"
  35. "github.com/ethereum/go-ethereum/eth"
  36. "github.com/ethereum/go-ethereum/eth/downloader"
  37. "github.com/ethereum/go-ethereum/log"
  38. "github.com/ethereum/go-ethereum/node"
  39. "github.com/ethereum/go-ethereum/p2p"
  40. "github.com/ethereum/go-ethereum/p2p/discover"
  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 []string
  58. )
  59. for i := 0; i < 4; i++ {
  60. // Start the node and wait until it's up
  61. node, err := makeMiner(genesis, enodes)
  62. if err != nil {
  63. panic(err)
  64. }
  65. defer node.Stop()
  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 _, enode := range enodes {
  71. enode, err := discover.ParseNode(enode)
  72. if err != nil {
  73. panic(err)
  74. }
  75. node.Server().AddPeer(enode)
  76. }
  77. // Start tracking the node and it's enode url
  78. nodes = append(nodes, node)
  79. enode := fmt.Sprintf("enode://%s@127.0.0.1:%d", node.Server().NodeInfo().ID, node.Server().NodeInfo().Ports.Listener)
  80. enodes = append(enodes, enode)
  81. // Inject the signer key and start sealing with it
  82. store := node.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  83. if _, err := store.NewAccount(""); err != nil {
  84. panic(err)
  85. }
  86. }
  87. // Iterate over all the nodes and start signing with them
  88. time.Sleep(3 * time.Second)
  89. for _, node := range nodes {
  90. var ethereum *eth.Ethereum
  91. if err := node.Service(&ethereum); err != nil {
  92. panic(err)
  93. }
  94. if err := ethereum.StartMining(1); err != nil {
  95. panic(err)
  96. }
  97. }
  98. time.Sleep(3 * time.Second)
  99. // Start injecting transactions from the faucets like crazy
  100. nonces := make([]uint64, len(faucets))
  101. for {
  102. index := rand.Intn(len(faucets))
  103. // Fetch the accessor for the relevant signer
  104. var ethereum *eth.Ethereum
  105. if err := nodes[index%len(nodes)].Service(&ethereum); err != nil {
  106. panic(err)
  107. }
  108. // Create a self transaction and inject into the pool
  109. 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])
  110. if err != nil {
  111. panic(err)
  112. }
  113. if err := ethereum.TxPool().AddLocal(tx); err != nil {
  114. panic(err)
  115. }
  116. nonces[index]++
  117. // Wait if we're too saturated
  118. if pend, _ := ethereum.TxPool().Stats(); pend > 2048 {
  119. time.Sleep(100 * time.Millisecond)
  120. }
  121. }
  122. }
  123. // makeGenesis creates a custom Ethash genesis block based on some pre-defined
  124. // faucet accounts.
  125. func makeGenesis(faucets []*ecdsa.PrivateKey) *core.Genesis {
  126. genesis := core.DefaultTestnetGenesisBlock()
  127. genesis.Difficulty = params.MinimumDifficulty
  128. genesis.GasLimit = 25000000
  129. genesis.Config.ChainID = big.NewInt(18)
  130. genesis.Config.EIP150Hash = common.Hash{}
  131. genesis.Alloc = core.GenesisAlloc{}
  132. for _, faucet := range faucets {
  133. genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{
  134. Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
  135. }
  136. }
  137. return genesis
  138. }
  139. func makeMiner(genesis *core.Genesis, nodes []string) (*node.Node, error) {
  140. // Define the basic configurations for the Ethereum node
  141. datadir, _ := ioutil.TempDir("", "")
  142. config := &node.Config{
  143. Name: "geth",
  144. Version: params.Version,
  145. DataDir: datadir,
  146. P2P: p2p.Config{
  147. ListenAddr: "0.0.0.0:0",
  148. NoDiscovery: true,
  149. MaxPeers: 25,
  150. },
  151. NoUSB: true,
  152. UseLightweightKDF: true,
  153. }
  154. // Start the node and configure a full Ethereum node on it
  155. stack, err := node.New(config)
  156. if err != nil {
  157. return nil, err
  158. }
  159. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  160. return eth.New(ctx, &eth.Config{
  161. Genesis: genesis,
  162. NetworkId: genesis.Config.ChainID.Uint64(),
  163. SyncMode: downloader.FullSync,
  164. DatabaseCache: 256,
  165. DatabaseHandles: 256,
  166. TxPool: core.DefaultTxPoolConfig,
  167. GPO: eth.DefaultConfig.GPO,
  168. Ethash: eth.DefaultConfig.Ethash,
  169. MinerGasPrice: big.NewInt(1),
  170. MinerRecommit: time.Second,
  171. })
  172. }); err != nil {
  173. return nil, err
  174. }
  175. // Start the node and return if successful
  176. return stack, stack.Start()
  177. }