stress_clique.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/node"
  37. "github.com/ethereum/go-ethereum/p2p"
  38. "github.com/ethereum/go-ethereum/p2p/enode"
  39. "github.com/ethereum/go-ethereum/params"
  40. )
  41. func main() {
  42. log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
  43. fdlimit.Raise(2048)
  44. // Generate a batch of accounts to seal and fund with
  45. faucets := make([]*ecdsa.PrivateKey, 128)
  46. for i := 0; i < len(faucets); i++ {
  47. faucets[i], _ = crypto.GenerateKey()
  48. }
  49. sealers := make([]*ecdsa.PrivateKey, 4)
  50. for i := 0; i < len(sealers); i++ {
  51. sealers[i], _ = crypto.GenerateKey()
  52. }
  53. // Create a Clique network based off of the Rinkeby config
  54. genesis := makeGenesis(faucets, sealers)
  55. var (
  56. nodes []*node.Node
  57. enodes []*enode.Node
  58. )
  59. for _, sealer := range sealers {
  60. // Start the node and wait until it's up
  61. node, err := makeSealer(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. signer, err := store.ImportECDSA(sealer, "")
  79. if err != nil {
  80. panic(err)
  81. }
  82. if err := store.Unlock(signer, ""); err != nil {
  83. panic(err)
  84. }
  85. }
  86. // Iterate over all the nodes and start signing with them
  87. time.Sleep(3 * time.Second)
  88. for _, node := range nodes {
  89. var ethereum *eth.Ethereum
  90. if err := node.Service(&ethereum); err != nil {
  91. panic(err)
  92. }
  93. if err := ethereum.StartMining(1); err != nil {
  94. panic(err)
  95. }
  96. }
  97. time.Sleep(3 * time.Second)
  98. // Start injecting transactions from the faucet like crazy
  99. nonces := make([]uint64, len(faucets))
  100. for {
  101. index := rand.Intn(len(faucets))
  102. // Fetch the accessor for the relevant signer
  103. var ethereum *eth.Ethereum
  104. if err := nodes[index%len(nodes)].Service(&ethereum); err != nil {
  105. panic(err)
  106. }
  107. // Create a self transaction and inject into the pool
  108. 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])
  109. if err != nil {
  110. panic(err)
  111. }
  112. if err := ethereum.TxPool().AddLocal(tx); err != nil {
  113. panic(err)
  114. }
  115. nonces[index]++
  116. // Wait if we're too saturated
  117. if pend, _ := ethereum.TxPool().Stats(); pend > 2048 {
  118. time.Sleep(100 * time.Millisecond)
  119. }
  120. }
  121. }
  122. // makeGenesis creates a custom Clique genesis block based on some pre-defined
  123. // signer and faucet accounts.
  124. func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core.Genesis {
  125. // Create a Clique network based off of the Rinkeby config
  126. genesis := core.DefaultRinkebyGenesisBlock()
  127. genesis.GasLimit = 25000000
  128. genesis.Config.ChainID = big.NewInt(18)
  129. genesis.Config.Clique.Period = 1
  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. // Sort the signers and embed into the extra-data section
  138. signers := make([]common.Address, len(sealers))
  139. for i, sealer := range sealers {
  140. signers[i] = crypto.PubkeyToAddress(sealer.PublicKey)
  141. }
  142. for i := 0; i < len(signers); i++ {
  143. for j := i + 1; j < len(signers); j++ {
  144. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  145. signers[i], signers[j] = signers[j], signers[i]
  146. }
  147. }
  148. }
  149. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  150. for i, signer := range signers {
  151. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  152. }
  153. // Return the genesis block for initialization
  154. return genesis
  155. }
  156. func makeSealer(genesis *core.Genesis) (*node.Node, error) {
  157. // Define the basic configurations for the Ethereum node
  158. datadir, _ := ioutil.TempDir("", "")
  159. config := &node.Config{
  160. Name: "geth",
  161. Version: params.Version,
  162. DataDir: datadir,
  163. P2P: p2p.Config{
  164. ListenAddr: "0.0.0.0:0",
  165. NoDiscovery: true,
  166. MaxPeers: 25,
  167. },
  168. NoUSB: true,
  169. }
  170. // Start the node and configure a full Ethereum node on it
  171. stack, err := node.New(config)
  172. if err != nil {
  173. return nil, err
  174. }
  175. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  176. return eth.New(ctx, &eth.Config{
  177. Genesis: genesis,
  178. NetworkId: genesis.Config.ChainID.Uint64(),
  179. SyncMode: downloader.FullSync,
  180. DatabaseCache: 256,
  181. DatabaseHandles: 256,
  182. TxPool: core.DefaultTxPoolConfig,
  183. GPO: eth.DefaultConfig.GPO,
  184. MinerGasFloor: genesis.GasLimit * 9 / 10,
  185. MinerGasCeil: genesis.GasLimit * 11 / 10,
  186. MinerGasPrice: big.NewInt(1),
  187. MinerRecommit: time.Second,
  188. })
  189. }); err != nil {
  190. return nil, err
  191. }
  192. // Start the node and return if successful
  193. return stack, stack.Start()
  194. }