2020-03-14 16:31:21 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2020-03-14 16:31:21 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-18 07:56:48 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-03-14 16:31:21 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveState_HotStateCanBeSaved(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-14 16:31:21 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
// This goes to hot section, verify it can save on epoch boundary.
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, service.SaveState(ctx, r, beaconState))
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
// Should save both state and state summary.
|
2020-07-06 17:22:12 +00:00
|
|
|
_, ok, err := service.epochBoundaryStateCache.getByRoot(r)
|
2020-07-18 07:56:48 +00:00
|
|
|
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")
|
2020-03-14 16:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveState_HotStateCached(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-14 16:31:21 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-14 16:31:21 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
// Cache the state prior.
|
|
|
|
r := [32]byte{'a'}
|
|
|
|
service.hotStateCache.Put(r, beaconState)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, service.SaveState(ctx, r, beaconState))
|
2020-03-14 16:31:21 +00:00
|
|
|
|
|
|
|
// Should not save the state and state summary.
|
2020-07-18 07:56:48 +00:00
|
|
|
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")
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsDoNotContain(t, hook, "Saved full state on epoch boundary")
|
2020-03-14 16:31:21 +00:00
|
|
|
}
|
2020-07-28 18:35:17 +00:00
|
|
|
|
|
|
|
func TestState_ForceCheckpoint_SavesStateToDatabase(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db, ssc := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
svc := New(db, ssc)
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-08-25 15:23:06 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
2020-07-28 18:35:17 +00:00
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
|
|
|
svc.hotStateCache.Put(r, beaconState)
|
|
|
|
|
2020-08-25 15:23:06 +00:00
|
|
|
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")
|
2020-08-03 18:34:54 +00:00
|
|
|
|
|
|
|
// Should not panic with genesis finalized root.
|
2020-08-25 15:23:06 +00:00
|
|
|
assert.NoError(t, svc.ForceCheckpoint(ctx, params.BeaconConfig().ZeroHash[:]))
|
2020-07-28 18:35:17 +00:00
|
|
|
}
|
2020-08-27 22:29:59 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|