prysm-pulse/beacon-chain/state/stategen/migrate_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

137 lines
5.1 KiB
Go

package stategen
import (
"context"
"testing"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/blocks"
testDB "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
"github.com/prysmaticlabs/prysm/v3/testing/util"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) {
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
service := New(beaconDB)
beaconState, _ := util.DeterministicGenesisState(t, 32)
b := util.NewBeaconBlock()
b.Block.Slot = 1
br, err := b.Block.HashTreeRoot()
require.NoError(t, err)
util.SaveBlock(t, ctx, service.beaconDB, 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, _ := util.DeterministicGenesisState(t, 32)
stateSlot := types.Slot(1)
require.NoError(t, beaconState.SetSlot(stateSlot))
b := util.NewBeaconBlock()
b.Block.Slot = 2
fRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
util.SaveBlock(t, ctx, service.beaconDB, 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 := util.DeterministicGenesisState(t, 32)
genesisStateRoot, err := beaconState.HashTreeRoot(ctx)
require.NoError(t, err)
genesis := blocks.NewGenesisBlock(genesisStateRoot[:])
util.SaveBlock(t, ctx, beaconDB, 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 := util.GenerateFullBlock(beaconState, pks, util.DefaultBlockGenConfig(), 1)
require.NoError(t, err)
r1, err := b1.Block.HashTreeRoot()
require.NoError(t, err)
util.SaveBlock(t, ctx, service.beaconDB, b1)
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &ethpb.StateSummary{Slot: 1, Root: r1[:]}))
b4, err := util.GenerateFullBlock(beaconState, pks, util.DefaultBlockGenConfig(), 4)
require.NoError(t, err)
r4, err := b4.Block.HashTreeRoot()
require.NoError(t, err)
util.SaveBlock(t, ctx, service.beaconDB, b4)
require.NoError(t, service.beaconDB.SaveStateSummary(ctx, &ethpb.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, _ := util.DeterministicGenesisState(t, 32)
stateSlot := types.Slot(1)
require.NoError(t, beaconState.SetSlot(stateSlot))
b := util.NewBeaconBlock()
b.Block.Slot = 2
fRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
util.SaveBlock(t, ctx, service.beaconDB, b)
require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState))
require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, fRoot))
service.saveHotStateDB.blockRootsOfSavedStates = [][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.blockRootsOfSavedStates)
assert.LogsDoNotContain(t, hook, "Saved state in DB")
}