stress_clique.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 Clique consensus engine.
  18. package main
  19. import (
  20. "bytes"
  21. "crypto/ecdsa"
  22. "io/ioutil"
  23. "math/big"
  24. "math/rand"
  25. "os"
  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/core"
  31. "github.com/ethereum/go-ethereum/core/types"
  32. "github.com/ethereum/go-ethereum/crypto"
  33. "github.com/ethereum/go-ethereum/eth"
  34. "github.com/ethereum/go-ethereum/eth/downloader"
  35. "github.com/ethereum/go-ethereum/log"
  36. "github.com/ethereum/go-ethereum/miner"
  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. sealers := make([]*ecdsa.PrivateKey, 4)
  51. for i := 0; i < len(sealers); i++ {
  52. sealers[i], _ = crypto.GenerateKey()
  53. }
  54. // Create a Clique network based off of the Rinkeby config
  55. genesis := makeGenesis(faucets, sealers)
  56. var (
  57. nodes []*eth.Ethereum
  58. enodes []*enode.Node
  59. )
  60. for _, sealer := range sealers {
  61. // Start the node and wait until it's up
  62. stack, ethBackend, err := makeSealer(genesis)
  63. if err != nil {
  64. panic(err)
  65. }
  66. defer stack.Close()
  67. for stack.Server().NodeInfo().Ports.Listener == 0 {
  68. time.Sleep(250 * time.Millisecond)
  69. }
  70. // Connect the node to all the previous ones
  71. for _, n := range enodes {
  72. stack.Server().AddPeer(n)
  73. }
  74. // Start tracking the node and its enode
  75. nodes = append(nodes, ethBackend)
  76. enodes = append(enodes, stack.Server().Self())
  77. // Inject the signer key and start sealing with it
  78. store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  79. signer, err := store.ImportECDSA(sealer, "")
  80. if err != nil {
  81. panic(err)
  82. }
  83. if err := store.Unlock(signer, ""); err != nil {
  84. panic(err)
  85. }
  86. }
  87. // Iterate over all the nodes and start signing on them
  88. time.Sleep(3 * time.Second)
  89. for _, node := range nodes {
  90. if err := node.StartMining(1); err != nil {
  91. panic(err)
  92. }
  93. }
  94. time.Sleep(3 * time.Second)
  95. // Start injecting transactions from the faucet like crazy
  96. nonces := make([]uint64, len(faucets))
  97. for {
  98. // Pick a random signer node
  99. index := rand.Intn(len(faucets))
  100. backend := nodes[index%len(nodes)]
  101. // Create a self transaction and inject into the pool
  102. tx, err := types.SignTx(types.NewTransaction(nonces[index], crypto.PubkeyToAddress(faucets[index].PublicKey), new(big.Int), 21000, big.NewInt(100000000000), nil), types.HomesteadSigner{}, faucets[index])
  103. if err != nil {
  104. panic(err)
  105. }
  106. if err := backend.TxPool().AddLocal(tx); err != nil {
  107. panic(err)
  108. }
  109. nonces[index]++
  110. // Wait if we're too saturated
  111. if pend, _ := backend.TxPool().Stats(); pend > 2048 {
  112. time.Sleep(100 * time.Millisecond)
  113. }
  114. }
  115. }
  116. // makeGenesis creates a custom Clique genesis block based on some pre-defined
  117. // signer and faucet accounts.
  118. func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core.Genesis {
  119. // Create a Clique network based off of the Rinkeby config
  120. genesis := core.DefaultRinkebyGenesisBlock()
  121. genesis.GasLimit = 25000000
  122. genesis.Config.ChainID = big.NewInt(18)
  123. genesis.Config.Clique.Period = 1
  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. // Sort the signers and embed into the extra-data section
  132. signers := make([]common.Address, len(sealers))
  133. for i, sealer := range sealers {
  134. signers[i] = crypto.PubkeyToAddress(sealer.PublicKey)
  135. }
  136. for i := 0; i < len(signers); i++ {
  137. for j := i + 1; j < len(signers); j++ {
  138. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  139. signers[i], signers[j] = signers[j], signers[i]
  140. }
  141. }
  142. }
  143. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  144. for i, signer := range signers {
  145. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  146. }
  147. // Return the genesis block for initialization
  148. return genesis
  149. }
  150. func makeSealer(genesis *core.Genesis) (*node.Node, *eth.Ethereum, error) {
  151. // Define the basic configurations for the Ethereum node
  152. datadir, _ := ioutil.TempDir("", "")
  153. config := &node.Config{
  154. Name: "geth",
  155. Version: params.Version,
  156. DataDir: datadir,
  157. P2P: p2p.Config{
  158. ListenAddr: "0.0.0.0:0",
  159. NoDiscovery: true,
  160. MaxPeers: 25,
  161. },
  162. }
  163. // Start the node and configure a full Ethereum node on it
  164. stack, err := node.New(config)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. // Create and register the backend
  169. ethBackend, err := eth.New(stack, &ethconfig.Config{
  170. Genesis: genesis,
  171. NetworkId: genesis.Config.ChainID.Uint64(),
  172. SyncMode: downloader.FullSync,
  173. DatabaseCache: 256,
  174. DatabaseHandles: 256,
  175. TxPool: core.DefaultTxPoolConfig,
  176. GPO: eth.DefaultConfig.GPO,
  177. Miner: miner.Config{
  178. GasFloor: genesis.GasLimit * 9 / 10,
  179. GasCeil: genesis.GasLimit * 11 / 10,
  180. GasPrice: big.NewInt(1),
  181. Recommit: time.Second,
  182. },
  183. })
  184. if err != nil {
  185. return nil, nil, err
  186. }
  187. err = stack.Start()
  188. return stack, ethBackend, err
  189. }