|
| 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 | + |
| 17 | +// +build none |
| 18 | + |
| 19 | +// This file contains a miner stress test based on the Clique consensus engine. |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "crypto/ecdsa" |
| 25 | + "fmt" |
| 26 | + "io/ioutil" |
| 27 | + "math/big" |
| 28 | + "math/rand" |
| 29 | + "os" |
| 30 | + "time" |
| 31 | + |
| 32 | + "github.com/ethereum/go-ethereum/accounts/keystore" |
| 33 | + "github.com/ethereum/go-ethereum/common" |
| 34 | + "github.com/ethereum/go-ethereum/common/fdlimit" |
| 35 | + "github.com/ethereum/go-ethereum/core" |
| 36 | + "github.com/ethereum/go-ethereum/core/types" |
| 37 | + "github.com/ethereum/go-ethereum/crypto" |
| 38 | + "github.com/ethereum/go-ethereum/eth" |
| 39 | + "github.com/ethereum/go-ethereum/eth/downloader" |
| 40 | + "github.com/ethereum/go-ethereum/log" |
| 41 | + "github.com/ethereum/go-ethereum/node" |
| 42 | + "github.com/ethereum/go-ethereum/p2p" |
| 43 | + "github.com/ethereum/go-ethereum/p2p/discover" |
| 44 | + "github.com/ethereum/go-ethereum/params" |
| 45 | +) |
| 46 | + |
| 47 | +func main() { |
| 48 | + log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) |
| 49 | + fdlimit.Raise(2048) |
| 50 | + |
| 51 | + // Generate a batch of accounts to seal and fund with |
| 52 | + faucets := make([]*ecdsa.PrivateKey, 128) |
| 53 | + for i := 0; i < len(faucets); i++ { |
| 54 | + faucets[i], _ = crypto.GenerateKey() |
| 55 | + } |
| 56 | + sealers := make([]*ecdsa.PrivateKey, 4) |
| 57 | + for i := 0; i < len(sealers); i++ { |
| 58 | + sealers[i], _ = crypto.GenerateKey() |
| 59 | + } |
| 60 | + // Create a Clique network based off of the Rinkeby config |
| 61 | + genesis := makeGenesis(faucets, sealers) |
| 62 | + |
| 63 | + var ( |
| 64 | + nodes []*node.Node |
| 65 | + enodes []string |
| 66 | + ) |
| 67 | + for _, sealer := range sealers { |
| 68 | + // Start the node and wait until it's up |
| 69 | + node, err := makeSealer(genesis, enodes) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + defer node.Stop() |
| 74 | + |
| 75 | + for node.Server().NodeInfo().Ports.Listener == 0 { |
| 76 | + time.Sleep(250 * time.Millisecond) |
| 77 | + } |
| 78 | + // Connect the node to al the previous ones |
| 79 | + for _, enode := range enodes { |
| 80 | + enode, err := discover.ParseNode(enode) |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + node.Server().AddPeer(enode) |
| 85 | + } |
| 86 | + // Start tracking the node and it's enode url |
| 87 | + nodes = append(nodes, node) |
| 88 | + |
| 89 | + enode := fmt.Sprintf("enode://%s@127.0.0.1:%d", node.Server().NodeInfo().ID, node.Server().NodeInfo().Ports.Listener) |
| 90 | + enodes = append(enodes, enode) |
| 91 | + |
| 92 | + // Inject the signer key and start sealing with it |
| 93 | + store := node.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) |
| 94 | + signer, err := store.ImportECDSA(sealer, "") |
| 95 | + if err != nil { |
| 96 | + panic(err) |
| 97 | + } |
| 98 | + if err := store.Unlock(signer, ""); err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + } |
| 102 | + // Iterate over all the nodes and start signing with them |
| 103 | + time.Sleep(3 * time.Second) |
| 104 | + |
| 105 | + for _, node := range nodes { |
| 106 | + var ethereum *eth.Ethereum |
| 107 | + if err := node.Service(ðereum); err != nil { |
| 108 | + panic(err) |
| 109 | + } |
| 110 | + if err := ethereum.StartMining(1); err != nil { |
| 111 | + panic(err) |
| 112 | + } |
| 113 | + } |
| 114 | + time.Sleep(3 * time.Second) |
| 115 | + |
| 116 | + // Start injecting transactions from the faucet like crazy |
| 117 | + nonces := make([]uint64, len(faucets)) |
| 118 | + for { |
| 119 | + index := rand.Intn(len(faucets)) |
| 120 | + |
| 121 | + // Fetch the accessor for the relevant signer |
| 122 | + var ethereum *eth.Ethereum |
| 123 | + if err := nodes[index%len(nodes)].Service(ðereum); err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | + // Create a self transaction and inject into the pool |
| 127 | + 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]) |
| 128 | + if err != nil { |
| 129 | + panic(err) |
| 130 | + } |
| 131 | + if err := ethereum.TxPool().AddLocal(tx); err != nil { |
| 132 | + panic(err) |
| 133 | + } |
| 134 | + nonces[index]++ |
| 135 | + |
| 136 | + // Wait if we're too saturated |
| 137 | + if pend, _ := ethereum.TxPool().Stats(); pend > 2048 { |
| 138 | + time.Sleep(100 * time.Millisecond) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// makeGenesis creates a custom Clique genesis block based on some pre-defined |
| 144 | +// signer and faucet accounts. |
| 145 | +func makeGenesis(faucets []*ecdsa.PrivateKey, sealers []*ecdsa.PrivateKey) *core.Genesis { |
| 146 | + // Create a Clique network based off of the Rinkeby config |
| 147 | + genesis := core.DefaultRinkebyGenesisBlock() |
| 148 | + genesis.GasLimit = 25000000 |
| 149 | + |
| 150 | + genesis.Config.ChainID = big.NewInt(18) |
| 151 | + genesis.Config.Clique.Period = 1 |
| 152 | + genesis.Config.EIP150Hash = common.Hash{} |
| 153 | + |
| 154 | + genesis.Alloc = core.GenesisAlloc{} |
| 155 | + for _, faucet := range faucets { |
| 156 | + genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{ |
| 157 | + Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil), |
| 158 | + } |
| 159 | + } |
| 160 | + // Sort the signers and embed into the extra-data section |
| 161 | + signers := make([]common.Address, len(sealers)) |
| 162 | + for i, sealer := range sealers { |
| 163 | + signers[i] = crypto.PubkeyToAddress(sealer.PublicKey) |
| 164 | + } |
| 165 | + for i := 0; i < len(signers); i++ { |
| 166 | + for j := i + 1; j < len(signers); j++ { |
| 167 | + if bytes.Compare(signers[i][:], signers[j][:]) > 0 { |
| 168 | + signers[i], signers[j] = signers[j], signers[i] |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + genesis.ExtraData = make([]byte, 32+len(signers)*common.AddressLength+65) |
| 173 | + for i, signer := range signers { |
| 174 | + copy(genesis.ExtraData[32+i*common.AddressLength:], signer[:]) |
| 175 | + } |
| 176 | + // Return the genesis block for initialization |
| 177 | + return genesis |
| 178 | +} |
| 179 | + |
| 180 | +func makeSealer(genesis *core.Genesis, nodes []string) (*node.Node, error) { |
| 181 | + // Define the basic configurations for the Ethereum node |
| 182 | + datadir, _ := ioutil.TempDir("", "") |
| 183 | + |
| 184 | + config := &node.Config{ |
| 185 | + Name: "geth", |
| 186 | + Version: params.Version, |
| 187 | + DataDir: datadir, |
| 188 | + P2P: p2p.Config{ |
| 189 | + ListenAddr: "0.0.0.0:0", |
| 190 | + NoDiscovery: true, |
| 191 | + MaxPeers: 25, |
| 192 | + }, |
| 193 | + NoUSB: true, |
| 194 | + } |
| 195 | + // Start the node and configure a full Ethereum node on it |
| 196 | + stack, err := node.New(config) |
| 197 | + if err != nil { |
| 198 | + return nil, err |
| 199 | + } |
| 200 | + if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { |
| 201 | + return eth.New(ctx, ð.Config{ |
| 202 | + Genesis: genesis, |
| 203 | + NetworkId: genesis.Config.ChainID.Uint64(), |
| 204 | + SyncMode: downloader.FullSync, |
| 205 | + DatabaseCache: 256, |
| 206 | + DatabaseHandles: 256, |
| 207 | + TxPool: core.DefaultTxPoolConfig, |
| 208 | + GPO: eth.DefaultConfig.GPO, |
| 209 | + MinerGasPrice: big.NewInt(1), |
| 210 | + MinerRecommit: time.Second, |
| 211 | + }) |
| 212 | + }); err != nil { |
| 213 | + return nil, err |
| 214 | + } |
| 215 | + // Start the node and return if successful |
| 216 | + return stack, stack.Start() |
| 217 | +} |
0 commit comments