Skip to content

Commit 5e69adc

Browse files
authored
cherry-pick 9907be8 polygon: Implement VeBlop consensus changes (#16803)
Cherry-pick 9907be8 from release/3.0 to main. --------- Co-authored-by: antonis19 <[email protected]>
1 parent 6cc7320 commit 5e69adc

17 files changed

+215
-59
lines changed

core/genesis_write.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"github.com/erigontech/erigon/execution/chain/params"
5353
chainspec "github.com/erigontech/erigon/execution/chain/spec"
5454
"github.com/erigontech/erigon/execution/types"
55+
polygonchain "github.com/erigontech/erigon/polygon/chain"
5556
)
5657

5758
// GenesisMismatchError is raised when trying to overwrite an existing
@@ -507,6 +508,14 @@ func GenesisWithoutStateToBlock(g *types.Genesis) (head *types.Header, withdrawa
507508
}
508509
}
509510

511+
// these fields need to be overriden for Bor running in a kurtosis devnet
512+
if g.Config != nil && g.Config.Bor != nil && g.Config.ChainID.Uint64() == polygonchain.BorKurtosisDevnetChainId {
513+
withdrawals = []*types.Withdrawal{}
514+
head.BlobGasUsed = new(uint64)
515+
head.ExcessBlobGas = new(uint64)
516+
emptyHash := common.HexToHash("0x0")
517+
head.ParentBeaconBlockRoot = &emptyHash
518+
}
510519
return
511520
}
512521

execution/consensus/misc/eip2935.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/holiman/uint256"
2121

