prysm-pulse/beacon-chain/state/stategen/migrate_test.go
2021-05-26 11:33:46 -07:00

139 lines
5.5 KiB
Go

package stategen
import (
"context"
"testing"
"github.com/prysmaticlabs/prysm/shared/interfaces"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"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 TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) {
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
service := New(beaconDB)
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
b := testutil.NewBeaconBlock()
b.Block.Slot = 1
br, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b)))
require.NoError(t, service.epochBoundaryStateCache.put(br, beaconState))
require.NoError(t, service.MigrateToCold(ctx, br))
wanted := &finalizedInfo{state: beaconState, root: br, slot: 1}
assert.DeepEqual(t, wanted, service.finalizedInfo, "Incorrect finalized info")
}
func TestMigrateToCold_HappyPath(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
service := New(beaconDB)
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
stateSlot := types.Slot(1)
require.NoError(t, beaconState.SetSlot(stateSlot))
b := testutil.NewBeaconBlock()
b.Block.Slot = 2
fRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b)))
require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState))
require.NoError(t, service.MigrateToCold(ctx, fRoot))
gotState, err := service.beaconDB.State(ctx, fRoot)
require.NoError(t, err)
assert.DeepSSZEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state")
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
assert.Equal(t, fRoot, gotRoot, "Did not save archived root")
lastIndex, err := service.beaconDB.LastArchivedSlot(ctx)
require.NoError(t, err)
assert.Equal(t, types.Slot(1), lastIndex, "Did not save last archived index")
require.LogsContain(t, hook, "Saved state in DB")
}
func TestMigrateToCold_RegeneratePath(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
service := New(beaconDB)
service.slotsPerArchivedPoint = 1
beaconState, pks := testutil.DeterministicGenesisState(t, 32)
genesisStateRoot, err := beaconState.HashTreeRoot(ctx)
require.NoError(t, err)
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
assert.NoError(t, beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(genesis)))
gRoot, err := genesis.Block.HashTreeRoot()
require.NoError(t, err)
assert.NoError(t, beaconDB.SaveState(ctx, beaconState, gRoot))
assert.NoError(t, beaconDB.SaveGenesisBlockRoot(ctx, gRoot))
b1, err := testutil.GenerateFullBlock(beaconState, pks, testutil.DefaultBlockGenConfig(), 1)
require.NoError(t, err)
r1, err := b1.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b1)))
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 1, Root: r1[:]}))
b4, err := testutil.GenerateFullBlock(beaconState, pks, testutil.DefaultBlockGenConfig(), 4)
require.NoError(t, err)
r4, err := b4.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b4)))
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: 4, Root: r4[:]}))
service.finalizedInfo = &finalizedInfo{
slot: 0,
root: genesisStateRoot,
state: beaconState,
}
require.NoError(t, service.MigrateToCold(ctx, r4))
s1, err := service.beaconDB.State(ctx, r1)
require.NoError(t, err)
assert.Equal(t, s1.Slot(), types.Slot(1), "Did not save state")
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, 1/service.slotsPerArchivedPoint)
assert.Equal(t, r1, gotRoot, "Did not save archived root")
lastIndex, err := service.beaconDB.LastArchivedSlot(ctx)
require.NoError(t, err)
assert.Equal(t, types.Slot(1), lastIndex, "Did not save last archived index")
require.LogsContain(t, hook, "Saved state in DB")
}
func TestMigrateToCold_StateExistsInDB(t *testing.T) {
hook := logTest.NewGlobal()
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
service := New(beaconDB)
service.slotsPerArchivedPoint = 1
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
stateSlot := types.Slot(1)
require.NoError(t, beaconState.SetSlot(stateSlot))
b := testutil.NewBeaconBlock()
b.Block.Slot = 2
fRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, service.beaconDB.SaveBlock(ctx, interfaces.WrappedPhase0SignedBeaconBlock(b)))
require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState))
require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, fRoot))
service.saveHotStateDB.savedStateRoots = [][32]byte{{1}, {2}, {3}, {4}, fRoot}
require.NoError(t, service.MigrateToCold(ctx, fRoot))
assert.DeepEqual(t, [][32]byte{{1}, {2}, {3}, {4}}, service.saveHotStateDB.savedStateRoots)
assert.LogsDoNotContain(t, hook, "Saved state in DB")
}