-
Notifications
You must be signed in to change notification settings - Fork 17
/
consensus.go
585 lines (517 loc) · 18.6 KB
/
consensus.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package main
import (
"context"
"errors"
"fmt"
"math"
"math/big"
"mergemock/api"
"mergemock/p2p"
"mergemock/rpc"
"mergemock/types"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/prysmaticlabs/prysm/crypto/bls/blst"
"github.com/prysmaticlabs/prysm/runtime/version"
"github.com/sirupsen/logrus"
)
type validator struct {
pk types.PublicKey
sk bls.SecretKey
}
type ConsensusCmd struct {
BeaconGenesisTime uint64 `ask:"--beacon-genesis-time" help:"Beacon genesis time"`
SlotTime time.Duration `ask:"--slot-time" help:"Time per slot"`
SlotsPerEpoch uint64 `ask:"--slots-per-epoch" help:"Slots per epoch"`
// TODO ideas:
// - % random gap slots (= missing beacon blocks)
// - % random finality
EngineAddr string `ask:"--engine" help:"Address of Engine JSON-RPC endpoint to use"`
BuilderAddr string `ask:"--builder" help:"Address of builder relay REST API endpoint to use"`
DataDir string `ask:"--datadir" help:"Directory to store execution chain data (empty for in-memory data)"`
EthashDir string `ask:"--ethashdir" help:"Directory to store ethash data"`
GenesisPath string `ask:"--genesis" help:"Genesis execution-config file"`
JwtSecretPath string `ask:"--jwt-secret" help:"JWT secret key for authenticated communication"`
Enode string `ask:"--node" help:"Enode of execution client, required to insert pre-merge blocks."`
SlotBound uint64 `ask:"--slot-bound" help:"Terminate after the specified number of slots."`
ValidatorCount uint64 `ask:"--validators" help:"Number of validators to emulate."`
GenesisValidatorsRoot string `ask:"--genesis-validators-root" help:"Root of genesis validators"`
// embed consensus behaviors
ConsensusBehavior `ask:"."`
// embed logger options
LogCmd `ask:".log" help:"Change logger configuration"`
TraceLogConfig `ask:".trace" help:"Tracing options"`
close chan struct{}
log logrus.Ext1FieldLogger
ctx context.Context
engine *rpc.Client
jwtSecret []byte
db ethdb.Database
genesisValidatorsRoot types.Root
ethashCfg ethash.Config
mockChain *MockChain
validators []validator
}
func (c *ConsensusCmd) Default() {
c.BeaconGenesisTime = uint64(time.Now().Unix()) + 5
c.EngineAddr = "http://127.0.0.1:8551"
c.GenesisPath = "genesis.json"
c.JwtSecretPath = "jwt.hex"
c.Enode = ""
c.ValidatorCount = 1
c.SlotTime = time.Second * 12
c.SlotsPerEpoch = 32
c.LogLvl = "info"
c.GenesisValidatorsRoot = "0x0000000000000000000000000000000000000000000000000000000000000000"
}
func (c *ConsensusCmd) Help() string {
return "Run a mock Consensus client."
}
func (c *ConsensusCmd) Run(ctx context.Context, args ...string) error {
log, err := c.LogCmd.Create()
if err != nil {
return err
}
if c.SlotTime < 50*time.Millisecond {
return fmt.Errorf("slot time %s is too small", c.SlotTime.String())
}
jwt, err := loadJwtSecret(c.JwtSecretPath)
if err != nil {
log.WithField("err", err).Fatal("Unable to read JWT secret")
}
c.jwtSecret = jwt
log.WithField("val", common.Bytes2Hex(c.jwtSecret[:])).Info("Loaded JWT secret")
c.genesisValidatorsRoot = types.Root(common.HexToHash(c.GenesisValidatorsRoot))
// Connect to execution client engine api
client, err := rpc.DialContext(ctx, c.EngineAddr, c.jwtSecret)
if err != nil {
return err
}
// Create a validator identities
if c.BuilderAddr != "" {
var registrations []types.SignedValidatorRegistration
for i := 0; i < int(c.ValidatorCount); i++ {
sk, err := blst.RandKey()
if err != nil {
return errors.New("unable to generate bls key pair")
}
var pk types.PublicKey
pk.FromSlice(sk.PublicKey().Marshal())
msg := &types.RegisterValidatorRequestMessage{
FeeRecipient: types.Address{0x42},
GasLimit: 30_000_000,
Timestamp: uint64(time.Now().Unix()),
Pubkey: pk,
}
root, err := types.ComputeSigningRoot(msg, types.DomainBuilder)
if err != nil {
return err
}
var sig types.Signature
sig.FromSlice(sk.Sign(root[:]).Marshal())
registrations = append(registrations, types.SignedValidatorRegistration{Message: msg, Signature: sig})
c.validators = append(c.validators, validator{pk, sk})
}
if err := api.BuilderRegisterValidators(ctx, log, c.BuilderAddr, registrations); err != nil {
return err
}
}
c.ethashCfg = ethash.Config{
PowMode: ethash.ModeNormal,
DatasetDir: c.EthashDir,
CacheDir: c.EthashDir,
DatasetsInMem: 1,
DatasetsOnDisk: 2,
CachesInMem: 2,
CachesOnDisk: 3,
}
db, err := NewDB(c.DataDir)
if err != nil {
return fmt.Errorf("failed to open new db: %v", err)
}
c.log = log
c.engine = client
c.db = db
c.ctx = ctx
c.close = make(chan struct{})
go c.RunNode()
return nil
}
func (c *ConsensusCmd) SlotTimestamp(slot uint64) uint64 {
return c.BeaconGenesisTime + uint64((time.Duration(slot) * c.SlotTime).Seconds())
}
func (c *ConsensusCmd) ValidateTimestamp(timestamp uint64, slot uint64) error {
expectedTimestamp := c.BeaconGenesisTime + uint64((time.Duration(slot) * c.SlotTime).Seconds())
if timestamp != expectedTimestamp {
return fmt.Errorf("wrong timestamp: got %d, expected %d", timestamp, expectedTimestamp)
}
return nil
}
func (c *ConsensusCmd) proofOfWorkPrelogue(log logrus.Ext1FieldLogger) (transitionBlock uint64, err error) {
// Create a temporary chain around the db, with ethash consensus, to run through the POW part.
engine := ethash.New(c.ethashCfg, nil, false)
mc, err := NewMockChain(log, engine, c.GenesisPath, c.db, &c.TraceLogConfig)
if err != nil {
return 0, fmt.Errorf("unable to initialize mock chain: %v", err)
}
if mc.chain.Config().TerminalTotalDifficulty.Cmp(common.Big0) != 1 {
// Already transitioned
return 0, nil
}
// Dial the peer to feed the POW blocks to
n, err := enode.Parse(enode.ValidSchemes, c.Enode)
if err != nil {
return 0, fmt.Errorf("malformatted enode address (%q): %v", c.Enode, err)
}
peer, err := p2p.Dial(n)
if err != nil {
return 0, fmt.Errorf("unable to connect to client: %v", err)
}
if err := peer.Peer(mc.chain, nil); err != nil {
return 0, fmt.Errorf("unable to peer with client: %v", err)
}
ctx, cancelPeer := context.WithCancel(c.ctx)
defer cancelPeer()
// keep peer connection alive until after the transition
go peer.KeepAlive(ctx, log)
defer mc.Close()
defer engine.Close()
// Send pre-transition blocks
for {
parent := mc.CurrentHeader()
if c.RNG.Float64() < c.Freq.ReorgFreq {
parent = c.calcReorgTarget(mc.chain, parent.Number.Uint64(), 0)
}
// build a block, without using the engine, and insert it into the engine
block, err := mc.MineBlock(parent)
if err != nil {
return 0, fmt.Errorf("failed to mine block: %v", err)
}
// announce block
newBlock := eth.NewBlockPacket{Block: block, TD: mc.CurrentTd()}
if err := peer.Write66(&newBlock, 23); err != nil {
return 0, fmt.Errorf("failed to msg peer: %v", err)
}
// check if terminal total difficulty is reached
ttd := mc.chain.Config().TerminalTotalDifficulty
td := mc.CurrentTd()
log.WithField("td", td).WithField("ttd", ttd).Debug("Comparing TD to terminal TD")
if td.Cmp(ttd) >= 0 {
log.Info("Terminal total difficulty reached, transitioning to POS")
return mc.CurrentHeader().Number.Uint64(), nil
}
}
}
func (c *ConsensusCmd) RunNode() {
var (
genesisTime = time.Unix(int64(c.BeaconGenesisTime), 0)
slots = time.NewTicker(c.SlotTime)
transitionBlock = uint64(0)
finalizedHash = common.Hash{}
safeHash = common.Hash{}
nextFinalized = common.Hash{}
posEngine = &ExecutionConsensusMock{
pow: ethash.New(c.ethashCfg, nil, false),
log: c.log,
}
payloadId = make(chan types.PayloadID)
)
defer slots.Stop()
// Run PoW prelouge if peered with client
if c.Enode != "" {
var err error
nr, err := c.proofOfWorkPrelogue(c.log.WithField("transitioned", false))
if err != nil {
c.log.WithField("err", err).Error("Failed to complete POW-prologue")
os.Exit(1)
}
transitionBlock = nr
} else {
c.log.Info("No peer, skipping pre-merge transition simulation, starting in POS mode")
}
// Initialize mock chain with existing db
mc, err := NewMockChain(c.log, posEngine, c.GenesisPath, c.db, &c.TraceLogConfig)
if err != nil {
c.log.WithField("err", err).Error("Unable to initialize mock chain")
os.Exit(1)
}
c.mockChain = mc
for {
select {
case tick := <-slots.C:
signedSlot := int64(math.Round(float64(tick.Sub(genesisTime)) / float64(c.SlotTime)))
if signedSlot < 0 {
// before genesis...
if signedSlot >= -10.0 {
c.log.WithField("remaining_slots", -signedSlot).Info("Counting down to genesis...")
}
continue
}
if signedSlot == 0 {
c.log.WithField("slot", 0).Info("Genesis!")
safeHash = c.mockChain.CurrentHeader().Hash()
continue
}
slot := uint64(signedSlot)
if c.SlotBound > 0 && slot > c.SlotBound {
c.log.WithField("testRuns", c.SlotBound).Info("All test runs successfully completed")
os.Exit(0)
}
if slot%c.SlotsPerEpoch == 0 {
last := finalizedHash
finalizedHash = nextFinalized
safeHash = finalizedHash
nextFinalized = c.mockChain.CurrentHeader().Hash()
c.log.WithField("slot", slot).WithField("last", last).WithField("new", finalizedHash).WithField("next", nextFinalized).Info("Finalized block updated")
}
// Gap slot
if c.RNG.Float64() < c.Freq.GapSlot {
c.log.WithField("slot", slot).Info("Mocking gap slot, no payload execution here")
// empty pending proposal
select {
case <-payloadId:
default:
}
continue
}
// Send bad hash
if c.RNG.Float64() < c.Freq.InvalidHashFreq {
c.log.Info("Sending payload with invalid hash")
payload := &types.ExecutionPayloadV1{
ParentHash: c.mockChain.CurrentHeader().Hash(),
FeeRecipient: common.Address{},
Number: c.mockChain.CurrentHeader().Number.Uint64(),
GasLimit: c.mockChain.CurrentHeader().GasLimit,
GasUsed: 0,
Timestamp: c.mockChain.CurrentHeader().Time + 1,
BaseFeePerGas: c.mockChain.CurrentHeader().BaseFee,
BlockHash: common.HexToHash("0xdeadbeef"),
}
go api.NewPayloadV1(c.ctx, c.engine, c.log, payload)
continue
}
// Fake some forking by building on an ancestor
parent := c.mockChain.CurrentHeader()
if c.RNG.Float64() < c.Freq.ReorgFreq {
min := transitionBlock
if final := c.mockChain.chain.GetHeaderByHash(finalizedHash); final != nil {
num := final.Number.Uint64()
if min < num {
min = num
}
}
parent = c.calcReorgTarget(c.mockChain.chain, parent.Number.Uint64(), min)
}
slotLog := c.log.WithField("slot", slot)
slotLog.WithField("previous", parent.Hash()).Info("Slot trigger")
// If we're proposing, get a block from the engine!
select {
case id := <-payloadId:
slotLog.WithField("payloadId", id).Info("Update forkchoice to block built by engine")
go c.mockProposal(slotLog, id, slot, false)
continue
default:
// Not proposing a block
}
// Build a block, without using the engine, and insert it into the engine
slotLog.Debug("Mocking external block")
// TODO: different proposers, gas limit (target in london) changes, etc.
coinbase := common.Address{1}
timestamp := c.SlotTimestamp(slot)
gasLimit := parent.GasLimit
extraData := []byte("proto says hi")
uncleBlocks := []*ethTypes.Header{}
creator := TransactionsCreator{c.ConsensusBehavior.TestAccounts.accounts, dummyTxCreator}
block, err := c.mockChain.AddNewBlock(parent.Hash(), coinbase, timestamp, gasLimit, creator, [32]byte{}, extraData, uncleBlocks, true)
if err != nil {
slotLog.WithError(err).Errorf("Failed to add block")
continue
}
slotLog.WithField("blockhash", block.Hash()).Debug("Built external block")
go func(log logrus.Ext1FieldLogger, block *ethTypes.Block, safe, final common.Hash) {
c.mockExecution(log, block)
latest := block.Hash()
// Note: head and safe hash are set to the same hash,
// until forkchoice updates are more attestation-weight aware.
var attributes *types.PayloadAttributesV1
if c.RNG.Float64() < c.Freq.ProposalFreq {
// proposing next slot!
attributes = c.makePayloadAttributes(slot + 1)
}
id, err := c.sendForkchoiceUpdated(latest, safe, final, attributes)
if err != nil {
maybeExit(c.SlotBound)
}
if id != nil {
payloadId <- *id
}
}(slotLog, block, safeHash, finalizedHash)
case <-c.close:
c.log.Info("Closing consensus mock node")
c.engine.Close()
if err := c.mockChain.Close(); err != nil {
c.log.WithError(err).Error("Failed closing mock chain")
}
if err := c.db.Close(); err != nil {
c.log.WithError(err).Error("Failed closing database")
}
}
}
}
func (c *ConsensusCmd) sendForkchoiceUpdated(latest, safe, final common.Hash, attributes *types.PayloadAttributesV1) (*types.PayloadID, error) {
result, _ := api.ForkchoiceUpdatedV1(c.ctx, c.engine, c.log, latest, safe, final, attributes)
if result.PayloadStatus.Status != types.ExecutionValid {
c.log.WithField("status", result.PayloadStatus).Error("Update not considered valid")
return nil, fmt.Errorf("update not considered valid")
}
return result.PayloadID, nil
}
func (c *ConsensusCmd) getMockProposal(ctx context.Context, log logrus.Ext1FieldLogger, payloadId types.PayloadID, slot uint64) (*types.ExecutionPayloadV1, error) {
// If the CL is connected to builder client, request the payload from there.
if c.BuilderAddr != "" {
idx := c.RNG.Int63n(int64(len(c.validators)))
header, err := api.BuilderGetHeader(c.ctx, log, c.BuilderAddr, slot, c.mockChain.CurrentHeader().Hash(), c.validators[idx].sk.PublicKey().Marshal())
if err != nil {
return nil, err
}
signedBlindedBeaconBlock := &types.SignedBlindedBeaconBlock{
Message: &types.BlindedBeaconBlock{
Slot: slot,
ProposerIndex: 1,
Body: &types.BlindedBeaconBlockBody{
Eth1Data: &types.Eth1Data{},
SyncAggregate: &types.SyncAggregate{},
ExecutionPayloadHeader: header,
},
},
Signature: types.Signature{},
}
domain := types.ComputeDomain(types.DomainTypeBeaconProposer, version.Bellatrix, &c.genesisValidatorsRoot)
root, err := types.ComputeSigningRoot(signedBlindedBeaconBlock.Message, domain)
if err != nil {
return nil, err
}
sig := c.validators[idx].sk.Sign(root[:]).Marshal()
signedBlindedBeaconBlock.Signature.FromSlice(sig)
payload, err := api.BuilderGetPayload(ctx, log, c.BuilderAddr, signedBlindedBeaconBlock)
if err != nil {
return nil, err
}
c.log.WithField("hash", payload.BlockHash.Hex()).Info("received payload from builder")
return payload, err
}
// Otherwise, get payload from EL.
payload, err := api.GetPayloadV1(c.ctx, c.engine, log, payloadId)
if err != nil {
return nil, err
}
return payload, err
}
func (c *ConsensusCmd) mockProposal(log logrus.Ext1FieldLogger, payloadId types.PayloadID, slot uint64, consensusFail bool) {
ctx, cancel := context.WithTimeout(c.ctx, time.Second*20)
defer cancel()
payload, err := c.getMockProposal(ctx, log, payloadId, slot)
if err != nil {
log.WithError(err).Error("Unable to retrieve proposal payload")
maybeExit(c.SlotBound)
return
}
if err := c.ValidateTimestamp(uint64(payload.Timestamp), slot); err != nil {
log.WithError(err).Error("Payload has bad timestamp")
maybeExit(c.SlotBound)
return
}
if consensusFail {
log.Debug("Mocking a failed proposal on consensus-side, ignoring produced payload of engine")
return
}
block, err := c.mockChain.ProcessPayload(payload)
if err != nil {
log.WithError(err).Error("Failed to process execution payload from engine")
maybeExit(c.SlotBound)
return
} else {
log.WithField("blockhash", block.Hash()).Debug("Processed payload in consensus mock world")
}
// Send it back to execution layer for execution
res, err := api.NewPayloadV1(ctx, c.engine, log, payload)
if err == nil && res.Status == types.ExecutionValid {
log.WithField("blockhash", block.Hash()).Debug("Processed payload in engine")
return
}
if err != nil {
log.WithError(err).Error("Failed to execute payload")
} else if res.Status == types.ExecutionInvalid {
log.WithField("blockhash", block.Hash()).Error("Engine just produced payload and failed to execute it after!")
} else {
log.WithField("status", res.Status).Error("Unrecognized execution status")
}
maybeExit(c.SlotBound)
}
func (c *ConsensusCmd) mockExecution(log logrus.Ext1FieldLogger, block *ethTypes.Block) {
ctx, cancel := context.WithTimeout(c.ctx, time.Second*20)
defer cancel()
// derive the random 32 bytes from the block hash for mocking ease
payload, err := api.BlockToPayload(block)
if err != nil {
log.WithError(err).Error("Failed to convert execution block to execution payload")
return
}
api.NewPayloadV1(ctx, c.engine, log, payload)
}
func dummyTxCreator(config *params.ChainConfig, bc core.ChainContext, statedb *state.StateDB, header *ethTypes.Header, cfg vm.Config, accounts []TestAccount) []*ethTypes.Transaction {
// TODO create some more txs and use all accounts
if len(accounts) != 0 {
signer := ethTypes.NewLondonSigner(config.ChainID)
txdata := ðTypes.DynamicFeeTx{
ChainID: config.ChainID,
Nonce: statedb.GetNonce(accounts[0].addr),
To: &accounts[0].addr,
Gas: 30000,
GasFeeCap: new(big.Int).Mul(big.NewInt(5), big.NewInt(params.GWei)),
GasTipCap: big.NewInt(2),
Data: []byte{},
}
tx := ethTypes.NewTx(txdata)
tx, _ = ethTypes.SignTx(tx, signer, accounts[0].pk)
return []*ethTypes.Transaction{tx}
} else {
return nil
}
}
func (c *ConsensusCmd) calcReorgTarget(chain *core.BlockChain, parent uint64, min uint64) *ethTypes.Header {
depth := c.RNG.Float64() * float64(c.ReorgMaxDepth)
target := uint64(math.Max(float64(parent)-depth, float64(min)))
return chain.GetHeaderByNumber(target)
}
func (c *ConsensusCmd) Close() error {
if c.close != nil {
c.close <- struct{}{}
}
return nil
}
func (c *ConsensusCmd) makePayloadAttributes(slot uint64) *types.PayloadAttributesV1 {
var prevRandao common.Hash
c.RNG.Read(prevRandao[:])
return &types.PayloadAttributesV1{
Timestamp: c.SlotTimestamp(slot),
PrevRandao: prevRandao,
SuggestedFeeRecipient: common.Address{0x13, 0x37},
}
}
func maybeExit(val uint64) {
if val != 0 {
os.Exit(1)
}
}