stress_clique.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 []*node.Node
  58. enodes []*enode.Node
  59. )
  60. for _, sealer := range sealers {
  61. // Start the node and wait until it's up
  62. node, err := makeSealer(genesis)
  63. if err != nil {
  64. panic(err)
  65. }
  66. defer node.Close()
  67. for node.Server().NodeInfo().Ports.Listener == 0 {
  68. time.Sleep(250 * time.Millisecond)
  69. }
  70. // Connect the node to al the previous ones
  71. for _, n := range enodes {
  72. node.Server().AddPeer(n)
  73. }
  74. // Start tracking the node and it's enode
  75. nodes = append(nodes, node)
  76. enodes = append(enodes, node.Server().Self())
  77. // Inject the signer key and start sealing with it
  78. store := node.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 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 faucet 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), 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 Clique genesis block based on some pre-defined
  124. // signer and faucet accounts.
  125. func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core.Genesis {
  126. // Create a Clique network based off of the Rinkeby config
  127. genesis := core.DefaultRinkebyGenesisBlock()
  128. genesis.GasLimit = 25000000
  129. genesis.Config.ChainID = big.NewInt(18)
  130. genesis.Config.Clique.Period = 1
  131. genesis.Config.EIP150Hash = common.Hash{}
  132. genesis.Alloc = core.GenesisAlloc{}
  133. for _, faucet := range faucets {
  134. genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{
  135. Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
  136. }
  137. }
  138. // Sort the signers and embed into the extra-data section
  139. signers := make([]common.Address, len(sealers))
  140. for i, sealer := range sealers {
  141. signers[i] = crypto.PubkeyToAddress(sealer.PublicKey)
  142. }
  143. for i := 0; i < len(signers); i++ {
  144. for j := i + 1; j < len(signers); j++ {
  145. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  146. signers[i], signers[j] = signers[j], signers[i]
  147. }
  148. }
  149. }
  150. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  151. for i, signer := range signers {
  152. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  153. }
  154. // Return the genesis block for initialization
  155. return genesis
  156. }
  157. func makeSealer(genesis *core.Genesis) (*node.Node, error) {
  158. // Define the basic configurations for the Ethereum node
  159. datadir, _ := ioutil.TempDir("", "")
  160. config := &node.Config{
  161. Name: "geth",
  162. Version: params.Version,
  163. DataDir: datadir,
  164. P2P: p2p.Config{
  165. ListenAddr: "0.0.0.0:0",
  166. NoDiscovery: true,
  167. MaxPeers: 25,
  168. },
  169. NoUSB: true,
  170. }
  171. // Start the node and configure a full Ethereum node on it
  172. stack, err := node.New(config)
  173. if err != nil {
  174. return nil, err
  175. }
  176. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  177. return eth.New(ctx, &eth.Config{
  178. Genesis: genesis,
  179. NetworkId: genesis.Config.ChainID.Uint64(),
  180. SyncMode: downloader.FullSync,
  181. DatabaseCache: 256,
  182. DatabaseHandles: 256,
  183. TxPool: core.DefaultTxPoolConfig,
  184. GPO: eth.DefaultConfig.GPO,
  185. Miner: miner.Config{
  186. GasFloor: genesis.GasLimit * 9 / 10,
  187. GasCeil: genesis.GasLimit * 11 / 10,
  188. GasPrice: big.NewInt(1),
  189. Recommit: time.Second,
  190. },
  191. })
  192. }); err != nil {
  193. return nil, err
  194. }
  195. // Start the node and return if successful
  196. return stack, stack.Start()
  197. }