Skip to content

Commit

Permalink
Caplin: fix antiquary regression (#12778)
Browse files Browse the repository at this point in the history
Co-authored-by: alex.sharov <[email protected]>
  • Loading branch information
Giulio2002 and AskAlexSharov authored Nov 20, 2024
1 parent 5000597 commit 1edb54c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ func (r *HistoricalStatesReader) ComputeCommittee(mix libcommon.Hash, indicies [
end := (lenIndicies * (index + 1)) / count
var shuffledIndicies []uint64

shuffledIndicies = make([]uint64, lenIndicies)
shuffledIndicies = shuffling.ComputeShuffledIndicies(cfg, mix, shuffledIndicies, indicies, slot)
shuffledIndicies, ok := r.shuffledIndiciesCache.Get(slot / cfg.SlotsPerEpoch)
if !ok {
shuffledIndicies = make([]uint64, lenIndicies)
shuffledIndicies = shuffling.ComputeShuffledIndicies(cfg, mix, shuffledIndicies, indicies, slot)
r.shuffledIndiciesCache.Add(slot/cfg.SlotsPerEpoch, shuffledIndicies)
}

return shuffledIndicies[start:end], nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"sync"
"time"

"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/erigontech/erigon/cl/persistence/base_encoding"
state_accessors "github.com/erigontech/erigon/cl/persistence/state"
"github.com/erigontech/erigon/cl/phase1/core/state"
"github.com/erigontech/erigon/cl/phase1/core/state/lru"
"github.com/erigontech/erigon/turbo/snapshotsync"
"github.com/erigontech/erigon/turbo/snapshotsync/freezeblocks"
"github.com/klauspost/compress/zstd"
Expand All @@ -48,20 +50,24 @@ type HistoricalStatesReader struct {
blockReader freezeblocks.BeaconSnapshotReader
stateSn *snapshotsync.CaplinStateSnapshots
genesisState *state.CachingBeaconState

shuffledIndiciesCache *lru.CacheWithTTL[uint64, []uint64]
}

func NewHistoricalStatesReader(
cfg *clparams.BeaconChainConfig,
blockReader freezeblocks.BeaconSnapshotReader,
validatorTable *state_accessors.StaticValidatorTable,
genesisState *state.CachingBeaconState, stateSn *snapshotsync.CaplinStateSnapshots) *HistoricalStatesReader {
shuffledIndiciesCache := lru.NewWithTTL[uint64, []uint64]("shuffledIndiciesCacheReader", 64, 2*time.Minute)

return &HistoricalStatesReader{
cfg: cfg,
blockReader: blockReader,
genesisState: genesisState,
validatorTable: validatorTable,
stateSn: stateSn,
cfg: cfg,
blockReader: blockReader,
genesisState: genesisState,
validatorTable: validatorTable,
stateSn: stateSn,
shuffledIndiciesCache: shuffledIndiciesCache,
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/capcli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ func (r *RetrieveHistoricalState) Run(ctx *Context) error {
if _, err := antiquary.FillStaticValidatorsTableIfNeeded(ctx, log.Root(), stateSn, vt); err != nil {
return err
}
fmt.Println(vt.WithdrawableEpoch(0, 1))

r.withPPROF.withProfile()
hr := historical_states_reader.NewHistoricalStatesReader(beaconConfig, snr, vt, gSpot, stateSn)
start := time.Now()
Expand Down
59 changes: 43 additions & 16 deletions turbo/snapshotsync/caplin_state_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ type CaplinStateSnapshots struct {
dirtyLock sync.RWMutex // guards `dirty` field
dirty map[string]*btree.BTreeG[*DirtySegment] // ordered map `type.Enum()` -> DirtySegments

visibleLock sync.RWMutex // guards `visible` field
visible map[string]VisibleSegments // ordered map `type.Enum()` -> VisbileSegments
visibleLock sync.RWMutex // guards `visible` field
visible sync.Map
//visible map[string]VisibleSegments // ordered map `type.Enum()` -> VisbileSegments

snapshotTypes SnapshotTypes

Expand Down Expand Up @@ -181,11 +182,11 @@ func NewCaplinStateSnapshots(cfg ethconfig.BlocksFreezing, beaconCfg *clparams.B
for k := range snapshotTypes.KeyValueGetters {
dirty[k] = btree.NewBTreeGOptions[*DirtySegment](DirtySegmentLess, btree.Options{Degree: 128, NoLocks: false})
}
visible := make(map[string]VisibleSegments)

c := &CaplinStateSnapshots{snapshotTypes: snapshotTypes, dir: dirs.SnapCaplin, tmpdir: dirs.Tmp, cfg: cfg, dirty: dirty, logger: logger, beaconCfg: beaconCfg}
for k := range snapshotTypes.KeyValueGetters {
visible[k] = make(VisibleSegments, 0)
c.visible.Store(k, make(VisibleSegments, 0))
}
c := &CaplinStateSnapshots{snapshotTypes: snapshotTypes, dir: dirs.SnapCaplin, tmpdir: dirs.Tmp, cfg: cfg, visible: visible, dirty: dirty, logger: logger, beaconCfg: beaconCfg}
c.recalcVisibleFiles()
return c
}
Expand Down Expand Up @@ -430,24 +431,39 @@ func (s *CaplinStateSnapshots) recalcVisibleFiles() {
return newVisibleSegments
}

for k := range s.visible {
s.visible[k] = getNewVisibleSegments(s.dirty[k])
}
// for k := range s.visible {
// s.visible[k] = getNewVisibleSegments(s.dirty[k])
// }
s.visible.Range(func(k, v interface{}) bool {
s.visible.Store(k, getNewVisibleSegments(s.dirty[k.(string)]))
return true
})
}

func (s *CaplinStateSnapshots) idxAvailability() uint64 {
s.visibleLock.RLock()
defer s.visibleLock.RUnlock()

min := uint64(math.MaxUint64)
for _, segs := range s.visible {
// for _, segs := range s.visible {
// if len(segs) == 0 {
// return 0
// }
// if segs[len(segs)-1].to < min {
// min = segs[len(segs)-1].to
// }
// }
s.visible.Range(func(_, v interface{}) bool {
segs := v.([]*VisibleSegment)
if len(segs) == 0 {
return 0
min = 0
return false
}
if segs[len(segs)-1].to < min {
min = segs[len(segs)-1].to
}
}
return true
})
if min == math.MaxUint64 {
return 0
}
Expand Down Expand Up @@ -523,9 +539,13 @@ func (s *CaplinStateSnapshots) View() *CaplinStateView {
s.dirtySegmentsLock.RLock()
defer s.dirtySegmentsLock.RUnlock()

for k, segments := range s.visible {
v.roTxs[k] = segments.BeginRo()
}
// for k, segments := range s.visible {
// v.roTxs[k] = segments.BeginRo()
// }
s.visible.Range(func(k, val interface{}) bool {
v.roTxs[k.(string)] = VisibleSegments(val.([]*VisibleSegment)).BeginRo()
return true
})
return v
}

Expand All @@ -544,10 +564,17 @@ func (v *CaplinStateView) Close() {
}

func (v *CaplinStateView) VisibleSegments(tbl string) []*VisibleSegment {
if v.s == nil || v.s.visible[tbl] == nil {
// if v.s == nil || v.s.visible[tbl] == nil {
// return nil
// }
// return v.s.visible[tbl]
if v.s == nil {
return nil
}
return v.s.visible[tbl]
if val, ok := v.s.visible.Load(tbl); ok {
return val.([]*VisibleSegment)
}
return nil
}

func (v *CaplinStateView) VisibleSegment(slot uint64, tbl string) (*VisibleSegment, bool) {
Expand Down

0 comments on commit 1edb54c

Please sign in to comment.