Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move validator set snapshot computation to bor_heimdall stage #8646

Merged
merged 58 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
8eb752e
Move validator set snapshot computation to bor_heimdall stage
AlexeyAkhunov Sep 14, 2023
b5bfa01
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Sep 15, 2023
d58d49d
Bor
AlexeyAkhunov Sep 21, 2023
51cd65f
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Sep 22, 2023
cb3c197
Intermediate
AlexeyAkhunov Oct 2, 2023
7587e79
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 2, 2023
0edce98
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 3, 2023
7ec0a99
Remove logging
AlexeyAkhunov Oct 3, 2023
6e02930
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 6, 2023
d7c0973
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 6, 2023
f2f992c
Verifications
AlexeyAkhunov Oct 6, 2023
4d391c3
Get 0 span
AlexeyAkhunov Oct 6, 2023
9f99972
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 6, 2023
504534b
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 7, 2023
e5443ca
commit span
AlexeyAkhunov Oct 7, 2023
707453b
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 7, 2023
060646c
Fix unwind
AlexeyAkhunov Oct 8, 2023
92ac166
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 8, 2023
024dd57
Remove changes
AlexeyAkhunov Oct 8, 2023
cedbf7f
Cleanup
AlexeyAkhunov Oct 8, 2023
dc70f94
Cleanup
AlexeyAkhunov Oct 8, 2023
ee4e5f2
Fetch one span in advance
AlexeyAkhunov Oct 9, 2023
13a6e09
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 9, 2023
771b526
Move extraData check to bor_heimdall stage
AlexeyAkhunov Oct 10, 2023
b70d79b
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 10, 2023
9c6e4db
Fixup
AlexeyAkhunov Oct 10, 2023
fb5a555
Intermediate
AlexeyAkhunov Oct 10, 2023
5a18fa5
Fixes
AlexeyAkhunov Oct 12, 2023
6d71289
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 12, 2023
a2c218e
Intermediate
AlexeyAkhunov Oct 12, 2023
e92aced
Intermediate|
AlexeyAkhunov Oct 15, 2023
be9e1f1
fixes
AlexeyAkhunov Oct 15, 2023
1be621c
Merge remote-tracking branch 'origin' into valid-snap
AlexeyAkhunov Oct 15, 2023
60fd453
Restore validator set
AlexeyAkhunov Oct 15, 2023
5070625
Fix lint
Oct 15, 2023
23c25eb
Debug print
AlexeyAkhunov Oct 16, 2023
2e0cc7c
Fix tests
AlexeyAkhunov Oct 17, 2023
720c060
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 17, 2023
a53691c
combine recents and signatures
Oct 17, 2023
f5851de
Fix compilation
Oct 17, 2023
5d9f2c3
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 18, 2023
b2faa44
Add optimisation
AlexeyAkhunov Oct 18, 2023
1c8f484
Fix BorSpan lookup
AlexeyAkhunov Oct 18, 2023
92aed2e
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 18, 2023
6ba3443
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 19, 2023
7a864b6
Fix logging
AlexeyAkhunov Oct 19, 2023
42b9328
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 19, 2023
6788b4a
Fix merge cleanup for borspan files
AlexeyAkhunov Oct 19, 2023
7bdf937
Merge remote-tracking branch 'origin/devel' into valid-snap
AlexeyAkhunov Oct 20, 2023
0cd719e
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
4c64a25
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
f3d4d3e
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
d379133
cleanup
AlexeyAkhunov Nov 4, 2023
caaca20
cleanup
AlexeyAkhunov Nov 4, 2023
038251b
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
921273a
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
8e8c45c
Merge remote-tracking branch 'origin/devel' into valid-snap-1
AlexeyAkhunov Nov 4, 2023
159dee7
Fix mumbai sync from scratch
AlexeyAkhunov Nov 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get 0 span
  • Loading branch information
AlexeyAkhunov committed Oct 6, 2023
commit 4d391c309715fafe33cf10623986b8ff9c0f3150
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/rpcservices/eth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ func (back *RemoteBackend) EventLookup(ctx context.Context, tx kv.Getter, txnHas
func (back *RemoteBackend) EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockNum uint64) ([]rlp.RawValue, error) {
return back.blockReader.EventsByBlock(ctx, tx, hash, blockNum)
}
func (back *RemoteBackend) Span(ctx context.Context, spanId uint64) ([]byte, error) {
return back.blockReader.Span(ctx, spanId)
func (back *RemoteBackend) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
return back.blockReader.Span(ctx, tx, spanId)
}

func (back *RemoteBackend) NodeInfo(ctx context.Context, limit uint32) ([]p2p.NodeInfo, error) {
Expand Down
63 changes: 1 addition & 62 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,19 +653,6 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t
}
}
}
/*
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
return err
}
// len(header.Extra) >= extraVanity+extraSeal has already been validated in ValidateHeaderExtraField, so this won't result in a panic
if !bytes.Equal(parentValidatorBytes, validatorsBytes) {
return &MismatchingValidatorsError{number - 1, validatorsBytes, parentValidatorBytes}
}

// All basic checks passed, verify the seal and return
return c.verifySeal(chain, header, parents, snap)
*/
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove validator list & seal verification?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, where is this checked now?

}

