Skip to content

Commit 71ff65b

Browse files
authored
miner/stress: add stress test for eip 1559 (#22930)
* miner/stress/1559: add 1559 stress tests * miner/stress: add 1559 stress test
1 parent 3094e7f commit 71ff65b

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

miner/stress/1559/main.go

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
// Copyright 2021 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+
// This file contains a miner stress test for eip 1559.
18+
package main
19+
20+
import (
21+
"crypto/ecdsa"
22+
"io/ioutil"
23+
"math/big"
24+
"math/rand"
25+
"os"
26+
"path/filepath"
27+
"time"
28+
29+
"github.com/ethereum/go-ethereum/accounts/keystore"
30+
"github.com/ethereum/go-ethereum/common"
31+
"github.com/ethereum/go-ethereum/common/fdlimit"
32+
"github.com/ethereum/go-ethereum/consensus/ethash"
33+
"github.com/ethereum/go-ethereum/core"
34+
"github.com/ethereum/go-ethereum/core/types"
35+
"github.com/ethereum/go-ethereum/crypto"
36+
"github.com/ethereum/go-ethereum/eth"
37+
"github.com/ethereum/go-ethereum/eth/downloader"
38+
"github.com/ethereum/go-ethereum/eth/ethconfig"
39+
"github.com/ethereum/go-ethereum/log"
40+
"github.com/ethereum/go-ethereum/miner"
41+
"github.com/ethereum/go-ethereum/node"
42+
"github.com/ethereum/go-ethereum/p2p"
43+
"github.com/ethereum/go-ethereum/p2p/enode"
44+
"github.com/ethereum/go-ethereum/params"
45+
)
46+
47+
var (
48+
londonBlock = big.NewInt(30) // Predefined london fork block for activating eip 1559.
49+
)
50+
51+
func main() {
52+
log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
53+
fdlimit.Raise(2048)
54+
55+
// Generate a batch of accounts to seal and fund with
56+
faucets := make([]*ecdsa.PrivateKey, 128)
57+
for i := 0; i < len(faucets); i++ {
58+
faucets[i], _ = crypto.GenerateKey()
59+
}
60+
// Pre-generate the ethash mining DAG so we don't race
61+
ethash.MakeDataset(1, filepath.Join(os.Getenv("HOME"), ".ethash"))
62+
63+
// Create an Ethash network based off of the Ropsten config
64+
genesis := makeGenesis(faucets)
65+
66+
var (
67+
nodes []*eth.Ethereum
68+
enodes []*enode.Node
69+
)
70+
for i := 0; i < 4; i++ {
71+
// Start the node and wait until it's up
72+
stack, ethBackend, err := makeMiner(genesis)
73+
if err != nil {
74+
panic(err)
75+
}
76+
defer stack.Close()
77+
78+
for stack.Server().NodeInfo().Ports.Listener == 0 {
79+
time.Sleep(250 * time.Millisecond)
80+
}
81+
// Connect the node to all the previous ones
82+
for _, n := range enodes {
83+
stack.Server().AddPeer(n)
84+
}
85+
// Start tracking the node and its enode
86+
nodes = append(nodes, ethBackend)
87+
enodes = append(enodes, stack.Server().Self())
88+
89+
// Inject the signer key and start sealing with it
90+
store := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
91+
if _, err := store.NewAccount(""); err != nil {
92+
panic(err)
93+
}
94+
}
95+
96+
// Iterate over all the nodes and start mining
97+
time.Sleep(3 * time.Second)
98+
for _, node := range nodes {
99+
if err := node.StartMining(1); err != nil {
100+
panic(err)
101+
}
102+
}
103+
time.Sleep(3 * time.Second)
104+
105+
// Start injecting transactions from the faucets like crazy
106+
var (
107+
nonces = make([]uint64, len(faucets))
108+
109+
// The signer activates the 1559 features even before the fork,
110+
// so the new 1559 txs can be created with this signer.
111+
signer = types.LatestSignerForChainID(genesis.Config.ChainID)
112+
)
113+
for {
114+
// Pick a random mining node
115+
index := rand.Intn(len(faucets))
116+
backend := nodes[index%len(nodes)]
117+
118+
headHeader := backend.BlockChain().CurrentHeader()
119+
baseFee := headHeader.BaseFee
120+
121+
// Create a self transaction and inject into the pool. The legacy
122+
// and 1559 transactions can all be created by random even if the
123+
// fork is not happened.
124+
tx := makeTransaction(nonces[index], faucets[index], signer, baseFee)
125+
if err := backend.TxPool().AddLocal(tx); err != nil {
126+
continue
127+
}
128+
nonces[index]++
129+
130+
// Wait if we're too saturated
131+
if pend, _ := backend.TxPool().Stats(); pend > 4192 {
132+
time.Sleep(100 * time.Millisecond)
133+
}
134+
135+
// Wait if the basefee is raised too fast
136+
if baseFee != nil && baseFee.Cmp(new(big.Int).Mul(big.NewInt(100), big.NewInt(params.GWei))) > 0 {
137+
time.Sleep(500 * time.Millisecond)
138+
}
139+
}
140+
}
141+
142+
func makeTransaction(nonce uint64, privKey *ecdsa.PrivateKey, signer types.Signer, baseFee *big.Int) *types.Transaction {
143+
// Generate legacy transaction
144+
if rand.Intn(2) == 0 {
145+
tx, err := types.SignTx(types.NewTransaction(nonce, crypto.PubkeyToAddress(privKey.PublicKey), new(big.Int), 21000, big.NewInt(100000000000+rand.Int63n(65536)), nil), signer, privKey)
146+
if err != nil {
147+
panic(err)
148+
}
149+
return tx
150+
}
151+
// Generate eip 1559 transaction
152+
recipient := crypto.PubkeyToAddress(privKey.PublicKey)
153+
154+
// Feecap and feetip are limited to 32 bytes. Offer a sightly
155+
// larger buffer for creating both valid and invalid transactions.
156+
var buf = make([]byte, 32+5)
157+
rand.Read(buf)
158+
tip := new(big.Int).SetBytes(buf)
159+
160+
// If the given base fee is nil(the 1559 is still not available),
161+
// generate a fake base fee in order to create 1559 tx forcibly.
162+
if baseFee == nil {
163+
baseFee = new(big.Int).SetInt64(int64(rand.Int31()))
164+
}
165+
// Generate the feecap, 75% valid feecap and 25% unguaranted.
166+
var feeCap *big.Int
167+
if rand.Intn(4) == 0 {
168+
rand.Read(buf)
169+
feeCap = new(big.Int).SetBytes(buf)
170+
} else {
171+
feeCap = new(big.Int).Add(baseFee, tip)
172+
}
173+
return types.MustSignNewTx(privKey, signer, &types.DynamicFeeTx{
174+
ChainID: signer.ChainID(),
175+
Nonce: nonce,
176+
Tip: tip,
177+
FeeCap: feeCap,
178+
Gas: 21000,
179+
To: &recipient,
180+
Value: big.NewInt(100),
181+
Data: nil,
182+
AccessList: nil,
183+
})
184+
}
185+
186+
// makeGenesis creates a custom Ethash genesis block based on some pre-defined
187+
// faucet accounts.
188+
func makeGenesis(faucets []*ecdsa.PrivateKey) *core.Genesis {
189+
genesis := core.DefaultRopstenGenesisBlock()
190+
191+
genesis.Config = params.AllEthashProtocolChanges
192+
genesis.Config.LondonBlock = londonBlock
193+
genesis.Difficulty = params.MinimumDifficulty
194+
195+
// Small gaslimit for easier basefee moving testing.
196+
genesis.GasLimit = 8_000_000
197+
198+
genesis.Config.ChainID = big.NewInt(18)
199+
genesis.Config.EIP150Hash = common.Hash{}
200+
201+
genesis.Alloc = core.GenesisAlloc{}
202+
for _, faucet := range faucets {
203+
genesis.Alloc[crypto.PubkeyToAddress(faucet.PublicKey)] = core.GenesisAccount{
204+
Balance: new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil),
205+
}
206+
}
207+
if londonBlock.Sign() == 0 {
208+
log.Info("Enabled the eip 1559 by default")
209+
} else {
210+
log.Info("Registered the london fork", "number", londonBlock)
211+
}
212+
return genesis
213+
}
214+
215+
func makeMiner(genesis *core.Genesis) (*node.Node, *eth.Ethereum, error) {
216+
// Define the basic configurations for the Ethereum node
217+
datadir, _ := ioutil.TempDir("", "")
218+
219+
config := &node.Config{
220+
Name: "geth",
221+
Version: params.Version,
222+
DataDir: datadir,
223+
P2P: p2p.Config{
224+
ListenAddr: "0.0.0.0:0",
225+
NoDiscovery: true,
226+
MaxPeers: 25,
227+
},
228+
UseLightweightKDF: true,
229+
}
230+
// Create the node and configure a full Ethereum node on it
231+
stack, err := node.New(config)
232+
if err != nil {
233+
return nil, nil, err
234+
}
235+
ethBackend, err := eth.New(stack, &ethconfig.Config{
236+
Genesis: genesis,
237+
NetworkId: genesis.Config.ChainID.Uint64(),
238+
SyncMode: downloader.FullSync,
239+
DatabaseCache: 256,
240+
DatabaseHandles: 256,
241+
TxPool: core.DefaultTxPoolConfig,
242+
GPO: ethconfig.Defaults.GPO,
243+
Ethash: ethconfig.Defaults.Ethash,
244+
Miner: miner.Config{
245+
GasFloor: genesis.GasLimit * 9 / 10,
246+
GasCeil: genesis.GasLimit * 11 / 10,
247+
GasPrice: big.NewInt(1),
248+
Recommit: time.Second,
249+
},
250+
})
251+
if err != nil {
252+
return nil, nil, err
253+
}
254+
err = stack.Start()
255+
return stack, ethBackend, err
256+
}
File renamed without changes.

0 commit comments

Comments
 (0)