2020-03-12 00:38:30 +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 00:38:30 +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 21:04:24 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-03-12 00:38:30 +00:00
|
|
|
"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 00:38:30 +00:00
|
|
|
)
|
|
|
|
|
2020-03-12 12:58:06 +00:00
|
|
|
func TestSaveColdState_NonArchivedPoint(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-12 12:58:06 +00:00
|
|
|
|
2020-03-30 22:10:45 +00:00
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-03-12 12:58:06 +00:00
|
|
|
service.slotsPerArchivedPoint = 2
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(1))
|
|
|
|
assert.NoError(t, service.saveColdState(ctx, [32]byte{}, beaconState))
|
2020-03-12 12:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 16:23:39 +00:00
|
|
|
func TestSaveColdState_CanSave(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, beaconState.SetSlot(1))
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, service.saveColdState(ctx, r, beaconState))
|
2020-03-31 16:23:39 +00:00
|
|
|
|
2020-07-18 07:56:48 +00:00
|
|
|
assert.Equal(t, true, service.beaconDB.HasArchivedPoint(ctx, 1), "Did not save cold state")
|
|
|
|
assert.Equal(t, r, service.beaconDB.ArchivedPointRoot(ctx, 1), "Did not get wanted root")
|
2020-03-31 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadColdStateByRoot_NoStateSummary(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
2020-07-18 07:56:48 +00:00
|
|
|
_, err := service.loadColdStateByRoot(ctx, [32]byte{'a'})
|
|
|
|
require.ErrorContains(t, errUnknownStateSummary.Error(), err, "Did not get correct error")
|
2020-03-31 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
func TestLoadStateByRoot_CanGet(t *testing.T) {
|
2020-03-31 16:23:39 +00:00
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
service.slotsPerArchivedPoint = 1
|
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
2020-05-05 04:30:24 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot))
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(ctx, blk))
|
|
|
|
require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, blkRoot))
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{
|
|
|
|
Root: blkRoot[:],
|
2020-04-13 16:32:02 +00:00
|
|
|
Slot: 100,
|
2020-03-31 16:23:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:22:12 +00:00
|
|
|
loadedState, err := service.StateByRoot(ctx, blkRoot)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.DeepEqual(t, beaconState.InnerStateUnsafe(), loadedState.InnerStateUnsafe(), "Did not correctly save state")
|
2020-03-31 16:23:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
func TestLoadColdStateBySlot_CanGet(t *testing.T) {
|
2020-03-31 16:23:39 +00:00
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, _ := testDB.SetupDB(t)
|
2020-03-31 16:23:39 +00:00
|
|
|
|
|
|
|
service := New(db, cache.NewStateSummaryCache())
|
|
|
|
|
|
|
|
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
|
|
|
|
blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}}
|
2020-05-05 04:30:24 +00:00
|
|
|
blkRoot, err := stateutil.BlockRoot(blk.Block)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, blkRoot))
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(ctx, blk))
|
|
|
|
require.NoError(t, service.beaconDB.SaveState(ctx, beaconState, blkRoot))
|
2020-03-31 16:23:39 +00:00
|
|
|
|
2020-04-13 16:32:02 +00:00
|
|
|
loadedState, err := service.loadColdStateBySlot(ctx, 200)
|
2020-07-18 07:56:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(200), loadedState.Slot(), "Did not correctly save state")
|
2020-03-31 16:23:39 +00:00
|
|
|
}
|