stress_clique.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "fmt"
  23. "io/ioutil"
  24. "math/big"
  25. "math/rand"
  26. "os"
  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/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/node"
  38. "github.com/ethereum/go-ethereum/p2p"
  39. "github.com/ethereum/go-ethereum/p2p/discover"
  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 []string
  59. )
  60. for _, sealer := range sealers {
  61. // Start the node and wait until it's up
  62. node, err := makeSealer(genesis, enodes)
  63. if err != nil {
  64. panic(err)
  65. }
  66. defer node.Stop()
  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 _, enode := range enodes {
  72. enode, err := discover.ParseNode(enode)
  73. if err != nil {
  74. panic(err)
  75. }
  76. node.Server().AddPeer(enode)
  77. }
  78. // Start tracking the node and it's enode url
  79. nodes = append(nodes, node)
  80. enode := fmt.Sprintf("enode://%s@127.0.0.1:%d", node.Server().NodeInfo().ID, node.Server().NodeInfo().Ports.Listener)
  81. enodes = append(enodes, enode)
  82. // Inject the signer key and start sealing with it
  83. store := node.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
  84. signer, err := store.ImportECDSA(sealer, "")
  85. if err != nil {
  86. panic(err)
  87. }
  88. if err := store.Unlock(signer, ""); err != nil {
  89. panic(err)
  90. }
  91. }
  92. // Iterate over all the nodes and start signing with them
  93. time.Sleep(3 * time.Second)
  94. for _, node := range nodes {
  95. var ethereum *eth.Ethereum
  96. if err := node.Service(&ethereum); err != nil {
  97. panic(err)
  98. }
  99. if err := ethereum.StartMining(1); err != nil {
  100. panic(err)
  101. }
  102. }
  103. time.Sleep(3 * time.Second)
  104. // Start injecting transactions from the faucet like crazy
  105. nonces := make([]uint64, len(faucets))
  106. for {
  107. index := rand.Intn(len(faucets))
  108. // Fetch the accessor for the relevant signer
  109. var ethereum *eth.Ethereum
  110. if err := nodes[index%len(nodes)].Service(&ethereum); err != nil {
  111. panic(err)
  112. }
  113. // Create a self transaction and inject into the pool
  114. 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])
  115. if err != nil {
  116. panic(err)
  117. }
  118. if err := ethereum.TxPool().AddLocal(tx); err != nil {
  119. panic(err)
  120. }
  121. nonces[index]++
  122. // Wait if we're too saturated
  123. if pend, _ := ethereum.TxPool().Stats(); pend > 2048 {
  124. time.Sleep(100 * time.Millisecond)
  125. }
  126. }
  127. }
  128. // makeGenesis creates a custom Clique genesis block based on some pre-defined
  129. // signer and faucet accounts.
  130. func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core.Genesis {
  131. // Create a Clique network based off of the Rinkeby config
  132. genesis := core.DefaultRinkebyGenesisBlock()
  133. genesis.GasLimit = 25000000
  134. genesis.Config.ChainID = big.NewInt(18)
  135. genesis.Config.Clique.Period = 1
  136. genesis.Config.EIP150Hash = common.Hash{}
  137. genesis.Alloc = core.GenesisAlloc{}
  138. for _, faucet := range faucets {
  139. genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{
  140. Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
  141. }
  142. }
  143. // Sort the signers and embed into the extra-data section
  144. signers := make([]common.Address, len(sealers))
  145. for i, sealer := range sealers {
  146. signers[i] = crypto.PubkeyToAddress(sealer.PublicKey)
  147. }
  148. for i := 0; i < len(signers); i++ {
  149. for j := i + 1; j < len(signers); j++ {
  150. if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
  151. signers[i], signers[j] = signers[j], signers[i]
  152. }
  153. }
  154. }
  155. genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65)
  156. for i, signer := range signers {
  157. copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:])
  158. }
  159. // Return the genesis block for initialization
  160. return genesis
  161. }
  162. func makeSealer(genesis *core.Genesis, nodes []string) (*node.Node, error) {
  163. // Define the basic configurations for the Ethereum node
  164. datadir, _ := ioutil.TempDir("", "")
  165. config := &node.Config{
  166. Name: "geth",
  167. Version: params.Version,
  168. DataDir: datadir,
  169. P2P: p2p.Config{
  170. ListenAddr: "0.0.0.0:0",
  171. NoDiscovery: true,
  172. MaxPeers: 25,
  173. },
  174. NoUSB: true,
  175. }
  176. // Start the node and configure a full Ethereum node on it
  177. stack, err := node.New(config)
  178. if err != nil {
  179. return nil, err
  180. }
  181. if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
  182. return eth.New(ctx, &eth.Config{
  183. Genesis: genesis,
  184. NetworkId: genesis.Config.ChainID.Uint64(),
  185. SyncMode: downloader.FullSync,
  186. DatabaseCache: 256,
  187. DatabaseHandles: 256,
  188. TxPool: core.DefaultTxPoolConfig,
  189. GPO: eth.DefaultConfig.GPO,
  190. MinerGasFloor: genesis.GasLimit * 9 / 10,
  191. MinerGasCeil: genesis.GasLimit * 11 / 10,
  192. MinerGasPrice: big.NewInt(1),
  193. MinerRecommit: time.Second,
  194. })
  195. }); err != nil {
  196. return nil, err
  197. }
  198. // Start the node and return if successful
  199. return stack, stack.Start()
  200. }