mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 23:48:06 -05:00
* Starting a quick PoC
* Rate limit to one epoch worth of blocks in memory
* Proof of concept working
* Quick comment out
* Save previous finalized checkpoint
* Test
* Minor fixes
* More run time fixes
* Remove panic
* Feature flag
* Removed unused methods
* Fixed tests
* E2e test
* comment
* Compatible with current initial sync
* Starting
* New cache
* Cache getters and setters
* It should be part of state gen
* Need to use cache for DB
* Don't have to use finalized state
* Rm unused file
* some changes to memory mgmt when using mempool
* More run time fixes
* Can sync to head
* Feedback
* Revert "some changes to memory mgmt when using mempool"
This reverts commit f5b3e7ff47.
* Fixed sync tests
* Fixed existing tests
* Test for state summary getter
* Gaz
* Fix kafka passthrough
* Fixed inputs
* Gaz
* Fixed build
* Fixed visibility
* Trying without the ignore
* Didn't work..
* Fix kafka
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
223 lines
5.8 KiB
Go
223 lines
5.8 KiB
Go
package stategen
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
func TestStateByRoot_ColdState(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.splitInfo.slot = 2
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}}
|
|
if err := db.SaveBlock(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bRoot, _ := ssz.HashTreeRoot(b.Block)
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
beaconState.SetSlot(1)
|
|
service.beaconDB.SaveState(ctx, beaconState, bRoot)
|
|
r := [32]byte{'a'}
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
Root: r[:],
|
|
Slot: 1,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
loadedState, err := service.StateByRoot(ctx, r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
|
|
t.Error("Did not correctly save state")
|
|
}
|
|
}
|
|
|
|
func TestStateByRoot_HotStateDB(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
blkRoot, _ := ssz.HashTreeRoot(blk.Block)
|
|
service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot)
|
|
if err := service.beaconDB.SaveState(ctx, beaconState, blkRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
targetSlot := uint64(10)
|
|
targetRoot := [32]byte{'a'}
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
Slot: targetSlot,
|
|
Root: targetRoot[:],
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
loadedState, err := service.StateByRoot(ctx, targetRoot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loadedState.Slot() != targetSlot {
|
|
t.Error("Did not correctly load state")
|
|
}
|
|
}
|
|
|
|
func TestStateByRoot_HotStateCached(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
r := [32]byte{'A'}
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
Root: r[:],
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
service.hotStateCache.Put(r, beaconState)
|
|
|
|
loadedState, err := service.StateByRoot(ctx, r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
|
|
t.Error("Did not correctly cache state")
|
|
}
|
|
}
|
|
|
|
func TestStateBySlot_ColdState(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.slotsPerArchivedPoint = params.BeaconConfig().SlotsPerEpoch * 2
|
|
service.splitInfo.slot = service.slotsPerArchivedPoint + 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
beaconState.SetSlot(1)
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}}
|
|
if err := db.SaveBlock(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bRoot, _ := ssz.HashTreeRoot(b.Block)
|
|
if err := db.SaveState(ctx, beaconState, bRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.SaveGenesisBlockRoot(ctx, bRoot)
|
|
|
|
r := [32]byte{}
|
|
if err := service.beaconDB.SaveArchivedPointRoot(ctx, r, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := service.beaconDB.SaveArchivedPointRoot(ctx, r, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
Slot: service.slotsPerArchivedPoint,
|
|
Root: r[:],
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
slot := uint64(20)
|
|
loadedState, err := service.StateBySlot(ctx, slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loadedState.Slot() != slot {
|
|
t.Error("Did not correctly save state")
|
|
}
|
|
}
|
|
|
|
func TestStateBySlot_HotStateDB(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
|
if err := db.SaveBlock(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bRoot, _ := ssz.HashTreeRoot(b.Block)
|
|
if err := db.SaveState(ctx, beaconState, bRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db.SaveGenesisBlockRoot(ctx, bRoot)
|
|
|
|
slot := uint64(10)
|
|
loadedState, err := service.StateBySlot(ctx, slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loadedState.Slot() != slot {
|
|
t.Error("Did not correctly load state")
|
|
}
|
|
}
|
|
|
|
func TestStateSummary_CanGetFromCacheOrDB(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
r := [32]byte{'a'}
|
|
summary := &pb.StateSummary{Slot: 100}
|
|
_, err := service.stateSummary(ctx, r)
|
|
if err != errUnknownStateSummary {
|
|
t.Fatal("Did not get wanted error")
|
|
}
|
|
|
|
service.stateSummaryCache.Put(r, summary)
|
|
got, err := service.stateSummary(ctx, r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !proto.Equal(got, summary) {
|
|
t.Error("Did not get wanted summary")
|
|
}
|
|
|
|
r = [32]byte{'b'}
|
|
summary = &pb.StateSummary{Root: r[:], Slot: 101}
|
|
_, err = service.stateSummary(ctx, r)
|
|
if err != errUnknownStateSummary {
|
|
t.Fatal("Did not get wanted error")
|
|
}
|
|
|
|
if err := service.beaconDB.SaveStateSummary(ctx, summary); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err = service.stateSummary(ctx, r)
|
|
if err != nil {
|
|
t.Fatal("Did not get wanted error")
|
|
}
|
|
if !proto.Equal(got, summary) {
|
|
t.Error("Did not get wanted summary")
|
|
}
|
|
}
|