/
generate.go
276 lines (244 loc) · 8.05 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"crypto/ecdsa"
"fmt"
"math"
"math/big"
"math/rand"
"strings"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/triedb"
"golang.org/x/exp/slices"
)
// generatorConfig is the configuration of the chain generator.
type generatorConfig struct {
// genesis options
forkInterval int // number of blocks between forks
lastFork string // last enabled fork
clique bool // create a clique chain
// chain options
txInterval int // frequency of blocks containing transactions
txCount int // number of txs in block
chainLength int // number of generated blocks
// output options
outputs []string // enabled outputs
outputDir string // path where output files should be placed
}
func (cfg generatorConfig) withDefaults() (generatorConfig, error) {
if cfg.lastFork == "" {
cfg.lastFork = lastFork
}
if cfg.txInterval == 0 {
cfg.txInterval = 1
}
if cfg.outputs == nil {
cfg.outputs = []string{"genesis", "chain", "txinfo"}
}
return cfg, nil
}
// generator is the central object in the chain generation process.
// It holds the configuration, state, and all instantiated transaction generators.
type generator struct {
cfg generatorConfig
genesis *core.Genesis
td *big.Int
accounts []genAccount
rand *rand.Rand
// Modifier lists.
virgins []*modifierInstance
mods []*modifierInstance
modOffset int
// for write/export
blockchain *core.BlockChain
}
type modifierInstance struct {
name string
blockModifier
}
type genAccount struct {
addr common.Address
key *ecdsa.PrivateKey
}
func newGenerator(cfg generatorConfig) *generator {
genesis := cfg.createGenesis()
return &generator{
cfg: cfg,
genesis: genesis,
rand: rand.New(rand.NewSource(10)),
td: new(big.Int).Set(genesis.Difficulty),
virgins: cfg.createBlockModifiers(),
accounts: slices.Clone(knownAccounts),
}
}
func (cfg *generatorConfig) createBlockModifiers() (list []*modifierInstance) {
for name, new := range modRegistry {
list = append(list, &modifierInstance{
name: name,
blockModifier: new(),
})
}
slices.SortFunc(list, func(a, b *modifierInstance) int {
return strings.Compare(a.name, b.name)
})
return list
}
// run produces a chain and writes it.
func (g *generator) run() error {
db := rawdb.NewMemoryDatabase()
engine := g.createConsensusEngine(db)
// Init genesis block.
trieconfig := *triedb.HashDefaults
trieconfig.Preimages = true
triedb := triedb.NewDatabase(db, &trieconfig)
genesis := g.genesis.MustCommit(db, triedb)
// Create the blocks.
chain, _ := core.GenerateChain(g.genesis.Config, genesis, engine, db, g.cfg.chainLength, g.modifyBlock)
// Import the chain. This runs all block validation rules.
bc, err := g.importChain(engine, chain)
if err != nil {
return err
}
g.blockchain = bc
return g.write()
}
func (g *generator) createConsensusEngine(db ethdb.Database) consensus.Engine {
var inner consensus.Engine
if g.genesis.Config.Clique != nil {
cliqueEngine := clique.New(g.genesis.Config.Clique, db)
cliqueEngine.Authorize(cliqueSignerAddr, func(signer accounts.Account, mimeType string, message []byte) ([]byte, error) {
sig, err := crypto.Sign(crypto.Keccak256(message), cliqueSignerKey)
return sig, err
})
inner = instaSeal{cliqueEngine}
} else {
inner = ethash.NewFaker()
}
return beacon.New(inner)
}
func (g *generator) importChain(engine consensus.Engine, chain []*types.Block) (*core.BlockChain, error) {
db := rawdb.NewMemoryDatabase()
cacheconfig := core.DefaultCacheConfigWithScheme("hash")
cacheconfig.Preimages = true
vmconfig := vm.Config{EnablePreimageRecording: true}
blockchain, err := core.NewBlockChain(db, cacheconfig, g.genesis, nil, engine, vmconfig, nil, nil)
if err != nil {
return nil, fmt.Errorf("can't create blockchain: %v", err)
}
i, err := blockchain.InsertChain(chain)
if err != nil {
blockchain.Stop()
return nil, fmt.Errorf("chain validation error (block %d): %v", chain[i].Number(), err)
}
return blockchain, nil
}
func (g *generator) modifyBlock(i int, gen *core.BlockGen) {
fmt.Println("generating block", gen.Number())
if g.genesis.Config.Clique != nil {
g.setClique(i, gen)
}
g.setDifficulty(i, gen)
g.setParentBeaconRoot(i, gen)
g.runModifiers(i, gen)
}
func (g *generator) setClique(i int, gen *core.BlockGen) {
mergeblock := g.genesis.Config.MergeNetsplitBlock
if mergeblock != nil && gen.Number().Cmp(mergeblock) >= 0 {
return
}
gen.SetCoinbase(cliqueSignerAddr)
// Add a positive vote to keep the signer in the set.
gen.SetNonce(types.BlockNonce{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
// The clique engine requires the block to have blank extra-data of the correct length before sealing.
gen.SetExtra(make([]byte, 32+65))
}
func (g *generator) setDifficulty(i int, gen *core.BlockGen) {
chaincfg := g.genesis.Config
mergeblock := chaincfg.MergeNetsplitBlock
if mergeblock == nil {
mergeblock = new(big.Int).SetUint64(math.MaxUint64)
}
switch gen.Number().Cmp(mergeblock) {
case 1:
gen.SetPoS()
case 0:
gen.SetPoS()
chaincfg.TerminalTotalDifficulty = new(big.Int).Set(g.td)
default:
g.td = g.td.Add(g.td, gen.Difficulty())
}
}
func (g *generator) setParentBeaconRoot(i int, gen *core.BlockGen) {
if g.genesis.Config.IsCancun(gen.Number(), gen.Timestamp()) {
var h common.Hash
g.rand.Read(h[:])
gen.SetParentBeaconRoot(h)
}
}
// runModifiers executes the chain modifiers.
func (g *generator) runModifiers(i int, gen *core.BlockGen) {
totalMods := len(g.mods) + len(g.virgins)
if totalMods == 0 || g.cfg.txInterval == 0 || i%g.cfg.txInterval != 0 {
return
}
ctx := &genBlockContext{index: i, block: gen, gen: g}
// Modifier scheduling: we cycle through the available modifiers until enough have
// executed successfully. It also stops when all of them return false from apply()
// because this usually means there is no gas left.
count := 0
refused := 0 // count of consecutive times apply() returned false
run := func(mod *modifierInstance) bool {
ok := mod.apply(ctx)
if ok {
fmt.Println(" -", mod.name)
count++
refused = 0
} else {
refused++
}
return ok
}
// In order to avoid a pathological situation where a modifier never executes because
// of unfortunate scheduling, we first try modifiers from g.virgins.
for i := 0; i < len(g.virgins) && count < g.cfg.txCount; i++ {
mod := g.virgins[i]
if run(mod) {
g.mods = append(g.mods, mod)
g.virgins = append(g.virgins[:i], g.virgins[i+1:]...)
i--
}
}
// If there is any space left, fill it using g.mods.
for len(g.mods) > 0 && count < g.cfg.txCount && refused < totalMods {
index := g.modOffset % len(g.mods)
run(g.mods[index])
g.modOffset++
}
}
// instaSeal wraps a consensus engine with instant block sealing. When a block is produced
// using FinalizeAndAssemble, it also applies Seal.
type instaSeal struct{ consensus.Engine }
// FinalizeAndAssemble implements consensus.Engine, accumulating the block and uncle rewards,
// setting the final state and assembling the block.
func (e instaSeal) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, withdrawals []*types.Withdrawal) (*types.Block, error) {
block, err := e.Engine.FinalizeAndAssemble(chain, header, state, txs, uncles, receipts, withdrawals)
if err != nil {
return nil, err
}
sealedBlock := make(chan *types.Block, 1)
if err = e.Engine.Seal(chain, block, sealedBlock, nil); err != nil {
return nil, err
}
return <-sealedBlock, nil
}