Expand Down Expand Up @@ -1053,16 +1040,9 @@ func (c *Bor) FinalizeAndAssemble(chainConfig *chain.Config, header *types.Heade
if isSprintStart(headerNumber, c.config.CalculateSprint(headerNumber)) {
cx := statefull.ChainContext{Chain: chain, Bor: c}

// check and commit span
err := c.checkAndCommitSpan(state, header, cx, syscall)
if err != nil {
c.logger.Error("Error while committing span", "err", err)
return nil, nil, types.Receipts{}, err
}

if c.HeimdallClient != nil {
// commit states
if err = c.CommitStates(state, header, cx, syscall); err != nil {
if err := c.CommitStates(state, header, cx, syscall); err != nil {
c.logger.Error("Error while committing states", "err", err)
return nil, nil, types.Receipts{}, err
}
Expand Down Expand Up @@ -1304,47 +1284,6 @@ func (c *Bor) Close() error {
return nil
}

func (c *Bor) checkAndCommitSpan(
state *state.IntraBlockState,
header *types.Header,
chain statefull.ChainContext,
syscall consensus.SystemCall,
) error {
headerNumber := header.Number.Uint64()

span, err := c.spanner.GetCurrentSpan(syscall)
if err != nil {
return err
}

if c.needToCommitSpan(span, headerNumber) {
err := c.fetchAndCommitSpan(span.ID+1, state, header, chain, syscall)
return err
}

return nil
}

func (c *Bor) needToCommitSpan(currentSpan *span.Span, headerNumber uint64) bool {
// if span is nil
if currentSpan == nil {
return false
}

// check span is not set initially
if currentSpan.EndBlock == 0 {
return true
}
sprintLength := c.config.CalculateSprint(headerNumber)

// if current block is first block of last sprint in current span
if currentSpan.EndBlock > sprintLength && currentSpan.EndBlock-sprintLength+1 == headerNumber {
return true
}

return false
}

func (c *Bor) getSpanForBlock(blockNum uint64) (*span.HeimdallSpan, error) {
c.logger.Debug("Getting span", "for block", blockNum)
var borSpan *span.HeimdallSpan
Expand Down
10 changes: 6 additions & 4 deletions consensus/bor/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ func (s *Snapshot) Apply(parent *types.Header, headers []*types.Header, logger l
// Sanity check that the headers can be applied
for i := 0; i < len(headers)-1; i++ {
if headers[i+1].Number.Uint64() != headers[i].Number.Uint64()+1 {
logger.Error("out of range", "header", headers[0].Number.Uint64(), "parent", headers[i].Number.Uint64())
return nil, errOutOfRangeChain
}
}

if headers[0].Number.Uint64() != s.Number+1 {
logger.Error("out of range", "header", headers[0].Number.Uint64(), "snapshot", s.Number)
return nil, errOutOfRangeChain
}
// Iterate through the headers and create a new snapshot
Expand Down Expand Up @@ -161,7 +163,7 @@ func (s *Snapshot) Apply(parent *types.Header, headers []*types.Header, logger l
return snap, &UnauthorizedSignerError{header.Hash(), number, signer.Bytes()}
}
if succession, err = snap.GetSignerSuccessionNumber(signer); err != nil {
return nil, err
return snap, err
}

// add recents
Expand All @@ -170,13 +172,13 @@ func (s *Snapshot) Apply(parent *types.Header, headers []*types.Header, logger l
validSigner = true

if parent != nil && header.Time < parent.Time+CalcProducerDelay(number, succession, s.config) {
return nil, &BlockTooSoonError{number, succession}
return snap, &BlockTooSoonError{number, succession}
}

// change validator set and change proposer
if number > 0 && (number+1)%sprintLen == 0 {
if err := ValidateHeaderExtraField(header.Extra); err != nil {
return nil, err
return snap, err
}
validatorBytes := header.Extra[extraVanity : len(header.Extra)-extraSeal]

Expand All @@ -188,7 +190,7 @@ func (s *Snapshot) Apply(parent *types.Header, headers []*types.Header, logger l
}

if number > 64 && !validSigner {
return nil, &UnauthorizedSignerError{header.Hash(), number, signer.Bytes()}
return snap, &UnauthorizedSignerError{header.Hash(), number, signer.Bytes()}
}
parent = header
}
Expand Down
23 changes: 10 additions & 13 deletions eth/stagedsync/stage_bor_heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/ledgerwatch/erigon/consensus/bor/contract"
"github.com/ledgerwatch/erigon/consensus/bor/heimdall"
"github.com/ledgerwatch/erigon/consensus/bor/heimdall/span"
"github.com/ledgerwatch/erigon/consensus/bor/valset"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/dataflow"
"github.com/ledgerwatch/erigon/eth/borfinality/generics"
Expand Down Expand Up @@ -248,7 +247,7 @@ func BorHeimdallForward(
return err
}
}
if err = persistValidatorSets(u, ctx, cfg.blockReader, cfg.chainConfig.Bor, chain, blockNum, header.Hash(), recents, signatures, cfg.snapDb, logger); err != nil {
if err = persistValidatorSets(u, ctx, tx, cfg.blockReader, cfg.chainConfig.Bor, chain, blockNum, header.Hash(), recents, signatures, cfg.snapDb, logger); err != nil {
return fmt.Errorf("persistValidatorSets: %w", err)
}
}
Expand Down Expand Up @@ -406,6 +405,7 @@ func fetchAndWriteSpans(
func persistValidatorSets(
u Unwinder,
ctx context.Context,
tx kv.Tx,
blockReader services.FullBlockReader,
config *chain.BorConfig,
chain consensus.ChainHeaderReader,
Expand Down Expand Up @@ -486,22 +486,17 @@ func persistValidatorSets(
hash := zeroHeader.Hash()

// get validators and current span
zeroSpanBytes, err := blockReader.Span(ctx, 0)
zeroSpanBytes, err := blockReader.Span(ctx, tx, 0)
if err != nil {
return err
}
var zeroSpan *span.HeimdallSpan
if err = json.Unmarshal(zeroSpanBytes, zeroSpan); err != nil {
var zeroSpan span.HeimdallSpan
if err = json.Unmarshal(zeroSpanBytes, &zeroSpan); err != nil {
return err
}

validators := make([]*valset.Validator, len(zeroSpan.SelectedProducers))
for i := range zeroSpan.SelectedProducers {
validators[i] = &zeroSpan.SelectedProducers[i]
}

// new snap shot
snap = bor.NewSnapshot(config, signatures, 0, hash, validators, logger)
snap = bor.NewSnapshot(config, signatures, 0, hash, zeroSpan.ValidatorSet.Validators, logger)
if err := snap.Store(snapDb); err != nil {
return fmt.Errorf("snap.Store (0): %w", err)
}
Expand All @@ -513,8 +508,10 @@ func persistValidatorSets(
initialHeaders = append(initialHeaders, header)
if len(initialHeaders) == cap(initialHeaders) {
if snap, err = snap.Apply(parentHeader, initialHeaders, logger); err != nil {
if signErr, ok := err.(*bor.UnauthorizedSignerError); ok {
u.UnwindTo(signErr.Number-1, signErr.Hash)
if snap != nil {
// Error is not critical and means that the header is invalid
//u.UnwindTo(signErr.Number-1, signErr.Hash)
return fmt.Errorf("snap.Apply (inside loop): %w", err)
} else {
return fmt.Errorf("snap.Apply (inside loop): %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/services/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type BorEventReader interface {
}

type BorSpanReader interface {
Span(ctx context.Context, spanNum uint64) ([]byte, error)
Span(ctx context.Context, tx kv.Getter, spanNum uint64) ([]byte, error)
}

type CanonicalReader interface {
Expand Down
43 changes: 40 additions & 3 deletions turbo/snapshotsync/freezeblocks/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (r *RemoteBlockReader) EventsByBlock(ctx context.Context, tx kv.Tx, hash co
return result, nil
}

func (r *RemoteBlockReader) Span(ctx context.Context, spanId uint64) ([]byte, error) {
func (r *RemoteBlockReader) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
return nil, nil
}

Expand Down Expand Up @@ -1082,6 +1082,43 @@ func (r *BlockReader) LastFrozenEventID() uint64 {
return lastEventID
}

func (r *BlockReader) Span(ctx context.Context, spanId uint64) ([]byte, error) {
return nil, nil
func (r *BlockReader) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]byte, error) {
// Compute starting block of the span
var startBlock uint64
if spanId > 0 {
startBlock = (spanId-1)*spanLength + zerothSpanEnd + 1
}
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], spanId)
if startBlock >= r.FrozenBorBlocks() {
v, err := tx.GetOne(kv.BorSpans, buf[:])
if err != nil {
return nil, err
}
if v == nil {
return nil, fmt.Errorf("span %d not found", spanId)
}
return common.Copy(v), nil
}
view := r.borSn.View()
defer view.Close()
segments := view.Spans()
for i := len(segments) - 1; i >= 0; i-- {
sn := segments[i]
if sn.ranges.from > startBlock {
continue
}
if sn.ranges.to <= startBlock {
continue
}
if sn.idx.KeyCount() == 0 {
continue
}
offset := sn.idx.OrdinalLookup(spanId)
gg := sn.seg.MakeGetter()
gg.Reset(offset)
result, _ := gg.Next(nil)
return common.Copy(result), nil
}
return nil, fmt.Errorf("span %d not found", spanId)
}