2020-03-12 02:27:16 +00:00
|
|
|
package stategen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-30 22:10:45 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
|
2020-03-12 02:27:16 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2020-03-12 02:27:16 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"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-12 02:27:16 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
func TestMigrateToCold_CanSaveFinalizedInfo(t *testing.T) {
|
2020-03-12 02:27:16 +00:00
|
|
|
ctx := context.Background()
|
2020-07-09 23:50:48 +00:00
|
|
|
db, c := testDB.SetupDB(t)
|
|
|
|
service := New(db, c)
|
2020-07-06 17:22:12 +00:00
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-09 23:50:48 +00:00
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 1}}
|
|
|
|
br, err := stateutil.BlockRoot(b.Block)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(ctx, b))
|
|
|
|
require.NoError(t, service.epochBoundaryStateCache.put(br, beaconState))
|
|
|
|
require.NoError(t, service.MigrateToCold(ctx, br))
|
2020-03-12 02:27:16 +00:00
|
|
|
|
2020-07-09 23:50:48 +00:00
|
|
|
wanted := &finalizedInfo{state: beaconState, root: br, slot: 1}
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.DeepEqual(t, wanted, service.finalizedInfo, "Incorrect finalized info")
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
func TestMigrateToCold_HappyPath(t *testing.T) {
|
2020-03-12 02:27:16 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-12 02:27:16 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-07-06 17:22:12 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
2020-03-12 02:27:16 +00:00
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-06 17:22:12 +00:00
|
|
|
stateSlot := uint64(1)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(stateSlot))
|
2020-07-09 23:50:48 +00:00
|
|
|
b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}}
|
|
|
|
fRoot, err := stateutil.BlockRoot(b.Block)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(ctx, b))
|
|
|
|
require.NoError(t, service.epochBoundaryStateCache.put(fRoot, beaconState))
|
|
|
|
require.NoError(t, service.MigrateToCold(ctx, fRoot))
|
2020-03-27 20:28:38 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
gotState, err := service.beaconDB.State(ctx, fRoot)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state")
|
2020-07-06 17:22:12 +00:00
|
|
|
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, fRoot, gotRoot, "Did not save archived root")
|
2020-07-18 18:05:04 +00:00
|
|
|
lastIndex, err := service.beaconDB.LastArchivedSlot(ctx)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), lastIndex, "Did not save last archived index")
|
2020-03-12 02:27:16 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Saved state in DB")
|
2020-03-12 02:27:16 +00:00
|
|
|
}
|
2020-05-14 20:03:14 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
func TestMigrateToCold_RegeneratePath(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
2020-05-14 20:03:14 +00:00
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-05-14 20:03:14 +00:00
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-07-06 17:22:12 +00:00
|
|
|
service.slotsPerArchivedPoint = 1
|
2020-05-14 20:03:14 +00:00
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-06 17:22:12 +00:00
|
|
|
stateSlot := uint64(1)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(stateSlot))
|
2020-07-09 23:50:48 +00:00
|
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: 2}}
|
2020-07-06 17:22:12 +00:00
|
|
|
fRoot, err := stateutil.BlockRoot(blk.Block)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(ctx, blk))
|
|
|
|
require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, fRoot))
|
2020-07-06 17:22:12 +00:00
|
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
|
|
Slot: 1,
|
|
|
|
Root: fRoot[:],
|
|
|
|
}); err != nil {
|
2020-05-14 20:03:14 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-06 17:22:12 +00:00
|
|
|
service.finalizedInfo = &finalizedInfo{
|
|
|
|
slot: 1,
|
|
|
|
root: fRoot,
|
|
|
|
state: beaconState,
|
2020-05-14 20:03:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, service.MigrateToCold(ctx, fRoot))
|
2020-05-14 20:03:14 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
gotState, err := service.beaconDB.State(ctx, fRoot)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state")
|
2020-07-06 17:22:12 +00:00
|
|
|
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, fRoot, gotRoot, "Did not save archived root")
|
2020-07-18 18:05:04 +00:00
|
|
|
lastIndex, err := service.beaconDB.LastArchivedSlot(ctx)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), lastIndex, "Did not save last archived index")
|
2020-05-22 01:15:16 +00:00
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
testutil.AssertLogsContain(t, hook, "Saved state in DB")
|
2020-05-22 01:15:16 +00:00
|
|
|
}
|