Skip to content

Commit

Permalink
Fixed archive CL pks (#9886)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Apr 9, 2024
1 parent 70406e6 commit 012199f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
7 changes: 3 additions & 4 deletions cl/antiquary/state_antiquary.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ func (s *Antiquary) IncrementBeaconState(ctx context.Context, to uint64) error {
stateAntiquaryCollector := newBeaconStatesCollector(s.cfg, s.dirs.Tmp, s.logger)
defer stateAntiquaryCollector.close()

// buffers

if err := s.initializeStateAntiquaryIfNeeded(ctx, tx); err != nil {
return err
}
Expand All @@ -136,6 +134,7 @@ func (s *Antiquary) IncrementBeaconState(ctx context.Context, to uint64) error {
return true
})
}
s.validatorsTable.SetSlot(s.currentState.Slot())

logLvl := log.LvlInfo
if to-s.currentState.Slot() < 96 {
Expand Down Expand Up @@ -358,6 +357,7 @@ func (s *Antiquary) IncrementBeaconState(ctx context.Context, to uint64) error {
return err
}
defer rwTx.Rollback()

start = time.Now()
// We now need to store the state
if err := stateAntiquaryCollector.flush(ctx, rwTx); err != nil {
Expand Down Expand Up @@ -457,7 +457,7 @@ func (s *Antiquary) initializeStateAntiquaryIfNeeded(ctx context.Context, tx kv.
return err
}
if computedBlockRoot != expectedBlockRoot {
log.Warn("Block root mismatch, trying again", "slot", attempt, "expected", expectedBlockRoot)
log.Debug("Block root mismatch, trying again", "slot", attempt, "expected", expectedBlockRoot)
// backoff more
backoffStep += 10
continue
Expand All @@ -476,6 +476,5 @@ func computeSlotToBeRequested(tx kv.Tx, cfg *clparams.BeaconChainConfig, genesis
if targetSlot > clparams.SlotsPerDump*backoffStep {
return findNearestSlotBackwards(tx, cfg, targetSlot-clparams.SlotsPerDump*backoffStep)
}
fmt.Println(genesisSlot)
return genesisSlot, nil
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//go:build integration

package historical_states_reader_test

import (
"context"
"fmt"
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
Expand Down Expand Up @@ -43,6 +42,7 @@ func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postSt
require.NoError(t, err)
_, err = postState.HashSSZ()
require.NoError(t, err)
fmt.Println(s.Validators().Get(0).PublicKey(), postState.Validators().Get(0).PublicKey())
require.Equal(t, libcommon.Hash(postHash), blocks[len(blocks)-1].Block.StateRoot)
}

Expand Down
62 changes: 58 additions & 4 deletions cl/persistence/state/static_validator_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,51 @@ func (s *StaticValidator) ToValidator(v solid.Validator, slot uint64) {
v.SetWithdrawableEpoch(s.WithdrawableEpoch(slot))
}

func (s *StaticValidator) Reset(slot uint64) {
for i := 0; i < len(s.publicKeys); i++ {
if s.publicKeys[i].Slot > slot {
s.publicKeys = s.publicKeys[:i]
break
}
}
for i := 0; i < len(s.withdrawalCredentials); i++ {
if s.withdrawalCredentials[i].Slot > slot {
s.withdrawalCredentials = s.withdrawalCredentials[:i]
break
}
}
for i := 0; i < len(s.slashed); i++ {
if s.slashed[i].Slot > slot {
s.slashed = s.slashed[:i]
break
}
}
for i := 0; i < len(s.activationEligibility); i++ {
if s.activationEligibility[i].Slot > slot {
s.activationEligibility = s.activationEligibility[:i]
break
}
}
for i := 0; i < len(s.activationEpoch); i++ {
if s.activationEpoch[i].Slot > slot {
s.activationEpoch = s.activationEpoch[:i]
break
}
}
for i := 0; i < len(s.exitEpoch); i++ {
if s.exitEpoch[i].Slot > slot {
s.exitEpoch = s.exitEpoch[:i]
break
}
}
for i := 0; i < len(s.withdrawableEpoch); i++ {
if s.withdrawableEpoch[i].Slot > slot {
s.withdrawableEpoch = s.withdrawableEpoch[:i]
break
}
}
}

type staticValidatorField[V any] struct {
Slot uint64
Field V
Expand Down Expand Up @@ -230,7 +275,7 @@ func (s *StaticValidatorTable) AddValidator(v solid.Validator, validatorIndex, s
return nil
}
s.validatorTable = append(s.validatorTable, NewStaticValidatorFromValidator(v, slot))
if validatorIndex >= uint64(len(s.validatorTable)) {
if validatorIndex != uint64(len(s.validatorTable))-1 {
return fmt.Errorf("validator index mismatch")
}
return nil
Expand Down Expand Up @@ -376,12 +421,21 @@ func (s *StaticValidatorTable) GetStaticValidator(validatorIndex uint64) *Static
func (s *StaticValidatorTable) SetSlot(slot uint64) {
s.sync.Lock()
defer s.sync.Unlock()
if slot <= s.slot && s.slot != 0 {
return
}
s.resetTable(slot)
s.slot = slot
}

func (s *StaticValidatorTable) resetTable(slot uint64) {
for i, v := range s.validatorTable {
v.Reset(slot)
// if we remove all public keys, we can remove all subsequent fields
if len(v.publicKeys) == 0 {
s.validatorTable = s.validatorTable[:i]
break
}
}
}

func (s *StaticValidatorTable) Slot() uint64 {
s.sync.RLock()
defer s.sync.RUnlock()
Expand Down

0 comments on commit 012199f

Please sign in to comment.