mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
840ffc84ac
* Starting saving state during hot * Add a log * Add helpers to turn on/off mode * Add locks * Add missing return * Clean up * Add logic to migration to handle db roots * Add tests for on and off * Add more tests * Add test for migrate * @prestonvanloon's feedback Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
201 lines
7.4 KiB
Go
201 lines
7.4 KiB
Go
package stategen
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestSaveState_HotStateCanBeSaved(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.slotsPerArchivedPoint = 1
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
// This goes to hot section, verify it can save on epoch boundary.
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
r := [32]byte{'a'}
|
|
require.NoError(t, service.SaveState(ctx, r, beaconState))
|
|
|
|
// Should save both state and state summary.
|
|
_, ok, err := service.epochBoundaryStateCache.getByRoot(r)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, true, ok, "Should have saved the state")
|
|
assert.Equal(t, true, service.stateSummaryCache.Has(r), "Should have saved the state summary")
|
|
}
|
|
|
|
func TestSaveState_HotStateCached(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.slotsPerArchivedPoint = 1
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
// Cache the state prior.
|
|
r := [32]byte{'a'}
|
|
service.hotStateCache.Put(r, beaconState)
|
|
require.NoError(t, service.SaveState(ctx, r, beaconState))
|
|
|
|
// Should not save the state and state summary.
|
|
assert.Equal(t, false, service.beaconDB.HasState(ctx, r), "Should not have saved the state")
|
|
assert.Equal(t, false, service.beaconDB.HasStateSummary(ctx, r), "Should have saved the state summary")
|
|
require.LogsDoNotContain(t, hook, "Saved full state on epoch boundary")
|
|
}
|
|
|
|
func TestState_ForceCheckpoint_SavesStateToDatabase(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, ssc := testDB.SetupDB(t)
|
|
|
|
svc := New(db, ssc)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
r := [32]byte{'a'}
|
|
svc.hotStateCache.Put(r, beaconState)
|
|
|
|
require.Equal(t, false, db.HasState(ctx, r), "Database has state stored already")
|
|
assert.NoError(t, svc.ForceCheckpoint(ctx, r[:]))
|
|
assert.Equal(t, true, db.HasState(ctx, r), "Did not save checkpoint to database")
|
|
|
|
// Should not panic with genesis finalized root.
|
|
assert.NoError(t, svc.ForceCheckpoint(ctx, params.BeaconConfig().ZeroHash[:]))
|
|
}
|
|
|
|
func TestSaveState_AlreadyHas(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
r := [32]byte{'A'}
|
|
|
|
// Pre cache the hot state.
|
|
service.hotStateCache.Put(r, beaconState)
|
|
require.NoError(t, service.saveStateByRoot(ctx, r, beaconState))
|
|
|
|
// Should not save the state and state summary.
|
|
assert.Equal(t, false, service.beaconDB.HasState(ctx, r), "Should not have saved the state")
|
|
assert.Equal(t, false, service.beaconDB.HasStateSummary(ctx, r), "Should have saved the state summary")
|
|
require.LogsDoNotContain(t, hook, "Saved full state on epoch boundary")
|
|
}
|
|
|
|
func TestSaveState_CanSaveOnEpochBoundary(t *testing.T) {
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
r := [32]byte{'A'}
|
|
|
|
require.NoError(t, service.saveStateByRoot(ctx, r, beaconState))
|
|
|
|
// Should save both state and state summary.
|
|
_, ok, err := service.epochBoundaryStateCache.getByRoot(r)
|
|
require.NoError(t, err)
|
|
require.Equal(t, true, ok, "Did not save epoch boundary state")
|
|
assert.Equal(t, true, service.stateSummaryCache.Has(r), "Should have saved the state summary")
|
|
// Should have not been saved in DB.
|
|
require.Equal(t, false, db.HasState(ctx, r))
|
|
}
|
|
|
|
func TestSaveState_NoSaveNotEpochBoundary(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch-1))
|
|
r := [32]byte{'A'}
|
|
b := testutil.NewBeaconBlock()
|
|
require.NoError(t, db.SaveBlock(ctx, b))
|
|
gRoot, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot))
|
|
require.NoError(t, service.SaveState(ctx, r, beaconState))
|
|
|
|
// Should only save state summary.
|
|
assert.Equal(t, false, service.beaconDB.HasState(ctx, r), "Should not have saved the state")
|
|
assert.Equal(t, true, service.stateSummaryCache.Has(r), "Should have saved the state summary")
|
|
require.LogsDoNotContain(t, hook, "Saved full state on epoch boundary")
|
|
// Should have not been saved in DB.
|
|
require.Equal(t, false, db.HasState(ctx, r))
|
|
}
|
|
|
|
func TestSaveState_CanSaveHotStateToDB(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.EnableSaveHotStateToDB(ctx)
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
require.NoError(t, beaconState.SetSlot(defaultHotStateDBInterval))
|
|
|
|
r := [32]byte{'A'}
|
|
require.NoError(t, service.saveStateByRoot(ctx, r, beaconState))
|
|
|
|
require.LogsContain(t, hook, "Saving hot state to DB")
|
|
// Should have saved in DB.
|
|
require.Equal(t, true, db.HasState(ctx, r))
|
|
}
|
|
|
|
func TestEnableSaveHotStateToDB_Enabled(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
service.EnableSaveHotStateToDB(ctx)
|
|
require.LogsContain(t, hook, "Entering mode to save hot states in DB")
|
|
require.Equal(t, true, service.saveHotStateDB.enabled)
|
|
}
|
|
|
|
func TestEnableSaveHotStateToDB_AlreadyEnabled(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.saveHotStateDB.enabled = true
|
|
service.EnableSaveHotStateToDB(ctx)
|
|
require.LogsDoNotContain(t, hook, "Entering mode to save hot states in DB")
|
|
require.Equal(t, true, service.saveHotStateDB.enabled)
|
|
}
|
|
|
|
func TestEnableSaveHotStateToDB_Disabled(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
service.saveHotStateDB.enabled = true
|
|
service.saveHotStateDB.savedStateRoots = [][32]byte{{'a'}}
|
|
require.NoError(t, service.DisableSaveHotStateToDB(ctx))
|
|
require.LogsContain(t, hook, "Exiting mode to save hot states in DB")
|
|
require.Equal(t, false, service.saveHotStateDB.enabled)
|
|
require.Equal(t, 0, len(service.saveHotStateDB.savedStateRoots))
|
|
}
|
|
|
|
func TestEnableSaveHotStateToDB_AlreadyDisabled(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
ctx := context.Background()
|
|
db, _ := testDB.SetupDB(t)
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
require.NoError(t, service.DisableSaveHotStateToDB(ctx))
|
|
require.LogsDoNotContain(t, hook, "Exiting mode to save hot states in DB")
|
|
require.Equal(t, false, service.saveHotStateDB.enabled)
|
|
}
|