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

heimdall: use span id as naming #9097

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header, s
// where it fetches producers internally. As we fetch data from span
// in Erigon, use directly the `GetCurrentProducers` function.
if isSprintStart(number+1, c.config.CalculateSprint(number)) {
spanID := span.NumAt(number + 1)
spanID := span.IDAt(number + 1)
newValidators, err := c.spanner.GetCurrentProducers(spanID, c.authorizedSigner.Load().signer, chain)
if err != nil {
return errUnknownValidators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ const (
zerothSpanEnd = 255 // End block of 0th span
)

// NumAt returns the corresponding span number for the given block number.
func NumAt(blockNum uint64) uint64 {
// IDAt returns the corresponding span id for the given block number.
func IDAt(blockNum uint64) uint64 {
if blockNum > zerothSpanEnd {
return 1 + (blockNum-zerothSpanEnd-1)/spanLength
}
return 0
}

// EndBlockNum returns the number of the last block in the given span number.
func EndBlockNum(spanNum uint64) uint64 {
if spanNum > 0 {
return spanNum*spanLength + zerothSpanEnd
// EndBlockNum returns the number of the last block in the given span.
func EndBlockNum(spanID uint64) uint64 {
if spanID > 0 {
return spanID*spanLength + zerothSpanEnd
}
return zerothSpanEnd
}

// BlockInLastSprintOfSpan returns true if a block num is within the last sprint of a span and false otherwise.
func BlockInLastSprintOfSpan(blockNum uint64, config *chain.BorConfig) bool {
spanNum := NumAt(blockNum)
spanNum := IDAt(blockNum)
endBlockNum := EndBlockNum(spanNum)
sprintLen := config.CalculateSprint(blockNum)
startBlockNum := endBlockNum - sprintLen + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
"github.com/ledgerwatch/erigon-lib/chain"
)

func TestSpanNumAt(t *testing.T) {
assert.Equal(t, uint64(0), NumAt(0))
assert.Equal(t, uint64(0), NumAt(1))
assert.Equal(t, uint64(0), NumAt(2))
assert.Equal(t, uint64(0), NumAt(zerothSpanEnd))
assert.Equal(t, uint64(1), NumAt(zerothSpanEnd+1))
assert.Equal(t, uint64(1), NumAt(zerothSpanEnd+2))
assert.Equal(t, uint64(1), NumAt(6655))
assert.Equal(t, uint64(2), NumAt(6656))
assert.Equal(t, uint64(2), NumAt(6657))
assert.Equal(t, uint64(2), NumAt(13055))
assert.Equal(t, uint64(3), NumAt(13056))
assert.Equal(t, uint64(6839), NumAt(43763456))
func TestSpanIDAt(t *testing.T) {
assert.Equal(t, uint64(0), IDAt(0))
assert.Equal(t, uint64(0), IDAt(1))
assert.Equal(t, uint64(0), IDAt(2))
assert.Equal(t, uint64(0), IDAt(zerothSpanEnd))
assert.Equal(t, uint64(1), IDAt(zerothSpanEnd+1))
assert.Equal(t, uint64(1), IDAt(zerothSpanEnd+2))
assert.Equal(t, uint64(1), IDAt(6655))
assert.Equal(t, uint64(2), IDAt(6656))
assert.Equal(t, uint64(2), IDAt(6657))
assert.Equal(t, uint64(2), IDAt(13055))
assert.Equal(t, uint64(3), IDAt(13056))
assert.Equal(t, uint64(6839), IDAt(43763456))
}

func TestSpanEndBlockNum(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions eth/stagedsync/stage_bor_heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func BorHeimdallForward(
nextSpanId = lastSpanId + 1
}
var endSpanID uint64
if span.NumAt(headNumber) > 0 {
endSpanID = span.NumAt(headNumber + 1)
if span.IDAt(headNumber) > 0 {
endSpanID = span.IDAt(headNumber + 1)
}

if span.BlockInLastSprintOfSpan(headNumber, cfg.chainConfig.Bor) {
Expand Down Expand Up @@ -292,7 +292,7 @@ func BorHeimdallForward(
}

sprintLength := cfg.chainConfig.Bor.CalculateSprint(blockNum)
spanID := span.NumAt(blockNum)
spanID := span.IDAt(blockNum)
if (spanID > 0) && ((blockNum+1)%sprintLength == 0) {
if err = checkHeaderExtraData(u, ctx, chain, blockNum, header, cfg.chainConfig.Bor); err != nil {
return err
Expand Down Expand Up @@ -361,7 +361,7 @@ func checkHeaderExtraData(
header *types.Header,
config *chain.BorConfig,
) error {
spanID := span.NumAt(blockNum + 1)
spanID := span.IDAt(blockNum + 1)
spanBytes := chain.BorSpan(spanID)
var sp span.HeimdallSpan
if err := json.Unmarshal(spanBytes, &sp); err != nil {
Expand Down Expand Up @@ -819,7 +819,7 @@ func BorHeimdallUnwind(u *UnwindState, ctx context.Context, s *StageState, tx kv
return err
}
defer spanCursor.Close()
lastSpanToKeep := span.NumAt(u.UnwindPoint)
lastSpanToKeep := span.IDAt(u.UnwindPoint)
var spanIdBytes [8]byte
binary.BigEndian.PutUint64(spanIdBytes[:], lastSpanToKeep+1)
for k, _, err = spanCursor.Seek(spanIdBytes[:]); err == nil && k != nil; k, _, err = spanCursor.Next() {
Expand Down
2 changes: 1 addition & 1 deletion polygon/sync/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (impl *HeimdallImpl) FetchMilestones(ctx context.Context, start uint64) ([]
}

func (impl *HeimdallImpl) FetchSpan(ctx context.Context, start uint64) (*span.HeimdallSpan, error) {
return impl.client.Span(ctx, span.NumAt(start))
return impl.client.Span(ctx, span.IDAt(start))
}

func (impl *HeimdallImpl) OnMilestoneEvent(ctx context.Context, callback func(*milestone.Milestone)) error {
Expand Down
6 changes: 3 additions & 3 deletions turbo/snapshotsync/freezeblocks/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ func (r *BlockReader) LastFrozenSpanID() uint64 {
return 0
}

lastSpanID := span.NumAt(lastSegment.to)
lastSpanID := span.IDAt(lastSegment.to)
if lastSpanID > 0 {
lastSpanID--
}
Expand Down Expand Up @@ -1172,11 +1172,11 @@ func (r *BlockReader) Span(ctx context.Context, tx kv.Getter, spanId uint64) ([]
if sn.idx == nil {
continue
}
spanFrom := span.NumAt(sn.from)
spanFrom := span.IDAt(sn.from)
if spanId < spanFrom {
continue
}
spanTo := span.NumAt(sn.to)
spanTo := span.IDAt(sn.to)
if spanId >= spanTo {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/freezeblocks/block_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,7 @@ func (br *BlockRetire) PruneAncientBlocks(tx kv.RwTx, limit int) error {
canDeleteTo := CanDeleteTo(currentProgress, br.blockReader.FrozenBorBlocks())
br.logger.Info("[snapshots] Prune Bor Blocks", "to", canDeleteTo, "limit", limit)

if err := br.blockWriter.PruneBorBlocks(context.Background(), tx, canDeleteTo, limit, span.NumAt); err != nil {
if err := br.blockWriter.PruneBorBlocks(context.Background(), tx, canDeleteTo, limit, span.IDAt); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions turbo/snapshotsync/freezeblocks/bor_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ func DumpBorEvents(ctx context.Context, db kv.RoDB, blockFrom, blockTo uint64, w
func DumpBorSpans(ctx context.Context, db kv.RoDB, blockFrom, blockTo uint64, workers int, lvl log.Lvl, logger log.Logger, collect func([]byte) error) error {
logEvery := time.NewTicker(20 * time.Second)
defer logEvery.Stop()
spanFrom := span.NumAt(blockFrom)
spanTo := span.NumAt(blockTo)
spanFrom := span.IDAt(blockFrom)
spanTo := span.IDAt(blockTo)
from := hexutility.EncodeTs(spanFrom)
if err := kv.BigChunks(db, kv.BorSpans, from, func(tx kv.Tx, spanIdBytes, spanBytes []byte) (bool, error) {
spanId := binary.BigEndian.Uint64(spanIdBytes)
Expand Down Expand Up @@ -508,7 +508,7 @@ func BorSpansIdx(ctx context.Context, segmentFilePath string, version uint8, blo
g := d.MakeGetter()
var idxFilePath = filepath.Join(snapDir, snaptype.IdxFileName(version, blockFrom, blockTo, snaptype.BorSpans.String()))

baseSpanId := span.NumAt(blockFrom)
baseSpanId := span.IDAt(blockFrom)

rs, err := recsplit.NewRecSplit(recsplit.RecSplitArgs{
KeyCount: d.Count(),
Expand Down
Loading