2222
"github.com/erigontech/erigon-lib/common"
23-
"github.com/erigontech/erigon-lib/log/v3"
2423
"github.com/erigontech/erigon/core/state"
2524
"github.com/erigontech/erigon/execution/chain"
2625
"github.com/erigontech/erigon/execution/chain/params"
@@ -34,7 +33,7 @@ func StoreBlockHashesEip2935(header *types.Header, state *state.IntraBlockState,
3433
return err
3534
}
3635
if codeSize == 0 {
37-
log.Debug("[EIP-2935] No code deployed to HistoryStorageAddress before call to store EIP-2935 history")
36+
// log.Debug("[EIP-2935] No code deployed to HistoryStorageAddress before call to store EIP-2935 history")
3837
return nil
3938
}
4039
headerNum := header.Number.Uint64()

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ require (
8080
github.com/huin/goupnp v1.3.0
8181
github.com/jackpal/go-nat-pmp v1.0.2
8282
github.com/jedib0t/go-pretty/v6 v6.5.9
83+
github.com/jellydator/ttlcache/v3 v3.4.0
8384
github.com/jinzhu/copier v0.4.0
8485
github.com/json-iterator/go v1.1.12
8586
github.com/klauspost/compress v1.18.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0
551551
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
552552
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
553553
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
554+
github.com/jellydator/ttlcache/v3 v3.4.0 h1:YS4P125qQS0tNhtL6aeYkheEaB/m8HCqdMMP4mnWdTY=
555+
github.com/jellydator/ttlcache/v3 v3.4.0/go.mod h1:Hw9EgjymziQD3yGsQdf1FqFdpp7YjFMd4Srg5EJlgD4=
554556
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
555557
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
556558
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

polygon/bor/bor.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ func CalcProducerDelay(number uint64, succession int, c *borcfg.BorConfig) uint6
182182
// When the block is the first block of the sprint, it is expected to be delayed by `producerDelay`.
183183
// That is to allow time for block propagation in the last sprint
184184
delay := c.CalculatePeriod(number)
185+
// Since there is only one producer in veblop, we don't need to add producer delay and backup multiplier
186+
if c.IsVeBlop(number) {
187+
return delay
188+
}
185189
if c.IsSprintStart(number) {
186190
delay = c.CalculateProducerDelay(number)
187191
}
@@ -807,11 +811,14 @@ func (c *Bor) Finalize(config *chain.Config, header *types.Header, state *state.
807811
cx := statefull.ChainContext{Chain: chain, Bor: c}
808812

809813
if c.blockReader != nil {
810-
// check and commit span
811-
if err := c.checkAndCommitSpan(header, syscall); err != nil {
812-
err := fmt.Errorf("Finalize.checkAndCommitSpan: %w", err)
813-
c.logger.Error("[bor] committing span", "err", err)
814-
return nil, err
814+
// post VeBlop spans won't be committed to smart contract
815+
if !c.config.IsVeBlop(header.Number.Uint64()) {
816+
// check and commit span
817+
if err := c.checkAndCommitSpan(header, syscall); err != nil {
818+
err := fmt.Errorf("Finalize.checkAndCommitSpan: %w", err)
819+
c.logger.Error("[bor] committing span", "err", err)
820+
return nil, err
821+
}
815822
}
816823

817824
// commit states
@@ -869,11 +876,14 @@ func (c *Bor) FinalizeAndAssemble(chainConfig *chain.Config, header *types.Heade
869876
cx := statefull.ChainContext{Chain: chain, Bor: c}
870877

871878
if c.blockReader != nil {
872-
// check and commit span
873-
if err := c.checkAndCommitSpan(header, syscall); err != nil {
874-
err := fmt.Errorf("FinalizeAndAssemble.checkAndCommitSpan: %w", err)
875-
c.logger.Error("[bor] committing span", "err", err)
876-
return nil, nil, err
879+
// Post VeBlop spans won't be commited to smart contract
880+
if !c.config.IsVeBlop(header.Number.Uint64()) {
881+
// check and commit span
882+
if err := c.checkAndCommitSpan(header, syscall); err != nil {
883+
err := fmt.Errorf("FinalizeAndAssemble.checkAndCommitSpan: %w", err)
884+
c.logger.Error("[bor] committing span", "err", err)
885+
return nil, nil, err
886+
}
877887
}
878888
// commit states
879889
if err := c.CommitStates(header, cx, syscall, true); err != nil {

polygon/chain/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ var (
5353
BorDevnetChainConfig = readChainSpec("chainspecs/bor-devnet.json")
5454
)
5555

56+
var (
57+
BorKurtosisDevnetChainId uint64 = 4927
58+
)
59+
5660
func init() {
5761
chainspec.RegisterChain(networkname.Amoy, AmoyChainConfig, AmoyGenesisBlock(), AmoyGenesisHash, AmoyBootnodes,
5862
"enrtree://AKUEZKN7PSKVNR65FZDHECMKOJQSGPARGTPPBI7WS2VUL4EGR6XPC@amoy.polygon-peers.io")

polygon/heimdall/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func (s *Service) SynchronizeMilestones(ctx context.Context) (*Milestone, bool,
178178
return s.milestoneScraper.Synchronize(ctx)
179179
}
180180

181+
func (s *Service) AnticipateNewSpanWithTimeout(ctx context.Context, timeout time.Duration) (bool, error) {
182+
s.logger.Info(heimdallLogPrefix(fmt.Sprintf("anticipating new span update within %.0f seconds", timeout.Seconds())))
183+
return s.spanBlockProducersTracker.AnticipateNewSpanWithTimeout(ctx, timeout)
184+
}
185+
181186
func (s *Service) SynchronizeSpans(ctx context.Context, blockNum uint64) error {
182187
s.logger.Debug(heimdallLogPrefix("synchronizing spans..."), "blockNum", blockNum)
183188

polygon/heimdall/span_block_producers_tracker.go

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,32 @@ func newSpanBlockProducersTracker(
4040
}
4141

4242
return &spanBlockProducersTracker{
43-
logger: logger,
44-
borConfig: borConfig,
45-
store: store,
46-
recentSelections: recentSelectionsLru,
47-
newSpans: make(chan *Span),
48-
idleSignal: make(chan struct{}),
43+
logger: logger,
44+
borConfig: borConfig,
45+
store: store,
46+
recentSelections: recentSelectionsLru,
47+
newSpans: make(chan *Span),
48+
idleSignal: make(chan struct{}),
49+
spanProcessedSignal: make(chan struct{}),
4950
}
5051
}
5152

5253
type spanBlockProducersTracker struct {
53-
logger log.Logger
54-
borConfig *borcfg.BorConfig
55-
store EntityStore[*SpanBlockProducerSelection]
56-
recentSelections *lru.Cache[uint64, SpanBlockProducerSelection] // sprint number -> SpanBlockProducerSelection
57-
newSpans chan *Span
58-
queued atomic.Int32
59-
idleSignal chan struct{}
54+
logger log.Logger
55+
borConfig *borcfg.BorConfig
56+
store EntityStore[*SpanBlockProducerSelection]
57+
recentSelections *lru.Cache[uint64, SpanBlockProducerSelection] // sprint number -> SpanBlockProducerSelection
58+
newSpans chan *Span
59+
queued atomic.Int32
60+
idleSignal chan struct{}
61+
spanProcessedSignal chan struct{} // signal that a new span was fully processed
6062
}
6163

6264
func (t *spanBlockProducersTracker) Run(ctx context.Context) error {
6365
t.logger.Info(heimdallLogPrefix("running span block producers tracker component"))
6466

6567
defer close(t.idleSignal)
68+
defer close(t.spanProcessedSignal)
6669
for {
6770
select {
6871
case <-ctx.Done():
@@ -73,6 +76,12 @@ func (t *spanBlockProducersTracker) Run(ctx context.Context) error {
7376
return err
7477
}
7578

79+
// signal that the span was observed (non-blocking)
80+
select {
81+
case t.spanProcessedSignal <- struct{}{}:
82+
default:
83+
}
84+
7685
t.queued.Add(-1)
7786
if t.queued.Load() == 0 {
7887
select {
@@ -84,6 +93,23 @@ func (t *spanBlockProducersTracker) Run(ctx context.Context) error {
8493
}
8594
}
8695

96+
// Anticipates a new span to be observe and fully processed withing the given timeout period.
97+
// Returns true if a new span was processed, false if no new span was processed
98+
func (t *spanBlockProducersTracker) AnticipateNewSpanWithTimeout(ctx context.Context, timeout time.Duration) (bool, error) {
99+
select {
100+
case <-ctx.Done():
101+
return false, ctx.Err()
102+
case _, ok := <-t.spanProcessedSignal:
103+
if !ok {
104+
return false, errors.New("spanProcessed channel was closed")
105+
}
106+
return true, nil
107+
108+
case <-time.After(timeout): // timeout
109+
}
110+
return false, nil
111+
}
112+
87113
func (t *spanBlockProducersTracker) Synchronize(ctx context.Context) error {
88114
if t.queued.Load() == 0 {
89115
return nil
@@ -111,7 +137,7 @@ func (t *spanBlockProducersTracker) ObserveSpanAsync(ctx context.Context, span *
111137
}
112138

113139
func (t *spanBlockProducersTracker) ObserveSpan(ctx context.Context, newSpan *Span) error {
114-
t.logger.Debug(heimdallLogPrefix("block producers tracker observing span"), "id", newSpan.Id)
140+
t.logger.Debug(heimdallLogPrefix("block producers tracker observing span"), "newSpan", newSpan)
115141

116142
lastProducerSelection, ok, err := t.store.LastEntity(ctx)
117143
if err != nil {
@@ -204,11 +230,6 @@ func (t *spanBlockProducersTracker) Producers(ctx context.Context, blockNum uint
204230
func (t *spanBlockProducersTracker) producers(ctx context.Context, blockNum uint64) (*ValidatorSet, int, error) {
205231
currentSprintNum := t.borConfig.CalculateSprintNumber(blockNum)
206232

207-
// have we previously calculated the producers for the same sprint num (chain tip optimisation)
208-
if selection, ok := t.recentSelections.Get(currentSprintNum); ok {
209-
return selection.Producers.Copy(), 0, nil
210-
}
211-
212233
// have we previously calculated the producers for the previous sprint num of the same span (chain tip optimisation)
213234
spanId, ok, err := t.store.EntityIdFromBlockNum(ctx, blockNum)
214235
if err != nil {
@@ -217,20 +238,7 @@ func (t *spanBlockProducersTracker) producers(ctx context.Context, blockNum uint
217238
if !ok {
218239
return nil, 0, fmt.Errorf("could not get spanId from blockNum=%d", blockNum)
219240
}
220-
var prevSprintNum uint64
221-
if currentSprintNum > 0 {
222-
prevSprintNum = currentSprintNum - 1
223-
}
224-
if selection, ok := t.recentSelections.Get(prevSprintNum); ok && SpanId(spanId) == selection.SpanId {
225-
producersCopy := selection.Producers.Copy()
226-
producersCopy.IncrementProposerPriority(1)
227-
selectionCopy := selection
228-
selectionCopy.Producers = producersCopy
229-
t.recentSelections.Add(currentSprintNum, selectionCopy)
230-
return producersCopy, 1, nil
231-
}
232241

233-
// no recent selection that we can easily use, re-calculate from DB
234242
producerSelection, ok, err := t.store.Entity(ctx, spanId)
235243
if err != nil {
236244
return nil, 0, err
@@ -252,7 +260,5 @@ func (t *spanBlockProducersTracker) producers(ctx context.Context, blockNum uint
252260
producers = GetUpdatedValidatorSet(producers, producers.Validators, t.logger)
253261
producers.IncrementProposerPriority(1)
254262
}
255-
256-
t.recentSelections.Add(currentSprintNum, *producerSelection)
257263
return producers, increments, nil
258264
}

polygon/heimdall/validator_set.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ func NewValidatorSet(valz []*Validator) *ValidatorSet {
239239
return vals
240240
}
241241

242+
func (vals *ValidatorSet) ValidatorAddresses() []common.Address {
243+
addresses := make([]common.Address, len(vals.Validators))
244+
for i, val := range vals.Validators {
245+
addresses[i] = val.Address
246+
}
247+
return addresses
248+
}
249+
242250
// Nil or empty validator sets are invalid.
243251
func (vals *ValidatorSet) IsNilOrEmpty() bool {
244252
return vals == nil || len(vals.Validators) == 0

polygon/sync/block_producers_reader.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ package sync
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
"github.com/erigontech/erigon/polygon/heimdall"
2324
)
2425

2526
type blockProducersReader interface {
2627
Producers(ctx context.Context, blockNum uint64) (*heimdall.ValidatorSet, error)
2728
}
29+
30+
type blockProducersTracker interface {
31+
blockProducersReader
32+
AnticipateNewSpanWithTimeout(ctx context.Context, timeout time.Duration) (bool, error)
33+
}

0 commit comments

Comments
 (0)