github.com/core-coin/go-core/v2@v2.1.9/miner/stress_cryptore.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 //go:build none 18 // +build none 19 20 // This file contains a miner stress test based on the Cryptore consensus engine. 21 package main 22 23 import ( 24 "io/ioutil" 25 "math/big" 26 "math/rand" 27 "os" 28 "path/filepath" 29 "time" 30 31 "github.com/core-coin/go-core/v2/consensus/cryptore" 32 33 "github.com/core-coin/go-core/v2/accounts/keystore" 34 "github.com/core-coin/go-core/v2/common" 35 "github.com/core-coin/go-core/v2/common/fdlimit" 36 "github.com/core-coin/go-core/v2/core" 37 "github.com/core-coin/go-core/v2/core/types" 38 "github.com/core-coin/go-core/v2/crypto" 39 "github.com/core-coin/go-core/v2/log" 40 "github.com/core-coin/go-core/v2/miner" 41 "github.com/core-coin/go-core/v2/node" 42 "github.com/core-coin/go-core/v2/p2p" 43 "github.com/core-coin/go-core/v2/p2p/enode" 44 "github.com/core-coin/go-core/v2/params" 45 "github.com/core-coin/go-core/v2/xcb" 46 "github.com/core-coin/go-core/v2/xcb/downloader" 47 ) 48 49 func main() { 50 log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) 51 fdlimit.Raise(2048) 52 53 // Generate a batch of accounts to seal and fund with 54 faucets := make([]*goldilocks.PrivateKey, 128) 55 for i := 0; i < len(faucets); i++ { 56 faucets[i], _ = crypto.GenerateKey(crand.Reader) 57 } 58 // Pre-generate the cryptore mining DAG so we don't race 59 cryptore.MakeDataset(1, filepath.Join(os.Getenv("HOME"), ".cryptore")) 60 61 // Create an Cryptore network based off of the Devin config 62 genesis := makeGenesis(faucets) 63 64 var ( 65 nodes []*xcb.Core 66 enodes []*enode.Node 67 ) 68 for i := 0; i < 4; i++ { 69 // Start the node and wait until it's up 70 stack, xcbBackend, err := makeMiner(genesis) 71 if err != nil { 72 panic(err) 73 } 74 defer stack.Close() 75 76 for stack.Server().NodeInfo().Ports.Listener == 0 { 77 time.Sleep(250 * time.Millisecond) 78 } 79 // Connect the node to all the previous ones 80 for _, n := range enodes { 81 stack.Server().AddPeer(n) 82 } 83 // Start tracking the node and its enode 84 nodes = append(nodes, xcbBackend) 85 enodes = append(enodes, stack.Server().Self()) 86 87 // Inject the signer key and start sealing with it 88 store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 89 if _, err := store.NewAccount(""); err != nil { 90 panic(err) 91 } 92 } 93 94 // Iterate over all the nodes and start mining 95 time.Sleep(3 * time.Second) 96 for _, node := range nodes { 97 if err := node.StartMining(1); err != nil { 98 panic(err) 99 } 100 } 101 time.Sleep(3 * time.Second) 102 103 // Start injecting transactions from the faucets like crazy 104 nonces := make([]uint64, len(faucets)) 105 for { 106 // Pick a random mining node 107 index := rand.Intn(len(faucets)) 108 backend := nodes[index%len(nodes)] 109 110 // Create a self transaction and inject into the pool 111 tx, err := types.SignTx(types.NewTransaction(nonces[index], faucets[index].Address(), new(big.Int), 21000, big.NewInt(100000000000+rand.Int63n(65536)), nil), types.HomesteadSigner{}, faucets[index]) 112 if err != nil { 113 panic(err) 114 } 115 if err := backend.TxPool().AddLocal(tx); err != nil { 116 panic(err) 117 } 118 nonces[index]++ 119 120 // Wait if we're too saturated 121 if pend, _ := backend.TxPool().Stats(); pend > 2048 { 122 time.Sleep(100 * time.Millisecond) 123 } 124 } 125 } 126 127 // makeGenesis creates a custom Cryptore genesis block based on some pre-defined 128 // faucet accounts. 129 func makeGenesis(faucets []*goldilocks.PrivateKey) *core.Genesis { 130 genesis := core.DefaultDevinGenesisBlock() 131 genesis.Difficulty = params.MinimumDifficulty 132 genesis.EnergyLimit = 25000000 133 134 genesis.Config.NetworkID = big.NewInt(18) 135 genesis.Config.CIP150Hash = common.Hash{} 136 137 genesis.Alloc = core.GenesisAlloc{} 138 for _, faucet := range faucets { 139 genesis.Alloc[faucet.Address()] = core.GenesisAccount{ 140 Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil), 141 } 142 } 143 return genesis 144 } 145 146 func makeMiner(genesis *core.Genesis) (*node.Node, *xcb.Core, error) { 147 // Define the basic configurations for the Core node 148 datadir, _ := ioutil.TempDir("", "") 149 150 config := &node.Config{ 151 Name: "gocore", 152 Version: params.Version, 153 DataDir: datadir, 154 P2P: p2p.Config{ 155 ListenAddr: "0.0.0.0:0", 156 NoDiscovery: true, 157 MaxPeers: 25, 158 }, 159 UseLightweightKDF: true, 160 } 161 // Create the node and configure a full Core node on it 162 stack, err := node.New(config) 163 if err != nil { 164 return nil, nil, err 165 } 166 xcbBackend, err := xcb.New(stack, &xcb.Config{ 167 Genesis: genesis, 168 NetworkId: genesis.Config.NetworkID.Uint64(), 169 SyncMode: downloader.FullSync, 170 DatabaseCache: 256, 171 DatabaseHandles: 256, 172 TxPool: core.DefaultTxPoolConfig, 173 GPO: xcb.DefaultConfig.GPO, 174 Cryptore: xcb.DefaultConfig.Cryptore, 175 Miner: miner.Config{ 176 EnergyFloor: genesis.EnergyLimit * 9 / 10, 177 EnergyCeil: genesis.EnergyLimit * 11 / 10, 178 EnergyPrice: big.NewInt(1), 179 Recommit: time.Second, 180 }, 181 }) 182 if err != nil { 183 return nil, nil, err 184 } 185 186 err = stack.Start() 187 return stack, xcbBackend, err 188 }