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

[Grindmas] Added tests to Beacon API, also fixed stuff. #9074

Merged
merged 14 commits into from
Dec 25, 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
7 changes: 4 additions & 3 deletions cl/antiquary/antiquary.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Antiquary struct {
beaconDB persistence.BlockSource
backfilled *atomic.Bool
cfg *clparams.BeaconChainConfig
states bool
states, blocks bool
fs afero.Fs
validatorsTable *state_accessors.StaticValidatorTable
genesisState *state.CachingBeaconState
Expand All @@ -43,7 +43,7 @@ type Antiquary struct {
balances32 []byte
}

func NewAntiquary(ctx context.Context, genesisState *state.CachingBeaconState, validatorsTable *state_accessors.StaticValidatorTable, cfg *clparams.BeaconChainConfig, dirs datadir.Dirs, downloader proto_downloader.DownloaderClient, mainDB kv.RwDB, sn *freezeblocks.CaplinSnapshots, reader freezeblocks.BeaconSnapshotReader, beaconDB persistence.BlockSource, logger log.Logger, states bool, fs afero.Fs) *Antiquary {
func NewAntiquary(ctx context.Context, genesisState *state.CachingBeaconState, validatorsTable *state_accessors.StaticValidatorTable, cfg *clparams.BeaconChainConfig, dirs datadir.Dirs, downloader proto_downloader.DownloaderClient, mainDB kv.RwDB, sn *freezeblocks.CaplinSnapshots, reader freezeblocks.BeaconSnapshotReader, beaconDB persistence.BlockSource, logger log.Logger, states, blocks bool, fs afero.Fs) *Antiquary {
backfilled := &atomic.Bool{}
backfilled.Store(false)
return &Antiquary{
Expand All @@ -61,12 +61,13 @@ func NewAntiquary(ctx context.Context, genesisState *state.CachingBeaconState, v
fs: fs,
validatorsTable: validatorsTable,
genesisState: genesisState,
blocks: blocks,
}
}

// Antiquate is the function that starts transactions seeding and shit, very cool but very shit too as a name.
func (a *Antiquary) Loop() error {
if a.downloader == nil {
if a.downloader == nil || !a.blocks {
return nil // Just skip if we don't have a downloader
}
// Skip if we dont support backfilling for the current network
Expand Down
22 changes: 12 additions & 10 deletions cl/antiquary/state_antiquary.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ func (s *Antiquary) IncrementBeaconState(ctx context.Context, to uint64) error {
return err
}
log.Info("Recovered Beacon State", "slot", s.currentState.Slot(), "elapsed", end, "root", libcommon.Hash(hashRoot).String())

if err := s.currentState.InitBeaconState(); err != nil {
return err
}
}
s.balances32 = s.balances32[:0]
s.balances32 = append(s.balances32, s.currentState.RawBalances()...)
Expand Down Expand Up @@ -734,16 +736,16 @@ func (s *Antiquary) collectGenesisState(ctx context.Context, compressor *zstd.En
if err := s.antiquateFullUint64List(inactivities, slot, state.RawInactivityScores(), &commonBuffer, compressor); err != nil {
return err
}
}

committee := *state.CurrentSyncCommittee()
if err := currentSyncCommittee.Collect(base_encoding.Encode64ToBytes4(slot), libcommon.Copy(committee[:])); err != nil {
return err
}
committeeSlot := s.cfg.RoundSlotToSyncCommitteePeriod(slot)
committee := *state.CurrentSyncCommittee()
if err := currentSyncCommittee.Collect(base_encoding.Encode64ToBytes4(committeeSlot), libcommon.Copy(committee[:])); err != nil {
return err
}

committee = *state.NextSyncCommittee()
if err := nextSyncCommittee.Collect(base_encoding.Encode64ToBytes4(slot), libcommon.Copy(committee[:])); err != nil {
return err
committee = *state.NextSyncCommittee()
if err := nextSyncCommittee.Collect(base_encoding.Encode64ToBytes4(committeeSlot), libcommon.Copy(committee[:])); err != nil {
return err
}
}

var b bytes.Buffer
Expand Down
4 changes: 2 additions & 2 deletions cl/antiquary/state_antiquary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (

func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postState *state.CachingBeaconState) {
db := memdb.NewTestDB(t)
reader, _ := tests.LoadChain(blocks, db, t)
reader, _ := tests.LoadChain(blocks, postState, db, t)

ctx := context.Background()
vt := state_accessors.NewStaticValidatorTable()
f := afero.NewMemMapFs()
a := NewAntiquary(ctx, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, nil, log.New(), true, f)
a := NewAntiquary(ctx, preState, vt, &clparams.MainnetBeaconConfig, datadir.New("/tmp"), nil, db, nil, reader, nil, log.New(), true, true, f)
require.NoError(t, a.IncrementBeaconState(ctx, blocks[len(blocks)-1].Block.Slot+33))
// TODO: add more meaning here, like checking db values, will do so once i see some bugs
}
Expand Down
26 changes: 23 additions & 3 deletions cl/antiquary/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/persistence"
"github.com/ledgerwatch/erigon/cl/persistence/beacon_indicies"
state_accessors "github.com/ledgerwatch/erigon/cl/persistence/state"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/spf13/afero"
Expand Down Expand Up @@ -61,17 +62,35 @@ func (m *MockBlockReader) ReadBlockBySlot(ctx context.Context, tx kv.Tx, slot ui
}

func (m *MockBlockReader) ReadBlockByRoot(ctx context.Context, tx kv.Tx, blockRoot libcommon.Hash) (*cltypes.SignedBeaconBlock, error) {
panic("implement me")
// do a linear search
for _, v := range m.u {
r, err := v.Block.HashSSZ()
if err != nil {
return nil, err
}

if r == blockRoot {
return v, nil
}
}
return nil, nil
}
func (m *MockBlockReader) ReadHeaderByRoot(ctx context.Context, tx kv.Tx, blockRoot libcommon.Hash) (*cltypes.SignedBeaconBlockHeader, error) {
panic("implement me")
block, err := m.ReadBlockByRoot(ctx, tx, blockRoot)
if err != nil {
return nil, err
}
if block == nil {
return nil, nil
}
return block.SignedBeaconBlockHeader(), nil
}

func (m *MockBlockReader) FrozenSlots() uint64 {
panic("implement me")
}

func LoadChain(blocks []*cltypes.SignedBeaconBlock, db kv.RwDB, t *testing.T) (*MockBlockReader, afero.Fs) {
func LoadChain(blocks []*cltypes.SignedBeaconBlock, s *state.CachingBeaconState, db kv.RwDB, t *testing.T) (*MockBlockReader, afero.Fs) {
tx, err := db.BeginRw(context.Background())
require.NoError(t, err)
defer tx.Rollback()
Expand All @@ -86,6 +105,7 @@ func LoadChain(blocks []*cltypes.SignedBeaconBlock, db kv.RwDB, t *testing.T) (*
require.NoError(t, source.WriteBlock(context.Background(), tx, block, true))
require.NoError(t, beacon_indicies.WriteHighestFinalized(tx, block.Block.Slot+64))
}
require.NoError(t, state_accessors.InitializeStaticTables(tx, s))

require.NoError(t, tx.Commit())
return m, fs
Expand Down
7 changes: 6 additions & 1 deletion cl/beacon/beaconhttp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func HandleEndpoint[T any](h EndpointHandler[T]) http.HandlerFunc {
ans, err := h.Handle(r)
if err != nil {
log.Error("beacon api request error", "err", err)
endpointError := WrapEndpointError(err)
var endpointError *EndpointError
if e, ok := err.(*EndpointError); ok {
endpointError = e
} else {
endpointError = WrapEndpointError(err)
}
endpointError.WriteTo(w)
return
}
Expand Down
4 changes: 3 additions & 1 deletion cl/beacon/handler/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,7 @@ func (a *ApiHandler) getBlockRoot(r *http.Request) (*beaconResponse, error) {
if err != nil {
return nil, err
}
return newBeaconResponse(struct{ Root libcommon.Hash }{Root: root}).withFinalized(canonicalRoot == root && *slot <= a.forkchoiceStore.FinalizedSlot()), nil
return newBeaconResponse(struct {
Root libcommon.Hash `json:"root"`
}{Root: root}).withFinalized(canonicalRoot == root && *slot <= a.forkchoiceStore.FinalizedSlot()), nil
}
Loading
Loading